Skip to content

Commit

Permalink
wip - fix home timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriha-chan committed Oct 6, 2023
1 parent ec97df2 commit 4f22526
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/core/NoteCreateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ export class NoteCreateService implements OnApplicationShutdown {
});

// TODO: あまりにも数が多いと redisPipeline.exec に失敗する(理由は不明)ため、3万件程度を目安に分割して実行するようにする
for (const following of followings) {
for (const following of [...followings, note.userId]) {
// 自分自身以外への返信
if (note.replyId && note.replyUserId !== note.userId) {
if (!following.withReplies) continue;
Expand Down
48 changes: 48 additions & 0 deletions packages/backend/src/core/QueryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ export class QueryService {
q.setParameters(blockedQuery.getParameters());
}

@bindThis
public generateChannelQuery(q: SelectQueryBuilder<any>, me?: { id: MiUser['id'] } | null): void {
if (me == null) {
q.andWhere('note.channelId IS NULL');
} else {
q.leftJoinAndSelect('note.channel', 'channel');

const channelFollowingQuery = this.channelFollowingsRepository.createQueryBuilder('channelFollowing')
.select('channelFollowing.followeeId')
.where('channelFollowing.followerId = :followerId', { followerId: me.id });

q.andWhere(new Brackets(qb => { qb
// チャンネルのノートではない
.where('note.channelId IS NULL')
// または自分がフォローしているチャンネルのノート
.orWhere(`note.channelId IN (${ channelFollowingQuery.getQuery() })`);
}));

q.setParameters(channelFollowingQuery.getParameters());
}
}

@bindThis
public generateMutedNoteThreadQuery(q: SelectQueryBuilder<any>, me: { id: MiUser['id'] }): void {
const mutedQuery = this.noteThreadMutingsRepository.createQueryBuilder('threadMuted')
Expand Down Expand Up @@ -176,6 +198,32 @@ export class QueryService {
q.setParameters(mutingQuery.getParameters());
}

@bindThis
public generateRepliesQuery(q: SelectQueryBuilder<any>, withReplies: boolean, me?: Pick<MiUser, 'id'> | null): void {
if (me == null) {
q.andWhere(new Brackets(qb => { qb
.where('note.replyId IS NULL') // 返信ではない
.orWhere(new Brackets(qb => { qb // 返信だけど投稿者自身への返信
.where('note.replyId IS NOT NULL')
.andWhere('note.replyUserId = note.userId');
}));
}));
} else if (!withReplies) {
q.andWhere(new Brackets(qb => { qb
.where('note.replyId IS NULL') // 返信ではない
.orWhere('note.replyUserId = :meId', { meId: me.id }) // 返信だけど自分のノートへの返信
.orWhere(new Brackets(qb => { qb // 返信だけど自分の行った返信
.where('note.replyId IS NOT NULL')
.andWhere('note.userId = :meId', { meId: me.id });
}))
.orWhere(new Brackets(qb => { qb // 返信だけど投稿者自身への返信
.where('note.replyId IS NOT NULL')
.andWhere('note.replyUserId = note.userId');
}));
}));
}
}

@bindThis
public generateVisibilityQuery(q: SelectQueryBuilder<any>, me?: { id: MiUser['id'] } | null): void {
// This code must always be synchronized with the checks in Notes.isVisibleForMe.
Expand Down
12 changes: 6 additions & 6 deletions packages/backend/src/server/api/endpoints/notes/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-

// fallback to postgres
if (noteIds.length < limit) {
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note.id'),
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'),
ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate);
if (followings.length > 0) {
const meOrFolloweeIds = [me.id, ...followings];
const followingIds = Object.keys(followings);
if (followingIds.length > 0) {
const meOrFolloweeIds = [me.id, ...followingIds];
query.andWhere('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds });
} else {
query.andWhere('note.userId = :meId', { meId: me.id });
Expand All @@ -107,11 +108,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.queryService.generateRepliesQuery(query, ps.withReplies, me);
this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateMutedUserQuery(query, me);
this.queryService.generateMutedNoteQuery(query, me);
this.queryService.generateBlockedUserQuery(query, me);
this.queryService.generateMutedUserRenotesQueryForNotes(query, me);

noteIds = noteIds.concat(await query.limit(limit - noteIds.length).getMany());
let ids = await query.limit(limit - noteIds.length).getMany();
noteIds = noteIds.concat(ids.map(note => note.id));
}

if (noteIds.length === 0) {
Expand Down

0 comments on commit 4f22526

Please sign in to comment.