2022-03-26 06:34:00 +00:00
|
|
|
import { In } from 'typeorm';
|
2021-04-02 01:36:11 +00:00
|
|
|
import * as mfm from 'mfm-js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { Note } from '@/models/entities/note.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Users, PollVotes, DriveFiles, NoteReactions, Followings, Polls, Channels } from '../index.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
|
|
|
import { nyaize } from '@/misc/nyaize.js';
|
|
|
|
import { awaitAll } from '@/prelude/await-all.js';
|
|
|
|
import { convertLegacyReaction, convertLegacyReactions, decodeReaction } from '@/misc/reaction-lib.js';
|
|
|
|
import { NoteReaction } from '@/models/entities/note-reaction.js';
|
|
|
|
import { aggregateNoteEmojis, populateEmojis, prefetchEmojis } from '@/misc/populate-emojis.js';
|
2022-03-26 06:34:00 +00:00
|
|
|
import { db } from '@/db/postgre.js';
|
|
|
|
|
|
|
|
async function hideNote(packedNote: Packed<'Note'>, meId: User['id'] | null) {
|
|
|
|
// 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 Followings.findOneBy({
|
|
|
|
followeeId: packedNote.userId,
|
|
|
|
followerId: meId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
hide = true;
|
|
|
|
} else {
|
|
|
|
hide = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
if (hide) {
|
|
|
|
packedNote.visibleUserIds = undefined;
|
|
|
|
packedNote.fileIds = [];
|
|
|
|
packedNote.files = [];
|
|
|
|
packedNote.text = null;
|
|
|
|
packedNote.poll = undefined;
|
|
|
|
packedNote.cw = null;
|
|
|
|
packedNote.isHidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const NoteRepository = db.getRepository(Note).extend({
|
|
|
|
async isVisibleForMe(note: Note, meId: User['id'] | null): Promise<boolean> {
|
2021-09-05 07:25:30 +00:00
|
|
|
// 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 {
|
|
|
|
// フォロワーかどうか
|
2022-03-26 06:34:00 +00:00
|
|
|
const following = await Followings.findOneBy({
|
2021-09-05 07:25:30 +00:00
|
|
|
followeeId: note.userId,
|
2021-12-09 14:58:30 +00:00
|
|
|
followerId: meId,
|
2021-09-05 07:25:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
async pack(
|
2019-04-07 12:50:36 +00:00
|
|
|
src: Note['id'] | Note,
|
2021-03-24 02:05:37 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-07 12:50:36 +00:00
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
2021-03-19 01:53:09 +00:00
|
|
|
_hint_?: {
|
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
|
|
|
};
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2021-09-22 13:35:55 +00:00
|
|
|
): Promise<Packed<'Note'>> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: true,
|
2021-12-09 14:58:30 +00:00
|
|
|
skipHide: false,
|
2019-04-07 12:50:36 +00:00
|
|
|
}, options);
|
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
const meId = me ? me.id : null;
|
2022-03-26 06:34:00 +00:00
|
|
|
const note = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
2019-04-07 12:50:36 +00:00
|
|
|
const host = note.userHost;
|
|
|
|
|
|
|
|
async function populatePoll() {
|
2022-03-26 06:34:00 +00:00
|
|
|
const poll = await Polls.findOneByOrFail({ noteId: note.id });
|
2019-04-07 12:50:36 +00:00
|
|
|
const choices = poll.choices.map(c => ({
|
|
|
|
text: c,
|
|
|
|
votes: poll.votes[poll.choices.indexOf(c)],
|
2021-12-09 14:58:30 +00:00
|
|
|
isVoted: false,
|
2019-04-07 12:50:36 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
if (poll.multiple) {
|
2022-03-26 06:34:00 +00:00
|
|
|
const votes = await PollVotes.findBy({
|
2019-04-12 16:43:22 +00:00
|
|
|
userId: meId!,
|
2021-12-09 14:58:30 +00:00
|
|
|
noteId: note.id,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const myChoices = votes.map(v => v.choice);
|
|
|
|
for (const myChoice of myChoices) {
|
|
|
|
choices[myChoice].isVoted = true;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-26 06:34:00 +00:00
|
|
|
const vote = await PollVotes.findOneBy({
|
2019-04-12 16:43:22 +00:00
|
|
|
userId: meId!,
|
2021-12-09 14:58:30 +00:00
|
|
|
noteId: note.id,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (vote) {
|
|
|
|
choices[vote.choice].isVoted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
multiple: poll.multiple,
|
|
|
|
expiresAt: poll.expiresAt,
|
2021-12-09 14:58:30 +00:00
|
|
|
choices,
|
2019-04-07 12:50:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function populateMyReaction() {
|
2021-03-19 01:53:09 +00: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」のようにはしない
|
|
|
|
}
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const reaction = await NoteReactions.findOneBy({
|
2019-04-12 16:43:22 +00:00
|
|
|
userId: meId!,
|
2019-04-07 12:50:36 +00:00
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reaction) {
|
2020-01-29 19:37:25 +00:00
|
|
|
return convertLegacyReaction(reaction.reaction);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 06:02:15 +00:00
|
|
|
return undefined;
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let text = note.text;
|
|
|
|
|
2022-02-25 11:03:17 +00:00
|
|
|
if (note.name && (note.url ?? note.uri)) {
|
|
|
|
text = `【${note.name}】\n${(note.text || '').trim()}\n\n${note.url ?? note.uri}`;
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:44:21 +00:00
|
|
|
const channel = note.channelId
|
|
|
|
? note.channel
|
|
|
|
? note.channel
|
2022-03-26 06:34:00 +00:00
|
|
|
: await Channels.findOneBy({ id: note.channelId })
|
2020-08-18 13:44:21 +00:00
|
|
|
: null;
|
|
|
|
|
2021-03-21 15:44:38 +00:00
|
|
|
const reactionEmojiNames = Object.keys(note.reactions).filter(x => x?.startsWith(':')).map(x => decodeReaction(x).reaction).map(x => x.replace(/:/g, ''));
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
const packed: Packed<'Note'> = await awaitAll({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: note.id,
|
2019-04-23 13:35:26 +00:00
|
|
|
createdAt: note.createdAt.toISOString(),
|
2019-04-07 12:50:36 +00:00
|
|
|
userId: note.userId,
|
2022-02-25 11:03:17 +00:00
|
|
|
user: Users.pack(note.user ?? note.userId, me, {
|
2021-03-20 04:54:59 +00:00
|
|
|
detail: false,
|
|
|
|
}),
|
2019-04-07 12:50:36 +00:00
|
|
|
text: text,
|
|
|
|
cw: note.cw,
|
|
|
|
visibility: note.visibility,
|
2019-04-13 05:55:59 +00:00
|
|
|
localOnly: note.localOnly || undefined,
|
|
|
|
visibleUserIds: note.visibility === 'specified' ? note.visibleUserIds : undefined,
|
2019-04-08 07:49:05 +00:00
|
|
|
renoteCount: note.renoteCount,
|
|
|
|
repliesCount: note.repliesCount,
|
2020-02-18 21:36:50 +00:00
|
|
|
reactions: convertLegacyReactions(note.reactions),
|
2019-06-07 11:40:05 +00:00
|
|
|
tags: note.tags.length > 0 ? note.tags : undefined,
|
2021-03-21 15:44:38 +00:00
|
|
|
emojis: populateEmojis(note.emojis.concat(reactionEmojiNames), host),
|
2019-04-07 12:50:36 +00:00
|
|
|
fileIds: note.fileIds,
|
|
|
|
files: DriveFiles.packMany(note.fileIds),
|
|
|
|
replyId: note.replyId,
|
|
|
|
renoteId: note.renoteId,
|
2020-08-18 13:44:21 +00:00
|
|
|
channelId: note.channelId || undefined,
|
|
|
|
channel: channel ? {
|
|
|
|
id: channel.id,
|
|
|
|
name: channel.name,
|
|
|
|
} : undefined,
|
2019-04-17 16:05:40 +00:00
|
|
|
mentions: note.mentions.length > 0 ? note.mentions : undefined,
|
2019-04-16 22:25:34 +00:00
|
|
|
uri: note.uri || undefined,
|
2020-04-02 12:59:14 +00:00
|
|
|
url: note.url || undefined,
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
...(opts.detail ? {
|
2021-03-24 02:05:37 +00:00
|
|
|
reply: note.replyId ? this.pack(note.reply || note.replyId, me, {
|
2021-03-19 01:53:09 +00:00
|
|
|
detail: false,
|
2021-12-09 14:58:30 +00:00
|
|
|
_hint_: options?._hint_,
|
2019-04-13 05:55:59 +00:00
|
|
|
}) : undefined,
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
renote: note.renoteId ? this.pack(note.renote || note.renoteId, me, {
|
2021-03-19 01:53:09 +00:00
|
|
|
detail: true,
|
2021-12-09 14:58:30 +00:00
|
|
|
_hint_: options?._hint_,
|
2019-04-13 05:55:59 +00:00
|
|
|
}) : undefined,
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2019-04-13 05:55:59 +00:00
|
|
|
poll: note.hasPoll ? populatePoll() : undefined,
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
...(meId ? {
|
2021-12-09 14:58:30 +00:00
|
|
|
myReaction: populateMyReaction(),
|
|
|
|
} : {}),
|
|
|
|
} : {}),
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (packed.user.isCat && packed.text) {
|
2021-04-02 01:36:11 +00:00
|
|
|
const tokens = packed.text ? mfm.parse(packed.text) : [];
|
|
|
|
mfm.inspect(tokens, node => {
|
|
|
|
if (node.type === 'text') {
|
2021-09-27 16:50:02 +00:00
|
|
|
// TODO: quoteなtextはskip
|
2021-04-02 01:36:11 +00:00
|
|
|
node.props.text = nyaize(node.props.text);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
packed.text = mfm.toString(tokens);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.skipHide) {
|
2022-03-26 06:34:00 +00:00
|
|
|
await hideNote(packed, meId);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return packed;
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-25 04:27:07 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
async packMany(
|
2021-03-22 02:38:32 +00:00
|
|
|
notes: Note[],
|
2021-03-24 02:05:37 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-25 04:27:07 +00:00
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
|
|
|
}
|
|
|
|
) {
|
2021-03-19 01:53:09 +00:00
|
|
|
if (notes.length === 0) return [];
|
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
const meId = me ? me.id : null;
|
2021-03-19 01:53:09 +00:00
|
|
|
const myReactionsMap = new Map<Note['id'], NoteReaction | null>();
|
|
|
|
if (meId) {
|
2021-03-22 02:38:32 +00:00
|
|
|
const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!);
|
|
|
|
const targets = [...notes.map(n => n.id), ...renoteIds];
|
2022-03-26 06:34:00 +00:00
|
|
|
const myReactions = await NoteReactions.findBy({
|
2021-03-19 01:53:09 +00:00
|
|
|
userId: meId,
|
|
|
|
noteId: In(targets),
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) || null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 03:41:33 +00:00
|
|
|
await prefetchEmojis(aggregateNoteEmojis(notes));
|
|
|
|
|
2021-03-19 01:53:09 +00:00
|
|
|
return await Promise.all(notes.map(n => this.pack(n, me, {
|
|
|
|
...options,
|
|
|
|
_hint_: {
|
2021-12-09 14:58:30 +00:00
|
|
|
myReactions: myReactionsMap,
|
|
|
|
},
|
2021-03-19 01:53:09 +00:00
|
|
|
})));
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
|
|
|
});
|