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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
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
22 changes: 11 additions & 11 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ 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(),
Expand Down Expand Up @@ -259,15 +259,15 @@ 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(),
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
6 changes: 6 additions & 0 deletions lib/index/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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 @@ -8,6 +8,7 @@ export const Tag = {
Collection: 'collection',
Episode: 'episode',
Group: 'group',
Index: 'index',
Person: 'person',
Subject: 'subject',
Timeline: 'timeline',
Expand Down
15 changes: 15 additions & 0 deletions lib/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,18 @@ export const CollectionType = t.Integer({
- 4 = 搁置
- 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 = 剧集`,
});
13 changes: 13 additions & 0 deletions lib/types/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,19 @@ export function toIndex(index: orm.IIndex, user: orm.IUser): res.IIndex {
};
}

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,
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 @@ -486,6 +486,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));
item.desc = undefined;
result[d.id] = item;
}
}
return result;
}

/** Cached */
export async function fetchSlimCharacterByID(
id: number,
Expand Down
20 changes: 20 additions & 0 deletions lib/types/res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,26 @@ export const SlimIndex = t.Object(
{ $id: 'SlimIndex', title: 'SlimIndex' },
);

export type IIndexRelated = Static<typeof IndexRelated>;
export const IndexRelated = t.Object(
{
id: t.Integer(),
cat: t.Integer(),
rid: t.Integer(),
type: t.Integer(),
sid: t.Integer(),
order: t.Integer(),
comment: 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 IGroup = Static<typeof Group>;
export const Group = t.Object(
{
Expand Down
154 changes: 154 additions & 0 deletions routes/__snapshots__/index.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions routes/private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as blog from './routes/blog.ts';
import * as calendar from './routes/calendar.ts';
import * as character from './routes/character.ts';
import * as episode from './routes/episode.ts';
import * as index from './routes/index.ts';
import * as misc from './routes/misc.ts';
import * as person from './routes/person.ts';
import * as post from './routes/post.ts';
Expand Down Expand Up @@ -74,6 +75,7 @@ async function API(app: App) {
await app.register(character.setup);
await app.register(episode.setup);
await app.register(group.setup);
await app.register(index.setup);
await app.register(misc.setup);
await app.register(person.setup);
await app.register(post.setup);
Expand Down
Loading