2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-10-07 03:05:17 +00:00
|
|
|
import * as Redis from 'ioredis';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { normalizeForSearch } from '@/misc/normalize-for-search.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { MiHashtag } from '@/models/Hashtag.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { HashtagsRepository } from '@/models/_.js';
|
2022-12-04 01:16:03 +00:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-10-07 03:05:17 +00:00
|
|
|
import { FeaturedService } from '@/core/FeaturedService.js';
|
2023-10-07 05:05:58 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-12-24 06:23:56 +00:00
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class HashtagService {
|
|
|
|
constructor(
|
2023-10-07 03:05:17 +00:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis, // TODO: 専用のRedisサーバーを設定できるようにする
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
@Inject(DI.hashtagsRepository)
|
|
|
|
private hashtagsRepository: HashtagsRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
2023-10-07 03:05:17 +00:00
|
|
|
private featuredService: FeaturedService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private idService: IdService,
|
2023-10-07 05:05:58 +00:00
|
|
|
private metaService: MetaService,
|
2023-12-24 06:23:56 +00:00
|
|
|
private utilityService: UtilityService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async updateHashtags(user: { id: MiUser['id']; host: MiUser['host']; }, tags: string[]) {
|
2022-09-17 18:27:08 +00:00
|
|
|
for (const tag of tags) {
|
|
|
|
await this.updateHashtag(user, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async updateUsertags(user: MiUser, tags: string[]) {
|
2022-09-17 18:27:08 +00:00
|
|
|
for (const tag of tags) {
|
|
|
|
await this.updateHashtag(user, tag, true, true);
|
|
|
|
}
|
|
|
|
|
2023-10-18 00:54:18 +00:00
|
|
|
for (const tag of user.tags.filter(x => !tags.includes(x))) {
|
2022-09-17 18:27:08 +00:00
|
|
|
await this.updateHashtag(user, tag, true, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async updateHashtag(user: { id: MiUser['id']; host: MiUser['host']; }, tag: string, isUserAttached = false, inc = true) {
|
2022-09-17 18:27:08 +00:00
|
|
|
tag = normalizeForSearch(tag);
|
|
|
|
|
2023-10-07 03:05:17 +00:00
|
|
|
// TODO: サンプリング
|
|
|
|
this.updateHashtagsRanking(tag, user.id);
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const index = await this.hashtagsRepository.findOneBy({ name: tag });
|
|
|
|
|
|
|
|
if (index == null && !inc) return;
|
|
|
|
|
|
|
|
if (index != null) {
|
|
|
|
const q = this.hashtagsRepository.createQueryBuilder('tag').update()
|
|
|
|
.where('name = :name', { name: tag });
|
|
|
|
|
|
|
|
const set = {} as any;
|
|
|
|
|
|
|
|
if (isUserAttached) {
|
|
|
|
if (inc) {
|
|
|
|
// 自分が初めてこのタグを使ったなら
|
|
|
|
if (!index.attachedUserIds.some(id => id === user.id)) {
|
|
|
|
set.attachedUserIds = () => `array_append("attachedUserIds", '${user.id}')`;
|
|
|
|
set.attachedUsersCount = () => '"attachedUsersCount" + 1';
|
|
|
|
}
|
|
|
|
// 自分が(ローカル内で)初めてこのタグを使ったなら
|
|
|
|
if (this.userEntityService.isLocalUser(user) && !index.attachedLocalUserIds.some(id => id === user.id)) {
|
|
|
|
set.attachedLocalUserIds = () => `array_append("attachedLocalUserIds", '${user.id}')`;
|
|
|
|
set.attachedLocalUsersCount = () => '"attachedLocalUsersCount" + 1';
|
|
|
|
}
|
|
|
|
// 自分が(リモートで)初めてこのタグを使ったなら
|
|
|
|
if (this.userEntityService.isRemoteUser(user) && !index.attachedRemoteUserIds.some(id => id === user.id)) {
|
|
|
|
set.attachedRemoteUserIds = () => `array_append("attachedRemoteUserIds", '${user.id}')`;
|
|
|
|
set.attachedRemoteUsersCount = () => '"attachedRemoteUsersCount" + 1';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
set.attachedUserIds = () => `array_remove("attachedUserIds", '${user.id}')`;
|
|
|
|
set.attachedUsersCount = () => '"attachedUsersCount" - 1';
|
|
|
|
if (this.userEntityService.isLocalUser(user)) {
|
|
|
|
set.attachedLocalUserIds = () => `array_remove("attachedLocalUserIds", '${user.id}')`;
|
|
|
|
set.attachedLocalUsersCount = () => '"attachedLocalUsersCount" - 1';
|
|
|
|
} else {
|
|
|
|
set.attachedRemoteUserIds = () => `array_remove("attachedRemoteUserIds", '${user.id}')`;
|
|
|
|
set.attachedRemoteUsersCount = () => '"attachedRemoteUsersCount" - 1';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-07 03:05:17 +00:00
|
|
|
// 自分が初めてこのタグを使ったなら
|
2022-09-17 18:27:08 +00:00
|
|
|
if (!index.mentionedUserIds.some(id => id === user.id)) {
|
|
|
|
set.mentionedUserIds = () => `array_append("mentionedUserIds", '${user.id}')`;
|
|
|
|
set.mentionedUsersCount = () => '"mentionedUsersCount" + 1';
|
|
|
|
}
|
|
|
|
// 自分が(ローカル内で)初めてこのタグを使ったなら
|
|
|
|
if (this.userEntityService.isLocalUser(user) && !index.mentionedLocalUserIds.some(id => id === user.id)) {
|
|
|
|
set.mentionedLocalUserIds = () => `array_append("mentionedLocalUserIds", '${user.id}')`;
|
|
|
|
set.mentionedLocalUsersCount = () => '"mentionedLocalUsersCount" + 1';
|
|
|
|
}
|
|
|
|
// 自分が(リモートで)初めてこのタグを使ったなら
|
|
|
|
if (this.userEntityService.isRemoteUser(user) && !index.mentionedRemoteUserIds.some(id => id === user.id)) {
|
|
|
|
set.mentionedRemoteUserIds = () => `array_append("mentionedRemoteUserIds", '${user.id}')`;
|
|
|
|
set.mentionedRemoteUsersCount = () => '"mentionedRemoteUsersCount" + 1';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(set).length > 0) {
|
|
|
|
q.set(set);
|
|
|
|
q.execute();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isUserAttached) {
|
|
|
|
this.hashtagsRepository.insert({
|
2023-10-16 01:45:22 +00:00
|
|
|
id: this.idService.gen(),
|
2022-09-17 18:27:08 +00:00
|
|
|
name: tag,
|
|
|
|
mentionedUserIds: [],
|
|
|
|
mentionedUsersCount: 0,
|
|
|
|
mentionedLocalUserIds: [],
|
|
|
|
mentionedLocalUsersCount: 0,
|
|
|
|
mentionedRemoteUserIds: [],
|
|
|
|
mentionedRemoteUsersCount: 0,
|
|
|
|
attachedUserIds: [user.id],
|
|
|
|
attachedUsersCount: 1,
|
|
|
|
attachedLocalUserIds: this.userEntityService.isLocalUser(user) ? [user.id] : [],
|
|
|
|
attachedLocalUsersCount: this.userEntityService.isLocalUser(user) ? 1 : 0,
|
|
|
|
attachedRemoteUserIds: this.userEntityService.isRemoteUser(user) ? [user.id] : [],
|
|
|
|
attachedRemoteUsersCount: this.userEntityService.isRemoteUser(user) ? 1 : 0,
|
2023-08-16 08:51:28 +00:00
|
|
|
} as MiHashtag);
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
|
|
|
this.hashtagsRepository.insert({
|
2023-10-16 01:45:22 +00:00
|
|
|
id: this.idService.gen(),
|
2022-09-17 18:27:08 +00:00
|
|
|
name: tag,
|
|
|
|
mentionedUserIds: [user.id],
|
|
|
|
mentionedUsersCount: 1,
|
|
|
|
mentionedLocalUserIds: this.userEntityService.isLocalUser(user) ? [user.id] : [],
|
|
|
|
mentionedLocalUsersCount: this.userEntityService.isLocalUser(user) ? 1 : 0,
|
|
|
|
mentionedRemoteUserIds: this.userEntityService.isRemoteUser(user) ? [user.id] : [],
|
|
|
|
mentionedRemoteUsersCount: this.userEntityService.isRemoteUser(user) ? 1 : 0,
|
|
|
|
attachedUserIds: [],
|
|
|
|
attachedUsersCount: 0,
|
|
|
|
attachedLocalUserIds: [],
|
|
|
|
attachedLocalUsersCount: 0,
|
|
|
|
attachedRemoteUserIds: [],
|
|
|
|
attachedRemoteUsersCount: 0,
|
2023-08-16 08:51:28 +00:00
|
|
|
} as MiHashtag);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-07 03:05:17 +00:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async updateHashtagsRanking(hashtag: string, userId: MiUser['id']): Promise<void> {
|
2023-10-07 05:05:58 +00:00
|
|
|
const instance = await this.metaService.fetch();
|
|
|
|
const hiddenTags = instance.hiddenTags.map(t => normalizeForSearch(t));
|
|
|
|
if (hiddenTags.includes(hashtag)) return;
|
2024-02-09 01:07:18 +00:00
|
|
|
if (this.utilityService.isKeyWordIncluded(hashtag, instance.sensitiveWords)) return;
|
2023-10-07 03:05:17 +00:00
|
|
|
|
|
|
|
// YYYYMMDDHHmm (10分間隔)
|
|
|
|
const now = new Date();
|
|
|
|
now.setMinutes(Math.floor(now.getMinutes() / 10) * 10, 0, 0);
|
|
|
|
const window = `${now.getUTCFullYear()}${(now.getUTCMonth() + 1).toString().padStart(2, '0')}${now.getUTCDate().toString().padStart(2, '0')}${now.getUTCHours().toString().padStart(2, '0')}${now.getUTCMinutes().toString().padStart(2, '0')}`;
|
|
|
|
|
|
|
|
const exist = await this.redisClient.sismember(`hashtagUsers:${hashtag}`, userId);
|
|
|
|
if (exist === 1) return;
|
|
|
|
|
|
|
|
this.featuredService.updateHashtagsRanking(hashtag, 1);
|
|
|
|
|
|
|
|
const redisPipeline = this.redisClient.pipeline();
|
|
|
|
|
|
|
|
// チャート用
|
2023-10-07 22:51:10 +00:00
|
|
|
redisPipeline.pfadd(`hashtagUsers:${hashtag}:${window}`, userId);
|
2023-10-07 03:05:17 +00:00
|
|
|
redisPipeline.expire(`hashtagUsers:${hashtag}:${window}`,
|
|
|
|
60 * 60 * 24 * 3, // 3日間
|
|
|
|
'NX', // "NX -- Set expiry only when the key has no expiry" = 有効期限がないときだけ設定
|
|
|
|
);
|
|
|
|
|
|
|
|
// ユニークカウント用
|
2023-10-07 22:51:10 +00:00
|
|
|
// TODO: Bloom Filter を使うようにしても良さそう
|
2023-10-07 03:05:17 +00:00
|
|
|
redisPipeline.sadd(`hashtagUsers:${hashtag}`, userId);
|
|
|
|
redisPipeline.expire(`hashtagUsers:${hashtag}`,
|
|
|
|
60 * 60, // 1時間
|
|
|
|
'NX', // "NX -- Set expiry only when the key has no expiry" = 有効期限がないときだけ設定
|
|
|
|
);
|
|
|
|
|
|
|
|
redisPipeline.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async getChart(hashtag: string, range: number): Promise<number[]> {
|
|
|
|
const now = new Date();
|
|
|
|
now.setMinutes(Math.floor(now.getMinutes() / 10) * 10, 0, 0);
|
|
|
|
|
|
|
|
const redisPipeline = this.redisClient.pipeline();
|
|
|
|
|
|
|
|
for (let i = 0; i < range; i++) {
|
|
|
|
const window = `${now.getUTCFullYear()}${(now.getUTCMonth() + 1).toString().padStart(2, '0')}${now.getUTCDate().toString().padStart(2, '0')}${now.getUTCHours().toString().padStart(2, '0')}${now.getUTCMinutes().toString().padStart(2, '0')}`;
|
2023-10-07 22:51:10 +00:00
|
|
|
redisPipeline.pfcount(`hashtagUsers:${hashtag}:${window}`);
|
2023-10-07 03:05:17 +00:00
|
|
|
now.setMinutes(now.getMinutes() - (i * 10), 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await redisPipeline.exec();
|
|
|
|
|
|
|
|
if (result == null) return [];
|
|
|
|
|
|
|
|
return result.map(x => x[1]) as number[];
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async getCharts(hashtags: string[], range: number): Promise<Record<string, number[]>> {
|
|
|
|
const now = new Date();
|
|
|
|
now.setMinutes(Math.floor(now.getMinutes() / 10) * 10, 0, 0);
|
|
|
|
|
|
|
|
const redisPipeline = this.redisClient.pipeline();
|
|
|
|
|
|
|
|
for (let i = 0; i < range; i++) {
|
|
|
|
const window = `${now.getUTCFullYear()}${(now.getUTCMonth() + 1).toString().padStart(2, '0')}${now.getUTCDate().toString().padStart(2, '0')}${now.getUTCHours().toString().padStart(2, '0')}${now.getUTCMinutes().toString().padStart(2, '0')}`;
|
|
|
|
for (const hashtag of hashtags) {
|
2023-10-07 22:51:10 +00:00
|
|
|
redisPipeline.pfcount(`hashtagUsers:${hashtag}:${window}`);
|
2023-10-07 03:05:17 +00:00
|
|
|
}
|
|
|
|
now.setMinutes(now.getMinutes() - (i * 10), 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await redisPipeline.exec();
|
|
|
|
|
|
|
|
if (result == null) return {};
|
|
|
|
|
|
|
|
// key is hashtag
|
|
|
|
const charts = {} as Record<string, number[]>;
|
|
|
|
for (const hashtag of hashtags) {
|
|
|
|
charts[hashtag] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < range; i++) {
|
|
|
|
for (let j = 0; j < hashtags.length; j++) {
|
|
|
|
charts[hashtags[j]].push(result[(i * hashtags.length) + j][1] as number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return charts;
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|