Refactorgin

This commit is contained in:
syuilo 2019-04-25 04:17:03 +09:00
parent ee5720df2c
commit f5091d524b
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69

View File

@ -106,8 +106,6 @@ type Option = {
}; };
export default async (user: User, data: Option, silent = false) => new Promise<Note>(async (res, rej) => { export default async (user: User, data: Option, silent = false) => new Promise<Note>(async (res, rej) => {
const isFirstNote = user.notesCount === 0;
if (data.createdAt == null) data.createdAt = new Date(); if (data.createdAt == null) data.createdAt = new Date();
if (data.visibility == null) data.visibility = 'public'; if (data.visibility == null) data.visibility = 'public';
if (data.viaMobile == null) data.viaMobile = false; if (data.viaMobile == null) data.viaMobile = false;
@ -195,8 +193,6 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
// 統計を更新 // 統計を更新
notesChart.update(note, true); notesChart.update(note, true);
perUserNotesChart.update(user, note, true); perUserNotesChart.update(user, note, true);
// ローカルユーザーのチャートはタイムライン取得時に更新しているのでリモートユーザーの場合だけでよい
if (Users.isRemoteUser(user)) activeUsersChart.update(user);
// Register host // Register host
if (Users.isRemoteUser(user)) { if (Users.isRemoteUser(user)) {
@ -212,19 +208,6 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
// Increment notes count (user) // Increment notes count (user)
incNotesCountOfUser(user); incNotesCountOfUser(user);
// 未読通知を作成
if (data.visibility == 'specified') {
if (data.visibleUsers == null) throw new Error('invalid param');
for (const u of data.visibleUsers) {
insertNoteUnread(u, note, true);
}
} else {
for (const u of mentionedUsers) {
insertNoteUnread(u, note, false);
}
}
if (data.reply) { if (data.reply) {
saveReply(data.reply, note); saveReply(data.reply, note);
} }
@ -233,77 +216,91 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
incRenoteCount(data.renote); incRenoteCount(data.renote);
} }
// Pack the note
const noteObj = await Notes.pack(note);
if (isFirstNote) {
(noteObj as any).isFirstNote = true;
}
if (!silent) { if (!silent) {
// ローカルユーザーのチャートはタイムライン取得時に更新しているのでリモートユーザーの場合だけでよい
if (Users.isRemoteUser(user)) activeUsersChart.update(user);
// 未読通知を作成
if (data.visibility == 'specified') {
if (data.visibleUsers == null) throw new Error('invalid param');
for (const u of data.visibleUsers) {
insertNoteUnread(u, note, true);
}
} else {
for (const u of mentionedUsers) {
insertNoteUnread(u, note, false);
}
}
// Pack the note
const noteObj = await Notes.pack(note);
if (user.notesCount === 0) {
(noteObj as any).isFirstNote = true;
}
publishNotesStream(noteObj); publishNotesStream(noteObj);
}
const nm = new NotificationManager(user, note); const nm = new NotificationManager(user, note);
const nmRelatedPromises = []; const nmRelatedPromises = [];
createMentionedEvents(mentionedUsers, note, nm); createMentionedEvents(mentionedUsers, note, nm);
const noteActivity = await renderNoteOrRenoteActivity(data, note); const noteActivity = await renderNoteOrRenoteActivity(data, note);
if (Users.isLocalUser(user)) { if (Users.isLocalUser(user)) {
deliverNoteToMentionedRemoteUsers(mentionedUsers, user, noteActivity); deliverNoteToMentionedRemoteUsers(mentionedUsers, user, noteActivity);
}
const profile = await UserProfiles.findOne(user.id).then(ensure);
// If has in reply to note
if (data.reply) {
// Fetch watchers
nmRelatedPromises.push(notifyToWatchersOfReplyee(data.reply, user, nm));
// この投稿をWatchする
if (Users.isLocalUser(user) && profile.autoWatch) {
watch(user.id, data.reply);
} }
// 通知 const profile = await UserProfiles.findOne(user.id).then(ensure);
if (data.reply.userHost === null) {
nm.push(data.reply.userId, 'reply');
publishMainStream(data.reply.userId, 'reply', noteObj);
}
}
// If it is renote // If has in reply to note
if (data.renote) { if (data.reply) {
const type = data.text ? 'quote' : 'renote'; // Fetch watchers
nmRelatedPromises.push(notifyToWatchersOfReplyee(data.reply, user, nm));
// Notify // この投稿をWatchする
if (data.renote.userHost === null) { if (Users.isLocalUser(user) && profile.autoWatch) {
nm.push(data.renote.userId, type); watch(user.id, data.reply);
}
// 通知
if (data.reply.userHost === null) {
nm.push(data.reply.userId, 'reply');
publishMainStream(data.reply.userId, 'reply', noteObj);
}
} }
// Fetch watchers // If it is renote
nmRelatedPromises.push(notifyToWatchersOfRenotee(data.renote, user, nm, type)); if (data.renote) {
const type = data.text ? 'quote' : 'renote';
// この投稿をWatchする // Notify
if (Users.isLocalUser(user) && profile.autoWatch) { if (data.renote.userHost === null) {
watch(user.id, data.renote); nm.push(data.renote.userId, type);
}
// Fetch watchers
nmRelatedPromises.push(notifyToWatchersOfRenotee(data.renote, user, nm, type));
// この投稿をWatchする
if (Users.isLocalUser(user) && profile.autoWatch) {
watch(user.id, data.renote);
}
// Publish event
if ((user.id !== data.renote.userId) && data.renote.userHost === null) {
publishMainStream(data.renote.userId, 'renote', noteObj);
}
} }
// Publish event
if ((user.id !== data.renote.userId) && data.renote.userHost === null) {
publishMainStream(data.renote.userId, 'renote', noteObj);
}
}
if (!silent) {
publish(user, note, data.reply, data.renote, noteActivity); publish(user, note, data.reply, data.renote, noteActivity);
}
Promise.all(nmRelatedPromises).then(() => { Promise.all(nmRelatedPromises).then(() => {
nm.deliver(); nm.deliver();
}); });
}
// Register to search database // Register to search database
index(note); index(note);