2022-03-26 07:34:00 +01:00
|
|
|
import { In } from 'typeorm';
|
2021-04-02 03:36:11 +02:00
|
|
|
import * as mfm from 'mfm-js';
|
2022-02-27 03:07:39 +01: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 07:34:00 +01: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 15:35:26 +02:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
if (hide) {
|
|
|
|
packedNote.visibleUserIds = undefined;
|
|
|
|
packedNote.fileIds = [];
|
|
|
|
packedNote.files = [];
|
|
|
|
packedNote.text = null;
|
|
|
|
packedNote.poll = undefined;
|
|
|
|
packedNote.cw = null;
|
|
|
|
packedNote.isHidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 09:19:27 +01:00
|
|
|
async function populatePoll(note: Note, meId: User['id'] | null) {
|
|
|
|
const poll = await Polls.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 PollVotes.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 PollVotes.findOneBy({
|
|
|
|
userId: meId,
|
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (vote) {
|
|
|
|
choices[vote.choice].isVoted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
multiple: poll.multiple,
|
|
|
|
expiresAt: poll.expiresAt,
|
|
|
|
choices,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function populateMyReaction(note: Note, meId: User['id'], _hint_?: {
|
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
|
|
|
}) {
|
|
|
|
if (_hint_?.myReactions) {
|
|
|
|
const reaction = _hint_.myReactions.get(note.id);
|
|
|
|
if (reaction) {
|
|
|
|
return convertLegacyReaction(reaction.reaction);
|
|
|
|
} else if (reaction === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
|
|
|
|
}
|
|
|
|
|
|
|
|
const reaction = await NoteReactions.findOneBy({
|
|
|
|
userId: meId,
|
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reaction) {
|
|
|
|
return convertLegacyReaction(reaction.reaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
export const NoteRepository = db.getRepository(Note).extend({
|
|
|
|
async isVisibleForMe(note: Note, meId: User['id'] | null): Promise<boolean> {
|
2022-06-11 09:14:44 +02:00
|
|
|
// This code must always be synchronized with the checks in generateVisibilityQuery.
|
2021-09-05 09:25:30 +02:00
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
|
|
|
if (note.visibility === 'specified') {
|
|
|
|
if (meId == null) {
|
|
|
|
return false;
|
|
|
|
} else if (meId === note.userId) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// 指定されているかどうか
|
2022-05-29 08:15:52 +02:00
|
|
|
return note.visibleUserIds.some((id: any) => meId === id);
|
2021-09-05 09:25:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-05-25 16:17:00 +02:00
|
|
|
const [following, user] = await Promise.all([
|
2022-05-29 08:15:52 +02:00
|
|
|
Followings.count({
|
|
|
|
where: {
|
|
|
|
followeeId: note.userId,
|
|
|
|
followerId: meId,
|
|
|
|
},
|
|
|
|
take: 1,
|
2022-05-25 16:17:00 +02:00
|
|
|
}),
|
|
|
|
Users.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.
|
|
|
|
*/
|
2022-05-29 08:15:52 +02:00
|
|
|
return following > 0 || (note.userHost != null && user.host != null);
|
2021-09-05 09:25:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-03-26 07:34:00 +01:00
|
|
|
},
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
async pack(
|
2019-04-07 14:50:36 +02:00
|
|
|
src: Note['id'] | Note,
|
2021-03-24 03:05:37 +01:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
2021-03-19 02:53:09 +01:00
|
|
|
_hint_?: {
|
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
|
|
|
};
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
2021-09-22 15:35:55 +02:00
|
|
|
): Promise<Packed<'Note'>> {
|
2019-04-07 14:50:36 +02:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: true,
|
2021-12-09 15:58:30 +01:00
|
|
|
skipHide: false,
|
2019-04-07 14:50:36 +02:00
|
|
|
}, options);
|
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
const meId = me ? me.id : null;
|
2022-03-26 07:34:00 +01:00
|
|
|
const note = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
2019-04-07 14:50:36 +02:00
|
|
|
const host = note.userHost;
|
|
|
|
|
|
|
|
let text = note.text;
|
|
|
|
|
2022-02-25 12:03:17 +01:00
|
|
|
if (note.name && (note.url ?? note.uri)) {
|
|
|
|
text = `【${note.name}】\n${(note.text || '').trim()}\n\n${note.url ?? note.uri}`;
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
const channel = note.channelId
|
|
|
|
? note.channel
|
|
|
|
? note.channel
|
2022-03-26 07:34:00 +01:00
|
|
|
: await Channels.findOneBy({ id: note.channelId })
|
2020-08-18 15:44:21 +02:00
|
|
|
: null;
|
|
|
|
|
2021-03-21 16:44:38 +01:00
|
|
|
const reactionEmojiNames = Object.keys(note.reactions).filter(x => x?.startsWith(':')).map(x => decodeReaction(x).reaction).map(x => x.replace(/:/g, ''));
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
const packed: Packed<'Note'> = await awaitAll({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: note.id,
|
2019-04-23 15:35:26 +02:00
|
|
|
createdAt: note.createdAt.toISOString(),
|
2019-04-07 14:50:36 +02:00
|
|
|
userId: note.userId,
|
2022-02-25 12:03:17 +01:00
|
|
|
user: Users.pack(note.user ?? note.userId, me, {
|
2021-03-20 05:54:59 +01:00
|
|
|
detail: false,
|
|
|
|
}),
|
2019-04-07 14:50:36 +02:00
|
|
|
text: text,
|
|
|
|
cw: note.cw,
|
|
|
|
visibility: note.visibility,
|
2019-04-13 07:55:59 +02:00
|
|
|
localOnly: note.localOnly || undefined,
|
|
|
|
visibleUserIds: note.visibility === 'specified' ? note.visibleUserIds : undefined,
|
2019-04-08 09:49:05 +02:00
|
|
|
renoteCount: note.renoteCount,
|
|
|
|
repliesCount: note.repliesCount,
|
2020-02-18 22:36:50 +01:00
|
|
|
reactions: convertLegacyReactions(note.reactions),
|
2019-06-07 13:40:05 +02:00
|
|
|
tags: note.tags.length > 0 ? note.tags : undefined,
|
2021-03-21 16:44:38 +01:00
|
|
|
emojis: populateEmojis(note.emojis.concat(reactionEmojiNames), host),
|
2019-04-07 14:50:36 +02:00
|
|
|
fileIds: note.fileIds,
|
|
|
|
files: DriveFiles.packMany(note.fileIds),
|
|
|
|
replyId: note.replyId,
|
|
|
|
renoteId: note.renoteId,
|
2020-08-18 15:44:21 +02:00
|
|
|
channelId: note.channelId || undefined,
|
|
|
|
channel: channel ? {
|
|
|
|
id: channel.id,
|
|
|
|
name: channel.name,
|
|
|
|
} : undefined,
|
2019-04-17 18:05:40 +02:00
|
|
|
mentions: note.mentions.length > 0 ? note.mentions : undefined,
|
2019-04-17 00:25:34 +02:00
|
|
|
uri: note.uri || undefined,
|
2020-04-02 14:59:14 +02:00
|
|
|
url: note.url || undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
...(opts.detail ? {
|
2021-03-24 03:05:37 +01:00
|
|
|
reply: note.replyId ? this.pack(note.reply || note.replyId, me, {
|
2021-03-19 02:53:09 +01:00
|
|
|
detail: false,
|
2021-12-09 15:58:30 +01:00
|
|
|
_hint_: options?._hint_,
|
2019-04-13 07:55:59 +02:00
|
|
|
}) : undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
renote: note.renoteId ? this.pack(note.renote || note.renoteId, me, {
|
2021-03-19 02:53:09 +01:00
|
|
|
detail: true,
|
2021-12-09 15:58:30 +01:00
|
|
|
_hint_: options?._hint_,
|
2019-04-13 07:55:59 +02:00
|
|
|
}) : undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-03-26 09:19:27 +01:00
|
|
|
poll: note.hasPoll ? populatePoll(note, meId) : undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
...(meId ? {
|
2022-03-26 09:19:27 +01:00
|
|
|
myReaction: populateMyReaction(note, meId, options?._hint_),
|
2021-12-09 15:58:30 +01:00
|
|
|
} : {}),
|
|
|
|
} : {}),
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (packed.user.isCat && packed.text) {
|
2021-04-02 03:36:11 +02:00
|
|
|
const tokens = packed.text ? mfm.parse(packed.text) : [];
|
|
|
|
mfm.inspect(tokens, node => {
|
|
|
|
if (node.type === 'text') {
|
2021-09-27 18:50:02 +02:00
|
|
|
// TODO: quoteなtextはskip
|
2021-04-02 03:36:11 +02:00
|
|
|
node.props.text = nyaize(node.props.text);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
packed.text = mfm.toString(tokens);
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.skipHide) {
|
2022-03-26 07:34:00 +01:00
|
|
|
await hideNote(packed, meId);
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return packed;
|
2022-03-26 07:34:00 +01:00
|
|
|
},
|
2019-04-25 06:27:07 +02:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
async packMany(
|
2021-03-22 03:38:32 +01:00
|
|
|
notes: Note[],
|
2021-03-24 03:05:37 +01:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-25 06:27:07 +02:00
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
|
|
|
}
|
|
|
|
) {
|
2021-03-19 02:53:09 +01:00
|
|
|
if (notes.length === 0) return [];
|
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
const meId = me ? me.id : null;
|
2021-03-19 02:53:09 +01:00
|
|
|
const myReactionsMap = new Map<Note['id'], NoteReaction | null>();
|
|
|
|
if (meId) {
|
2021-03-22 03:38:32 +01:00
|
|
|
const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!);
|
|
|
|
const targets = [...notes.map(n => n.id), ...renoteIds];
|
2022-03-26 07:34:00 +01:00
|
|
|
const myReactions = await NoteReactions.findBy({
|
2021-03-19 02:53:09 +01:00
|
|
|
userId: meId,
|
|
|
|
noteId: In(targets),
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) || null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 04:41:33 +01:00
|
|
|
await prefetchEmojis(aggregateNoteEmojis(notes));
|
|
|
|
|
2021-03-19 02:53:09 +01:00
|
|
|
return await Promise.all(notes.map(n => this.pack(n, me, {
|
|
|
|
...options,
|
|
|
|
_hint_: {
|
2021-12-09 15:58:30 +01:00
|
|
|
myReactions: myReactionsMap,
|
|
|
|
},
|
2021-03-19 02:53:09 +01:00
|
|
|
})));
|
2022-03-26 07:34:00 +01:00
|
|
|
},
|
|
|
|
});
|