2019-04-07 07:50:36 -05:00
|
|
|
import { EntityRepository, Repository, In } from 'typeorm';
|
2021-04-01 20:36:11 -05:00
|
|
|
import * as mfm from 'mfm-js';
|
2021-08-19 08:04:15 -05:00
|
|
|
import { Note } from '@/models/entities/note';
|
|
|
|
import { User } from '@/models/entities/user';
|
2021-08-19 07:55:45 -05:00
|
|
|
import { Users, PollVotes, DriveFiles, NoteReactions, Followings, Polls, Channels } from '../index';
|
2021-09-22 08:35:55 -05:00
|
|
|
import { Packed } from '@/misc/schema';
|
2021-08-19 07:55:45 -05:00
|
|
|
import { nyaize } from '@/misc/nyaize';
|
2021-08-19 08:04:15 -05:00
|
|
|
import { awaitAll } from '@/prelude/await-all';
|
2021-08-19 07:55:45 -05:00
|
|
|
import { convertLegacyReaction, convertLegacyReactions, decodeReaction } from '@/misc/reaction-lib';
|
2021-08-19 08:04:15 -05:00
|
|
|
import { NoteReaction } from '@/models/entities/note-reaction';
|
2021-08-19 07:55:45 -05:00
|
|
|
import { aggregateNoteEmojis, populateEmojis, prefetchEmojis } from '@/misc/populate-emojis';
|
2019-04-23 08:35:26 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
@EntityRepository(Note)
|
|
|
|
export class NoteRepository extends Repository<Note> {
|
2021-09-05 02:25:30 -05:00
|
|
|
public async isVisibleForMe(note: Note, meId: User['id'] | null): Promise<boolean> {
|
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
|
|
|
if (note.visibility === 'specified') {
|
|
|
|
if (meId == null) {
|
|
|
|
return false;
|
|
|
|
} else if (meId === note.userId) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// 指定されているかどうか
|
|
|
|
const specified = note.visibleUserIds.some((id: any) => meId === id);
|
|
|
|
|
|
|
|
if (specified) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 = await Followings.findOne({
|
|
|
|
followeeId: note.userId,
|
2021-12-09 08:58:30 -06:00
|
|
|
followerId: meId,
|
2021-09-05 02:25:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-22 08:35:55 -05:00
|
|
|
private async hideNote(packedNote: Packed<'Note'>, meId: User['id'] | null) {
|
2021-09-05 02:25:30 -05:00
|
|
|
// TODO: isVisibleForMe を使うようにしても良さそう(型違うけど)
|
2019-04-07 07:50:36 -05:00
|
|
|
let hide = false;
|
|
|
|
|
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
2019-04-23 08:35:26 -05:00
|
|
|
if (packedNote.visibility === 'specified') {
|
2019-04-07 07:50:36 -05:00
|
|
|
if (meId == null) {
|
|
|
|
hide = true;
|
|
|
|
} else if (meId === packedNote.userId) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
// 指定されているかどうか
|
2019-04-23 08:35:26 -05:00
|
|
|
const specified = packedNote.visibleUserIds!.some((id: any) => meId === id);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
if (specified) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
hide = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
|
2020-04-03 18:46:54 -05:00
|
|
|
if (packedNote.visibility === 'followers') {
|
2019-04-07 07:50:36 -05:00
|
|
|
if (meId == null) {
|
|
|
|
hide = true;
|
|
|
|
} else if (meId === packedNote.userId) {
|
|
|
|
hide = false;
|
2021-09-11 11:12:23 -05:00
|
|
|
} else if (packedNote.reply && (meId === packedNote.reply.userId)) {
|
2019-04-07 07:50:36 -05:00
|
|
|
// 自分の投稿に対するリプライ
|
|
|
|
hide = false;
|
2019-04-23 08:35:26 -05:00
|
|
|
} else if (packedNote.mentions && packedNote.mentions.some(id => meId === id)) {
|
2019-04-07 07:50:36 -05:00
|
|
|
// 自分へのメンション
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
// フォロワーかどうか
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followeeId: packedNote.userId,
|
2021-12-09 08:58:30 -06:00
|
|
|
followerId: meId,
|
2019-04-07 07:50:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
hide = true;
|
|
|
|
} else {
|
|
|
|
hide = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hide) {
|
2019-04-23 08:35:26 -05:00
|
|
|
packedNote.visibleUserIds = undefined;
|
2019-04-07 07:50:36 -05:00
|
|
|
packedNote.fileIds = [];
|
|
|
|
packedNote.files = [];
|
|
|
|
packedNote.text = null;
|
2019-04-23 08:35:26 -05:00
|
|
|
packedNote.poll = undefined;
|
2019-04-07 07:50:36 -05:00
|
|
|
packedNote.cw = null;
|
|
|
|
packedNote.isHidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async pack(
|
|
|
|
src: Note['id'] | Note,
|
2021-03-23 21:05:37 -05:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-07 07:50:36 -05:00
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
2021-03-18 20:53:09 -05:00
|
|
|
_hint_?: {
|
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
|
|
|
};
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
2021-09-22 08:35:55 -05:00
|
|
|
): Promise<Packed<'Note'>> {
|
2019-04-07 07:50:36 -05:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: true,
|
2021-12-09 08:58:30 -06:00
|
|
|
skipHide: false,
|
2019-04-07 07:50:36 -05:00
|
|
|
}, options);
|
|
|
|
|
2021-03-23 21:05:37 -05:00
|
|
|
const meId = me ? me.id : null;
|
2021-02-13 00:33:38 -06:00
|
|
|
const note = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-04-07 07:50:36 -05:00
|
|
|
const host = note.userHost;
|
|
|
|
|
|
|
|
async function populatePoll() {
|
2021-02-13 00:33:38 -06:00
|
|
|
const poll = await Polls.findOneOrFail(note.id);
|
2019-04-07 07:50:36 -05:00
|
|
|
const choices = poll.choices.map(c => ({
|
|
|
|
text: c,
|
|
|
|
votes: poll.votes[poll.choices.indexOf(c)],
|
2021-12-09 08:58:30 -06:00
|
|
|
isVoted: false,
|
2019-04-07 07:50:36 -05:00
|
|
|
}));
|
|
|
|
|
|
|
|
if (poll.multiple) {
|
|
|
|
const votes = await PollVotes.find({
|
2019-04-12 11:43:22 -05:00
|
|
|
userId: meId!,
|
2021-12-09 08:58:30 -06:00
|
|
|
noteId: note.id,
|
2019-04-07 07:50:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const myChoices = votes.map(v => v.choice);
|
|
|
|
for (const myChoice of myChoices) {
|
|
|
|
choices[myChoice].isVoted = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const vote = await PollVotes.findOne({
|
2019-04-12 11:43:22 -05:00
|
|
|
userId: meId!,
|
2021-12-09 08:58:30 -06:00
|
|
|
noteId: note.id,
|
2019-04-07 07:50:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (vote) {
|
|
|
|
choices[vote.choice].isVoted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
multiple: poll.multiple,
|
|
|
|
expiresAt: poll.expiresAt,
|
2021-12-09 08:58:30 -06:00
|
|
|
choices,
|
2019-04-07 07:50:36 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function populateMyReaction() {
|
2021-03-18 20:53:09 -05:00
|
|
|
if (options?._hint_?.myReactions) {
|
|
|
|
const reaction = options._hint_.myReactions.get(note.id);
|
|
|
|
if (reaction) {
|
|
|
|
return convertLegacyReaction(reaction.reaction);
|
|
|
|
} else if (reaction === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
|
|
|
|
}
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const reaction = await NoteReactions.findOne({
|
2019-04-12 11:43:22 -05:00
|
|
|
userId: meId!,
|
2019-04-07 07:50:36 -05:00
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reaction) {
|
2020-01-29 13:37:25 -06:00
|
|
|
return convertLegacyReaction(reaction.reaction);
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
|
|
|
|
2019-04-13 01:02:15 -05:00
|
|
|
return undefined;
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let text = note.text;
|
|
|
|
|
2020-04-02 07:59:14 -05:00
|
|
|
if (note.name && (note.url || note.uri)) {
|
|
|
|
text = `【${note.name}】\n${(note.text || '').trim()}\n\n${note.url || note.uri}`;
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
|
|
|
|
2020-08-18 08:44:21 -05:00
|
|
|
const channel = note.channelId
|
|
|
|
? note.channel
|
|
|
|
? note.channel
|
|
|
|
: await Channels.findOne(note.channelId)
|
|
|
|
: null;
|
|
|
|
|
2021-03-21 10:44:38 -05:00
|
|
|
const reactionEmojiNames = Object.keys(note.reactions).filter(x => x?.startsWith(':')).map(x => decodeReaction(x).reaction).map(x => x.replace(/:/g, ''));
|
|
|
|
|
2022-01-18 07:27:10 -06:00
|
|
|
const packed: Packed<'Note'> = await awaitAll({
|
2019-04-07 07:50:36 -05:00
|
|
|
id: note.id,
|
2019-04-23 08:35:26 -05:00
|
|
|
createdAt: note.createdAt.toISOString(),
|
2019-04-07 07:50:36 -05:00
|
|
|
userId: note.userId,
|
2021-03-23 21:05:37 -05:00
|
|
|
user: Users.pack(note.user || note.userId, me, {
|
2021-03-19 23:54:59 -05:00
|
|
|
detail: false,
|
|
|
|
}),
|
2019-04-07 07:50:36 -05:00
|
|
|
text: text,
|
|
|
|
cw: note.cw,
|
|
|
|
visibility: note.visibility,
|
2019-04-13 00:55:59 -05:00
|
|
|
localOnly: note.localOnly || undefined,
|
|
|
|
visibleUserIds: note.visibility === 'specified' ? note.visibleUserIds : undefined,
|
2019-04-08 02:49:05 -05:00
|
|
|
renoteCount: note.renoteCount,
|
|
|
|
repliesCount: note.repliesCount,
|
2020-02-18 15:36:50 -06:00
|
|
|
reactions: convertLegacyReactions(note.reactions),
|
2019-06-07 06:40:05 -05:00
|
|
|
tags: note.tags.length > 0 ? note.tags : undefined,
|
2021-03-21 10:44:38 -05:00
|
|
|
emojis: populateEmojis(note.emojis.concat(reactionEmojiNames), host),
|
2019-04-07 07:50:36 -05:00
|
|
|
fileIds: note.fileIds,
|
|
|
|
files: DriveFiles.packMany(note.fileIds),
|
|
|
|
replyId: note.replyId,
|
|
|
|
renoteId: note.renoteId,
|
2020-08-18 08:44:21 -05:00
|
|
|
channelId: note.channelId || undefined,
|
|
|
|
channel: channel ? {
|
|
|
|
id: channel.id,
|
|
|
|
name: channel.name,
|
|
|
|
} : undefined,
|
2019-04-17 11:05:40 -05:00
|
|
|
mentions: note.mentions.length > 0 ? note.mentions : undefined,
|
2019-04-16 17:25:34 -05:00
|
|
|
uri: note.uri || undefined,
|
2020-04-02 07:59:14 -05:00
|
|
|
url: note.url || undefined,
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
...(opts.detail ? {
|
2021-03-23 21:05:37 -05:00
|
|
|
reply: note.replyId ? this.pack(note.reply || note.replyId, me, {
|
2021-03-18 20:53:09 -05:00
|
|
|
detail: false,
|
2021-12-09 08:58:30 -06:00
|
|
|
_hint_: options?._hint_,
|
2019-04-13 00:55:59 -05:00
|
|
|
}) : undefined,
|
2019-04-07 07:50:36 -05:00
|
|
|
|
2021-03-23 21:05:37 -05:00
|
|
|
renote: note.renoteId ? this.pack(note.renote || note.renoteId, me, {
|
2021-03-18 20:53:09 -05:00
|
|
|
detail: true,
|
2021-12-09 08:58:30 -06:00
|
|
|
_hint_: options?._hint_,
|
2019-04-13 00:55:59 -05:00
|
|
|
}) : undefined,
|
2019-04-07 07:50:36 -05:00
|
|
|
|
2019-04-13 00:55:59 -05:00
|
|
|
poll: note.hasPoll ? populatePoll() : undefined,
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
...(meId ? {
|
2021-12-09 08:58:30 -06:00
|
|
|
myReaction: populateMyReaction(),
|
|
|
|
} : {}),
|
|
|
|
} : {}),
|
2019-04-07 07:50:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (packed.user.isCat && packed.text) {
|
2021-04-01 20:36:11 -05:00
|
|
|
const tokens = packed.text ? mfm.parse(packed.text) : [];
|
|
|
|
mfm.inspect(tokens, node => {
|
|
|
|
if (node.type === 'text') {
|
2021-09-27 11:50:02 -05:00
|
|
|
// TODO: quoteなtextはskip
|
2021-04-01 20:36:11 -05:00
|
|
|
node.props.text = nyaize(node.props.text);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
packed.text = mfm.toString(tokens);
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.skipHide) {
|
|
|
|
await this.hideNote(packed, meId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return packed;
|
|
|
|
}
|
2019-04-24 23:27:07 -05:00
|
|
|
|
2021-03-18 20:53:09 -05:00
|
|
|
public async packMany(
|
2021-03-21 21:38:32 -05:00
|
|
|
notes: Note[],
|
2021-03-23 21:05:37 -05:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-24 23:27:07 -05:00
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
|
|
|
}
|
|
|
|
) {
|
2021-03-18 20:53:09 -05:00
|
|
|
if (notes.length === 0) return [];
|
|
|
|
|
2021-03-23 21:05:37 -05:00
|
|
|
const meId = me ? me.id : null;
|
2021-03-18 20:53:09 -05:00
|
|
|
const myReactionsMap = new Map<Note['id'], NoteReaction | null>();
|
|
|
|
if (meId) {
|
2021-03-21 21:38:32 -05:00
|
|
|
const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!);
|
|
|
|
const targets = [...notes.map(n => n.id), ...renoteIds];
|
2021-03-18 20:53:09 -05:00
|
|
|
const myReactions = await NoteReactions.find({
|
|
|
|
userId: meId,
|
|
|
|
noteId: In(targets),
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) || null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 22:41:33 -05:00
|
|
|
await prefetchEmojis(aggregateNoteEmojis(notes));
|
|
|
|
|
2021-03-18 20:53:09 -05:00
|
|
|
return await Promise.all(notes.map(n => this.pack(n, me, {
|
|
|
|
...options,
|
|
|
|
_hint_: {
|
2021-12-09 08:58:30 -06:00
|
|
|
myReactions: myReactionsMap,
|
|
|
|
},
|
2021-03-18 20:53:09 -05:00
|
|
|
})));
|
2019-04-24 23:27:07 -05:00
|
|
|
}
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|