1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-17 08:08:17 +09:00
hotomoe/src/services/chart/per-user-following.ts

163 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-10-23 05:36:35 +09:00
import autobind from 'autobind-decorator';
import Chart, { Obj } from './';
import Following from '../../models/following';
import { IUser, isLocalUser } from '../../models/user';
2019-02-25 04:02:18 +09:00
import { SchemaType } from '../../misc/schema';
2018-10-23 05:36:35 +09:00
2019-02-25 04:02:18 +09:00
export const logSchema = {
/**
*
*/
followings: {
type: 'object' as 'object',
properties: {
2018-10-23 05:36:35 +09:00
/**
2019-02-25 04:02:18 +09:00
*
2018-10-23 05:36:35 +09:00
*/
2019-02-25 04:02:18 +09:00
total: {
type: 'number',
description: 'フォローしている合計',
},
2018-10-23 05:36:35 +09:00
/**
*
*/
2019-02-25 04:02:18 +09:00
inc: {
type: 'number',
description: 'フォローした数',
},
2018-10-23 05:36:35 +09:00
/**
*
*/
2019-02-25 04:02:18 +09:00
dec: {
type: 'number',
description: 'フォロー解除した数',
},
}
},
2018-10-23 05:36:35 +09:00
2019-02-25 04:02:18 +09:00
/**
*
*/
followers: {
type: 'object' as 'object',
properties: {
2018-10-23 05:36:35 +09:00
/**
2019-02-25 04:02:18 +09:00
*
2018-10-23 05:36:35 +09:00
*/
2019-02-25 04:02:18 +09:00
total: {
type: 'number',
description: 'フォローされている合計',
},
2018-10-23 05:36:35 +09:00
/**
*
*/
2019-02-25 04:02:18 +09:00
inc: {
type: 'number',
description: 'フォローされた数',
},
2018-10-23 05:36:35 +09:00
/**
*
*/
2019-02-25 04:02:18 +09:00
dec: {
type: 'number',
description: 'フォロー解除された数',
},
}
},
};
2018-10-23 05:36:35 +09:00
2019-02-25 04:02:18 +09:00
export const perUserFollowingLogSchema = {
type: 'object' as 'object',
properties: {
local: {
type: 'object' as 'object',
properties: logSchema
},
remote: {
type: 'object' as 'object',
properties: logSchema
},
}
2018-10-23 05:36:35 +09:00
};
2019-02-25 04:02:18 +09:00
type PerUserFollowingLog = SchemaType<typeof perUserFollowingLogSchema>;
2018-10-23 05:36:35 +09:00
class PerUserFollowingChart extends Chart<PerUserFollowingLog> {
constructor() {
super('perUserFollowing', true);
}
@autobind
protected async getTemplate(init: boolean, latest?: PerUserFollowingLog, group?: any): Promise<PerUserFollowingLog> {
const [
localFollowingsCount,
localFollowersCount,
remoteFollowingsCount,
remoteFollowersCount
] = init ? await Promise.all([
Following.count({ followerId: group, '_followee.host': null }),
Following.count({ followeeId: group, '_follower.host': null }),
Following.count({ followerId: group, '_followee.host': { $ne: null } }),
Following.count({ followeeId: group, '_follower.host': { $ne: null } })
]) : [
latest ? latest.local.followings.total : 0,
latest ? latest.local.followers.total : 0,
latest ? latest.remote.followings.total : 0,
latest ? latest.remote.followers.total : 0
];
return {
local: {
followings: {
total: localFollowingsCount,
inc: 0,
dec: 0
},
followers: {
total: localFollowersCount,
inc: 0,
dec: 0
}
},
remote: {
followings: {
total: remoteFollowingsCount,
inc: 0,
dec: 0
},
followers: {
total: remoteFollowersCount,
inc: 0,
dec: 0
}
}
};
}
@autobind
public async update(follower: IUser, followee: IUser, isFollow: boolean) {
const update: Obj = {};
update.total = isFollow ? 1 : -1;
if (isFollow) {
update.inc = 1;
} else {
update.dec = 1;
}
this.inc({
[isLocalUser(follower) ? 'local' : 'remote']: { followings: update }
}, follower._id);
this.inc({
[isLocalUser(followee) ? 'local' : 'remote']: { followers: update }
}, followee._id);
}
}
export default new PerUserFollowingChart();