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 Oct 16, 2017
1 parent cf0262e commit e81cef1
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 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,18 +15,30 @@ 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' },
$or: [
{'phone.number': {$in: contacts}},
{'services.facebook.id': {$in: facebookFriendsIds}},
{'profile.name': {$in: ['Ethan Gonzalez', 'Bryan Wallace', 'Avery Stewart', 'Katie Peterson', 'Ray Edwards']}}
]
};
} else {
selector = {
$or: [
{'phone.number': {$in: contacts}},
{'services.facebook.id': {$in: facebookFriendsIds}},
{'profile.name': {$in: ['Ethan Gonzalez', 'Bryan Wallace', 'Avery Stewart', 'Katie Peterson', 'Ray Edwards']}}
]
}
Expand Down

0 comments on commit e81cef1

Please sign in to comment.