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

refactor: subject comments #845

Merged
merged 27 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions drizzle/orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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 ISubjectTopic = typeof schema.chiiSubjectTopics.$inferSelect;
export type ISubjectPost = typeof schema.chiiSubjectPosts.$inferSelect;
export type ISubjectEpStatus = typeof schema.chiiEpStatus.$inferSelect;

export type IEpisode = typeof schema.chiiEpisodes.$inferSelect;
Expand Down
54 changes: 25 additions & 29 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,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 @@ -663,7 +663,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 @@ -988,19 +988,19 @@ export const chiiSubjectInterests = mysqlTable(
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 @@ -1066,26 +1066,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
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);
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