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