2016-12-28 23:49:51 +01:00
|
|
|
import * as mongodb from 'mongodb';
|
2018-03-29 13:32:18 +02:00
|
|
|
import Following from '../../../models/following';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-04-19 05:43:25 +02:00
|
|
|
export const getFriendIds = async (me: mongodb.ObjectID, includeMe = true) => {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Fetch relation to other users who the I follows
|
|
|
|
// SELECT followee
|
2018-04-19 05:43:25 +02:00
|
|
|
const followings = await Following
|
2016-12-28 23:49:51 +01:00
|
|
|
.find({
|
2018-04-02 14:57:36 +02:00
|
|
|
followerId: me
|
2016-12-28 23:49:51 +01:00
|
|
|
}, {
|
2017-01-17 22:10:56 +01:00
|
|
|
fields: {
|
2018-03-29 07:48:47 +02:00
|
|
|
followeeId: true
|
2017-01-17 22:10:56 +01:00
|
|
|
}
|
2017-01-17 03:11:22 +01:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// ID list of other users who the I follows
|
2018-04-19 05:43:25 +02:00
|
|
|
const myfollowingIds = followings.map(following => following.followeeId);
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
if (includeMe) {
|
|
|
|
myfollowingIds.push(me);
|
|
|
|
}
|
|
|
|
|
|
|
|
return myfollowingIds;
|
|
|
|
};
|
2018-04-19 05:43:25 +02:00
|
|
|
|
|
|
|
export const getFriends = async (me: mongodb.ObjectID, includeMe = true) => {
|
|
|
|
// Fetch relation to other users who the I follows
|
|
|
|
const followings = await Following
|
|
|
|
.find({
|
|
|
|
followerId: me
|
|
|
|
});
|
|
|
|
|
|
|
|
// ID list of other users who the I follows
|
|
|
|
const myfollowings = followings.map(following => ({
|
|
|
|
id: following.followeeId,
|
|
|
|
stalk: following.stalk
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (includeMe) {
|
|
|
|
myfollowings.push({
|
|
|
|
id: me,
|
|
|
|
stalk: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return myfollowings;
|
|
|
|
};
|