2019-03-07 13:19:32 +01:00
|
|
|
import { updateQuestion } from '../../../remote/activitypub/models/question';
|
|
|
|
import ms = require('ms');
|
|
|
|
import Logger from '../../logger';
|
|
|
|
import renderUpdate from '../../../remote/activitypub/renderer/update';
|
|
|
|
import { renderActivity } from '../../../remote/activitypub/renderer';
|
|
|
|
import { deliver } from '../../../queue';
|
|
|
|
import renderNote from '../../../remote/activitypub/renderer/note';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Users, Notes, Followings } from '../../../models';
|
|
|
|
import { Note } from '../../../models/entities/note';
|
2019-03-07 13:19:32 +01:00
|
|
|
|
|
|
|
const logger = new Logger('pollsUpdate');
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export async function triggerUpdate(note: Note) {
|
2019-03-07 13:19:32 +01:00
|
|
|
if (!note.updatedAt || Date.now() - new Date(note.updatedAt).getTime() > ms('1min')) {
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.info(`Updating ${note.id}`);
|
2019-03-07 13:19:32 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
const updated = await updateQuestion(note.uri);
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.info(`Updated ${note.id} ${updated ? 'changed' : 'nochange'}`);
|
2019-03-07 13:19:32 +01:00
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export async function deliverQuestionUpdate(noteId: Note['id']) {
|
|
|
|
const note = await Notes.findOne(noteId);
|
2019-03-07 13:19:32 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const user = await Users.findOne(note.userId);
|
2019-03-07 13:19:32 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const followers = await Followings.find({
|
|
|
|
followeeId: user.id
|
2019-03-07 13:19:32 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const queue: string[] = [];
|
|
|
|
|
|
|
|
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーならUpdateを配信
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(user)) {
|
2019-03-07 13:19:32 +01:00
|
|
|
for (const following of followers) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const follower = {
|
|
|
|
inbox: following.followerInbox,
|
|
|
|
sharedInbox: following.followerSharedInbox
|
|
|
|
};
|
2019-03-07 13:19:32 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (following.followerHost !== null) {
|
2019-03-07 13:19:32 +01:00
|
|
|
const inbox = follower.sharedInbox || follower.inbox;
|
|
|
|
if (!queue.includes(inbox)) queue.push(inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queue.length > 0) {
|
|
|
|
const content = renderActivity(renderUpdate(await renderNote(note, false), user));
|
|
|
|
for (const inbox of queue) {
|
|
|
|
deliver(user, content, inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|