2018-10-29 11:32:42 +00:00
|
|
|
import User, { isLocalUser, isRemoteUser, pack as packUser, IUser } from '../../models/user';
|
|
|
|
import Following from '../../models/following';
|
|
|
|
import FollowRequest from '../../models/follow-request';
|
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-02-07 19:31:33 +00:00
|
|
|
import perUserFollowingChart from '../../services/chart/per-user-following';
|
2018-10-29 11:32:42 +00:00
|
|
|
import Blocking from '../../models/blocking';
|
|
|
|
|
|
|
|
export default async function(blocker: IUser, blockee: IUser) {
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
cancelRequest(blocker, blockee),
|
|
|
|
cancelRequest(blockee, blocker),
|
|
|
|
unFollow(blocker, blockee),
|
|
|
|
unFollow(blockee, blocker)
|
|
|
|
]);
|
|
|
|
|
|
|
|
await Blocking.insert({
|
|
|
|
createdAt: new Date(),
|
|
|
|
blockerId: blocker._id,
|
|
|
|
blockeeId: blockee._id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isLocalUser(blocker) && 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cancelRequest(follower: IUser, followee: IUser) {
|
|
|
|
const request = await FollowRequest.findOne({
|
|
|
|
followeeId: followee._id,
|
|
|
|
followerId: follower._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (request == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await FollowRequest.remove({
|
|
|
|
followeeId: followee._id,
|
|
|
|
followerId: follower._id
|
|
|
|
});
|
|
|
|
|
|
|
|
await User.update({ _id: followee._id }, {
|
|
|
|
$inc: {
|
|
|
|
pendingReceivedFollowRequestsCount: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isLocalUser(followee)) {
|
|
|
|
packUser(followee, followee, {
|
|
|
|
detail: true
|
|
|
|
}).then(packed => publishMainStream(followee._id, 'meUpdated', packed));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLocalUser(follower)) {
|
2018-10-30 05:34:32 +00:00
|
|
|
packUser(followee, follower, {
|
|
|
|
detail: true
|
|
|
|
}).then(packed => publishMainStream(follower._id, 'unfollow', packed));
|
2018-10-29 11:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローリクエストをしていたらUndoFollow送信
|
|
|
|
if (isLocalUser(follower) && 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送信
|
|
|
|
if (isRemoteUser(follower) && 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function unFollow(follower: IUser, followee: IUser) {
|
|
|
|
const following = await Following.findOne({
|
|
|
|
followerId: follower._id,
|
|
|
|
followeeId: followee._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Following.remove({
|
|
|
|
_id: following._id
|
|
|
|
});
|
|
|
|
|
|
|
|
//#region Decrement following count
|
|
|
|
User.update({ _id: follower._id }, {
|
|
|
|
$inc: {
|
|
|
|
followingCount: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Decrement followers count
|
|
|
|
User.update({ _id: followee._id }, {
|
|
|
|
$inc: {
|
|
|
|
followersCount: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
perUserFollowingChart.update(follower, followee, false);
|
|
|
|
|
|
|
|
// Publish unfollow event
|
|
|
|
if (isLocalUser(follower)) {
|
2018-10-30 05:34:32 +00:00
|
|
|
packUser(followee, follower, {
|
|
|
|
detail: true
|
|
|
|
}).then(packed => publishMainStream(follower._id, 'unfollow', packed));
|
2018-10-29 11:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローをしていたらUndoFollow送信
|
|
|
|
if (isLocalUser(follower) && 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);
|
|
|
|
}
|
|
|
|
}
|