-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
214 lines (204 loc) · 5.82 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
Likes = new Meteor.Collection("likes");
Messages = new Meteor.Collection("messages");
var NonEmptyString = Match.Where(function(x) {
check(x, String);
return x.length !== 0;
});
var ConvertedNumber = Match.Where(function(x) {
check(x, String);
return parseInt(x) !== NaN;
});
if (Meteor.isServer) {
var Telepathy = DDP.connect("http://telepathy.meteor.com");
//Server HTTP methods
HTTP.methods({
'users': function() {
var users = Meteor.users.find({}).fetch();
return JSON.stringify(users);
},
'user/:username': function(){
var user = Meteor.users.findOne({"services.facebook.username": this.params.username});
return JSON.stringify(user);
},
'list': function() {
return '<b>Default content type is text/html</b>';
},
'push/fb': {
get: function() {
console.log('GET');
var hubChallenge = this.query["hub.challenge"];
//TODO: Implement verification
return hubChallenge;
},
post: function(data) {
console.log('POST');
Meteor.call('findNewLike', data, function(err, result){
if (err) {
console.log('ERROR');
console.log(err);
}
console.log(result);
});
}
}
});
//Server Meteor methods
Meteor.methods({
monitorInbox: function(user, interval) {
var fbId = user.services.facebook.id;
var fbAccessToken = user.services.facebook.accessToken;
var result = null;
try {
result = HTTP.get(
"https://graph.facebook.com/" + fbId + "/inbox?limit=1",
{"params": {"access_token": fbAccessToken}}
);
var result_data = result.data ? result.data.data : null;
for (var i = 0; i < result_data.length; i++) {
var thread = result_data[i];
var comments = thread.comments.data;
var lastComment = comments[comments.length - 1];
var currentDate = new Date();
var commentDate = new Date(moment(lastComment.created_time).format());
// console.log('Current date: ' + currentDate);
// console.log('Comment date: ' + commentDate);
var secondDiff = Math.round((currentDate - commentDate)/1000);
if (secondDiff <= interval) {
var newMessage = {
userId: user._id,
user_fbId: fbId,
message: lastComment
};
var storedMessage = Messages.findOne(newMessage);
if (!storedMessage) {
console.log('Adding new message');
console.log(lastComment);
Telepathy.call('message', newMessage.message.message);
Messages.insert(newMessage);
}
}
}
} catch (e) {
console.log("Error!");
console.log(JSON.stringify(e));
return false;
}
return true;
},
getSubscriptions: function() {
console.log('GETTING SUBSCRIPTIONS');
var appId = Meteor.settings.fb.appId;
var access_token = Meteor.settings.fb.appId + "|" + Meteor.settings.fb.secret;
var result = null;
try {
result = HTTP.get(
"https://graph.facebook.com/v2.2/" + appId + "/subscriptions",
{"params": {"access_token": access_token}}
);
console.log("Results found: ");
console.log(result);
if (!result) return false;
} catch (e) {
console.log("Error!");
console.log(JSON.stringify(e));
return false;
}
return result;
},
addSubscription: function() {
console.log('ADDING SUBSCRIPTION');
var appId = Meteor.settings.fb.appId;
var access_token = Meteor.settings.fb.appId + "|" + Meteor.settings.fb.secret;
var object = 'user';
var callback_url = Meteor.settings.fb_subscriptions.callback_url;
var fields = Meteor.settings.fb_subscriptions.fields;
var verify_token = Meteor.settings.fb_subscriptions.verify_token;
var result = null;
try {
result = HTTP.post(
"https://graph.facebook.com/v2.2/" + appId + "/subscriptions",
{
"params": {"access_token": access_token},
"data": {
"object": object,
"callback_url": callback_url,
"fields": fields,
"verify_token": verify_token
}
}
);
console.log("Result: ");
console.log(result);
if (!result) return false;
} catch (e) {
console.log("Error!");
console.log(JSON.stringify(e));
return false;
}
return result;
},
deleteSubscription: function() {
console.log('DELETING SUBSCRIPTION');
var appId = Meteor.settings.fb.appId;
var access_token = Meteor.settings.fb.appId + "|" + Meteor.settings.fb.secret;
var object = 'user';
var result = null;
try {
result = HTTP.del(
"https://graph.facebook.com/v2.2/" + appId + "/subscriptions",
{
"params": {"access_token": access_token},
"data": {
"object": object
}
}
);
console.log("Result: ");
console.log(result);
if (!result) return false;
} catch (e) {
console.log("Error!");
console.log(JSON.stringify(e));
return false;
}
return result;
},
findNewLike: function(data) {
console.log('FINDING NEW LIKE: ');
console.log(data.entry);
//Get unique changed ids
var changedIds = [];
for (var i = 0; i < data.entry.length; i++) {
var entry = data.entry[i];
if (changedIds.indexOf(entry.id) < 0) {
changedIds.push(entry.id);
}
}
//Get new likes of each id
var access_token = Meteor.settings.fb.appId + "|" + Meteor.settings.fb.secret;
for (var i = 0; i < changedIds.length; i++) {
try {
result = HTTP.del(
"https://graph.facebook.com/v2.2/" + changedIds[i] + "/likes",
{
"params": {"access_token": access_token},
}
);
var resultData = result.data ? result.data.data : null;
if (!resultData) return false;
console.log('ADDING NEW LIKE');
console.log(resultData[0]);
Likes.insert({
userId: changedIds[i],
like: resultData[0]
});
return true;
} catch (e) {
console.log("Error!");
console.log(JSON.stringify(e));
return false;
}
}
}
});
}