2023-10-06 07:24:25 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import * as Redis from 'ioredis';
|
2023-10-06 11:30:08 +02:00
|
|
|
import type { MiNote, MiUser } from '@/models/_.js';
|
2023-10-06 07:24:25 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
|
2023-10-06 09:10:59 +02:00
|
|
|
const GLOBAL_NOTES_RANKING_WINDOW = 1000 * 60 * 60 * 24 * 3; // 3日ごと
|
2023-10-06 11:30:08 +02:00
|
|
|
const PER_USER_NOTES_RANKING_WINDOW = 1000 * 60 * 60 * 24 * 7; // 1週間ごと
|
2023-10-07 05:05:17 +02:00
|
|
|
const HASHTAG_RANKING_WINDOW = 1000 * 60 * 60; // 1時間ごと
|
2023-10-06 09:10:59 +02:00
|
|
|
|
2023-10-06 07:24:25 +02:00
|
|
|
@Injectable()
|
|
|
|
export class FeaturedService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.redis)
|
2023-10-06 09:58:38 +02:00
|
|
|
private redisClient: Redis.Redis, // TODO: 専用のRedisサーバーを設定できるようにする
|
2023-10-06 07:24:25 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-10-06 09:58:38 +02:00
|
|
|
private getCurrentWindow(windowRange: number): number {
|
2023-10-06 07:24:25 +02:00
|
|
|
const passed = new Date().getTime() - new Date(new Date().getFullYear(), 0, 1).getTime();
|
2023-10-06 09:58:38 +02:00
|
|
|
return Math.floor(passed / windowRange);
|
2023-10-06 07:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-10-06 09:58:38 +02:00
|
|
|
private async updateRankingOf(name: string, windowRange: number, element: string, score = 1): Promise<void> {
|
|
|
|
const currentWindow = this.getCurrentWindow(windowRange);
|
2023-10-06 07:24:25 +02:00
|
|
|
const redisTransaction = this.redisClient.multi();
|
|
|
|
redisTransaction.zincrby(
|
2023-10-06 09:58:38 +02:00
|
|
|
`${name}:${currentWindow}`,
|
|
|
|
score,
|
|
|
|
element);
|
2023-11-09 14:51:19 +01:00
|
|
|
redisTransaction.expire(
|
|
|
|
`${name}:${currentWindow}`,
|
|
|
|
(windowRange * 3) / 1000,
|
|
|
|
'NX'); // "NX -- Set expiry only when the key has no expiry" = 有効期限がないときだけ設定
|
2023-10-06 07:24:25 +02:00
|
|
|
await redisTransaction.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-10-09 06:22:58 +02:00
|
|
|
private async getRankingOf(name: string, windowRange: number, threshold: number): Promise<string[]> {
|
2023-10-06 09:58:38 +02:00
|
|
|
const currentWindow = this.getCurrentWindow(windowRange);
|
2023-10-06 07:24:25 +02:00
|
|
|
const previousWindow = currentWindow - 1;
|
|
|
|
|
2023-10-09 08:34:03 +02:00
|
|
|
const redisPipeline = this.redisClient.pipeline();
|
|
|
|
redisPipeline.zrange(
|
|
|
|
`${name}:${currentWindow}`, 0, threshold, 'REV', 'WITHSCORES');
|
|
|
|
redisPipeline.zrange(
|
|
|
|
`${name}:${previousWindow}`, 0, threshold, 'REV', 'WITHSCORES');
|
2023-10-27 11:37:17 +02:00
|
|
|
const [currentRankingResult, previousRankingResult] = await redisPipeline.exec().then(result => result ? result.map(r => (r[1] ?? []) as string[]) : [[], []]);
|
2023-10-06 07:24:25 +02:00
|
|
|
|
2023-10-06 09:58:38 +02:00
|
|
|
const ranking = new Map<string, number>();
|
2023-10-06 07:24:25 +02:00
|
|
|
for (let i = 0; i < currentRankingResult.length; i += 2) {
|
|
|
|
const noteId = currentRankingResult[i];
|
|
|
|
const score = parseInt(currentRankingResult[i + 1], 10);
|
|
|
|
ranking.set(noteId, score);
|
|
|
|
}
|
|
|
|
for (let i = 0; i < previousRankingResult.length; i += 2) {
|
|
|
|
const noteId = previousRankingResult[i];
|
|
|
|
const score = parseInt(previousRankingResult[i + 1], 10);
|
|
|
|
const exist = ranking.get(noteId);
|
|
|
|
if (exist != null) {
|
|
|
|
ranking.set(noteId, (exist + score) / 2);
|
|
|
|
} else {
|
|
|
|
ranking.set(noteId, score);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Array.from(ranking.keys());
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-10-06 10:01:06 +02:00
|
|
|
public updateGlobalNotesRanking(noteId: MiNote['id'], score = 1): Promise<void> {
|
2023-10-06 09:58:38 +02:00
|
|
|
return this.updateRankingOf('featuredGlobalNotesRanking', GLOBAL_NOTES_RANKING_WINDOW, noteId, score);
|
|
|
|
}
|
2023-10-06 07:24:25 +02:00
|
|
|
|
2023-10-06 09:58:38 +02:00
|
|
|
@bindThis
|
2023-10-06 11:30:08 +02:00
|
|
|
public updateInChannelNotesRanking(channelId: MiNote['channelId'], noteId: MiNote['id'], score = 1): Promise<void> {
|
2023-10-06 09:58:38 +02:00
|
|
|
return this.updateRankingOf(`featuredInChannelNotesRanking:${channelId}`, GLOBAL_NOTES_RANKING_WINDOW, noteId, score);
|
|
|
|
}
|
2023-10-06 07:24:25 +02:00
|
|
|
|
2023-10-06 11:30:08 +02:00
|
|
|
@bindThis
|
|
|
|
public updatePerUserNotesRanking(userId: MiUser['id'], noteId: MiNote['id'], score = 1): Promise<void> {
|
|
|
|
return this.updateRankingOf(`featuredPerUserNotesRanking:${userId}`, PER_USER_NOTES_RANKING_WINDOW, noteId, score);
|
|
|
|
}
|
|
|
|
|
2023-10-07 05:05:17 +02:00
|
|
|
@bindThis
|
|
|
|
public updateHashtagsRanking(hashtag: string, score = 1): Promise<void> {
|
|
|
|
return this.updateRankingOf('featuredHashtagsRanking', HASHTAG_RANKING_WINDOW, hashtag, score);
|
|
|
|
}
|
|
|
|
|
2023-10-06 09:58:38 +02:00
|
|
|
@bindThis
|
2023-10-09 06:22:58 +02:00
|
|
|
public getGlobalNotesRanking(threshold: number): Promise<MiNote['id'][]> {
|
|
|
|
return this.getRankingOf('featuredGlobalNotesRanking', GLOBAL_NOTES_RANKING_WINDOW, threshold);
|
2023-10-06 09:58:38 +02:00
|
|
|
}
|
2023-10-06 07:24:25 +02:00
|
|
|
|
2023-10-06 09:58:38 +02:00
|
|
|
@bindThis
|
2023-10-09 06:22:58 +02:00
|
|
|
public getInChannelNotesRanking(channelId: MiNote['channelId'], threshold: number): Promise<MiNote['id'][]> {
|
|
|
|
return this.getRankingOf(`featuredInChannelNotesRanking:${channelId}`, GLOBAL_NOTES_RANKING_WINDOW, threshold);
|
2023-10-06 07:24:25 +02:00
|
|
|
}
|
2023-10-06 11:30:08 +02:00
|
|
|
|
|
|
|
@bindThis
|
2023-10-09 06:22:58 +02:00
|
|
|
public getPerUserNotesRanking(userId: MiUser['id'], threshold: number): Promise<MiNote['id'][]> {
|
|
|
|
return this.getRankingOf(`featuredPerUserNotesRanking:${userId}`, PER_USER_NOTES_RANKING_WINDOW, threshold);
|
2023-10-06 11:30:08 +02:00
|
|
|
}
|
2023-10-07 05:05:17 +02:00
|
|
|
|
|
|
|
@bindThis
|
2023-10-09 06:22:58 +02:00
|
|
|
public getHashtagsRanking(threshold: number): Promise<string[]> {
|
|
|
|
return this.getRankingOf('featuredHashtagsRanking', HASHTAG_RANKING_WINDOW, threshold);
|
2023-10-07 05:05:17 +02:00
|
|
|
}
|
2023-10-06 07:24:25 +02:00
|
|
|
}
|