2021-04-02 01:36:11 +00:00
|
|
|
import * as mfm from 'mfm-js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import es from '../../db/elasticsearch.js';
|
|
|
|
import { publishMainStream, publishNotesStream } from '@/services/stream.js';
|
|
|
|
import DeliverManager from '@/remote/activitypub/deliver-manager.js';
|
|
|
|
import renderNote from '@/remote/activitypub/renderer/note.js';
|
|
|
|
import renderCreate from '@/remote/activitypub/renderer/create.js';
|
|
|
|
import renderAnnounce from '@/remote/activitypub/renderer/announce.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import { resolveUser } from '@/remote/resolve-user.js';
|
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { updateHashtags } from '../update-hashtag.js';
|
|
|
|
import { concat } from '@/prelude/array.js';
|
|
|
|
import { insertNoteUnread } from '@/services/note/unread.js';
|
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js';
|
|
|
|
import { extractMentions } from '@/misc/extract-mentions.js';
|
|
|
|
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm.js';
|
|
|
|
import { extractHashtags } from '@/misc/extract-hashtags.js';
|
|
|
|
import { Note, IMentionedRemoteUsers } from '@/models/entities/note.js';
|
|
|
|
import { Mutings, Users, NoteWatchings, Notes, Instances, UserProfiles, Antennas, Followings, MutedNotes, Channels, ChannelFollowings, Blockings, NoteThreadMutings } from '@/models/index.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { App } from '@/models/entities/app.js';
|
2019-10-31 20:43:54 +00:00
|
|
|
import { Not, getConnection, In } from 'typeorm';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { User, ILocalUser, IRemoteUser } from '@/models/entities/user.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { notesChart, perUserNotesChart, activeUsersChart, instanceChart } from '@/services/chart/index.js';
|
|
|
|
import { Poll, IPoll } from '@/models/entities/poll.js';
|
|
|
|
import { createNotification } from '../create-notification.js';
|
|
|
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
|
|
|
import { checkHitAntenna } from '@/misc/check-hit-antenna.js';
|
|
|
|
import { checkWordMute } from '@/misc/check-word-mute.js';
|
|
|
|
import { addNoteToAntenna } from '../add-note-to-antenna.js';
|
|
|
|
import { countSameRenotes } from '@/misc/count-same-renotes.js';
|
|
|
|
import { deliverToRelays } from '../relay.js';
|
|
|
|
import { Channel } from '@/models/entities/channel.js';
|
|
|
|
import { normalizeForSearch } from '@/misc/normalize-for-search.js';
|
|
|
|
import { getAntennas } from '@/misc/antenna-cache.js';
|
2022-03-06 07:06:27 +00:00
|
|
|
import { endedPollNotificationQueue } from '@/queue/queues.js';
|
2018-04-02 08:11:14 +00:00
|
|
|
|
2018-07-21 02:08:27 +00:00
|
|
|
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
|
2018-05-06 19:08:39 +00:00
|
|
|
|
|
|
|
class NotificationManager {
|
2021-03-24 02:05:37 +00:00
|
|
|
private notifier: { id: User['id']; };
|
2019-04-07 12:50:36 +00:00
|
|
|
private note: Note;
|
2019-02-04 09:27:45 +00:00
|
|
|
private queue: {
|
2019-04-07 12:50:36 +00:00
|
|
|
target: ILocalUser['id'];
|
2018-07-21 02:08:27 +00:00
|
|
|
reason: NotificationType;
|
2019-02-04 09:27:45 +00:00
|
|
|
}[];
|
2018-05-06 19:08:39 +00:00
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
constructor(notifier: { id: User['id']; }, note: Note) {
|
2018-06-21 02:35:28 +00:00
|
|
|
this.notifier = notifier;
|
2018-05-06 19:08:39 +00:00
|
|
|
this.note = note;
|
2018-07-20 23:47:48 +00:00
|
|
|
this.queue = [];
|
2018-05-06 19:08:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public push(notifiee: ILocalUser['id'], reason: NotificationType) {
|
2018-05-06 19:08:39 +00:00
|
|
|
// 自分自身へは通知しない
|
2019-04-07 12:50:36 +00:00
|
|
|
if (this.notifier.id === notifiee) return;
|
2018-05-06 19:08:39 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const exist = this.queue.find(x => x.target === notifiee);
|
2018-05-06 19:08:39 +00:00
|
|
|
|
2018-07-20 23:47:48 +00:00
|
|
|
if (exist) {
|
|
|
|
// 「メンションされているかつ返信されている」場合は、メンションとしての通知ではなく返信としての通知にする
|
2022-02-02 17:41:22 +00:00
|
|
|
if (reason !== 'mention') {
|
2018-07-20 23:47:48 +00:00
|
|
|
exist.reason = reason;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.queue.push({
|
|
|
|
reason: reason,
|
2021-12-09 14:58:30 +00:00
|
|
|
target: notifiee,
|
2018-05-06 19:08:39 +00:00
|
|
|
});
|
2018-06-24 06:51:03 +00:00
|
|
|
}
|
2018-05-06 19:08:39 +00:00
|
|
|
}
|
2018-07-20 23:47:48 +00:00
|
|
|
|
2018-12-11 11:36:55 +00:00
|
|
|
public async deliver() {
|
|
|
|
for (const x of this.queue) {
|
2018-07-20 23:47:48 +00:00
|
|
|
// ミュート情報を取得
|
2019-04-07 12:50:36 +00:00
|
|
|
const mentioneeMutes = await Mutings.find({
|
2021-12-09 14:58:30 +00:00
|
|
|
muterId: x.target,
|
2018-07-20 23:47:48 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const mentioneesMutedUserIds = mentioneeMutes.map(m => m.muteeId);
|
2018-07-20 23:47:48 +00:00
|
|
|
|
|
|
|
// 通知される側のユーザーが通知する側のユーザーをミュートしていない限りは通知する
|
2019-04-07 12:50:36 +00:00
|
|
|
if (!mentioneesMutedUserIds.includes(this.notifier.id)) {
|
2020-03-28 09:07:41 +00:00
|
|
|
createNotification(x.target, x.reason, {
|
|
|
|
notifierId: this.notifier.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
noteId: this.note.id,
|
2018-07-20 23:47:48 +00:00
|
|
|
});
|
|
|
|
}
|
2018-12-11 11:36:55 +00:00
|
|
|
}
|
2018-07-20 23:47:48 +00:00
|
|
|
}
|
2018-05-06 19:08:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 21:59:53 +00:00
|
|
|
type Option = {
|
2019-04-12 16:43:22 +00:00
|
|
|
createdAt?: Date | null;
|
|
|
|
name?: string | null;
|
|
|
|
text?: string | null;
|
|
|
|
reply?: Note | null;
|
|
|
|
renote?: Note | null;
|
|
|
|
files?: DriveFile[] | null;
|
|
|
|
poll?: IPoll | null;
|
|
|
|
localOnly?: boolean | null;
|
|
|
|
cw?: string | null;
|
2018-04-04 18:21:11 +00:00
|
|
|
visibility?: string;
|
2019-04-12 16:43:22 +00:00
|
|
|
visibleUsers?: User[] | null;
|
2020-08-18 13:44:21 +00:00
|
|
|
channel?: Channel | null;
|
2019-04-12 16:43:22 +00:00
|
|
|
apMentions?: User[] | null;
|
|
|
|
apHashtags?: string[] | null;
|
|
|
|
apEmojis?: string[] | null;
|
|
|
|
uri?: string | null;
|
2020-04-02 12:59:14 +00:00
|
|
|
url?: string | null;
|
2019-04-12 16:43:22 +00:00
|
|
|
app?: App | null;
|
2018-07-20 21:59:53 +00:00
|
|
|
};
|
|
|
|
|
2022-02-05 21:24:06 +00:00
|
|
|
export default async (user: { id: User['id']; username: User['username']; host: User['host']; isSilenced: User['isSilenced']; createdAt: User['createdAt']; }, data: Option, silent = false) => new Promise<Note>(async (res, rej) => {
|
2020-08-18 13:44:21 +00:00
|
|
|
// チャンネル外にリプライしたら対象のスコープに合わせる
|
|
|
|
// (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで)
|
|
|
|
if (data.reply && data.channel && data.reply.channelId !== data.channel.id) {
|
|
|
|
if (data.reply.channelId) {
|
|
|
|
data.channel = await Channels.findOne(data.reply.channelId);
|
|
|
|
} else {
|
|
|
|
data.channel = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// チャンネル内にリプライしたら対象のスコープに合わせる
|
|
|
|
// (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで)
|
|
|
|
if (data.reply && (data.channel == null) && data.reply.channelId) {
|
|
|
|
data.channel = await Channels.findOne(data.reply.channelId);
|
|
|
|
}
|
|
|
|
|
2018-04-05 19:04:50 +00:00
|
|
|
if (data.createdAt == null) data.createdAt = new Date();
|
|
|
|
if (data.visibility == null) data.visibility = 'public';
|
2018-11-15 20:47:29 +00:00
|
|
|
if (data.localOnly == null) data.localOnly = false;
|
2020-08-18 13:44:21 +00:00
|
|
|
if (data.channel != null) data.visibility = 'public';
|
|
|
|
if (data.channel != null) data.visibleUsers = [];
|
2020-08-29 00:39:50 +00:00
|
|
|
if (data.channel != null) data.localOnly = true;
|
2018-04-04 18:21:11 +00:00
|
|
|
|
2019-01-30 08:25:56 +00:00
|
|
|
// サイレンス
|
2020-08-18 13:44:21 +00:00
|
|
|
if (user.isSilenced && data.visibility === 'public' && data.channel == null) {
|
2019-01-30 08:25:56 +00:00
|
|
|
data.visibility = 'home';
|
|
|
|
}
|
|
|
|
|
2018-09-24 07:02:01 +00:00
|
|
|
// Renote対象が「ホームまたは全体」以外の公開範囲ならreject
|
2020-02-16 12:11:27 +00:00
|
|
|
if (data.renote && data.renote.visibility !== 'public' && data.renote.visibility !== 'home' && data.renote.userId !== user.id) {
|
2018-11-13 10:34:09 +00:00
|
|
|
return rej('Renote target is not public or home');
|
2018-09-24 07:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 08:37:57 +00:00
|
|
|
// Renote対象がpublicではないならhomeにする
|
2020-02-16 12:11:27 +00:00
|
|
|
if (data.renote && data.renote.visibility !== 'public' && data.visibility === 'public') {
|
2019-01-31 08:37:57 +00:00
|
|
|
data.visibility = 'home';
|
|
|
|
}
|
|
|
|
|
2020-02-16 12:10:52 +00:00
|
|
|
// Renote対象がfollowersならfollowersにする
|
|
|
|
if (data.renote && data.renote.visibility === 'followers') {
|
|
|
|
data.visibility = 'followers';
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:37:57 +00:00
|
|
|
// 返信対象がpublicではないならhomeにする
|
2020-02-16 12:11:27 +00:00
|
|
|
if (data.reply && data.reply.visibility !== 'public' && data.visibility === 'public') {
|
2019-01-31 08:37:57 +00:00
|
|
|
data.visibility = 'home';
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:47:29 +00:00
|
|
|
// ローカルのみをRenoteしたらローカルのみにする
|
2020-08-18 13:44:21 +00:00
|
|
|
if (data.renote && data.renote.localOnly && data.channel == null) {
|
2018-11-15 20:47:29 +00:00
|
|
|
data.localOnly = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ローカルのみにリプライしたらローカルのみにする
|
2020-08-18 13:44:21 +00:00
|
|
|
if (data.reply && data.reply.localOnly && data.channel == null) {
|
2018-11-15 20:47:29 +00:00
|
|
|
data.localOnly = true;
|
|
|
|
}
|
|
|
|
|
2018-08-03 16:09:00 +00:00
|
|
|
if (data.text) {
|
|
|
|
data.text = data.text.trim();
|
|
|
|
}
|
|
|
|
|
2018-12-02 09:05:33 +00:00
|
|
|
let tags = data.apHashtags;
|
|
|
|
let emojis = data.apEmojis;
|
|
|
|
let mentionedUsers = data.apMentions;
|
2018-04-02 08:11:14 +00:00
|
|
|
|
2018-12-02 09:05:33 +00:00
|
|
|
// Parse MFM if needed
|
|
|
|
if (!tags || !emojis || !mentionedUsers) {
|
2021-11-11 17:02:25 +00:00
|
|
|
const tokens = data.text ? mfm.parse(data.text)! : [];
|
|
|
|
const cwTokens = data.cw ? mfm.parse(data.cw)! : [];
|
2019-01-22 19:49:16 +00:00
|
|
|
const choiceTokens = data.poll && data.poll.choices
|
2021-11-11 17:02:25 +00:00
|
|
|
? concat(data.poll.choices.map(choice => mfm.parse(choice)!))
|
2019-01-22 19:49:16 +00:00
|
|
|
: [];
|
|
|
|
|
|
|
|
const combinedTokens = tokens.concat(cwTokens).concat(choiceTokens);
|
2018-04-05 06:50:52 +00:00
|
|
|
|
2018-12-02 09:05:33 +00:00
|
|
|
tags = data.apHashtags || extractHashtags(combinedTokens);
|
2018-12-08 01:20:43 +00:00
|
|
|
|
2021-04-02 01:36:11 +00:00
|
|
|
emojis = data.apEmojis || extractCustomEmojisFromMfm(combinedTokens);
|
2018-12-02 09:05:33 +00:00
|
|
|
|
|
|
|
mentionedUsers = data.apMentions || await extractMentionedUsers(user, combinedTokens);
|
|
|
|
}
|
2018-07-20 20:35:43 +00:00
|
|
|
|
2019-09-26 20:18:09 +00:00
|
|
|
tags = tags.filter(tag => Array.from(tag || '').length <= 128).splice(0, 32);
|
2018-12-19 17:20:56 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
if (data.reply && (user.id !== data.reply.userId) && !mentionedUsers.some(u => u.id === data.reply!.userId)) {
|
2021-02-13 06:33:38 +00:00
|
|
|
mentionedUsers.push(await Users.findOneOrFail(data.reply.userId));
|
2018-09-16 13:48:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 17:41:22 +00:00
|
|
|
if (data.visibility === 'specified') {
|
2019-04-13 19:17:24 +00:00
|
|
|
if (data.visibleUsers == null) throw new Error('invalid param');
|
2019-04-12 16:43:22 +00:00
|
|
|
|
2018-12-11 11:36:55 +00:00
|
|
|
for (const u of data.visibleUsers) {
|
2019-04-07 12:50:36 +00:00
|
|
|
if (!mentionedUsers.some(x => x.id === u.id)) {
|
2018-09-17 17:14:12 +00:00
|
|
|
mentionedUsers.push(u);
|
|
|
|
}
|
2018-12-11 11:36:55 +00:00
|
|
|
}
|
2018-12-22 18:44:18 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
if (data.reply && !data.visibleUsers.some(x => x.id === data.reply!.userId)) {
|
2021-02-13 06:33:38 +00:00
|
|
|
data.visibleUsers.push(await Users.findOneOrFail(data.reply.userId));
|
2018-12-22 18:44:18 +00:00
|
|
|
}
|
2018-09-17 17:14:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 18:32:20 +00:00
|
|
|
const note = await insertNote(user, data, tags, emojis, mentionedUsers);
|
2018-07-20 20:35:43 +00:00
|
|
|
|
2018-07-20 21:59:53 +00:00
|
|
|
res(note);
|
2018-04-18 05:53:17 +00:00
|
|
|
|
2018-08-18 14:56:44 +00:00
|
|
|
// 統計を更新
|
2018-10-22 20:36:35 +00:00
|
|
|
notesChart.update(note, true);
|
|
|
|
perUserNotesChart.update(user, note, true);
|
2018-08-18 14:56:44 +00:00
|
|
|
|
2018-10-23 21:17:55 +00:00
|
|
|
// Register host
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isRemoteUser(user)) {
|
2019-02-07 06:00:44 +00:00
|
|
|
registerOrFetchInstanceDoc(user.host).then(i => {
|
2019-04-07 12:50:36 +00:00
|
|
|
Instances.increment({ id: i.id }, 'notesCount', 1);
|
2019-04-08 05:29:17 +00:00
|
|
|
instanceChart.updateNote(i.host, note, true);
|
2018-10-23 21:17:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:11:14 +00:00
|
|
|
// ハッシュタグ更新
|
2020-02-01 20:42:58 +00:00
|
|
|
if (data.visibility === 'public' || data.visibility === 'home') {
|
|
|
|
updateHashtags(user, tags);
|
|
|
|
}
|
2018-07-17 22:19:24 +00:00
|
|
|
|
2018-06-16 01:40:53 +00:00
|
|
|
// Increment notes count (user)
|
2018-07-20 20:35:43 +00:00
|
|
|
incNotesCountOfUser(user);
|
2018-04-04 14:12:35 +00:00
|
|
|
|
2020-07-27 04:34:20 +00:00
|
|
|
// Word mute
|
2021-03-23 06:06:56 +00:00
|
|
|
// TODO: cache
|
2020-07-27 04:34:20 +00:00
|
|
|
UserProfiles.find({
|
2021-12-09 14:58:30 +00:00
|
|
|
enableWordMute: true,
|
2020-07-27 04:34:20 +00:00
|
|
|
}).then(us => {
|
|
|
|
for (const u of us) {
|
|
|
|
checkWordMute(note, { id: u.userId }, u.mutedWords).then(shouldMute => {
|
|
|
|
if (shouldMute) {
|
2021-03-21 12:27:09 +00:00
|
|
|
MutedNotes.insert({
|
2020-07-27 04:34:20 +00:00
|
|
|
id: genId(),
|
|
|
|
userId: u.userId,
|
|
|
|
noteId: note.id,
|
|
|
|
reason: 'word',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
// Antenna
|
2021-03-22 01:45:07 +00:00
|
|
|
Followings.createQueryBuilder('following')
|
|
|
|
.andWhere(`following.followeeId = :userId`, { userId: note.userId })
|
|
|
|
.getMany()
|
2021-03-23 06:06:56 +00:00
|
|
|
.then(async followings => {
|
2021-08-17 12:48:59 +00:00
|
|
|
const blockings = await Blockings.find({ blockerId: user.id }); // TODO: キャッシュしたい
|
2021-03-22 01:45:07 +00:00
|
|
|
const followers = followings.map(f => f.followerId);
|
2021-03-23 06:06:56 +00:00
|
|
|
for (const antenna of (await getAntennas())) {
|
2021-08-17 12:48:59 +00:00
|
|
|
if (blockings.some(blocking => blocking.blockeeId === antenna.userId)) continue; // この処理は checkHitAntenna 内でやるようにしてもいいかも
|
2021-03-23 06:06:56 +00:00
|
|
|
checkHitAntenna(antenna, note, user, followers).then(hit => {
|
|
|
|
if (hit) {
|
|
|
|
addNoteToAntenna(antenna, note, user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-03-22 01:45:07 +00:00
|
|
|
});
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2020-08-18 13:44:21 +00:00
|
|
|
// Channel
|
|
|
|
if (note.channelId) {
|
|
|
|
ChannelFollowings.find({ followeeId: note.channelId }).then(followings => {
|
|
|
|
for (const following of followings) {
|
|
|
|
insertNoteUnread(following.followerId, note, {
|
|
|
|
isSpecified: false,
|
|
|
|
isMentioned: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-28 15:36:52 +00:00
|
|
|
if (data.reply) {
|
2018-07-20 20:35:43 +00:00
|
|
|
saveReply(data.reply, note);
|
2018-05-28 15:36:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 22:56:32 +00:00
|
|
|
// この投稿を除く指定したユーザーによる指定したノートのリノートが存在しないとき
|
2020-02-25 22:54:35 +00:00
|
|
|
if (data.renote && (await countSameRenotes(user.id, data.renote.id, note.id) === 0)) {
|
2018-07-20 22:05:51 +00:00
|
|
|
incRenoteCount(data.renote);
|
|
|
|
}
|
|
|
|
|
2022-03-06 07:06:27 +00:00
|
|
|
if (data.poll && data.poll.expiresAt) {
|
|
|
|
const delay = data.poll.expiresAt.getTime() - Date.now();
|
|
|
|
endedPollNotificationQueue.add({
|
|
|
|
noteId: note.id,
|
|
|
|
}, {
|
|
|
|
delay
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
if (!silent) {
|
2022-02-08 18:46:58 +00:00
|
|
|
if (Users.isLocalUser(user)) activeUsersChart.write(user);
|
2018-04-04 14:12:35 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
// 未読通知を作成
|
2022-02-02 17:41:22 +00:00
|
|
|
if (data.visibility === 'specified') {
|
2019-04-24 19:17:03 +00:00
|
|
|
if (data.visibleUsers == null) throw new Error('invalid param');
|
2018-08-13 23:16:21 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
for (const u of data.visibleUsers) {
|
2020-08-18 13:44:21 +00:00
|
|
|
// ローカルユーザーのみ
|
|
|
|
if (!Users.isLocalUser(u)) continue;
|
|
|
|
|
|
|
|
insertNoteUnread(u.id, note, {
|
|
|
|
isSpecified: true,
|
|
|
|
isMentioned: false,
|
|
|
|
});
|
2019-04-24 19:17:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const u of mentionedUsers) {
|
2020-08-18 13:44:21 +00:00
|
|
|
// ローカルユーザーのみ
|
|
|
|
if (!Users.isLocalUser(u)) continue;
|
|
|
|
|
|
|
|
insertNoteUnread(u.id, note, {
|
|
|
|
isSpecified: false,
|
|
|
|
isMentioned: true,
|
|
|
|
});
|
2019-04-24 19:17:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 00:00:20 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
// Pack the note
|
|
|
|
const noteObj = await Notes.pack(note);
|
2018-06-12 20:40:12 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
publishNotesStream(noteObj);
|
2018-07-20 22:05:51 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
const nm = new NotificationManager(user, note);
|
|
|
|
const nmRelatedPromises = [];
|
2018-06-12 20:40:12 +00:00
|
|
|
|
2019-11-23 00:43:47 +00:00
|
|
|
await createMentionedEvents(mentionedUsers, note, nm);
|
2019-04-10 06:04:27 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
// If has in reply to note
|
|
|
|
if (data.reply) {
|
|
|
|
// Fetch watchers
|
|
|
|
nmRelatedPromises.push(notifyToWatchersOfReplyee(data.reply, user, nm));
|
2018-08-02 00:37:13 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
// 通知
|
|
|
|
if (data.reply.userHost === null) {
|
2021-10-31 06:30:22 +00:00
|
|
|
const threadMuted = await NoteThreadMutings.findOne({
|
|
|
|
userId: data.reply.userId,
|
|
|
|
threadId: data.reply.threadId || data.reply.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!threadMuted) {
|
|
|
|
nm.push(data.reply.userId, 'reply');
|
|
|
|
publishMainStream(data.reply.userId, 'reply', noteObj);
|
|
|
|
}
|
2019-04-24 19:17:03 +00:00
|
|
|
}
|
2018-08-02 00:37:13 +00:00
|
|
|
}
|
2018-04-04 15:40:34 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
// If it is renote
|
|
|
|
if (data.renote) {
|
|
|
|
const type = data.text ? 'quote' : 'renote';
|
2018-04-04 15:40:34 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
// Notify
|
|
|
|
if (data.renote.userHost === null) {
|
|
|
|
nm.push(data.renote.userId, type);
|
|
|
|
}
|
2018-04-04 15:40:34 +00:00
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
// Fetch watchers
|
|
|
|
nmRelatedPromises.push(notifyToWatchersOfRenotee(data.renote, user, nm, type));
|
|
|
|
|
|
|
|
// Publish event
|
|
|
|
if ((user.id !== data.renote.userId) && data.renote.userHost === null) {
|
|
|
|
publishMainStream(data.renote.userId, 'renote', noteObj);
|
|
|
|
}
|
2018-04-04 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 19:17:03 +00:00
|
|
|
Promise.all(nmRelatedPromises).then(() => {
|
|
|
|
nm.deliver();
|
|
|
|
});
|
2019-11-09 09:51:54 +00:00
|
|
|
|
|
|
|
//#region AP deliver
|
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
(async () => {
|
|
|
|
const noteActivity = await renderNoteOrRenoteActivity(data, note);
|
|
|
|
const dm = new DeliverManager(user, noteActivity);
|
|
|
|
|
|
|
|
// メンションされたリモートユーザーに配送
|
|
|
|
for (const u of mentionedUsers.filter(u => Users.isRemoteUser(u))) {
|
|
|
|
dm.addDirectRecipe(u as IRemoteUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
|
|
|
|
if (data.reply && data.reply.userHost !== null) {
|
|
|
|
const u = await Users.findOne(data.reply.userId);
|
|
|
|
if (u && Users.isRemoteUser(u)) dm.addDirectRecipe(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送
|
|
|
|
if (data.renote && data.renote.userHost !== null) {
|
|
|
|
const u = await Users.findOne(data.renote.userId);
|
|
|
|
if (u && Users.isRemoteUser(u)) dm.addDirectRecipe(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
// フォロワーに配送
|
|
|
|
if (['public', 'home', 'followers'].includes(note.visibility)) {
|
|
|
|
dm.addFollowersRecipe();
|
|
|
|
}
|
|
|
|
|
2020-05-10 09:42:31 +00:00
|
|
|
if (['public'].includes(note.visibility)) {
|
|
|
|
deliverToRelays(user, noteActivity);
|
|
|
|
}
|
|
|
|
|
2019-11-09 09:51:54 +00:00
|
|
|
dm.execute();
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
//#endregion
|
2019-04-24 19:17:03 +00:00
|
|
|
}
|
2018-07-20 23:47:48 +00:00
|
|
|
|
2020-08-18 13:44:21 +00:00
|
|
|
if (data.channel) {
|
|
|
|
Channels.increment({ id: data.channel.id }, 'notesCount', 1);
|
|
|
|
Channels.update(data.channel.id, {
|
|
|
|
lastNotedAt: new Date(),
|
|
|
|
});
|
|
|
|
|
|
|
|
Notes.count({
|
|
|
|
userId: user.id,
|
|
|
|
channelId: data.channel.id,
|
|
|
|
}).then(count => {
|
|
|
|
// この処理が行われるのはノート作成後なので、ノートが一つしかなかったら最初の投稿だと判断できる
|
|
|
|
// TODO: とはいえノートを削除して何回も投稿すればその分だけインクリメントされる雑さもあるのでどうにかしたい
|
|
|
|
if (count === 1) {
|
2021-03-24 02:05:37 +00:00
|
|
|
Channels.increment({ id: data.channel!.id }, 'usersCount', 1);
|
2020-08-18 13:44:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-04 04:21:30 +00:00
|
|
|
// Register to search database
|
2018-07-20 20:35:43 +00:00
|
|
|
index(note);
|
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
async function renderNoteOrRenoteActivity(data: Option, note: Note) {
|
2018-11-15 20:47:29 +00:00
|
|
|
if (data.localOnly) return null;
|
|
|
|
|
2022-02-02 17:41:22 +00:00
|
|
|
const content = data.renote && data.text == null && data.poll == null && (data.files == null || data.files.length === 0)
|
2019-04-07 12:50:36 +00:00
|
|
|
? renderAnnounce(data.renote.uri ? data.renote.uri : `${config.url}/notes/${data.renote.id}`, note)
|
2018-08-25 05:12:44 +00:00
|
|
|
: renderCreate(await renderNote(note, false), note);
|
2018-07-20 22:05:51 +00:00
|
|
|
|
2019-01-30 17:29:36 +00:00
|
|
|
return renderActivity(content);
|
2018-07-20 22:05:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
function incRenoteCount(renote: Note) {
|
2021-03-21 13:15:45 +00:00
|
|
|
Notes.createQueryBuilder().update()
|
|
|
|
.set({
|
|
|
|
renoteCount: () => '"renoteCount" + 1',
|
2021-12-09 14:58:30 +00:00
|
|
|
score: () => '"score" + 1',
|
2021-03-21 13:15:45 +00:00
|
|
|
})
|
|
|
|
.where('id = :id', { id: renote.id })
|
|
|
|
.execute();
|
2018-07-20 22:05:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
async function insertNote(user: { id: User['id']; host: User['host']; }, data: Option, tags: string[], emojis: string[], mentionedUsers: User[]) {
|
2019-04-11 16:30:10 +00:00
|
|
|
const insert = new Note({
|
2019-04-12 16:43:22 +00:00
|
|
|
id: genId(data.createdAt!),
|
|
|
|
createdAt: data.createdAt!,
|
2019-04-07 12:50:36 +00:00
|
|
|
fileIds: data.files ? data.files.map(file => file.id) : [],
|
|
|
|
replyId: data.reply ? data.reply.id : null,
|
|
|
|
renoteId: data.renote ? data.renote.id : null,
|
2020-08-18 13:44:21 +00:00
|
|
|
channelId: data.channel ? data.channel.id : null,
|
2021-10-31 06:30:22 +00:00
|
|
|
threadId: data.reply
|
|
|
|
? data.reply.threadId
|
|
|
|
? data.reply.threadId
|
|
|
|
: data.reply.id
|
|
|
|
: null,
|
2019-03-14 15:23:24 +00:00
|
|
|
name: data.name,
|
2018-07-20 21:59:53 +00:00
|
|
|
text: data.text,
|
2019-04-07 12:50:36 +00:00
|
|
|
hasPoll: data.poll != null,
|
2018-07-20 21:59:53 +00:00
|
|
|
cw: data.cw == null ? null : data.cw,
|
2021-02-07 01:43:34 +00:00
|
|
|
tags: tags.map(tag => normalizeForSearch(tag)),
|
2018-11-03 18:32:20 +00:00
|
|
|
emojis,
|
2019-04-07 12:50:36 +00:00
|
|
|
userId: user.id,
|
2019-04-12 16:43:22 +00:00
|
|
|
localOnly: data.localOnly!,
|
2019-04-07 12:50:36 +00:00
|
|
|
visibility: data.visibility as any,
|
2022-02-02 17:41:22 +00:00
|
|
|
visibleUserIds: data.visibility === 'specified'
|
2018-07-20 21:59:53 +00:00
|
|
|
? data.visibleUsers
|
2019-04-07 12:50:36 +00:00
|
|
|
? data.visibleUsers.map(u => u.id)
|
2018-07-20 21:59:53 +00:00
|
|
|
: []
|
|
|
|
: [],
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
attachedFileTypes: data.files ? data.files.map(file => file.type) : [],
|
|
|
|
|
2018-07-20 21:59:53 +00:00
|
|
|
// 以下非正規化データ
|
2019-04-07 12:50:36 +00:00
|
|
|
replyUserId: data.reply ? data.reply.userId : null,
|
|
|
|
replyUserHost: data.reply ? data.reply.userHost : null,
|
|
|
|
renoteUserId: data.renote ? data.renote.userId : null,
|
|
|
|
renoteUserHost: data.renote ? data.renote.userHost : null,
|
|
|
|
userHost: user.host,
|
2019-04-11 16:30:10 +00:00
|
|
|
});
|
2018-07-20 21:59:53 +00:00
|
|
|
|
|
|
|
if (data.uri != null) insert.uri = data.uri;
|
2020-04-02 12:59:14 +00:00
|
|
|
if (data.url != null) insert.url = data.url;
|
2018-07-20 21:59:53 +00:00
|
|
|
|
|
|
|
// Append mentions data
|
|
|
|
if (mentionedUsers.length > 0) {
|
2019-04-07 12:50:36 +00:00
|
|
|
insert.mentions = mentionedUsers.map(u => u.id);
|
2019-10-31 20:43:54 +00:00
|
|
|
const profiles = await UserProfiles.find({ userId: In(insert.mentions) });
|
|
|
|
insert.mentionedRemoteUsers = JSON.stringify(mentionedUsers.filter(u => Users.isRemoteUser(u)).map(u => {
|
2022-02-02 17:41:22 +00:00
|
|
|
const profile = profiles.find(p => p.userId === u.id);
|
2019-10-31 20:43:54 +00:00
|
|
|
const url = profile != null ? profile.url : null;
|
|
|
|
return {
|
|
|
|
uri: u.uri,
|
|
|
|
url: url == null ? undefined : url,
|
|
|
|
username: u.username,
|
2021-12-09 14:58:30 +00:00
|
|
|
host: u.host,
|
2019-10-31 20:43:54 +00:00
|
|
|
} as IMentionedRemoteUsers[0];
|
|
|
|
}));
|
2018-07-20 21:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿を作成
|
|
|
|
try {
|
2019-04-11 16:30:10 +00:00
|
|
|
if (insert.hasPoll) {
|
|
|
|
// Start transaction
|
|
|
|
await getConnection().transaction(async transactionalEntityManager => {
|
2020-06-03 23:59:03 +00:00
|
|
|
await transactionalEntityManager.insert(Note, insert);
|
2019-04-11 16:30:10 +00:00
|
|
|
|
|
|
|
const poll = new Poll({
|
2020-06-03 23:59:03 +00:00
|
|
|
noteId: insert.id,
|
2019-04-12 16:43:22 +00:00
|
|
|
choices: data.poll!.choices,
|
|
|
|
expiresAt: data.poll!.expiresAt,
|
|
|
|
multiple: data.poll!.multiple,
|
|
|
|
votes: new Array(data.poll!.choices.length).fill(0),
|
2020-06-03 23:59:03 +00:00
|
|
|
noteVisibility: insert.visibility,
|
2019-04-11 16:30:10 +00:00
|
|
|
userId: user.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
userHost: user.host,
|
2019-04-11 16:30:10 +00:00
|
|
|
});
|
|
|
|
|
2020-06-03 23:59:03 +00:00
|
|
|
await transactionalEntityManager.insert(Poll, poll);
|
2019-04-11 16:30:10 +00:00
|
|
|
});
|
|
|
|
} else {
|
2020-06-03 23:59:03 +00:00
|
|
|
await Notes.insert(insert);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 13:15:45 +00:00
|
|
|
return insert;
|
2018-07-20 21:59:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
// duplicate key error
|
2019-04-07 12:50:36 +00:00
|
|
|
if (isDuplicateKeyValueError(e)) {
|
2019-04-12 04:07:56 +00:00
|
|
|
const err = new Error('Duplicated note');
|
|
|
|
err.name = 'duplicated';
|
|
|
|
throw err;
|
2018-07-20 21:59:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
console.error(e);
|
|
|
|
|
2020-06-03 23:59:03 +00:00
|
|
|
throw e;
|
2018-07-20 21:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
function index(note: Note) {
|
2019-02-07 12:02:33 +00:00
|
|
|
if (note.text == null || config.elasticsearch == null) return;
|
2018-07-20 20:35:43 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
es!.index({
|
2019-08-09 04:04:35 +00:00
|
|
|
index: config.elasticsearch.index || 'misskey_note',
|
2019-04-07 12:50:36 +00:00
|
|
|
id: note.id.toString(),
|
2018-07-20 20:35:43 +00:00
|
|
|
body: {
|
2021-02-07 01:43:34 +00:00
|
|
|
text: normalizeForSearch(note.text),
|
2019-04-24 22:46:39 +00:00
|
|
|
userId: note.userId,
|
2021-12-09 14:58:30 +00:00
|
|
|
userHost: note.userHost,
|
|
|
|
},
|
2018-07-20 20:35:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
async function notifyToWatchersOfRenotee(renote: Note, user: { id: User['id']; }, nm: NotificationManager, type: NotificationType) {
|
2019-04-07 12:50:36 +00:00
|
|
|
const watchers = await NoteWatchings.find({
|
|
|
|
noteId: renote.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: Not(user.id),
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
2018-07-20 20:35:43 +00:00
|
|
|
|
2018-12-11 11:36:55 +00:00
|
|
|
for (const watcher of watchers) {
|
2018-07-20 20:35:43 +00:00
|
|
|
nm.push(watcher.userId, type);
|
2018-12-11 11:36:55 +00:00
|
|
|
}
|
2018-07-20 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
async function notifyToWatchersOfReplyee(reply: Note, user: { id: User['id']; }, nm: NotificationManager) {
|
2019-04-07 12:50:36 +00:00
|
|
|
const watchers = await NoteWatchings.find({
|
|
|
|
noteId: reply.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: Not(user.id),
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
2018-07-20 20:35:43 +00:00
|
|
|
|
2018-12-11 11:36:55 +00:00
|
|
|
for (const watcher of watchers) {
|
2018-07-20 20:35:43 +00:00
|
|
|
nm.push(watcher.userId, 'reply');
|
2018-12-11 11:36:55 +00:00
|
|
|
}
|
2018-07-20 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
async function createMentionedEvents(mentionedUsers: User[], note: Note, nm: NotificationManager) {
|
|
|
|
for (const u of mentionedUsers.filter(u => Users.isLocalUser(u))) {
|
2021-10-31 06:30:22 +00:00
|
|
|
const threadMuted = await NoteThreadMutings.findOne({
|
|
|
|
userId: u.id,
|
|
|
|
threadId: note.threadId || note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (threadMuted) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const detailPackedNote = await Notes.pack(note, u, {
|
2021-12-09 14:58:30 +00:00
|
|
|
detail: true,
|
2018-09-17 17:14:12 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
publishMainStream(u.id, 'mention', detailPackedNote);
|
2018-07-20 20:35:43 +00:00
|
|
|
|
|
|
|
// Create notification
|
2019-04-07 12:50:36 +00:00
|
|
|
nm.push(u.id, 'mention');
|
2018-12-11 11:36:55 +00:00
|
|
|
}
|
2018-07-20 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
function saveReply(reply: Note, note: Note) {
|
|
|
|
Notes.increment({ id: reply.id }, 'repliesCount', 1);
|
2018-07-20 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
function incNotesCountOfUser(user: { id: User['id']; }) {
|
2021-03-21 13:09:32 +00:00
|
|
|
Users.createQueryBuilder().update()
|
|
|
|
.set({
|
|
|
|
updatedAt: new Date(),
|
2021-12-09 14:58:30 +00:00
|
|
|
notesCount: () => '"notesCount" + 1',
|
2021-03-21 13:09:32 +00:00
|
|
|
})
|
|
|
|
.where('id = :id', { id: user.id })
|
|
|
|
.execute();
|
2018-07-20 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 01:36:11 +00:00
|
|
|
async function extractMentionedUsers(user: { host: User['host']; }, tokens: mfm.MfmNode[]): Promise<User[]> {
|
2018-07-20 20:35:43 +00:00
|
|
|
if (tokens == null) return [];
|
|
|
|
|
2018-12-19 02:16:29 +00:00
|
|
|
const mentions = extractMentions(tokens);
|
2018-07-21 02:06:01 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
let mentionedUsers = (await Promise.all(mentions.map(m =>
|
2019-04-07 12:50:36 +00:00
|
|
|
resolveUser(m.username, m.host || user.host).catch(() => null)
|
2019-04-12 16:43:22 +00:00
|
|
|
))).filter(x => x != null) as User[];
|
2018-10-30 12:55:16 +00:00
|
|
|
|
|
|
|
// Drop duplicate users
|
|
|
|
mentionedUsers = mentionedUsers.filter((u, i, self) =>
|
2019-04-07 12:50:36 +00:00
|
|
|
i === self.findIndex(u2 => u.id === u2.id)
|
2018-09-06 15:10:03 +00:00
|
|
|
);
|
2018-07-20 20:35:43 +00:00
|
|
|
|
|
|
|
return mentionedUsers;
|
|
|
|
}
|