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 for index #946

Merged
merged 20 commits into from
Feb 25, 2025
1 change: 1 addition & 0 deletions drizzle/orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type IPersonCollect = typeof schema.chiiPersonCollects.$inferSelect;

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

export type IBlogEntry = typeof schema.chiiBlogEntries.$inferSelect;
export type IBlogPhoto = typeof schema.chiiBlogPhotos.$inferSelect;
Expand Down
24 changes: 13 additions & 11 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ export const chiiGroupPosts = mysqlTable('chii_group_posts', {
export const chiiIndexes = mysqlTable('chii_index', {
id: mediumint('idx_id').autoincrement().notNull(),
type: tinyint('idx_type').default(0).notNull(),
title: varchar('idx_title', { length: 80 }).notNull(),
desc: mediumtext('idx_desc').notNull(),
title: htmlEscapedString('varchar')('idx_title', { length: 80 }).notNull(),
desc: htmlEscapedString('mediumtext')('idx_desc').notNull(),
replies: mediumint('idx_replies').notNull(),
total: mediumint('idx_subject_total').notNull(),
collects: mediumint('idx_collects').notNull(),
stats: mediumtext('idx_stats').notNull(),
award: mediumint('idx_award').default(0).notNull(),
createdAt: int('idx_dateline').notNull(),
updatedAt: int('idx_lasttouch').notNull(),
uid: mediumint('idx_uid').notNull(),
Expand All @@ -260,15 +261,16 @@ export const chiiIndexComments = mysqlTable('chii_index_comments', {
});

export const chiiIndexRelated = mysqlTable('chii_index_related', {
idxRltId: mediumint('idx_rlt_id').autoincrement().notNull(),
idxRltCat: tinyint('idx_rlt_cat').notNull(),
idxRltRid: mediumint('idx_rlt_rid').notNull(),
idxRltType: smallint('idx_rlt_type').notNull(),
idxRltSid: mediumint('idx_rlt_sid').notNull(),
idxRltOrder: mediumint('idx_rlt_order').notNull(),
idxRltComment: mediumtext('idx_rlt_comment').notNull(),
idxRltDateline: int('idx_rlt_dateline').notNull(),
idxRltBan: tinyint('idx_rlt_ban').default(0).notNull(),
id: mediumint('idx_rlt_id').autoincrement().notNull(),
cat: tinyint('idx_rlt_cat').notNull(),
rid: mediumint('idx_rlt_rid').notNull(),
type: smallint('idx_rlt_type').notNull(),
sid: mediumint('idx_rlt_sid').notNull(),
order: mediumint('idx_rlt_order').notNull(),
award: varchar('idx_rlt_award', { length: 255 }).notNull(),
comment: htmlEscapedString('mediumtext')('idx_rlt_comment').notNull(),
createdAt: int('idx_rlt_dateline').notNull(),
ban: tinyint('idx_rlt_ban').default(0).notNull(),
});

export const chiiLikes = mysqlTable('chii_likes', {
Expand Down
2 changes: 1 addition & 1 deletion lib/index/cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function getSlimCacheKey(id: number): string {
return `idx:v3:slim:${id}`;
return `idx:v4:slim:${id}`;
}
12 changes: 12 additions & 0 deletions lib/index/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export enum IndexType {
User = 0,
Public = 1,
Award = 2,
}

export enum IndexRelatedCategory {
Subject = 0,
Character = 1,
Person = 2,
Ep = 3,
}
1 change: 1 addition & 0 deletions lib/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const Tag = {
Episode: 'episode',
Friend: 'friend',
Group: 'group',
Index: 'index',
Person: 'person',
Search: 'search',
Subject: 'subject',
Expand Down
29 changes: 29 additions & 0 deletions lib/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ export const CollectionType = t.Integer({
- 5 = 抛弃`,
});

export const IndexRelatedCategory = t.Integer({
$id: 'IndexRelatedCategory',
enum: [0, 1, 2, 3],
'x-ms-enum': {
name: 'IndexRelatedCategory',
modelAsString: false,
},
'x-enum-varnames': ['Subject', 'Character', 'Person', 'Episode'],
description: `目录关联类型
- 0 = 条目
- 1 = 角色
- 2 = 人物
- 3 = 剧集`,
});

export const EpisodeCollectionStatus = t.Integer({
$id: 'EpisodeCollectionStatus',
enum: [0, 1, 2, 3],
Expand Down Expand Up @@ -91,6 +106,20 @@ export const GroupMemberRole = t.Integer({
- 3 = 禁言成员`,
});

export const IndexType = t.Integer({
$id: 'IndexType',
enum: [0, 1, 2],
'x-ms-enum': {
name: 'IndexType',
modelAsString: false,
},
'x-enum-varnames': ['User', 'Public', 'Award'],
description: `目录类型
- 0 = 用户
- 1 = 公共
- 2 = TBA`,
});

export type IEpisodeWikiInfo = Static<typeof EpisodeWikiInfo>;
export const EpisodeWikiInfo = t.Object(
{
Expand Down
17 changes: 17 additions & 0 deletions lib/types/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ export function toPerson(person: orm.IPerson): res.IPerson {
export function toSlimIndex(index: orm.IIndex): res.ISlimIndex {
return {
id: index.id,
uid: index.uid,
type: index.type,
title: index.title,
total: index.total,
Expand All @@ -527,18 +528,34 @@ export function toSlimIndex(index: orm.IIndex): res.ISlimIndex {
export function toIndex(index: orm.IIndex): res.IIndex {
return {
id: index.id,
uid: index.uid,
type: index.type,
title: index.title,
desc: index.desc,
replies: index.replies,
total: index.total,
collects: index.collects,
stats: toIndexStats(index.stats),
award: index.award,
createdAt: index.createdAt,
updatedAt: index.updatedAt,
};
}

export function toIndexRelated(related: orm.IIndexRelated): res.IIndexRelated {
return {
id: related.id,
cat: related.cat,
rid: related.rid,
type: related.type,
sid: related.sid,
order: related.order,
comment: related.comment,
award: related.award,
createdAt: related.createdAt,
};
}

export function toCharacterSubjectRelation(
subject: orm.ISubject,
fields: orm.ISubjectFields,
Expand Down
31 changes: 31 additions & 0 deletions lib/types/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,37 @@ async function fetchEpisodeItemByID(episodeID: number): Promise<res.IEpisode | u
return item;
}

/** Cached */
export async function fetchSlimEpisodesByIDs(
episodeIDs: number[],
): Promise<Record<number, res.IEpisode>> {
const cached = await redis.mget(episodeIDs.map((id) => getSubjectEpCacheKey(id)));
const result: Record<number, res.IEpisode> = {};
const missing = [];
for (const [idx, id] of episodeIDs.entries()) {
if (cached[idx]) {
const item = JSON.parse(cached[idx]) as res.IEpisode;
item.desc = undefined;
result[id] = item;
} else {
missing.push(id);
}
}
if (missing.length > 0) {
const data = await db
.select()
.from(schema.chiiEpisodes)
.where(op.inArray(schema.chiiEpisodes.id, missing));
for (const d of data) {
const item = convert.toEpisode(d);
await redis.setex(getSubjectEpCacheKey(d.id), ONE_MONTH, JSON.stringify(item));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个月是不是久了点

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

也还好吧,有清理逻辑的

item.desc = undefined;
result[d.id] = item;
}
}
return result;
}

/** Cached */
export async function fetchSlimCharacterByID(
id: number,
Expand Down
30 changes: 28 additions & 2 deletions lib/types/res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
EpisodeCollectionStatus,
EpisodeType,
GroupMemberRole,
IndexRelatedCategory,
IndexType,
Ref,
SubjectType,
} from '@app/lib/types/common.ts';
Expand Down Expand Up @@ -814,16 +816,19 @@ export type IIndex = Static<typeof Index>;
export const Index = t.Object(
{
id: t.Integer(),
type: t.Integer(),
uid: t.Integer(),
type: Ref(IndexType),
title: t.String(),
desc: t.String(),
replies: t.Integer(),
total: t.Integer(),
collects: t.Integer(),
stats: Ref(IndexStats),
award: t.Integer(),
createdAt: t.Integer(),
updatedAt: t.Integer(),
collectedAt: t.Optional(t.Integer()),
user: t.Optional(Ref(SlimUser)),
},
{ $id: 'Index', title: 'Index' },
);
Expand All @@ -832,14 +837,35 @@ export type ISlimIndex = Static<typeof SlimIndex>;
export const SlimIndex = t.Object(
{
id: t.Integer(),
type: t.Integer(),
uid: t.Integer(),
type: Ref(IndexType),
title: t.String(),
total: t.Integer(),
createdAt: t.Integer(),
},
{ $id: 'SlimIndex', title: 'SlimIndex' },
);

export type IIndexRelated = Static<typeof IndexRelated>;
export const IndexRelated = t.Object(
{
id: t.Integer(),
cat: Ref(IndexRelatedCategory),
rid: t.Integer(),
type: t.Integer(),
sid: t.Integer(),
order: t.Integer(),
comment: t.String(),
award: t.String(),
createdAt: t.Integer(),
subject: t.Optional(Ref(SlimSubject)),
character: t.Optional(Ref(SlimCharacter)),
person: t.Optional(Ref(SlimPerson)),
episode: t.Optional(Ref(Episode)),
},
{ $id: 'IndexRelated', title: 'IndexRelated' },
);

export type IGroupMember = Static<typeof GroupMember>;
export const GroupMember = t.Object(
{
Expand Down
9 changes: 8 additions & 1 deletion lib/user/stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { db, op, schema } from '@app/drizzle';
import { IndexType } from '@app/lib/index/types.ts';
import redis from '@app/lib/redis';
import { CollectionPrivacy, CollectionType, SubjectType } from '@app/lib/subject/type.ts';
import type * as res from '@app/lib/types/res.ts';
Expand Down Expand Up @@ -62,7 +63,13 @@ export async function countUserIndex(uid: number): Promise<res.IUserIndexStats>
const [{ create = 0 } = {}] = await db
.select({ create: op.count() })
.from(schema.chiiIndexes)
.where(op.and(op.eq(schema.chiiIndexes.uid, uid), op.eq(schema.chiiIndexes.ban, 0)));
.where(
op.and(
op.eq(schema.chiiIndexes.uid, uid),
op.eq(schema.chiiIndexes.ban, 0),
op.eq(schema.chiiIndexes.type, IndexType.User),
),
);
const [{ collect = 0 } = {}] = await db
.select({ collect: op.count() })
.from(schema.chiiIndexCollects)
Expand Down
Loading