2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-03 11:49:58 +09:00
|
|
|
import Redis from 'ioredis';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-04-08 13:12:49 +09:00
|
|
|
import type { ChannelsRepository, Note, NotesRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
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';
|
2023-04-03 11:49:58 +09:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { ApiError } from '../../error.js';
|
2020-08-18 22:44:21 +09:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['notes', 'channels'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2020-08-18 22:44:21 +09:00
|
|
|
|
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2020-08-18 22:44:21 +09:00
|
|
|
items: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2020-08-18 22:44:21 +09:00
|
|
|
ref: 'Note',
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2020-08-18 22:44:21 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchChannel: {
|
|
|
|
message: 'No such channel.',
|
|
|
|
code: 'NO_SUCH_CHANNEL',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '4d0eeeba-a02c-4c3c-9966-ef60d38d2e7f',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2020-08-18 22:44:21 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
channelId: { 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: ['channelId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
2023-04-03 11:49:58 +09:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
2020-08-18 22:44:21 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Inject(DI.channelsRepository)
|
|
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
|
2023-04-03 11:49:58 +09:00
|
|
|
private idService: IdService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private queryService: QueryService,
|
|
|
|
private activeUsersChart: ActiveUsersChart,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const channel = await this.channelsRepository.findOneBy({
|
|
|
|
id: ps.channelId,
|
|
|
|
});
|
2020-08-18 22:44:21 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (channel == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchChannel);
|
|
|
|
}
|
2020-08-18 22:44:21 +09:00
|
|
|
|
2023-04-08 13:12:49 +09:00
|
|
|
let timeline: Note[] = [];
|
|
|
|
|
2023-04-03 11:49:58 +09:00
|
|
|
const noteIdsRes = await this.redisClient.xrevrange(
|
|
|
|
`channelTimeline:${channel.id}`,
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : '+',
|
|
|
|
'-',
|
|
|
|
'COUNT', ps.limit + 1); // untilIdに指定したものも含まれるため+1
|
|
|
|
|
|
|
|
if (noteIdsRes.length === 0) {
|
2023-04-08 13:12:49 +09:00
|
|
|
//#region Construct query
|
|
|
|
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
|
|
|
|
.andWhere('note.channelId = :channelId', { channelId: channel.id })
|
|
|
|
.innerJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
|
|
|
.leftJoinAndSelect('note.channel', 'channel');
|
|
|
|
|
|
|
|
if (me) {
|
|
|
|
this.queryService.generateMutedUserQuery(query, me);
|
|
|
|
this.queryService.generateMutedNoteQuery(query, me);
|
|
|
|
this.queryService.generateBlockedUserQuery(query, me);
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
timeline = await query.take(ps.limit).getMany();
|
|
|
|
} else {
|
|
|
|
const noteIds = noteIdsRes.map(x => x[1][1]).filter(x => x !== ps.untilId);
|
|
|
|
|
|
|
|
if (noteIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
//#region Construct query
|
|
|
|
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')
|
|
|
|
.leftJoinAndSelect('note.channel', 'channel');
|
|
|
|
|
|
|
|
if (me) {
|
|
|
|
this.queryService.generateMutedUserQuery(query, me);
|
|
|
|
this.queryService.generateMutedNoteQuery(query, me);
|
|
|
|
this.queryService.generateBlockedUserQuery(query, me);
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
timeline = await query.getMany();
|
|
|
|
timeline.sort((a, b) => a.id > b.id ? -1 : 1);
|
2023-04-03 11:49:58 +09:00
|
|
|
}
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (me) this.activeUsersChart.read(me);
|
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(timeline, me);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|