2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { NotesRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-10-06 07:24:25 +02:00
|
|
|
import { FeaturedService } from '@/core/FeaturedService.js';
|
2018-10-25 00:04:15 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2023-02-17 04:38:30 +01:00
|
|
|
allowGet: true,
|
|
|
|
cacheSec: 3600,
|
2018-10-25 00:04:15 +02:00
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-23 03:20:58 +01:00
|
|
|
items: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'Note',
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-23 03:20:58 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-10-25 00:04:15 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
2023-02-25 10:26:35 +01:00
|
|
|
channelId: { type: 'string', nullable: true, format: 'misskey:id' },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
2023-08-17 14:20:58 +02:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2023-10-06 07:24:25 +02:00
|
|
|
private globalNotesRankingCache: string[] = [];
|
|
|
|
private globalNotesRankingCacheLastFetchedAt = 0;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
2018-10-25 00:04:15 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
private noteEntityService: NoteEntityService,
|
2023-10-06 07:24:25 +02:00
|
|
|
private featuredService: FeaturedService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-10-06 07:24:25 +02:00
|
|
|
let noteIds: string[];
|
|
|
|
if (ps.channelId) {
|
|
|
|
noteIds = await this.featuredService.getInChannelNotesRanking(ps.channelId, 50);
|
|
|
|
} else {
|
|
|
|
if (this.globalNotesRankingCacheLastFetchedAt !== 0 && (Date.now() - this.globalNotesRankingCacheLastFetchedAt < 1000 * 60 * 30)) {
|
|
|
|
noteIds = this.globalNotesRankingCache;
|
|
|
|
} else {
|
|
|
|
noteIds = await this.featuredService.getGlobalNotesRanking(100);
|
|
|
|
this.globalNotesRankingCache = noteIds;
|
|
|
|
this.globalNotesRankingCacheLastFetchedAt = Date.now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noteIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
noteIds.sort((a, b) => a > b ? -1 : 1);
|
|
|
|
noteIds.slice(ps.offset, ps.offset + ps.limit);
|
2019-02-20 14:31:21 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const query = this.notesRepository.createQueryBuilder('note')
|
2023-10-06 07:24:25 +02:00
|
|
|
.where('note.id IN (:...noteIds)', { noteIds: noteIds })
|
2022-09-17 20:27:08 +02:00
|
|
|
.innerJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
2023-10-06 07:24:25 +02:00
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
|
|
|
.leftJoinAndSelect('note.channel', 'channel');
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-10-06 07:24:25 +02:00
|
|
|
const notes = await query.getMany();
|
|
|
|
notes.sort((a, b) => a.id > b.id ? -1 : 1);
|
2018-10-25 00:04:15 +02:00
|
|
|
|
2023-10-06 07:24:25 +02:00
|
|
|
// TODO: ミュート等考慮
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(notes, me);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|