From dc529711ced031155f53fa321159ec2830ef8b05 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Sun, 1 Apr 2018 19:43:26 +0900 Subject: Implement remote follow --- src/common/notify.ts | 50 ++++++++++++++++++++++++ src/common/remote/activitypub/renderer/follow.ts | 8 ++++ src/common/remote/activitypub/resolve-person.ts | 1 + src/common/remote/webfinger.ts | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/common/notify.ts create mode 100644 src/common/remote/activitypub/renderer/follow.ts (limited to 'src/common') diff --git a/src/common/notify.ts b/src/common/notify.ts new file mode 100644 index 0000000000..fc65820d3b --- /dev/null +++ b/src/common/notify.ts @@ -0,0 +1,50 @@ +import * as mongo from 'mongodb'; +import Notification from '../models/notification'; +import Mute from '../models/mute'; +import event from './event'; +import { pack } from '../models/notification'; + +export default ( + notifiee: mongo.ObjectID, + notifier: mongo.ObjectID, + type: string, + content?: any +) => new Promise(async (resolve, reject) => { + if (notifiee.equals(notifier)) { + return resolve(); + } + + // Create notification + const notification = await Notification.insert(Object.assign({ + createdAt: new Date(), + notifieeId: notifiee, + notifierId: notifier, + type: type, + isRead: false + }, content)); + + resolve(notification); + + // Publish notification event + event(notifiee, 'notification', + await pack(notification)); + + // 3秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する + setTimeout(async () => { + const fresh = await Notification.findOne({ _id: notification._id }, { isRead: true }); + if (!fresh.isRead) { + //#region ただしミュートしているユーザーからの通知なら無視 + const mute = await Mute.find({ + muterId: notifiee, + deletedAt: { $exists: false } + }); + const mutedUserIds = mute.map(m => m.muteeId.toString()); + if (mutedUserIds.indexOf(notifier.toString()) != -1) { + return; + } + //#endregion + + event(notifiee, 'unread_notification', await pack(notification)); + } + }, 3000); +}); diff --git a/src/common/remote/activitypub/renderer/follow.ts b/src/common/remote/activitypub/renderer/follow.ts new file mode 100644 index 0000000000..05c0ecca06 --- /dev/null +++ b/src/common/remote/activitypub/renderer/follow.ts @@ -0,0 +1,8 @@ +import config from '../../../../conf'; +import { IRemoteAccount } from '../../../../models/user'; + +export default ({ username }, { account }) => ({ + type: 'Follow', + actor: `${config.url}/@${username}`, + object: (account as IRemoteAccount).uri +}); diff --git a/src/common/remote/activitypub/resolve-person.ts b/src/common/remote/activitypub/resolve-person.ts index 999a37eea1..c44911a571 100644 --- a/src/common/remote/activitypub/resolve-person.ts +++ b/src/common/remote/activitypub/resolve-person.ts @@ -66,6 +66,7 @@ export default async (value, usernameLower, hostLower, acctLower) => { id: object.publicKey.id, publicKeyPem: object.publicKey.publicKeyPem }, + inbox: object.inbox, uri: object.id, }, }); diff --git a/src/common/remote/webfinger.ts b/src/common/remote/webfinger.ts index 23f0aaa55f..f5e3d89e1e 100644 --- a/src/common/remote/webfinger.ts +++ b/src/common/remote/webfinger.ts @@ -1,6 +1,6 @@ const WebFinger = require('webfinger.js'); -const webFinger = new WebFinger({}); +const webFinger = new WebFinger({ tls_only: false }); type ILink = { href: string; -- cgit v1.2.3-freya