summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/notes/local-timeline.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-10-03 20:26:11 +0900
committerMar0xy <marie@kaifa.ch>2023-10-13 17:58:11 +0200
commitf6ba5cfaf4b147d7b9dc6349fea250f1fdf1088d (patch)
treed3c10281ce2bfee3f79162f658565c28c64c1bbf /packages/backend/src/server/api/endpoints/notes/local-timeline.ts
parentupd: delete reactions properly in the DB (diff)
downloadsharkey-f6ba5cfaf4b147d7b9dc6349fea250f1fdf1088d.tar.gz
sharkey-f6ba5cfaf4b147d7b9dc6349fea250f1fdf1088d.tar.bz2
sharkey-f6ba5cfaf4b147d7b9dc6349fea250f1fdf1088d.zip
merge: timeline 1
Diffstat (limited to 'packages/backend/src/server/api/endpoints/notes/local-timeline.ts')
-rw-r--r--packages/backend/src/server/api/endpoints/notes/local-timeline.ts102
1 files changed, 55 insertions, 47 deletions
diff --git a/packages/backend/src/server/api/endpoints/notes/local-timeline.ts b/packages/backend/src/server/api/endpoints/notes/local-timeline.ts
index 0fefddc51b..ed57ca1a30 100644
--- a/packages/backend/src/server/api/endpoints/notes/local-timeline.ts
+++ b/packages/backend/src/server/api/endpoints/notes/local-timeline.ts
@@ -5,14 +5,16 @@
import { Brackets } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
-import type { NotesRepository } from '@/models/_.js';
+import * as Redis from 'ioredis';
+import type { MiNote, NotesRepository } from '@/models/_.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
-import { QueryService } from '@/core/QueryService.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import ActiveUsersChart from '@/core/chart/charts/active-users.js';
import { DI } from '@/di-symbols.js';
import { RoleService } from '@/core/RoleService.js';
import { IdService } from '@/core/IdService.js';
+import { CacheService } from '@/core/CacheService.js';
+import { isUserRelated } from '@/misc/is-user-related.js';
import { ApiError } from '../../error.js';
export const meta = {
@@ -41,11 +43,7 @@ export const paramDef = {
type: 'object',
properties: {
withFiles: { type: 'boolean', default: false },
- withReplies: { type: 'boolean', default: false },
withRenotes: { type: 'boolean', default: true },
- fileType: { type: 'array', items: {
- type: 'string',
- } },
excludeNsfw: { type: 'boolean', default: false },
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
sinceId: { type: 'string', format: 'misskey:id' },
@@ -59,14 +57,17 @@ export const paramDef = {
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor(
+ @Inject(DI.redisForTimelines)
+ private redisForTimelines: Redis.Redis,
+
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
private noteEntityService: NoteEntityService,
- private queryService: QueryService,
private roleService: RoleService,
private activeUsersChart: ActiveUsersChart,
private idService: IdService,
+ private cacheService: CacheService,
) {
super(meta, paramDef, async (ps, me) => {
const policies = await this.roleService.getUserPolicies(me ? me.id : null);
@@ -74,56 +75,63 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
throw new ApiError(meta.errors.ltlDisabled);
}
- //#region Construct query
- const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'),
- ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
- .andWhere('note.id > :minId', { minId: this.idService.genId(new Date(Date.now() - (1000 * 60 * 60 * 24 * 10))) }) // 10日前まで
- .andWhere('(note.visibility = \'public\') AND (note.userHost IS NULL)')
+ const [
+ userIdsWhoMeMuting,
+ userIdsWhoMeMutingRenotes,
+ userIdsWhoBlockingMe,
+ ] = me ? await Promise.all([
+ this.cacheService.userMutingsCache.fetch(me.id),
+ this.cacheService.renoteMutingsCache.fetch(me.id),
+ this.cacheService.userBlockedCache.fetch(me.id),
+ ]) : [new Set<string>(), new Set<string>(), new Set<string>()];
+
+ let timeline: MiNote[] = [];
+
+ const limit = ps.limit + (ps.untilId ? 1 : 0); // untilIdに指定したものも含まれるため+1
+ let noteIdsRes: [string, string[]][] = [];
+
+ if (!ps.sinceId && !ps.sinceDate) {
+ noteIdsRes = await this.redisForTimelines.xrevrange(
+ ps.withFiles ? 'localTimelineWithFiles' : 'localTimeline',
+ ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
+ '-',
+ 'COUNT', limit);
+ }
+
+ const noteIds = noteIdsRes.map(x => x[1][1]).filter(x => x !== ps.untilId);
+
+ if (noteIds.length === 0) {
+ return [];
+ }
+
+ const query = this.notesRepository.createQueryBuilder('note')
+ .where('note.id IN (:...noteIds)', { noteIds: noteIds })
.innerJoinAndSelect('note.user', 'user')
.leftJoinAndSelect('note.reply', 'reply')
.leftJoinAndSelect('note.renote', 'renote')
.leftJoinAndSelect('reply.user', 'replyUser')
- .leftJoinAndSelect('renote.user', 'renoteUser');
-
- this.queryService.generateChannelQuery(query, me);
- this.queryService.generateRepliesQuery(query, ps.withReplies, me);
- this.queryService.generateVisibilityQuery(query, me);
- if (me) this.queryService.generateMutedUserQuery(query, me);
- if (me) this.queryService.generateMutedNoteQuery(query, me);
- if (me) this.queryService.generateBlockedUserQuery(query, me);
- if (me) this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
+ .leftJoinAndSelect('renote.user', 'renoteUser')
+ .leftJoinAndSelect('note.channel', 'channel');
- if (ps.withFiles) {
- query.andWhere('note.fileIds != \'{}\'');
- }
+ timeline = await query.getMany();
- if (ps.fileType != null) {
- query.andWhere('note.fileIds != \'{}\'');
- query.andWhere(new Brackets(qb => {
- for (const type of ps.fileType!) {
- const i = ps.fileType!.indexOf(type);
- qb.orWhere(`:type${i} = ANY(note.attachedFileTypes)`, { [`type${i}`]: type });
+ timeline = timeline.filter(note => {
+ if (me && (note.userId === me.id)) {
+ return true;
+ }
+ if (me && isUserRelated(note, userIdsWhoBlockingMe)) return false;
+ if (me && isUserRelated(note, userIdsWhoMeMuting)) return false;
+ if (note.renoteId) {
+ if (note.text == null && note.fileIds.length === 0 && !note.hasPoll) {
+ if (me && isUserRelated(note, userIdsWhoMeMutingRenotes)) return false;
+ if (ps.withRenotes === false) return false;
}
- }));
-
- if (ps.excludeNsfw) {
- query.andWhere('note.cw IS NULL');
- query.andWhere('0 = (SELECT COUNT(*) FROM drive_file df WHERE df.id = ANY(note."fileIds") AND df."isSensitive" = TRUE)');
}
- }
- if (ps.withRenotes === false) {
- query.andWhere(new Brackets(qb => {
- qb.orWhere('note.renoteId IS NULL');
- qb.orWhere(new Brackets(qb => {
- qb.orWhere('note.text IS NOT NULL');
- qb.orWhere('note.fileIds != \'{}\'');
- }));
- }));
- }
- //#endregion
+ return true;
+ });
- const timeline = await query.limit(ps.limit).getMany();
+ timeline.sort((a, b) => a.id > b.id ? -1 : 1);
process.nextTick(() => {
if (me) {