Navigation Menu

Skip to content

Commit

Permalink
Step 17.21: Update users publication to publish Facebook friends
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Jun 15, 2017
1 parent 8a644bb commit 564a3f1
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions api/server/publications.ts
Expand Up @@ -3,6 +3,7 @@ import { Users } from './collections/users';
import { Messages } from './collections/messages';
import { Chats } from './collections/chats';
import { Pictures } from './collections/pictures';
import { facebookService } from "./services/facebook";

Meteor.publishComposite('users', function(
pattern: string,
Expand All @@ -14,13 +15,31 @@ Meteor.publishComposite('users', function(

let selector = {};

var facebookFriendsIds: string[] = [];
if (Users.collection.findOne({'_id': this.userId}).services.facebook) {
//FIXME: add definitions for the promise Meteor package
//TODO: handle error: token may be expired
const accessToken = (<any>Promise).await(facebookService.getAccessToken(this.userId));
//TODO: handle error: user may have denied permissions
const facebookFriends = (<any>Promise).await(facebookService.getFriends(accessToken));
facebookFriendsIds = facebookFriends.map((friend) => friend.id);
}

if (pattern) {
selector = {
'profile.name': { $regex: pattern, $options: 'i' },
'phone.number': {$in: contacts}
$or: [
{'phone.number': {$in: contacts}},
{'services.facebook.id': {$in: facebookFriendsIds}}
]
};
} else {
selector = {'phone.number': {$in: contacts}}
selector = {
$or: [
{'phone.number': {$in: contacts}},
{'services.facebook.id': {$in: facebookFriendsIds}}
]
}
}

return {
Expand Down

0 comments on commit 564a3f1

Please sign in to comment.