2023-02-16 14:09:41 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DataSource, In } from 'typeorm';
|
|
|
|
import * as mfm from 'mfm-js';
|
|
|
|
import { ModuleRef } from '@nestjs/core';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import type { Packed } from '@/misc/schema.js';
|
|
|
|
import { nyaize } from '@/misc/nyaize.js';
|
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
|
|
|
import type { NoteReaction } from '@/models/entities/NoteReaction.js';
|
2022-09-22 21:21:31 +00:00
|
|
|
import type { UsersRepository, NotesRepository, FollowingsRepository, PollsRepository, PollVotesRepository, NoteReactionsRepository, ChannelsRepository, DriveFilesRepository } from '@/models/index.js';
|
2022-12-30 23:43:13 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-04 07:48:50 +00:00
|
|
|
import { isNotNull } from '@/misc/is-not-null.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { OnModuleInit } from '@nestjs/common';
|
|
|
|
import type { CustomEmojiService } from '../CustomEmojiService.js';
|
|
|
|
import type { ReactionService } from '../ReactionService.js';
|
|
|
|
import type { UserEntityService } from './UserEntityService.js';
|
|
|
|
import type { DriveFileEntityService } from './DriveFileEntityService.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NoteEntityService implements OnModuleInit {
|
|
|
|
private userEntityService: UserEntityService;
|
|
|
|
private driveFileEntityService: DriveFileEntityService;
|
|
|
|
private customEmojiService: CustomEmojiService;
|
|
|
|
private reactionService: ReactionService;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private moduleRef: ModuleRef,
|
|
|
|
|
|
|
|
@Inject(DI.db)
|
|
|
|
private db: DataSource,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pollsRepository)
|
|
|
|
private pollsRepository: PollsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pollVotesRepository)
|
|
|
|
private pollVotesRepository: PollVotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.noteReactionsRepository)
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.channelsRepository)
|
|
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
//private userEntityService: UserEntityService,
|
|
|
|
//private driveFileEntityService: DriveFileEntityService,
|
|
|
|
//private customEmojiService: CustomEmojiService,
|
|
|
|
//private reactionService: ReactionService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
onModuleInit() {
|
|
|
|
this.userEntityService = this.moduleRef.get('UserEntityService');
|
|
|
|
this.driveFileEntityService = this.moduleRef.get('DriveFileEntityService');
|
|
|
|
this.customEmojiService = this.moduleRef.get('CustomEmojiService');
|
|
|
|
this.reactionService = this.moduleRef.get('ReactionService');
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async hideNote(packedNote: Packed<'Note'>, meId: User['id'] | null) {
|
2022-09-17 18:27:08 +00:00
|
|
|
// TODO: isVisibleForMe を使うようにしても良さそう(型違うけど)
|
|
|
|
let hide = false;
|
|
|
|
|
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
|
|
|
if (packedNote.visibility === 'specified') {
|
|
|
|
if (meId == null) {
|
|
|
|
hide = true;
|
|
|
|
} else if (meId === packedNote.userId) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
// 指定されているかどうか
|
|
|
|
const specified = packedNote.visibleUserIds!.some((id: any) => meId === id);
|
|
|
|
|
|
|
|
if (specified) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
hide = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
|
|
|
|
if (packedNote.visibility === 'followers') {
|
|
|
|
if (meId == null) {
|
|
|
|
hide = true;
|
|
|
|
} else if (meId === packedNote.userId) {
|
|
|
|
hide = false;
|
|
|
|
} else if (packedNote.reply && (meId === packedNote.reply.userId)) {
|
|
|
|
// 自分の投稿に対するリプライ
|
|
|
|
hide = false;
|
|
|
|
} else if (packedNote.mentions && packedNote.mentions.some(id => meId === id)) {
|
|
|
|
// 自分へのメンション
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
// フォロワーかどうか
|
|
|
|
const following = await this.followingsRepository.findOneBy({
|
|
|
|
followeeId: packedNote.userId,
|
|
|
|
followerId: meId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
hide = true;
|
|
|
|
} else {
|
|
|
|
hide = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hide) {
|
|
|
|
packedNote.visibleUserIds = undefined;
|
|
|
|
packedNote.fileIds = [];
|
|
|
|
packedNote.files = [];
|
|
|
|
packedNote.text = null;
|
|
|
|
packedNote.poll = undefined;
|
|
|
|
packedNote.cw = null;
|
|
|
|
packedNote.isHidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async populatePoll(note: Note, meId: User['id'] | null) {
|
2022-09-17 18:27:08 +00:00
|
|
|
const poll = await this.pollsRepository.findOneByOrFail({ noteId: note.id });
|
|
|
|
const choices = poll.choices.map(c => ({
|
|
|
|
text: c,
|
|
|
|
votes: poll.votes[poll.choices.indexOf(c)],
|
|
|
|
isVoted: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (meId) {
|
|
|
|
if (poll.multiple) {
|
|
|
|
const votes = await this.pollVotesRepository.findBy({
|
|
|
|
userId: meId,
|
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const myChoices = votes.map(v => v.choice);
|
|
|
|
for (const myChoice of myChoices) {
|
|
|
|
choices[myChoice].isVoted = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const vote = await this.pollVotesRepository.findOneBy({
|
|
|
|
userId: meId,
|
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (vote) {
|
|
|
|
choices[vote.choice].isVoted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
multiple: poll.multiple,
|
|
|
|
expiresAt: poll.expiresAt,
|
|
|
|
choices,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async populateMyReaction(note: Note, meId: User['id'], _hint_?: {
|
2022-09-17 18:27:08 +00:00
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
|
|
|
}) {
|
|
|
|
if (_hint_?.myReactions) {
|
|
|
|
const reaction = _hint_.myReactions.get(note.id);
|
|
|
|
if (reaction) {
|
|
|
|
return this.reactionService.convertLegacyReaction(reaction.reaction);
|
|
|
|
} else if (reaction === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
|
|
|
|
}
|
|
|
|
|
|
|
|
const reaction = await this.noteReactionsRepository.findOneBy({
|
|
|
|
userId: meId,
|
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reaction) {
|
|
|
|
return this.reactionService.convertLegacyReaction(reaction.reaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async isVisibleForMe(note: Note, meId: User['id'] | null): Promise<boolean> {
|
|
|
|
// This code must always be synchronized with the checks in generateVisibilityQuery.
|
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
|
|
|
if (note.visibility === 'specified') {
|
|
|
|
if (meId == null) {
|
|
|
|
return false;
|
|
|
|
} else if (meId === note.userId) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// 指定されているかどうか
|
|
|
|
return note.visibleUserIds.some((id: any) => meId === id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
|
|
|
|
if (note.visibility === 'followers') {
|
|
|
|
if (meId == null) {
|
|
|
|
return false;
|
|
|
|
} else if (meId === note.userId) {
|
|
|
|
return true;
|
|
|
|
} else if (note.reply && (meId === note.reply.userId)) {
|
|
|
|
// 自分の投稿に対するリプライ
|
|
|
|
return true;
|
|
|
|
} else if (note.mentions && note.mentions.some(id => meId === id)) {
|
|
|
|
// 自分へのメンション
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// フォロワーかどうか
|
|
|
|
const [following, user] = await Promise.all([
|
|
|
|
this.followingsRepository.count({
|
|
|
|
where: {
|
|
|
|
followeeId: note.userId,
|
|
|
|
followerId: meId,
|
|
|
|
},
|
|
|
|
take: 1,
|
|
|
|
}),
|
|
|
|
this.usersRepository.findOneByOrFail({ id: meId }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
/* If we know the following, everyhting is fine.
|
|
|
|
|
|
|
|
But if we do not know the following, it might be that both the
|
|
|
|
author of the note and the author of the like are remote users,
|
|
|
|
in which case we can never know the following. Instead we have
|
|
|
|
to assume that the users are following each other.
|
|
|
|
*/
|
|
|
|
return following > 0 || (note.userHost != null && user.host != null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-04 07:48:50 +00:00
|
|
|
@bindThis
|
|
|
|
public async packAttachedFiles(fileIds: Note['fileIds'], packedFiles: Map<Note['fileIds'][number], Packed<'DriveFile'> | null>): Promise<Packed<'DriveFile'>[]> {
|
|
|
|
const missingIds = [];
|
|
|
|
for (const id of fileIds) {
|
|
|
|
if (!packedFiles.has(id)) missingIds.push(id);
|
|
|
|
}
|
|
|
|
if (missingIds.length) {
|
|
|
|
const additionalMap = await this.driveFileEntityService.packManyByIdsMap(missingIds);
|
|
|
|
for (const [k, v] of additionalMap) {
|
|
|
|
packedFiles.set(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileIds.map(id => packedFiles.get(id)).filter(isNotNull);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async pack(
|
|
|
|
src: Note['id'] | Note,
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
|
|
|
_hint_?: {
|
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
2023-03-04 07:48:50 +00:00
|
|
|
packedFiles: Map<Note['fileIds'][number], Packed<'DriveFile'> | null>;
|
2022-09-17 18:27:08 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
): Promise<Packed<'Note'>> {
|
|
|
|
const opts = Object.assign({
|
|
|
|
detail: true,
|
|
|
|
skipHide: false,
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const meId = me ? me.id : null;
|
|
|
|
const note = typeof src === 'object' ? src : await this.notesRepository.findOneByOrFail({ id: src });
|
|
|
|
const host = note.userHost;
|
|
|
|
|
|
|
|
let text = note.text;
|
|
|
|
|
|
|
|
if (note.name && (note.url ?? note.uri)) {
|
2022-09-22 21:21:31 +00:00
|
|
|
text = `【${note.name}】\n${(note.text ?? '').trim()}\n\n${note.url ?? note.uri}`;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const channel = note.channelId
|
|
|
|
? note.channel
|
|
|
|
? note.channel
|
|
|
|
: await this.channelsRepository.findOneBy({ id: note.channelId })
|
|
|
|
: null;
|
|
|
|
|
2023-01-26 06:48:12 +00:00
|
|
|
const reactionEmojiNames = Object.keys(note.reactions)
|
|
|
|
.filter(x => x.startsWith(':') && x.includes('@') && !x.includes('@.')) // リモートカスタム絵文字のみ
|
|
|
|
.map(x => this.reactionService.decodeReaction(x).reaction.replaceAll(':', ''));
|
2023-03-04 07:48:50 +00:00
|
|
|
const packedFiles = options?._hint_?.packedFiles;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
const packed: Packed<'Note'> = await awaitAll({
|
|
|
|
id: note.id,
|
|
|
|
createdAt: note.createdAt.toISOString(),
|
|
|
|
userId: note.userId,
|
|
|
|
user: this.userEntityService.pack(note.user ?? note.userId, me, {
|
|
|
|
detail: false,
|
|
|
|
}),
|
|
|
|
text: text,
|
|
|
|
cw: note.cw,
|
|
|
|
visibility: note.visibility,
|
|
|
|
localOnly: note.localOnly ?? undefined,
|
|
|
|
visibleUserIds: note.visibility === 'specified' ? note.visibleUserIds : undefined,
|
|
|
|
renoteCount: note.renoteCount,
|
|
|
|
repliesCount: note.repliesCount,
|
|
|
|
reactions: this.reactionService.convertLegacyReactions(note.reactions),
|
2023-01-26 06:48:12 +00:00
|
|
|
reactionEmojis: this.customEmojiService.populateEmojis(reactionEmojiNames, host),
|
|
|
|
emojis: host != null ? this.customEmojiService.populateEmojis(note.emojis, host) : undefined,
|
2022-09-17 18:27:08 +00:00
|
|
|
tags: note.tags.length > 0 ? note.tags : undefined,
|
|
|
|
fileIds: note.fileIds,
|
2023-03-04 07:48:50 +00:00
|
|
|
files: packedFiles != null ? this.packAttachedFiles(note.fileIds, packedFiles) : this.driveFileEntityService.packManyByIds(note.fileIds),
|
2022-09-17 18:27:08 +00:00
|
|
|
replyId: note.replyId,
|
|
|
|
renoteId: note.renoteId,
|
|
|
|
channelId: note.channelId ?? undefined,
|
|
|
|
channel: channel ? {
|
|
|
|
id: channel.id,
|
|
|
|
name: channel.name,
|
|
|
|
} : undefined,
|
|
|
|
mentions: note.mentions.length > 0 ? note.mentions : undefined,
|
|
|
|
uri: note.uri ?? undefined,
|
|
|
|
url: note.url ?? undefined,
|
|
|
|
|
|
|
|
...(opts.detail ? {
|
|
|
|
reply: note.replyId ? this.pack(note.reply ?? note.replyId, me, {
|
|
|
|
detail: false,
|
|
|
|
_hint_: options?._hint_,
|
|
|
|
}) : undefined,
|
|
|
|
|
|
|
|
renote: note.renoteId ? this.pack(note.renote ?? note.renoteId, me, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options?._hint_,
|
|
|
|
}) : undefined,
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
poll: note.hasPoll ? this.populatePoll(note, meId) : undefined,
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
...(meId ? {
|
2022-09-18 18:11:50 +00:00
|
|
|
myReaction: this.populateMyReaction(note, meId, options?._hint_),
|
2022-09-17 18:27:08 +00:00
|
|
|
} : {}),
|
|
|
|
} : {}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (packed.user.isCat && packed.text) {
|
|
|
|
const tokens = packed.text ? mfm.parse(packed.text) : [];
|
2022-11-17 00:34:23 +00:00
|
|
|
function nyaizeNode(node: mfm.MfmNode) {
|
|
|
|
if (node.type === 'quote') return;
|
2022-09-17 18:27:08 +00:00
|
|
|
if (node.type === 'text') {
|
|
|
|
node.props.text = nyaize(node.props.text);
|
|
|
|
}
|
2022-11-17 00:34:23 +00:00
|
|
|
if (node.children) {
|
|
|
|
for (const child of node.children) {
|
|
|
|
nyaizeNode(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const node of tokens) {
|
|
|
|
nyaizeNode(node);
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
packed.text = mfm.toString(tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.skipHide) {
|
2022-09-18 18:11:50 +00:00
|
|
|
await this.hideNote(packed, meId);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return packed;
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async packMany(
|
|
|
|
notes: Note[],
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
if (notes.length === 0) return [];
|
|
|
|
|
|
|
|
const meId = me ? me.id : null;
|
|
|
|
const myReactionsMap = new Map<Note['id'], NoteReaction | null>();
|
|
|
|
if (meId) {
|
|
|
|
const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!);
|
|
|
|
const targets = [...notes.map(n => n.id), ...renoteIds];
|
|
|
|
const myReactions = await this.noteReactionsRepository.findBy({
|
|
|
|
userId: meId,
|
|
|
|
noteId: In(targets),
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) ?? null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 06:48:12 +00:00
|
|
|
await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes));
|
2023-03-04 07:48:50 +00:00
|
|
|
// TODO: 本当は renote とか reply がないのに renoteId とか replyId があったらここで解決しておく
|
|
|
|
const fileIds = notes.map(n => [n.fileIds, n.renote?.fileIds, n.reply?.fileIds]).flat(2).filter(isNotNull);
|
|
|
|
const packedFiles = await this.driveFileEntityService.packManyByIdsMap(fileIds);
|
2023-01-26 06:48:12 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return await Promise.all(notes.map(n => this.pack(n, me, {
|
|
|
|
...options,
|
|
|
|
_hint_: {
|
|
|
|
myReactions: myReactionsMap,
|
2023-03-04 07:48:50 +00:00
|
|
|
packedFiles,
|
2022-09-17 18:27:08 +00:00
|
|
|
},
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async countSameRenotes(userId: string, renoteId: string, excludeNoteId: string | undefined): Promise<number> {
|
|
|
|
// 指定したユーザーの指定したノートのリノートがいくつあるか数える
|
|
|
|
const query = this.notesRepository.createQueryBuilder('note')
|
|
|
|
.where('note.userId = :userId', { userId })
|
|
|
|
.andWhere('note.renoteId = :renoteId', { renoteId });
|
|
|
|
|
|
|
|
// 指定した投稿を除く
|
|
|
|
if (excludeNoteId) {
|
|
|
|
query.andWhere('note.id != :excludeNoteId', { excludeNoteId });
|
|
|
|
}
|
|
|
|
|
|
|
|
return await query.getCount();
|
|
|
|
}
|
|
|
|
}
|