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-04-04 16:22:41 +00:00
|
|
|
import renderFollow from '../../remote/activitypub/renderer/follow';
|
|
|
|
import renderUndo from '../../remote/activitypub/renderer/undo';
|
2018-04-05 14:24:51 +00:00
|
|
|
import { deliver } from '../../queue';
|
2019-03-02 09:51:59 +00:00
|
|
|
import Logger from '../logger';
|
2019-02-08 07:58:57 +00:00
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { User } from '../../models/entities/user';
|
|
|
|
import { Followings, Users, Instances } from '../../models';
|
|
|
|
import { instanceChart, perUserFollowingChart } from '../chart';
|
2019-02-03 09:16:57 +00:00
|
|
|
|
|
|
|
const logger = new Logger('following/delete');
|
2018-04-04 16:22:41 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
export default async function(follower: User, followee: User, silent = false) {
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id
|
2018-04-04 16:22:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
2019-02-03 09:16:57 +00:00
|
|
|
logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした');
|
2018-04-04 16:22:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
Followings.delete(following.id);
|
2018-04-04 16:22:41 +00:00
|
|
|
|
|
|
|
//#region Decrement following count
|
2019-04-07 12:50:36 +00:00
|
|
|
Users.decrement({ id: follower.id }, 'followingCount', 1);
|
2018-04-04 16:22:41 +00:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Decrement followers count
|
2019-04-07 12:50:36 +00:00
|
|
|
Users.decrement({ id: followee.id }, 'followersCount', 1);
|
2018-04-04 16:22:41 +00:00
|
|
|
//#endregion
|
|
|
|
|
2019-02-08 07:58:57 +00:00
|
|
|
//#region Update instance stats
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
2019-02-08 07:58:57 +00:00
|
|
|
registerOrFetchInstanceDoc(follower.host).then(i => {
|
2019-04-07 12:50:36 +00:00
|
|
|
Instances.decrement({ id: i.id }, 'followingCount', 1);
|
2019-02-08 07:58:57 +00:00
|
|
|
instanceChart.updateFollowing(i.host, false);
|
|
|
|
});
|
2019-04-07 12:50:36 +00:00
|
|
|
} else if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-02-08 07:58:57 +00:00
|
|
|
registerOrFetchInstanceDoc(followee.host).then(i => {
|
2019-04-07 12:50:36 +00:00
|
|
|
Instances.decrement({ id: i.id }, 'followersCount', 1);
|
2019-02-08 07:58:57 +00:00
|
|
|
instanceChart.updateFollowers(i.host, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-10-22 20:36:35 +00:00
|
|
|
perUserFollowingChart.update(follower, followee, false);
|
2018-10-21 08:51:35 +00:00
|
|
|
|
2018-04-04 16:22:41 +00:00
|
|
|
// Publish unfollow event
|
2019-04-07 12:50:36 +00:00
|
|
|
if (!silent && 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-04-04 16:22:41 +00:00
|
|
|
}
|
|
|
|
|
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-04-15 08:53:25 +00:00
|
|
|
deliver(follower, content, followee.inbox);
|
2018-04-04 16:22:41 +00:00
|
|
|
}
|
|
|
|
}
|