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