2018-07-04 20:13:05 +09:00
|
|
|
import es from '../../db/elasticsearch';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { publishMainStream, publishNotesStream } from '../stream';
|
2018-04-05 23:24:51 +09:00
|
|
|
import { deliver } from '../../queue';
|
2018-04-05 00:40:34 +09:00
|
|
|
import renderNote from '../../remote/activitypub/renderer/note';
|
|
|
|
import renderCreate from '../../remote/activitypub/renderer/create';
|
2018-04-08 06:55:26 +09:00
|
|
|
import renderAnnounce from '../../remote/activitypub/renderer/announce';
|
2019-01-31 02:29:36 +09:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-04-05 00:40:34 +09:00
|
|
|
import watch from './watch';
|
2019-01-30 16:56:27 +09:00
|
|
|
import { parse } from '../../mfm/parse';
|
2019-04-07 23:06:07 +09:00
|
|
|
import { resolveUser } from '../../remote/resolve-user';
|
2018-07-04 13:21:30 +09:00
|
|
|
import config from '../../config';
|
2019-02-17 23:41:47 +09:00
|
|
|
import { updateHashtag } from '../update-hashtag';
|
2019-04-13 01:43:22 +09:00
|
|
|
import { concat } from '../../prelude/array';
|
2018-09-19 14:18:34 +09:00
|
|
|
import insertNoteUnread from './unread';
|
2019-02-07 15:00:44 +09:00
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
2018-12-19 11:16:29 +09:00
|
|
|
import extractMentions from '../../misc/extract-mentions';
|
2018-12-20 19:41:04 +09:00
|
|
|
import extractEmojis from '../../misc/extract-emojis';
|
|
|
|
import extractHashtags from '../../misc/extract-hashtags';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Note } from '../../models/entities/note';
|
2019-04-12 01:30:10 +09:00
|
|
|
import { Mutings, Users, NoteWatchings, Followings, Notes, Instances, UserProfiles } from '../../models';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { DriveFile } from '../../models/entities/drive-file';
|
|
|
|
import { App } from '../../models/entities/app';
|
2019-04-12 01:30:10 +09:00
|
|
|
import { Not, getConnection } from 'typeorm';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { User, ILocalUser, IRemoteUser } from '../../models/entities/user';
|
|
|
|
import { genId } from '../../misc/gen-id';
|
|
|
|
import { notesChart, perUserNotesChart, activeUsersChart, instanceChart } from '../chart';
|
|
|
|
import { Poll, IPoll } from '../../models/entities/poll';
|
|
|
|
import { createNotification } from '../create-notification';
|
|
|
|
import { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error';
|
2019-04-13 01:43:22 +09:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
2018-04-02 17:11:14 +09:00
|
|
|
|
2018-07-21 11:08:27 +09:00
|
|
|
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
|
2018-05-07 04:08:39 +09:00
|
|
|
|
|
|
|
class NotificationManager {
|
2019-04-07 21:50:36 +09:00
|
|
|
private notifier: User;
|
|
|
|
private note: Note;
|
2019-02-04 18:27:45 +09:00
|
|
|
private queue: {
|
2019-04-07 21:50:36 +09:00
|
|
|
target: ILocalUser['id'];
|
2018-07-21 11:08:27 +09:00
|
|
|
reason: NotificationType;
|
2019-02-04 18:27:45 +09:00
|
|
|
}[];
|
2018-05-07 04:08:39 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
constructor(notifier: User, note: Note) {
|
2018-06-21 11:35:28 +09:00
|
|
|
this.notifier = notifier;
|
2018-05-07 04:08:39 +09:00
|
|
|
this.note = note;
|
2018-07-21 08:47:48 +09:00
|
|
|
this.queue = [];
|
2018-05-07 04:08:39 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
public push(notifiee: ILocalUser['id'], reason: NotificationType) {
|
2018-05-07 04:08:39 +09:00
|
|
|
// 自分自身へは通知しない
|
2019-04-07 21:50:36 +09:00
|
|
|
if (this.notifier.id === notifiee) return;
|
2018-05-07 04:08:39 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const exist = this.queue.find(x => x.target === notifiee);
|
2018-05-07 04:08:39 +09:00
|
|
|
|
2018-07-21 08:47:48 +09:00
|
|
|
if (exist) {
|
|
|
|
// 「メンションされているかつ返信されている」場合は、メンションとしての通知ではなく返信としての通知にする
|
|
|
|
if (reason != 'mention') {
|
|
|
|
exist.reason = reason;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.queue.push({
|
|
|
|
reason: reason,
|
|
|
|
target: notifiee
|
2018-05-07 04:08:39 +09:00
|
|
|
});
|
2018-06-24 15:51:03 +09:00
|
|
|
}
|
2018-05-07 04:08:39 +09:00
|
|
|
}
|
2018-07-21 08:47:48 +09:00
|
|
|
|
2018-12-11 20:36:55 +09:00
|
|
|
public async deliver() {
|
|
|
|
for (const x of this.queue) {
|
2018-07-21 08:47:48 +09:00
|
|
|
// ミュート情報を取得
|
2019-04-07 21:50:36 +09:00
|
|
|
const mentioneeMutes = await Mutings.find({
|
2018-07-21 08:47:48 +09:00
|
|
|
muterId: x.target
|
|
|
|
});
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const mentioneesMutedUserIds = mentioneeMutes.map(m => m.muteeId);
|
2018-07-21 08:47:48 +09:00
|
|
|
|
|
|
|
// 通知される側のユーザーが通知する側のユーザーをミュートしていない限りは通知する
|
2019-04-07 21:50:36 +09:00
|
|
|
if (!mentioneesMutedUserIds.includes(this.notifier.id)) {
|
|
|
|
createNotification(x.target, this.notifier.id, x.reason, {
|
|
|
|
noteId: this.note.id
|
2018-07-21 08:47:48 +09:00
|
|
|
});
|
|
|
|
}
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-07-21 08:47:48 +09:00
|
|
|
}
|
2018-05-07 04:08:39 +09:00
|
|
|
}
|
|
|
|
|
2018-07-21 06:59:53 +09:00
|
|
|
type Option = {
|
2019-04-13 01:43:22 +09:00
|
|
|
createdAt?: Date | null;
|
|
|
|
name?: string | null;
|
|
|
|
text?: string | null;
|
|
|
|
reply?: Note | null;
|
|
|
|
renote?: Note | null;
|
|
|
|
files?: DriveFile[] | null;
|
|
|
|
geo?: any | null;
|
|
|
|
poll?: IPoll | null;
|
|
|
|
viaMobile?: boolean | null;
|
|
|
|
localOnly?: boolean | null;
|
|
|
|
cw?: string | null;
|
2018-04-05 03:21:11 +09:00
|
|
|
visibility?: string;
|
2019-04-13 01:43:22 +09:00
|
|
|
visibleUsers?: User[] | null;
|
|
|
|
apMentions?: User[] | null;
|
|
|
|
apHashtags?: string[] | null;
|
|
|
|
apEmojis?: string[] | null;
|
|
|
|
questionUri?: string | null;
|
|
|
|
uri?: string | null;
|
|
|
|
app?: App | null;
|
2018-07-21 06:59:53 +09:00
|
|
|
};
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
export default async (user: User, data: Option, silent = false) => new Promise<Note>(async (res, rej) => {
|
2018-08-14 08:16:21 +09:00
|
|
|
const isFirstNote = user.notesCount === 0;
|
|
|
|
|
2018-04-06 04:04:50 +09:00
|
|
|
if (data.createdAt == null) data.createdAt = new Date();
|
|
|
|
if (data.visibility == null) data.visibility = 'public';
|
2018-04-08 06:55:26 +09:00
|
|
|
if (data.viaMobile == null) data.viaMobile = false;
|
2018-11-16 05:47:29 +09:00
|
|
|
if (data.localOnly == null) data.localOnly = false;
|
2018-04-05 03:21:11 +09:00
|
|
|
|
2019-01-30 17:25:56 +09:00
|
|
|
// サイレンス
|
|
|
|
if (user.isSilenced && data.visibility == 'public') {
|
|
|
|
data.visibility = 'home';
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:02:01 +09:00
|
|
|
// Renote対象が「ホームまたは全体」以外の公開範囲ならreject
|
|
|
|
if (data.renote && data.renote.visibility != 'public' && data.renote.visibility != 'home') {
|
2018-11-13 19:34:09 +09:00
|
|
|
return rej('Renote target is not public or home');
|
2018-09-24 16:02:01 +09:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:37:57 +09:00
|
|
|
// Renote対象がpublicではないならhomeにする
|
|
|
|
if (data.renote && data.renote.visibility != 'public' && data.visibility == 'public') {
|
|
|
|
data.visibility = 'home';
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象がpublicではないならhomeにする
|
|
|
|
if (data.reply && data.reply.visibility != 'public' && data.visibility == 'public') {
|
|
|
|
data.visibility = 'home';
|
|
|
|
}
|
|
|
|
|
2018-11-16 05:47:29 +09:00
|
|
|
// ローカルのみをRenoteしたらローカルのみにする
|
|
|
|
if (data.renote && data.renote.localOnly) {
|
|
|
|
data.localOnly = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ローカルのみにリプライしたらローカルのみにする
|
|
|
|
if (data.reply && data.reply.localOnly) {
|
|
|
|
data.localOnly = true;
|
|
|
|
}
|
|
|
|
|
2018-08-04 01:09:00 +09:00
|
|
|
if (data.text) {
|
|
|
|
data.text = data.text.trim();
|
|
|
|
}
|
|
|
|
|
2018-12-02 18:05:33 +09:00
|
|
|
let tags = data.apHashtags;
|
|
|
|
let emojis = data.apEmojis;
|
|
|
|
let mentionedUsers = data.apMentions;
|
2018-04-02 17:11:14 +09:00
|
|
|
|
2018-12-02 18:05:33 +09:00
|
|
|
// Parse MFM if needed
|
|
|
|
if (!tags || !emojis || !mentionedUsers) {
|
2019-04-13 01:43:22 +09:00
|
|
|
const tokens = data.text ? parse(data.text)! : [];
|
|
|
|
const cwTokens = data.cw ? parse(data.cw)! : [];
|
2019-01-23 04:49:16 +09:00
|
|
|
const choiceTokens = data.poll && data.poll.choices
|
2019-04-13 01:43:22 +09:00
|
|
|
? concat(data.poll.choices.map(choice => parse(choice)!))
|
2019-01-23 04:49:16 +09:00
|
|
|
: [];
|
|
|
|
|
|
|
|
const combinedTokens = tokens.concat(cwTokens).concat(choiceTokens);
|
2018-04-05 15:50:52 +09:00
|
|
|
|
2018-12-02 18:05:33 +09:00
|
|
|
tags = data.apHashtags || extractHashtags(combinedTokens);
|
2018-12-08 10:20:43 +09:00
|
|
|
|
2018-12-02 18:05:33 +09:00
|
|
|
emojis = data.apEmojis || extractEmojis(combinedTokens);
|
|
|
|
|
|
|
|
mentionedUsers = data.apMentions || await extractMentionedUsers(user, combinedTokens);
|
|
|
|
}
|
2018-07-21 05:35:43 +09:00
|
|
|
|
2018-12-20 02:20:56 +09:00
|
|
|
tags = tags.filter(tag => tag.length <= 100);
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
if (data.reply && (user.id !== data.reply.userId) && !mentionedUsers.some(u => u.id === data.reply!.userId)) {
|
|
|
|
mentionedUsers.push(await Users.findOne(data.reply.userId).then(ensure));
|
2018-09-16 22:48:57 +09:00
|
|
|
}
|
|
|
|
|
2018-09-18 02:14:12 +09:00
|
|
|
if (data.visibility == 'specified') {
|
2019-04-14 04:17:24 +09:00
|
|
|
if (data.visibleUsers == null) throw new Error('invalid param');
|
2019-04-13 01:43:22 +09:00
|
|
|
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const u of data.visibleUsers) {
|
2019-04-07 21:50:36 +09:00
|
|
|
if (!mentionedUsers.some(x => x.id === u.id)) {
|
2018-09-18 02:14:12 +09:00
|
|
|
mentionedUsers.push(u);
|
|
|
|
}
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-12-23 03:44:18 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
if (data.reply && !data.visibleUsers.some(x => x.id === data.reply!.userId)) {
|
|
|
|
data.visibleUsers.push(await Users.findOne(data.reply.userId).then(ensure));
|
2018-12-23 03:44:18 +09:00
|
|
|
}
|
2018-09-18 02:14:12 +09:00
|
|
|
}
|
|
|
|
|
2018-11-04 03:32:20 +09:00
|
|
|
const note = await insertNote(user, data, tags, emojis, mentionedUsers);
|
2018-07-21 05:35:43 +09:00
|
|
|
|
2018-07-21 06:59:53 +09:00
|
|
|
res(note);
|
2018-04-18 14:53:17 +09:00
|
|
|
|
2018-08-18 23:56:44 +09:00
|
|
|
// 統計を更新
|
2018-10-23 05:36:35 +09:00
|
|
|
notesChart.update(note, true);
|
|
|
|
perUserNotesChart.update(user, note, true);
|
2019-01-17 17:16:08 +09:00
|
|
|
// ローカルユーザーのチャートはタイムライン取得時に更新しているのでリモートユーザーの場合だけでよい
|
2019-04-07 21:50:36 +09:00
|
|
|
if (Users.isRemoteUser(user)) activeUsersChart.update(user);
|
2018-08-18 23:56:44 +09:00
|
|
|
|
2018-10-24 06:17:55 +09:00
|
|
|
// Register host
|
2019-04-07 21:50:36 +09:00
|
|
|
if (Users.isRemoteUser(user)) {
|
2019-02-07 15:00:44 +09:00
|
|
|
registerOrFetchInstanceDoc(user.host).then(i => {
|
2019-04-07 21:50:36 +09:00
|
|
|
Instances.increment({ id: i.id }, 'notesCount', 1);
|
2019-04-08 14:29:17 +09:00
|
|
|
instanceChart.updateNote(i.host, note, true);
|
2018-10-24 06:17:55 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-18 01:11:14 +09:00
|
|
|
// ハッシュタグ更新
|
2019-02-17 23:41:47 +09:00
|
|
|
for (const tag of tags) updateHashtag(user, tag);
|
2018-07-18 07:19:24 +09:00
|
|
|
|
2018-06-16 10:40:53 +09:00
|
|
|
// Increment notes count (user)
|
2018-07-21 05:35:43 +09:00
|
|
|
incNotesCountOfUser(user);
|
2018-04-04 23:12:35 +09:00
|
|
|
|
2018-09-19 14:18:34 +09:00
|
|
|
// 未読通知を作成
|
|
|
|
if (data.visibility == 'specified') {
|
2019-04-14 04:17:24 +09:00
|
|
|
if (data.visibleUsers == null) throw new Error('invalid param');
|
2019-04-13 01:43:22 +09:00
|
|
|
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const u of data.visibleUsers) {
|
2018-09-19 14:18:34 +09:00
|
|
|
insertNoteUnread(u, note, true);
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-09-19 14:18:34 +09:00
|
|
|
} else {
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const u of mentionedUsers) {
|
2018-09-19 14:18:34 +09:00
|
|
|
insertNoteUnread(u, note, false);
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-09-19 14:18:34 +09:00
|
|
|
}
|
|
|
|
|
2018-05-29 00:36:52 +09:00
|
|
|
if (data.reply) {
|
2018-07-21 05:35:43 +09:00
|
|
|
saveReply(data.reply, note);
|
2018-05-29 00:36:52 +09:00
|
|
|
}
|
|
|
|
|
2018-07-21 07:05:51 +09:00
|
|
|
if (data.renote) {
|
|
|
|
incRenoteCount(data.renote);
|
|
|
|
}
|
|
|
|
|
2018-07-21 05:35:43 +09:00
|
|
|
// Pack the note
|
2019-04-07 21:50:36 +09:00
|
|
|
const noteObj = await Notes.pack(note);
|
2018-04-04 23:12:35 +09:00
|
|
|
|
2018-08-14 08:16:21 +09:00
|
|
|
if (isFirstNote) {
|
|
|
|
noteObj.isFirstNote = true;
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
publishNotesStream(noteObj);
|
2018-09-17 09:00:20 +09:00
|
|
|
|
2018-07-21 05:35:43 +09:00
|
|
|
const nm = new NotificationManager(user, note);
|
2018-07-21 08:47:48 +09:00
|
|
|
const nmRelatedPromises = [];
|
2018-06-13 05:40:12 +09:00
|
|
|
|
2018-09-18 02:14:12 +09:00
|
|
|
createMentionedEvents(mentionedUsers, note, nm);
|
2018-06-13 05:40:12 +09:00
|
|
|
|
2019-01-31 02:29:36 +09:00
|
|
|
const noteActivity = await renderNoteOrRenoteActivity(data, note);
|
2018-07-21 07:05:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (Users.isLocalUser(user)) {
|
2018-07-21 05:35:43 +09:00
|
|
|
deliverNoteToMentionedRemoteUsers(mentionedUsers, user, noteActivity);
|
2018-06-13 05:40:12 +09:00
|
|
|
}
|
|
|
|
|
2019-04-17 14:32:59 +09:00
|
|
|
const profile = await UserProfiles.findOne(user.id).then(ensure);
|
2019-04-10 15:04:27 +09:00
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
// If has in reply to note
|
2018-04-06 04:04:50 +09:00
|
|
|
if (data.reply) {
|
2018-04-05 00:40:34 +09:00
|
|
|
// Fetch watchers
|
2018-07-21 08:47:48 +09:00
|
|
|
nmRelatedPromises.push(notifyToWatchersOfReplyee(data.reply, user, nm));
|
2018-04-05 00:40:34 +09:00
|
|
|
|
|
|
|
// この投稿をWatchする
|
2019-04-10 15:04:27 +09:00
|
|
|
if (Users.isLocalUser(user) && profile.autoWatch) {
|
2019-04-07 21:50:36 +09:00
|
|
|
watch(user.id, data.reply);
|
2018-04-05 00:40:34 +09:00
|
|
|
}
|
|
|
|
|
2018-07-04 13:21:30 +09:00
|
|
|
// 通知
|
2019-04-07 21:50:36 +09:00
|
|
|
if (data.reply.userHost === null) {
|
2018-08-02 09:37:13 +09:00
|
|
|
nm.push(data.reply.userId, 'reply');
|
2018-10-07 11:06:17 +09:00
|
|
|
publishMainStream(data.reply.userId, 'reply', noteObj);
|
2018-08-02 09:37:13 +09:00
|
|
|
}
|
2018-04-05 00:40:34 +09:00
|
|
|
}
|
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
// If it is renote
|
|
|
|
if (data.renote) {
|
|
|
|
const type = data.text ? 'quote' : 'renote';
|
2018-08-02 09:37:13 +09:00
|
|
|
|
|
|
|
// Notify
|
2019-04-07 21:50:36 +09:00
|
|
|
if (data.renote.userHost === null) {
|
2018-08-02 09:37:13 +09:00
|
|
|
nm.push(data.renote.userId, type);
|
|
|
|
}
|
2018-04-05 00:40:34 +09:00
|
|
|
|
|
|
|
// Fetch watchers
|
2018-07-21 08:47:48 +09:00
|
|
|
nmRelatedPromises.push(notifyToWatchersOfRenotee(data.renote, user, nm, type));
|
2018-04-05 00:40:34 +09:00
|
|
|
|
|
|
|
// この投稿をWatchする
|
2019-04-10 15:04:27 +09:00
|
|
|
if (Users.isLocalUser(user) && profile.autoWatch) {
|
2019-04-07 21:50:36 +09:00
|
|
|
watch(user.id, data.renote);
|
2018-04-05 00:40:34 +09:00
|
|
|
}
|
|
|
|
|
2018-08-02 09:37:13 +09:00
|
|
|
// Publish event
|
2019-04-07 21:50:36 +09:00
|
|
|
if ((user.id !== data.renote.userId) && data.renote.userHost === null) {
|
2018-10-07 11:06:17 +09:00
|
|
|
publishMainStream(data.renote.userId, 'renote', noteObj);
|
2018-04-05 00:40:34 +09:00
|
|
|
}
|
2018-07-21 07:05:51 +09:00
|
|
|
}
|
2018-04-05 00:40:34 +09:00
|
|
|
|
2018-07-21 07:05:51 +09:00
|
|
|
if (!silent) {
|
2019-04-07 21:50:36 +09:00
|
|
|
publish(user, note, data.reply, data.renote, noteActivity);
|
2018-04-05 00:40:34 +09:00
|
|
|
}
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2018-07-21 08:47:48 +09:00
|
|
|
Promise.all(nmRelatedPromises).then(() => {
|
|
|
|
nm.deliver();
|
|
|
|
});
|
|
|
|
|
2018-07-04 13:21:30 +09:00
|
|
|
// Register to search database
|
2018-07-21 05:35:43 +09:00
|
|
|
index(note);
|
|
|
|
});
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
async function renderNoteOrRenoteActivity(data: Option, note: Note) {
|
2018-11-16 05:47:29 +09:00
|
|
|
if (data.localOnly) return null;
|
|
|
|
|
2018-09-09 02:59:14 +09:00
|
|
|
const content = data.renote && data.text == null && data.poll == null && (data.files == null || data.files.length == 0)
|
2019-04-07 21:50:36 +09:00
|
|
|
? renderAnnounce(data.renote.uri ? data.renote.uri : `${config.url}/notes/${data.renote.id}`, note)
|
2018-08-25 14:12:44 +09:00
|
|
|
: renderCreate(await renderNote(note, false), note);
|
2018-07-21 07:05:51 +09:00
|
|
|
|
2019-01-31 02:29:36 +09:00
|
|
|
return renderActivity(content);
|
2018-07-21 07:05:51 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
function incRenoteCount(renote: Note) {
|
|
|
|
Notes.increment({ id: renote.id }, 'renoteCount', 1);
|
|
|
|
Notes.increment({ id: renote.id }, 'score', 1);
|
2018-07-21 07:05:51 +09:00
|
|
|
}
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
async function publish(user: User, note: Note, reply: Note | null | undefined, renote: Note | null | undefined, noteActivity: any) {
|
2019-04-07 21:50:36 +09:00
|
|
|
if (Users.isLocalUser(user)) {
|
2018-07-21 06:59:53 +09:00
|
|
|
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
|
2019-04-07 21:50:36 +09:00
|
|
|
if (reply && reply.userHost !== null) {
|
2019-04-13 01:43:22 +09:00
|
|
|
Users.findOne(reply.userId).then(ensure).then(u => {
|
2019-04-12 00:42:39 +09:00
|
|
|
deliver(user, noteActivity, u.inbox);
|
|
|
|
});
|
2018-07-21 06:59:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送
|
2019-04-07 21:50:36 +09:00
|
|
|
if (renote && renote.userHost !== null) {
|
2019-04-13 01:43:22 +09:00
|
|
|
Users.findOne(renote.userId).then(ensure).then(u => {
|
2019-04-12 00:42:39 +09:00
|
|
|
deliver(user, noteActivity, u.inbox);
|
|
|
|
});
|
2018-07-21 06:59:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['public', 'home', 'followers'].includes(note.visibility)) {
|
|
|
|
// フォロワーに配信
|
2019-04-13 01:43:22 +09:00
|
|
|
publishToFollowers(note, user, noteActivity);
|
2018-07-21 06:59:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
async function insertNote(user: User, data: Option, tags: string[], emojis: string[], mentionedUsers: User[]) {
|
2019-04-12 01:30:10 +09:00
|
|
|
const insert = new Note({
|
2019-04-13 01:43:22 +09:00
|
|
|
id: genId(data.createdAt!),
|
|
|
|
createdAt: data.createdAt!,
|
2019-04-07 21:50:36 +09:00
|
|
|
fileIds: data.files ? data.files.map(file => file.id) : [],
|
|
|
|
replyId: data.reply ? data.reply.id : null,
|
|
|
|
renoteId: data.renote ? data.renote.id : null,
|
2019-03-15 00:23:24 +09:00
|
|
|
name: data.name,
|
2018-07-21 06:59:53 +09:00
|
|
|
text: data.text,
|
2019-04-07 21:50:36 +09:00
|
|
|
hasPoll: data.poll != null,
|
2018-07-21 06:59:53 +09:00
|
|
|
cw: data.cw == null ? null : data.cw,
|
2019-04-07 21:50:36 +09:00
|
|
|
tags: tags.map(tag => tag.toLowerCase()),
|
2018-11-04 03:32:20 +09:00
|
|
|
emojis,
|
2019-04-07 21:50:36 +09:00
|
|
|
userId: user.id,
|
2019-04-13 01:43:22 +09:00
|
|
|
viaMobile: data.viaMobile!,
|
|
|
|
localOnly: data.localOnly!,
|
2018-07-21 06:59:53 +09:00
|
|
|
geo: data.geo || null,
|
2019-04-07 21:50:36 +09:00
|
|
|
appId: data.app ? data.app.id : null,
|
|
|
|
visibility: data.visibility as any,
|
2018-07-21 06:59:53 +09:00
|
|
|
visibleUserIds: data.visibility == 'specified'
|
|
|
|
? data.visibleUsers
|
2019-04-07 21:50:36 +09:00
|
|
|
? data.visibleUsers.map(u => u.id)
|
2018-07-21 06:59:53 +09:00
|
|
|
: []
|
|
|
|
: [],
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
attachedFileTypes: data.files ? data.files.map(file => file.type) : [],
|
|
|
|
|
2018-07-21 06:59:53 +09:00
|
|
|
// 以下非正規化データ
|
2019-04-07 21:50:36 +09: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-12 01:30:10 +09:00
|
|
|
});
|
2018-07-21 06:59:53 +09:00
|
|
|
|
|
|
|
if (data.uri != null) insert.uri = data.uri;
|
|
|
|
|
|
|
|
// Append mentions data
|
|
|
|
if (mentionedUsers.length > 0) {
|
2019-04-07 21:50:36 +09:00
|
|
|
insert.mentions = mentionedUsers.map(u => u.id);
|
|
|
|
insert.mentionedRemoteUsers = JSON.stringify(mentionedUsers.filter(u => Users.isRemoteUser(u)).map(u => ({
|
2018-07-21 06:59:53 +09:00
|
|
|
uri: (u as IRemoteUser).uri,
|
|
|
|
username: u.username,
|
|
|
|
host: u.host
|
2019-04-07 21:50:36 +09:00
|
|
|
})));
|
2018-07-21 06:59:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿を作成
|
|
|
|
try {
|
2019-04-12 01:30:10 +09:00
|
|
|
let note: Note;
|
|
|
|
if (insert.hasPoll) {
|
|
|
|
// Start transaction
|
|
|
|
await getConnection().transaction(async transactionalEntityManager => {
|
|
|
|
note = await transactionalEntityManager.save(insert);
|
|
|
|
|
|
|
|
const poll = new Poll({
|
|
|
|
noteId: note.id,
|
2019-04-13 01:43:22 +09:00
|
|
|
choices: data.poll!.choices,
|
|
|
|
expiresAt: data.poll!.expiresAt,
|
|
|
|
multiple: data.poll!.multiple,
|
|
|
|
votes: new Array(data.poll!.choices.length).fill(0),
|
2019-04-12 01:30:10 +09:00
|
|
|
noteVisibility: note.visibility,
|
|
|
|
userId: user.id,
|
|
|
|
userHost: user.host
|
|
|
|
});
|
|
|
|
|
|
|
|
await transactionalEntityManager.save(poll);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
note = await Notes.save(insert);
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
return note!;
|
2018-07-21 06:59:53 +09:00
|
|
|
} catch (e) {
|
|
|
|
// duplicate key error
|
2019-04-07 21:50:36 +09:00
|
|
|
if (isDuplicateKeyValueError(e)) {
|
2019-04-12 13:07:56 +09:00
|
|
|
const err = new Error('Duplicated note');
|
|
|
|
err.name = 'duplicated';
|
|
|
|
throw err;
|
2018-07-21 06:59:53 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
console.error(e);
|
|
|
|
|
2019-04-14 04:17:24 +09:00
|
|
|
throw new Error('something happened');
|
2018-07-21 06:59:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
function index(note: Note) {
|
2019-02-07 21:02:33 +09:00
|
|
|
if (note.text == null || config.elasticsearch == null) return;
|
2018-07-21 05:35:43 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
es!.index({
|
2018-07-21 05:35:43 +09:00
|
|
|
index: 'misskey',
|
|
|
|
type: 'note',
|
2019-04-07 21:50:36 +09:00
|
|
|
id: note.id.toString(),
|
2018-07-21 05:35:43 +09:00
|
|
|
body: {
|
|
|
|
text: note.text
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
async function notifyToWatchersOfRenotee(renote: Note, user: User, nm: NotificationManager, type: NotificationType) {
|
|
|
|
const watchers = await NoteWatchings.find({
|
|
|
|
noteId: renote.id,
|
|
|
|
userId: Not(user.id)
|
|
|
|
});
|
2018-07-21 05:35:43 +09:00
|
|
|
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const watcher of watchers) {
|
2018-07-21 05:35:43 +09:00
|
|
|
nm.push(watcher.userId, type);
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-07-21 05:35:43 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
async function notifyToWatchersOfReplyee(reply: Note, user: User, nm: NotificationManager) {
|
|
|
|
const watchers = await NoteWatchings.find({
|
|
|
|
noteId: reply.id,
|
|
|
|
userId: Not(user.id)
|
|
|
|
});
|
2018-07-21 05:35:43 +09:00
|
|
|
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const watcher of watchers) {
|
2018-07-21 05:35:43 +09:00
|
|
|
nm.push(watcher.userId, 'reply');
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-07-21 05:35:43 +09:00
|
|
|
}
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
async function publishToFollowers(note: Note, user: User, noteActivity: any) {
|
2019-04-07 21:50:36 +09:00
|
|
|
const followers = await Followings.find({
|
|
|
|
followeeId: note.userId
|
2018-07-21 05:35:43 +09:00
|
|
|
});
|
|
|
|
|
2018-07-21 19:33:56 +09:00
|
|
|
const queue: string[] = [];
|
|
|
|
|
2018-11-30 23:30:28 +09:00
|
|
|
for (const following of followers) {
|
2019-04-13 01:43:22 +09:00
|
|
|
if (Followings.isRemoteFollower(following)) {
|
2018-07-21 05:35:43 +09:00
|
|
|
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーなら投稿を配信
|
2019-04-07 21:50:36 +09:00
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
const inbox = following.followerSharedInbox || following.followerInbox;
|
2018-07-21 19:33:56 +09:00
|
|
|
if (!queue.includes(inbox)) queue.push(inbox);
|
2018-07-21 05:35:43 +09:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 23:30:28 +09:00
|
|
|
}
|
2018-07-21 19:33:56 +09:00
|
|
|
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const inbox of queue) {
|
2018-07-23 13:56:25 +09:00
|
|
|
deliver(user as any, noteActivity, inbox);
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-07-21 05:35:43 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
function deliverNoteToMentionedRemoteUsers(mentionedUsers: User[], user: ILocalUser, noteActivity: any) {
|
|
|
|
for (const u of mentionedUsers.filter(u => Users.isRemoteUser(u))) {
|
2018-07-21 05:35:43 +09:00
|
|
|
deliver(user, noteActivity, (u as IRemoteUser).inbox);
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-07-21 05:35:43 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
async function createMentionedEvents(mentionedUsers: User[], note: Note, nm: NotificationManager) {
|
|
|
|
for (const u of mentionedUsers.filter(u => Users.isLocalUser(u))) {
|
|
|
|
const detailPackedNote = await Notes.pack(note, u, {
|
2018-09-18 02:14:12 +09:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
publishMainStream(u.id, 'mention', detailPackedNote);
|
2018-07-21 05:35:43 +09:00
|
|
|
|
|
|
|
// Create notification
|
2019-04-07 21:50:36 +09:00
|
|
|
nm.push(u.id, 'mention');
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-07-21 05:35:43 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
function saveReply(reply: Note, note: Note) {
|
|
|
|
Notes.increment({ id: reply.id }, 'repliesCount', 1);
|
2018-07-21 05:35:43 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
function incNotesCountOfUser(user: User) {
|
|
|
|
Users.increment({ id: user.id }, 'notesCount', 1);
|
|
|
|
Users.update({ id: user.id }, {
|
|
|
|
updatedAt: new Date()
|
2018-07-21 05:35:43 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
async function extractMentionedUsers(user: User, tokens: ReturnType<typeof parse>): Promise<User[]> {
|
2018-07-21 05:35:43 +09:00
|
|
|
if (tokens == null) return [];
|
|
|
|
|
2018-12-19 11:16:29 +09:00
|
|
|
const mentions = extractMentions(tokens);
|
2018-07-21 11:06:01 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
let mentionedUsers = (await Promise.all(mentions.map(m =>
|
2019-04-07 21:50:36 +09:00
|
|
|
resolveUser(m.username, m.host || user.host).catch(() => null)
|
2019-04-13 01:43:22 +09:00
|
|
|
))).filter(x => x != null) as User[];
|
2018-10-30 21:55:16 +09:00
|
|
|
|
|
|
|
// Drop duplicate users
|
|
|
|
mentionedUsers = mentionedUsers.filter((u, i, self) =>
|
2019-04-07 21:50:36 +09:00
|
|
|
i === self.findIndex(u2 => u.id === u2.id)
|
2018-09-07 00:10:03 +09:00
|
|
|
);
|
2018-07-21 05:35:43 +09:00
|
|
|
|
|
|
|
return mentionedUsers;
|
|
|
|
}
|