2019-02-05 07:14:23 +02:00
|
|
|
import { publishMainStream } from '../stream';
|
2019-01-30 19:29:36 +02:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-10-29 13:32:42 +02:00
|
|
|
import renderFollow from '../../remote/activitypub/renderer/follow';
|
|
|
|
import renderUndo from '../../remote/activitypub/renderer/undo';
|
|
|
|
import renderBlock from '../../remote/activitypub/renderer/block';
|
|
|
|
import { deliver } from '../../queue';
|
|
|
|
import renderReject from '../../remote/activitypub/renderer/reject';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { User } from '../../models/entities/user';
|
|
|
|
import { Blockings, Users, FollowRequests, Followings } from '../../models';
|
|
|
|
import { perUserFollowingChart } from '../chart';
|
|
|
|
import { genId } from '../../misc/gen-id';
|
2018-10-29 13:32:42 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
export default async function(blocker: User, blockee: User) {
|
2018-10-29 13:32:42 +02:00
|
|
|
await Promise.all([
|
|
|
|
cancelRequest(blocker, blockee),
|
|
|
|
cancelRequest(blockee, blocker),
|
|
|
|
unFollow(blocker, blockee),
|
|
|
|
unFollow(blockee, blocker)
|
|
|
|
]);
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
await Blockings.save({
|
|
|
|
id: genId(),
|
2018-10-29 13:32:42 +02:00
|
|
|
createdAt: new Date(),
|
2019-04-07 15:50:36 +03:00
|
|
|
blockerId: blocker.id,
|
|
|
|
blockeeId: blockee.id,
|
2018-10-29 13:32:42 +02:00
|
|
|
});
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isLocalUser(blocker) && Users.isRemoteUser(blockee)) {
|
2019-01-30 19:29:36 +02:00
|
|
|
const content = renderActivity(renderBlock(blocker, blockee));
|
2018-10-29 13:32:42 +02:00
|
|
|
deliver(blocker, content, blockee.inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
async function cancelRequest(follower: User, followee: User) {
|
|
|
|
const request = await FollowRequests.findOne({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id
|
2018-10-29 13:32:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (request == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
await FollowRequests.delete({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id
|
2018-10-29 13:32:42 +02:00
|
|
|
});
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isLocalUser(followee)) {
|
|
|
|
Users.pack(followee, followee, {
|
2018-10-29 13:32:42 +02:00
|
|
|
detail: true
|
2019-04-07 15:50:36 +03:00
|
|
|
}).then(packed => publishMainStream(followee.id, 'meUpdated', packed));
|
2018-10-29 13:32:42 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isLocalUser(follower)) {
|
|
|
|
Users.pack(followee, follower, {
|
2018-10-30 07:34:32 +02:00
|
|
|
detail: true
|
2019-04-07 15:50:36 +03:00
|
|
|
}).then(packed => publishMainStream(follower.id, 'unfollow', packed));
|
2018-10-29 13:32:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローリクエストをしていたらUndoFollow送信
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-01-30 19:29:36 +02:00
|
|
|
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
|
2018-10-29 13:32:42 +02:00
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
// リモートからフォローリクエストを受けていたらReject送信
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
2019-01-30 19:29:36 +02:00
|
|
|
const content = renderActivity(renderReject(renderFollow(follower, followee, request.requestId), followee));
|
2018-10-29 13:32:42 +02:00
|
|
|
deliver(followee, content, follower.inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
async function unFollow(follower: User, followee: User) {
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id
|
2018-10-29 13:32:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
Followings.delete(following.id);
|
2018-10-29 13:32:42 +02:00
|
|
|
|
|
|
|
//#region Decrement following count
|
2019-04-07 15:50:36 +03:00
|
|
|
Users.decrement({ id: follower.id }, 'followingCount', 1);
|
2018-10-29 13:32:42 +02:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Decrement followers count
|
2019-04-07 15:50:36 +03:00
|
|
|
Users.decrement({ id: followee.id }, 'followersCount', 1);
|
2018-10-29 13:32:42 +02:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
perUserFollowingChart.update(follower, followee, false);
|
|
|
|
|
|
|
|
// Publish unfollow event
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isLocalUser(follower)) {
|
|
|
|
Users.pack(followee, follower, {
|
2018-10-30 07:34:32 +02:00
|
|
|
detail: true
|
2019-04-07 15:50:36 +03:00
|
|
|
}).then(packed => publishMainStream(follower.id, 'unfollow', packed));
|
2018-10-29 13:32:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローをしていたらUndoFollow送信
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-01-30 19:29:36 +02:00
|
|
|
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
|
2018-10-29 13:32:42 +02:00
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
|
|
|
}
|