summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/notes/mentions.ts
blob: f30e5a583f7942634bf89de1bfed0b5a4788dece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { Brackets } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
import type { NotesRepository, FollowingsRepository } from '@/models/_.js';
import { MiNote } 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 { DI } from '@/di-symbols.js';
import ActiveUsersChart from '@/core/chart/charts/active-users.js';

export const meta = {
	tags: ['notes'],

	requireCredential: true,
	kind: 'read:account',

	res: {
		type: 'array',
		optional: false, nullable: false,
		items: {
			type: 'object',
			optional: false, nullable: false,
			ref: 'Note',
		},
	},

	// 10 calls per 5 seconds
	limit: {
		duration: 1000 * 5,
		max: 10,
	},
} as const;

export const paramDef = {
	type: 'object',
	properties: {
		following: { type: 'boolean', default: false },
		limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
		sinceId: { type: 'string', format: 'misskey:id' },
		untilId: { type: 'string', format: 'misskey:id' },
		visibility: { type: 'string' },
	},
	required: [],
} as const;

@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
	constructor(
		@Inject(DI.notesRepository)
		private notesRepository: NotesRepository,

		@Inject(DI.followingsRepository)
		private followingsRepository: FollowingsRepository,

		private noteEntityService: NoteEntityService,
		private queryService: QueryService,
		private readonly activeUsersChart: ActiveUsersChart,
	) {
		super(meta, paramDef, async (ps, me) => {
			const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId)
				.innerJoin(qb => {
					qb
						.select('note.id', 'id')
						.from(qbb => qbb
							.select('note.id', 'id')
							.from(MiNote, 'note')
							.where(new Brackets(qbbb => qbbb
								// DM to me
								.orWhere(':meIdAsList <@ note.visibleUserIds')
								// Mentions me
								.orWhere(':meIdAsList <@ note.mentions'),
							))
							.setParameters({ meIdAsList: [me.id] })
						, 'source')
						.innerJoin(MiNote, 'note', 'note.id = source.id');

					this.queryService.generateVisibilityQuery(qb, me);
					this.queryService.generateBlockedHostQueryForNote(qb);
					this.queryService.generateMutedUserQueryForNotes(qb, me);
					this.queryService.generateMutedNoteThreadQuery(qb, me);
					this.queryService.generateBlockedUserQueryForNotes(qb, me);
					// A renote can't mention a user, so it will never appear here anyway.
					//this.queryService.generateMutedUserRenotesQueryForNotes(qb, me);

					if (ps.visibility) {
						qb.andWhere('note.visibility = :visibility', { visibility: ps.visibility });
					}

					if (ps.following) {
						this.queryService
							.andFollowingUser(qb, ':meId', 'note.userId')
							.setParameters({ meId: me.id });
					}

					return qb;
				}, 'source', 'source.id = note.id')
				.innerJoinAndSelect('note.user', 'user')
				.leftJoinAndSelect('note.reply', 'reply')
				.leftJoinAndSelect('note.renote', 'renote')
				.leftJoinAndSelect('reply.user', 'replyUser')
				.leftJoinAndSelect('renote.user', 'renoteUser')
				.limit(ps.limit);

			const mentions = await query.getMany();

			process.nextTick(() => {
				this.activeUsersChart.read(me);
			});

			return await this.noteEntityService.packMany(mentions, me);
		});
	}
}