summaryrefslogtreecommitdiff
path: root/src/server/api/common/get-friends.ts
blob: 876aa399f746bdffd64aa0aa68c1ae1d04fd302d (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
import * as mongodb from 'mongodb';
import Following from '../../../models/following';

export const getFriendIds = async (me: mongodb.ObjectID, includeMe = true) => {
	// Fetch relation to other users who the I follows
	// SELECT followee
	const followings = await Following
		.find({
			followerId: me
		}, {
			fields: {
				followeeId: true
			}
		});

	// ID list of other users who the I follows
	const myfollowingIds = followings.map(following => following.followeeId);

	if (includeMe) {
		myfollowingIds.push(me);
	}

	return myfollowingIds;
};

export const getFriends = async (me: mongodb.ObjectID, includeMe = true, remoteOnly = false) => {
	const q: any = remoteOnly ? {
		followerId: me,
		'_followee.host': { $ne: null }
	} : {
		followerId: me
	};
	// Fetch relation to other users who the I follows
	const followings = await Following
		.find(q);

	// ID list of other users who the I follows
	const myfollowings = followings.map(following => ({
		id: following.followeeId
	}));

	if (includeMe) {
		myfollowings.push({
			id: me
		});
	}

	return myfollowings;
};