2023-04-12 11:40:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-14 13:50:05 +09:00
|
|
|
import * as Redis from 'ioredis';
|
2023-04-12 11:40:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import type { NotesRepository, RolesRepository } from '@/models/index.js';
|
|
|
|
import { QueryService } from '@/core/QueryService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['role', 'notes'],
|
|
|
|
|
|
|
|
requireCredential: true,
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchRole: {
|
|
|
|
message: 'No such role.',
|
|
|
|
code: 'NO_SUCH_ROLE',
|
|
|
|
id: 'eb70323a-df61-4dd4-ad90-89c83c7cf26e',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
ref: 'Note',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
export const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
roleId: { type: 'string', format: 'misskey:id' },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
sinceId: { type: 'string', format: 'misskey:id' },
|
|
|
|
untilId: { type: 'string', format: 'misskey:id' },
|
|
|
|
sinceDate: { type: 'integer' },
|
|
|
|
untilDate: { type: 'integer' },
|
|
|
|
},
|
|
|
|
required: ['roleId'],
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.rolesRepository)
|
|
|
|
private rolesRepository: RolesRepository,
|
|
|
|
|
|
|
|
private idService: IdService,
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private queryService: QueryService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const role = await this.rolesRepository.findOneBy({
|
|
|
|
id: ps.roleId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (role == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchRole);
|
|
|
|
}
|
|
|
|
|
2023-04-13 08:33:36 +09:00
|
|
|
const limit = ps.limit + (ps.untilId ? 1 : 0) + (ps.sinceId ? 1 : 0); // untilIdに指定したものも含まれるため+1
|
2023-04-12 11:40:08 +09:00
|
|
|
const noteIdsRes = await this.redisClient.xrevrange(
|
|
|
|
`roleTimeline:${role.id}`,
|
2023-04-13 08:33:36 +09:00
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
|
|
|
ps.sinceId ? this.idService.parse(ps.sinceId).date.getTime() : ps.sinceDate ?? '-',
|
2023-04-12 11:40:08 +09:00
|
|
|
'COUNT', limit);
|
|
|
|
|
|
|
|
if (noteIdsRes.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-04-13 08:33:36 +09:00
|
|
|
const noteIds = noteIdsRes.map(x => x[1][1]).filter(x => x !== ps.untilId && x !== ps.sinceId);
|
2023-04-12 11:40:08 +09:00
|
|
|
|
|
|
|
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.generateVisibilityQuery(query, me);
|
|
|
|
this.queryService.generateMutedUserQuery(query, me);
|
|
|
|
this.queryService.generateBlockedUserQuery(query, me);
|
|
|
|
|
|
|
|
const notes = await query.getMany();
|
|
|
|
notes.sort((a, b) => a.id > b.id ? -1 : 1);
|
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(notes, me);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|