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