Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add api subject reviews #831

Merged
merged 29 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions drizzle/orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export type ISubject = typeof schema.chiiSubjects.$inferSelect;
export type ISubjectFields = typeof schema.chiiSubjectFields.$inferSelect;
export type ISubjectInterest = typeof schema.chiiSubjectInterests.$inferSelect;
export type ISubjectRelation = typeof schema.chiiSubjectRelations.$inferSelect;
export type ISubjectRelatedBlog = typeof schema.chiiSubjectRelatedBlogs.$inferSelect;
export type ISubjectTopic = typeof schema.chiiSubjectTopics.$inferSelect;
export type ISubjectPost = typeof schema.chiiSubjectPosts.$inferSelect;

export type IEpisode = typeof schema.chiiEpisodes.$inferSelect;

Expand All @@ -24,3 +27,6 @@ export type IPersonCollect = typeof schema.chiiPersonCollects.$inferSelect;

export type IIndex = typeof schema.chiiIndexes.$inferSelect;
export type IIndexCollect = typeof schema.chiiIndexCollects.$inferSelect;

export type IBlogEntry = typeof schema.chiiBlogEntries.$inferSelect;
export type IBlogPhoto = typeof schema.chiiBlogPhotos.$inferSelect;
141 changes: 112 additions & 29 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ export const chiiIndexComments = mysqlTable(
'chii_index_comments',
{
id: mediumint('idx_pst_id').autoincrement().notNull(),
mid: mediumint('idx_pst_mid').notNull(),
mid: mediumint('idx_pst_mid').notNull(), // index id
uid: mediumint('idx_pst_uid').notNull(),
related: mediumint('idx_pst_related').notNull(),
createdAt: int('idx_pst_dateline').notNull(),
Expand Down Expand Up @@ -662,7 +662,7 @@ export const chiiPersonCollects = mysqlTable(
{
id: mediumint('prsn_clt_id').autoincrement().notNull(),
cat: mysqlEnum('prsn_clt_cat', ['prsn', 'crt']).notNull(),
mid: mediumint('prsn_clt_mid').notNull(),
mid: mediumint('prsn_clt_mid').notNull(), // person id or character id
uid: mediumint('prsn_clt_uid').notNull(),
createdAt: int('prsn_clt_dateline').notNull(),
},
Expand Down Expand Up @@ -984,22 +984,42 @@ export const chiiSubjectInterests = mysqlTable(
},
);

export const chiiSubjectRelatedBlogs = mysqlTable(
'chii_subject_related_blog',
{
id: mediumint('srb_id').autoincrement().notNull(),
uid: mediumint('srb_uid').notNull(),
subjectID: mediumint('srb_subject_id').notNull(),
entryID: mediumint('srb_entry_id').notNull(), // blog etry id
spoiler: mediumint('srb_spoiler').notNull(),
like: mediumint('srb_like').notNull(),
dislike: mediumint('srb_dislike').notNull(),
trim21 marked this conversation as resolved.
Show resolved Hide resolved
createdAt: int('srb_dateline').notNull(),
},
(table) => {
return {
srbUid: index('srb_uid').on(table.uid, table.subjectID, table.entryID),
subjectRelated: index('subject_related').on(table.subjectID),
};
},
);

export const chiiSubjectPosts = mysqlTable(
'chii_subject_posts',
{
sbjPstId: mediumint('sbj_pst_id').autoincrement().notNull(),
sbjPstMid: mediumint('sbj_pst_mid').notNull(),
sbjPstUid: mediumint('sbj_pst_uid').notNull(),
sbjPstRelated: mediumint('sbj_pst_related').notNull(),
sbjPstContent: mediumtext('sbj_pst_content').notNull(),
sbjPstState: tinyint('sbj_pst_state').notNull(),
sbjPstDateline: int('sbj_pst_dateline').default(0).notNull(),
id: mediumint('sbj_pst_id').primaryKey().autoincrement().notNull(),
mid: mediumint('sbj_pst_mid').notNull(), // subject id
uid: mediumint('sbj_pst_uid').notNull(),
related: mediumint('sbj_pst_related').notNull(),
content: mediumtext('sbj_pst_content').notNull(),
state: tinyint('sbj_pst_state').notNull(),
createdAt: int('sbj_pst_dateline').default(0).notNull(),
},
(table) => {
return {
pssTopicId: index('pss_topic_id').on(table.sbjPstMid),
sbjPstRelated: index('sbj_pst_related').on(table.sbjPstRelated),
sbjPstUid: index('sbj_pst_uid').on(table.sbjPstUid),
pssTopicId: index('pss_topic_id').on(table.mid),
sbjPstRelated: index('sbj_pst_related').on(table.related),
sbjPstUid: index('sbj_pst_uid').on(table.uid),
};
},
);
Expand Down Expand Up @@ -1065,26 +1085,22 @@ export const chiiSubjectRev = mysqlTable('chii_subject_revisions', {
export const chiiSubjectTopics = mysqlTable(
'chii_subject_topics',
{
sbjTpcId: mediumint('sbj_tpc_id').autoincrement().notNull(),
sbjTpcSubjectId: mediumint('sbj_tpc_subject_id').notNull(),
sbjTpcUid: mediumint('sbj_tpc_uid').notNull(),
sbjTpcTitle: varchar('sbj_tpc_title', { length: 80 }).notNull(),
sbjTpcDateline: int('sbj_tpc_dateline').default(0).notNull(),
sbjTpcLastpost: int('sbj_tpc_lastpost').default(0).notNull(),
sbjTpcReplies: mediumint('sbj_tpc_replies').notNull(),
sbjTpcState: tinyint('sbj_tpc_state').notNull(),
sbjTpcDisplay: tinyint('sbj_tpc_display').default(1).notNull(),
id: mediumint('sbj_tpc_id').autoincrement().notNull(),
subjectID: mediumint('sbj_tpc_subject_id').notNull(),
uid: mediumint('sbj_tpc_uid').notNull(),
title: varchar('sbj_tpc_title', { length: 80 }).notNull(),
createdAt: int('sbj_tpc_dateline').default(0).notNull(),
updatedAt: int('sbj_tpc_lastpost').default(0).notNull(),
replies: mediumint('sbj_tpc_replies').notNull(),
state: tinyint('sbj_tpc_state').notNull(),
display: tinyint('sbj_tpc_display').default(1).notNull(),
},
(table) => {
return {
tpcSubjectId: index('tpc_subject_id').on(table.sbjTpcSubjectId),
tpcDisplay: index('tpc_display').on(table.sbjTpcDisplay),
sbjTpcUid: index('sbj_tpc_uid').on(table.sbjTpcUid),
sbjTpcLastpost: index('sbj_tpc_lastpost').on(
table.sbjTpcLastpost,
table.sbjTpcSubjectId,
table.sbjTpcDisplay,
),
tpcSubjectId: index('tpc_subject_id').on(table.subjectID),
tpcDisplay: index('tpc_display').on(table.display),
sbjTpcUid: index('sbj_tpc_uid').on(table.uid),
sbjTpcLastpost: index('sbj_tpc_lastpost').on(table.updatedAt, table.subjectID, table.display),
};
},
);
Expand Down Expand Up @@ -1187,3 +1203,70 @@ export const chiiUsergroup = mysqlTable('chii_usergroup', {
perm: mediumtext('usr_grp_perm').notNull(),
updatedAt: int('usr_grp_dateline').notNull(),
});

export const chiiBlogComments = mysqlTable(
'chii_blog_comments',
{
id: mediumint('blg_pst_id').autoincrement().notNull(),
mid: mediumint('blg_pst_mid').notNull(), // blog entry id
uid: mediumint('blg_pst_uid').notNull(),
related: mediumint('blg_pst_related').notNull(),
updatedAt: int('blg_pst_dateline').notNull(),
content: mediumtext('blg_pst_content').notNull(),
},
(table) => {
return {
blgCmtEid: index('blg_cmt_eid').on(table.mid),
blgCmtUid: index('blg_cmt_uid').on(table.uid),
blgPstRelated: index('blg_pst_related').on(table.related),
};
},
);

export const chiiBlogEntries = mysqlTable(
trim21 marked this conversation as resolved.
Show resolved Hide resolved
'chii_blog_entry',
{
id: mediumint('entry_id').autoincrement().notNull(),
type: smallint('entry_type').notNull(),
uid: mediumint('entry_uid').notNull(),
title: varchar('entry_title', { length: 80 }).notNull(),
icon: varchar('entry_icon', { length: 255 }).notNull(),
content: mediumtext('entry_content').notNull(),
tags: mediumtext('entry_tags').notNull(),
views: mediumint('entry_views').notNull(),
replies: mediumint('entry_replies').notNull(),
createdAt: int('entry_dateline').notNull(),
updatedAt: int('entry_lastpost').notNull(),
like: int('entry_like').notNull(), // 未使用
dislike: int('entry_dislike').notNull(), // 未使用
noreply: smallint('entry_noreply').notNull(),
related: tinyint('entry_related').default(0).notNull(),
public: customBoolean('entry_public').default(true).notNull(),
},
(table) => {
return {
entryType: index('entry_type').on(table.type, table.uid, table.noreply),
entryRelate: index('entry_relate').on(table.related),
entryPublic: index('entry_public').on(table.public),
entryDateline: index('entry_dateline').on(table.createdAt),
entryUid: index('entry_uid').on(table.uid, table.public),
};
},
);

export const chiiBlogPhotos = mysqlTable(
'chii_blog_photo',
{
id: mediumint('photo_id').autoincrement().notNull(),
eid: mediumint('photo_eid').notNull(),
uid: mediumint('photo_uid').notNull(),
target: varchar('photo_target', { length: 255 }).notNull(),
vote: mediumint('photo_vote').notNull(),
createdAt: int('photo_dateline').notNull(),
},
(table) => {
return {
photoEid: index('photo_eid').on(table.eid),
};
},
);
2 changes: 1 addition & 1 deletion lib/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
SubjectTopicRepo,
} from '@app/lib/orm/index.ts';
import { avatar } from '@app/lib/response.ts';
import { ListTopicDisplays } from '@app/lib/topic/index.ts';
import { ListTopicDisplays } from '@app/lib/topic/display.ts';

import type * as types from './__generated__/resolvers.ts';

Expand Down
2 changes: 1 addition & 1 deletion lib/orm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as schema from '@app/drizzle/schema.ts';
import config, { production, stage } from '@app/lib/config.ts';
import { UnexpectedNotFoundError } from '@app/lib/error.ts';
import { logger } from '@app/lib/logger.ts';
import type { CommentState, TopicDisplay } from '@app/lib/topic/index.ts';
import type { CommentState, TopicDisplay } from '@app/lib/topic/type.ts';

import * as entity from './entity/index.ts';
import {
Expand Down
7 changes: 5 additions & 2 deletions lib/services/turnstile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { stage } from '@app/lib/config.ts';
import { stage, testing } from '@app/lib/config.ts';
import config from '@app/lib/config.ts';

import { BaseExternalHttpSrv } from './base.ts';

Expand All @@ -10,7 +11,7 @@ const VerifyURL = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
* @see https://developers.cloudflare.com/turnstile/frequently-asked-questions/#are-there-sitekeys-and-secret-keys-that-can-be-used-for-testing
*/
export function createTurnstileDriver(secretKey: string) {
if (stage) {
if (stage || testing) {
return {
verify(): Promise<boolean> {
return Promise.resolve(true);
Expand Down Expand Up @@ -46,3 +47,5 @@ export class Turnstile extends BaseExternalHttpSrv {
return data.success;
}
}

export const turnstile = createTurnstileDriver(config.turnstile.siteKey);
13 changes: 13 additions & 0 deletions lib/subject/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,16 @@ export enum PersonType {
Character = 'crt',
Person = 'prsn',
}

export enum EpisodeType {
trim21 marked this conversation as resolved.
Show resolved Hide resolved
/** 本篇 */
Normal = 0,
/** 特别篇 */
Special = 1,
Op = 2,
ED = 3,
/** 预告/宣传/广告 */
Pre = 4,
MAD = 5,
Other = 6,
}
46 changes: 32 additions & 14 deletions lib/topic/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type { IAuth } from '@app/lib/auth/index.ts';
import type { IReply } from '@app/lib/topic/index.ts';
import { CommentState, TopicDisplay } from '@app/lib/topic/index.ts';
import { CommentState, TopicDisplay } from '@app/lib/topic/type.ts';

export const CanViewStateClosedTopic = 24 * 60 * 60 * 180;
export const CanViewStateDeleteTopic = 24 * 60 * 60 * 365;
Expand All @@ -22,13 +22,15 @@

export function CanViewTopicContent(
u: IAuth,
topic: { state: number; display: number; creatorID: number },
state: number,
display: number,
creatorID: number,
): boolean {
if (!u.login) {
// 未登录用户只能看到正常帖子
return (
topic.display === TopicDisplay.Normal &&
(topic.state === CommentState.Normal || topic.state == CommentState.AdminReopen)
display === TopicDisplay.Normal &&
(state === CommentState.Normal || state == CommentState.AdminReopen)
);
}

Expand All @@ -39,34 +41,34 @@
return true;
}

if (u.userID == topic.creatorID && topic.display == TopicDisplay.Review) {
if (u.userID == creatorID && display == TopicDisplay.Review) {
return true;
}

// 非管理员看不到删除和review的帖子
if (topic.display != TopicDisplay.Normal) {
if (display != TopicDisplay.Normal) {
return false;
}

// 注册时间决定
if (topic.state === CommentState.AdminCloseTopic) {
if (state === CommentState.AdminCloseTopic) {
return CanViewClosedTopic(u);
}

if (topic.state === CommentState.AdminDelete) {
if (state === CommentState.AdminDelete) {
return false;
}

if (topic.state === CommentState.UserDelete) {
if (state === CommentState.UserDelete) {
return CanViewDeleteTopic(u);
}

return (
topic.state === CommentState.Normal ||
topic.state === CommentState.AdminReopen ||
topic.state === CommentState.AdminMerge ||
topic.state === CommentState.AdminPin ||
topic.state === CommentState.AdminSilentTopic
state === CommentState.Normal ||
state === CommentState.AdminReopen ||
state === CommentState.AdminMerge ||
state === CommentState.AdminPin ||
state === CommentState.AdminSilentTopic

Check warning on line 71 in lib/topic/display.ts

View check run for this annotation

Codecov / codecov/patch

lib/topic/display.ts#L71

Added line #L71 was not covered by tests
);
}

Expand All @@ -78,6 +80,22 @@
return DateTime.now().toUnixInteger() - a.regTime > CanViewStateClosedTopic;
}

export function CanViewTopicReply(state: number): boolean {
switch (state) {
case CommentState.AdminDelete:
case CommentState.UserDelete:
case CommentState.AdminReopen:
case CommentState.AdminCloseTopic:
case CommentState.AdminSilentTopic: {
return false;
}

Check warning on line 91 in lib/topic/display.ts

View check run for this annotation

Codecov / codecov/patch

lib/topic/display.ts#L84-L91

Added lines #L84 - L91 were not covered by tests

default: {
return true;
}
}
}

Check warning on line 97 in lib/topic/display.ts

View check run for this annotation

Codecov / codecov/patch

lib/topic/display.ts#L93-L97

Added lines #L93 - L97 were not covered by tests

export function filterReply(x: IReply): IReply {
return {
...filterSubReply(x),
Expand Down
9 changes: 5 additions & 4 deletions lib/topic/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { afterAll, afterEach, describe, expect, test, vi } from 'vitest';
import { AppDataSource, GroupPostRepo, GroupTopicRepo } from '@app/lib/orm/index.ts';

import * as Topic from './index.ts';
import { CommentState, TopicParentType } from './type.ts';

describe('mocked', () => {
const transaction = vi.fn().mockResolvedValue({
Expand All @@ -26,11 +27,11 @@ describe('mocked', () => {

test('create topic reply', async () => {
const r = await Topic.createTopicReply({
topicType: Topic.Type.group,
topicType: TopicParentType.Group,
topicID: 10,
content: 'c',
userID: 1,
state: Topic.CommentState.Normal,
state: CommentState.Normal,
parentID: 6,
});

Expand All @@ -45,11 +46,11 @@ describe('should create topic reply', () => {
const topicBefore = await GroupTopicRepo.findOneOrFail({ where: { id: 375793 } });

const r = await Topic.createTopicReply({
topicType: Topic.Type.group,
topicType: TopicParentType.Group,
topicID: 375793,
content: 'new content for testing',
userID: 1,
state: Topic.CommentState.Normal,
state: CommentState.Normal,
parentID: 0,
});

Expand Down
Loading