summaryrefslogtreecommitdiff
path: root/src/services/chart/per-user-following.ts
blob: 8a94a4f155cdeae0d0b0ecb480a3586e09847aa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import autobind from 'autobind-decorator';
import Chart, { Obj } from './';
import Following from '../../models/following';
import { IUser, isLocalUser } from '../../models/user';
import { SchemaType } from '../../misc/schema';

export const logSchema = {
	/**
	 * フォローしている
	 */
	followings: {
		type: 'object' as 'object',
		properties: {
			/**
			 * フォローしている合計
			 */
			total: {
				type: 'number',
				description: 'フォローしている合計',
			},

			/**
			 * フォローした数
			 */
			inc: {
				type: 'number',
				description: 'フォローした数',
			},

			/**
			 * フォロー解除した数
			 */
			dec: {
				type: 'number',
				description: 'フォロー解除した数',
			},
		}
	},

	/**
	 * フォローされている
	 */
	followers: {
		type: 'object' as 'object',
		properties: {
			/**
			 * フォローされている合計
			 */
			total: {
				type: 'number',
				description: 'フォローされている合計',
			},

			/**
			 * フォローされた数
			 */
			inc: {
				type: 'number',
				description: 'フォローされた数',
			},

			/**
			 * フォロー解除された数
			 */
			dec: {
				type: 'number',
				description: 'フォロー解除された数',
			},
		}
	},
};

export const perUserFollowingLogSchema = {
	type: 'object' as 'object',
	properties: {
		local: {
			type: 'object' as 'object',
			properties: logSchema
		},
		remote: {
			type: 'object' as 'object',
			properties: logSchema
		},
	}
};

type PerUserFollowingLog = SchemaType<typeof perUserFollowingLogSchema>;

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();