Skip to content

Commit

Permalink
Merge pull request #53 from team-shahu/feat/use-cache-following-history
Browse files Browse the repository at this point in the history
enhance(backend): キャッシュを使うように?
  • Loading branch information
chan-mai authored Dec 18, 2024
2 parents 1d64401 + 813e3aa commit 7a9623a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- いいねボタンの実装 https://github.com/team-shahu/misskey/pull/41 https://github.com/team-shahu/misskey/pull/44 https://github.com/team-shahu/misskey/pull/45
- 独自機能ページの追加 https://github.com/team-shahu/misskey/pull/42
- 予約投稿機能 https://github.com/team-shahu/misskey/pull/46 https://github.com/team-shahu/misskey/pull/49 https://github.com/team-shahu/misskey/pull/51
- フォロー/フォロリクの履歴 https://github.com/team-shahu/misskey/pull/49 https://github.com/team-shahu/misskey/pull/50
- フォロー/フォロリクの履歴 https://github.com/team-shahu/misskey/pull/49 https://github.com/team-shahu/misskey/pull/50 https://github.com/team-shahu/misskey/pull/49 https://github.com/team-shahu/misskey/pull/53
- ドライブから削除したファイルをオブジェクトストレージからも葬るように https://github.com/team-shahu/misskey/pull/49 https://github.com/team-shahu/misskey/pull/52

## Special Thanks
Expand Down
36 changes: 25 additions & 11 deletions packages/backend/src/server/api/endpoints/following/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { QueryService } from '@/core/QueryService.js';
import type { FollowHistoryRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { DI } from '@/di-symbols.js';
import { CacheService } from '@/core/CacheService.js';

export const meta = {
tags: ['following', 'account'],
Expand Down Expand Up @@ -103,9 +104,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {

private userEntityService: UserEntityService,
private queryService: QueryService,
private cacheService: CacheService,
) {
super(meta, paramDef, async (ps, me) => {
if (ps.delete) {
await this.cacheService.userFollowingsCache.delete(me.id);
await this.followHistoryRepository
.createQueryBuilder()
.delete()
Expand Down Expand Up @@ -189,22 +192,33 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
break;
}

const blockedUserIds = await this.cacheService.userBlockedCache.fetch(me.id);
if (blockedUserIds.size > 0) {
query.andWhere('fromUser.id NOT IN (:...blockedUserIds)', { blockedUserIds: Array.from(blockedUserIds) });
query.andWhere('toUser.id NOT IN (:...blockedUserIds)', { blockedUserIds: Array.from(blockedUserIds) });
}

const histories = await query
.orderBy('history.timestamp', 'DESC')
.take(ps.limit)
.getMany();

return await Promise.all(histories.map(async history => ({
id: history.id,
type: history.type,
fromUser: history.fromUser
? await this.userEntityService.pack(history.fromUser ?? history.fromUserId, me)
: { name: 'unknown user' },
toUser: history.toUser
? await this.userEntityService.pack(history.toUser ?? history.toUserId, me)
: { name: 'unknown user' },
timestamp: history.timestamp.toISOString(),
})));
return await Promise.all(histories.map(async history => {
const userFollowings = await this.cacheService.userFollowingsCache.fetch(me.id);

return {
id: history.id,
type: history.type,
fromUser: history.fromUser
? await this.userEntityService.pack(history.fromUser ?? history.fromUserId, me)
: { name: 'unknown user' },
toUser: history.toUser
? await this.userEntityService.pack(history.toUser ?? history.toUserId, me)
: { name: 'unknown user' },
timestamp: history.timestamp.toISOString(),
isFollowing: Object.hasOwn(userFollowings, history.fromUserId),
};
}));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { QueryService } from '@/core/QueryService.js';
import type { FollowRequestHistoryRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { DI } from '@/di-symbols.js';
import { CacheService } from '@/core/CacheService.js';

export const meta = {
tags: ['following', 'account'],
Expand Down Expand Up @@ -103,6 +104,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {

private userEntityService: UserEntityService,
private queryService: QueryService,
private cacheService: CacheService,
) {
super(meta, paramDef, async (ps, me) => {
// 削除モードの部分だけを修正(他のコードは同じ)
Expand Down Expand Up @@ -190,22 +192,33 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
break;
}

const blockedUserIds = await this.cacheService.userBlockedCache.fetch(me.id);
if (blockedUserIds.size > 0) {
query.andWhere('fromUser.id NOT IN (:...blockedUserIds)', { blockedUserIds: Array.from(blockedUserIds) });
query.andWhere('toUser.id NOT IN (:...blockedUserIds)', { blockedUserIds: Array.from(blockedUserIds) });
}

const histories = await query
.orderBy('history.timestamp', 'DESC')
.take(ps.limit)
.getMany();

return await Promise.all(histories.map(async history => ({
id: history.id,
type: history.type,
fromUser: history.fromUser
? await this.userEntityService.pack(history.fromUser ?? history.fromUserId, me)
: { name: 'unknown user' },
toUser: history.toUser
? await this.userEntityService.pack(history.toUser ?? history.toUserId, me)
: { name: 'unknown user' },
timestamp: history.timestamp.toISOString(),
})));
return await Promise.all(histories.map(async history => {
const userFollowings = await this.cacheService.userFollowingsCache.fetch(me.id);

return {
id: history.id,
type: history.type,
fromUser: history.fromUser
? await this.userEntityService.pack(history.fromUser ?? history.fromUserId, me)
: { name: 'unknown user' },
toUser: history.toUser
? await this.userEntityService.pack(history.toUser ?? history.toUserId, me)
: { name: 'unknown user' },
timestamp: history.timestamp.toISOString(),
isFollowing: Object.hasOwn(userFollowings, history.fromUserId),
};
}));
});
}
}

0 comments on commit 7a9623a

Please sign in to comment.