-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
411 lines (364 loc) · 10.9 KB
/
app.rb
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
require "sinatra"
require 'koala'
require 'data_mapper'
enable :sessions
set :raise_errors, false
set :show_exceptions, false
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/users.db")
class User
include DataMapper::Resource
property :id, Serial
property :name, String
property :likes, Object
property :ings, Object
property :data, Object
property :friends, Object
property :nearbyfriends, Object
property :closefriends, Object
property :ignoredfriends, Object
property :friendpref, Integer #0 for all friends, 1 for close friends, 2 for nearby friends
property :objstate, Integer #0 for member of app, -1 for imported info
#property :recentlymessaged, Object
end
DataMapper.finalize.auto_upgrade!
# Scope defines what permissions that we are asking the user to grant.
# In this example, we are asking for the ability to publish stories
# about using the app, access to what the user likes, and to be able
# to use their pictures. You should rewrite this scope with whatever
# permissions your app needs.
# See https://developers.facebook.com/docs/reference/api/permissions/
# for a full list of permissions
FACEBOOK_SCOPE = 'user_likes,user_photos,user_photo_video_tags'
#User.raise_on_save_failure = true
#$queue = Array.new
#$queue_friends = Hash.new
#$queue_activities = Hash.new
def next_match (user)
#while ($queue.size == 0)
#end
#puts "Found element in queue!"
#user = $queue.pop
matching_data = Array.new
matching_ing = Array.new
rand_friend = ""
count = 0
while matching_data.size == 0 && matching_ing.size == 0
redirect "/youfuckingsuck" if count > 20
rand_friend = get_random_friend(user)
matching_ing = get_matching_ings(user, rand_friend)
break if matching_ing.size > 0
matching_data = get_matching_data(user, rand_friend)
count = count + 1
end
#verb_activity_goal(rand_friend, matching_ing, matching_data)
#user['friendid_queue'] << rand_friend['id']
#if $queue_friends[user] == nil
#$queue_friends[user] = Array.new
#$queue_activities[user] = Array.new
@rand_friend = rand_friend
#end
#$queue_friends[user] << rand_friend
if matching_ing.size > 0
@activity = matching_ing[rand(matching_ing.size)]
else
@activity = matching_data[rand(matching_data.size)]
end
#$queue_activities[user] << activity
#user['activityid_queue'] << activityid
#puts user.save
end
unless ENV["FACEBOOK_APP_ID"] && ENV["FACEBOOK_SECRET"]
abort("missing env vars: please set FACEBOOK_APP_ID and FACEBOOK_SECRET with your app credentials")
end
before do
# HTTPS redirect
if settings.environment == :production && request.scheme != 'https'
redirect "https://#{request.env['HTTP_HOST']}"
end
end
helpers do
def host
request.env['HTTP_HOST']
end
def scheme
request.scheme
end
def url_no_scheme(path = '')
"//#{host}#{path}"
end
def url(path = '')
"#{scheme}://#{host}#{path}"
end
def authenticator
@authenticator ||= Koala::Facebook::OAuth.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], url("/auth/facebook/callback"))
end
end
# the facebook session expired! reset ours and restart the process
error(Koala::Facebook::APIError) do
session[:access_token] = nil
redirect "/auth/facebook"
#redirect "/login"
end
def get_closefriends
friends = Array.new
@inbox = @graph.get_connections('me', 'inbox')
@inbox.each do |mailitem|
#puts mailitem["to"][1].to_s
mailitem['to']['data'].each do |user|
puts user.to_s
if user['id'] != @my_fbuser['id']
friends << user['id']
end
end
end
friends
end
def get_friends
friends = Array.new
@graph.get_connections('me', 'friends').each do |friend|
friends << friend['id']
#puts "NIGGERS: " + friend['id'].to_s
end
return friends
end
def get_nearbyfriends
newsfeed = @graph.fql_query("SELECT id, page_id FROM location_post WHERE distance(latitude, longitude, '40.1112272999', '-88.225622899' ) < 10000")
nearbyfriends = Array.new
newsfeed.each do |page|
nearbyfriends << @graph.get_object(page['id'])['from']['id']
end
nearbyfriends
end
def get_user(id = 'me')
fbuser = @graph.get_object(id)
fbid = fbuser['id']
puts "Getting FB user: " + fbid.to_s
user = User.get(fbid)
if (user != nil)
puts "User exists."
if id == 'me' && user['objstate'] == -1
puts "User is skeleton. Filling..."
user['nearbyfriends'] = get_nearbyfriends
user['closefriends'] = get_closefriends
user['friends'] = get_friends
user['ignoredfriends'] = Array.new
user['ignoredfriends'] << 0
user['objstate'] = 0
end
user.save
else
puts "Creating new user: " + fbid.to_s
user = User.new
user['id'] = fbid
user['name'] = fbuser['name']
user['likes'] = get_likes(id)
user['ings'] = get_ings(id)
user['data'] = get_data(id)
user['friendpref'] = 0
if id == 'me'
puts "User is self. Filling skeleton..."
user['friends'] = get_friends
user['nearbyfriends'] = get_nearbyfriends
user['closefriends'] = get_closefriends
user['ignoredfriends'] = Array.new
user['ignoredfriends'] << 0
user['objstate'] = 0
user.save
else
user['objstate'] = -1
end
user.save
end
return user
end
def get_likes(id = 'me')
@graph.get_connections(id, 'likes')
end
def get_ings(id = 'me')
ings = Array.new
likes = get_likes(id)
likes.each do |like|
if like['name'].end_with? 'ing'
ings << like
end
end
ings
end
def get_data(id = 'me')
@graph.get_connections(id, 'activities')# + @graph.get_connections(id, 'music') + @graph.get_connections(id, 'movies')
end
def get_matching_data(user1, user2)
matching_data = Array.new
user1['data'].each do |activity|
user2['data'].each do |activity2|
matching_data << activity if activity['name'].downcase == activity2['name'].downcase
end
end
matching_data
end
def get_matching_ings(user1, user2)
matching_ings = Array.new
user1['ings'].each do |activity|
user2['ings'].each do |activity2|
matching_ings << activity if activity['name'].downcase == activity2['name'].downcase
end
end
matching_ings
end
def get_ignoredfriends(id = 'me')
0 #TODO: figure out what the fuck i actually wanted to do
end
def get_random_friend(user)
#puts user['friends']
randf = User.new
case user['friendpref']
when 0 #for all friends
randf = get_user(user['friends'][rand(user['friends'].size)])
when 1 #for close friends
randf = get_user(user['closefriends'][rand(user['closefriends'].size)])
when 2 #for nearby friends
user['nearbyfriends'] = get_nearbyfriends if user['nearbyfriends'] == nil
randf = get_user(user['nearbyfriends'][rand(user['nearbyfriends'].size)])
end
randf
end
def queue(user)
thread = Thread.new {next_match(user)}
thread.run
$queue << thread
puts $queue.size
end
get "/" do
@graph = Koala::Facebook::API.new(session[:access_token])
#@graph = $graph
@app = @graph.get_object(ENV["FACEBOOK_APP_ID"])
if session[:access_token]
@my_fbuser = @graph.get_object("me")
@my_user = get_user("me")
#puts @my_user['ignoredfriends'].to_s + " " + @my_user['closefriends'].to_s
if session[:friendpref]
@my_user['friendpref'] = session[:friendpref]
session[:friendpref] = nil
@my_user.save
end
if session[:toignore]
@my_user['ignoredfriends'] << session[:toignore]
session[:toignore] = nil
@my_user.save
end
#$queue << @my_user << @my_user << @my_user << @my_user #for the hell of it
#queue(@my_user)
#queue(@my_user)
#queue(@my_user)
#pick a random friend
#friends_a = @friends.to_a
#we have a random friend... what can we do with them?
#puts $queue_friends[@my_user].size.to_s
#if $queue_friends[@my_user] == nil || $queue_friends[@my_user].size == 0
next_match(@my_user)
# puts "derpity"
#end
#rand_friend = $queue_friends[@my_user].pop
#activity = $queue_activities[@my_user].pop
verb_activity_goal(@rand_friend, @activity)
#@rand_friend = rand_friend
session["friendid"] = @rand_friend['id']
# for other data you can always run fql
@friends_using_app = @graph.fql_query("SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1")
display_message
session["friendpref"] = @my_user['friendpref']
erb :home
else
erb :login
end
#erb :index
end
def verb_activity_goal(friend, activity)
@activity = activity
@verb = ""#" fucking go "
puts @activity['category'] + ", " + @activity['name']
@verb = " watching that fucking " if @activity['category'].include? "Movie"
@verb = " listening to some fucking " if @activity['category'].include? "Music"
@verb = "" if @activity['category'].include? "Interest"
@activity['name'].downcase! if @activity['category'].include? "Interest"
@goal = "You should" + @verb + @activity['category'] + "with" + friend['name'] + "."
puts "You should" + @verb + @activity['name'] + " with " + friend['name'] + "."
end
get "/settings" do
fix_instance_vars
@my_user = get_user
erb :settings
end
get "/login" do
fix_instance_vars
@app = @graph.get_object(480611415295029)
erb :login
end
get "/fuckingclosefriends" do
if session[:access_token]
session['friendpref'] = 1
end
redirect "/"
end
get "/fuckingrandomasspeople" do
if session[:access_token]
session['friendpref'] = 0
end
redirect "/"
end
get "/fuckersnearby" do
if session[:access_token]
session['friendpref'] = 2
fix_instance_vars
@my_user = get_user
@my_user['nearbyfriends'] = get_nearbyfriends
end
redirect "/"
end
get "/fuckthatguy" do
if session[:friendid]
fix_instance_vars
@my_user = get_user
@my_user['ignoredfriends'] = Array.new if @my_user['ignoredfriends'] == nil
@my_user['ignoredfriends'] << session[:friendid]
#session[:toignore] = session[:friendid]
end
redirect "/"
end
get "/youfuckingsuck" do
session[:friendpref] = 0
@message = "We found all fuck for that. Try a broader search. Dick."
fix_instance_vars
erb :message
end
# used to close the browser window opened to post to wall/send to friends
get "/close" do
"<body onload='window.close();'/>"
end
get "/sign_out" do
session[:access_token] = nil
redirect '/'
end
get "/auth/facebook" do
session[:access_token] = nil
redirect authenticator.url_for_oauth_code(:permissions => FACEBOOK_SCOPE)
end
get '/auth/facebook/callback' do
session[:access_token] = authenticator.get_access_token(params[:code])
redirect '/'
end
def fix_instance_vars
@graph = Koala::Facebook::API.new(session[:access_token])
display_message
end
def display_message
@displaying = "Searching "
case session['friendpref']
when 0
@displaying += "all your fucking friends."
when 1
@displaying += "only those close best buddy fuckers that you love so much."
when 2
@displaying += "friends near your location. You creepy fucker."
end
end