Permalink
Browse files

Step 17.21: Update users publication to publish Facebook friends

  • Loading branch information...
darkbasic committed Jun 11, 2017
1 parent 8a644bb commit 564a3f18036d22ef835cb4cfa218ae386169e7f0
Showing with 21 additions and 2 deletions.
  1. +21 −2 api/server/publications.ts
View
@@ -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,
@@ -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 {

0 comments on commit 564a3f1

Please sign in to comment.