-
Notifications
You must be signed in to change notification settings - Fork 0
/
loom.py
612 lines (518 loc) · 24.3 KB
/
loom.py
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
from flask import Flask, request, render_template, render_template_string, send_from_directory, redirect
from flask_socketio import SocketIO, emit
from flask_login import LoginManager, login_required, login_user, logout_user, current_user
from urllib.parse import parse_qs
from pymongo import MongoClient
import json
import os
import sys
from event_mongodb import RootCollection, StoryCollection
from random_utils import get_random_username, get_invite_code
import process_twine
from pprint import pprint as pp
#####################
# environment setup #
#####################
app = Flask("LOOM")
app.config['SECRET_KEY'] = os.environ["SOCKET_SECRET"]
socketio = SocketIO(app, cors_allowed_origins="*")
client = MongoClient(os.environ["MONGODB_URI"],retryWrites=False)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
#to handle dev environ
if "localhost" in os.environ["MONGODB_URI"]:
db_name = "default"
else:
db_name = os.environ["MONGODB_URI"].split("/")[-1]
db = client[db_name]
root_db = RootCollection(db)
story_dbs = {}
@login_manager.user_loader
def load_user(user_id):
user = root_db.get_user_by_id(user_id)
return user
###################################
# prepare a story for the browser #
###################################
def get_loomed_twine(story):
twine = story["raw"]
with open("static/loom.js", "r") as loom_js_file:
loom_js = loom_js_file.read()
with open("static/loom.css") as css:
loom_css = css.read()
socket_inject = "<script type='text/javascript' src='/static/socketio.js'></script>"
iro_inject = "<script type='text/javascript' src='/static/iro.js'></script>"
bootjs_inject = "<script type='text/javascript' src='/static/bootstrap.bundle.min.js'></script>"
bootcss_inject = "<link rel='stylesheet' type='text/css' href='/static/bootstrap.min.css'>"
jitsi_inject = "<script src='https://meet.jit.si/external_api.js'></script>"
json5_inject = "<script src='https://unpkg.com/json5@^2.0.0/dist/index.min.js'></script>"
loomed = twine.replace("{LOOM_JS}", loom_js)
loomed = loomed.replace("{LOOM_CSS}", loom_css)
loomed = bootcss_inject+loomed
loomed = socket_inject+loomed
loomed = iro_inject+loomed
loomed = jitsi_inject+loomed
loomed = json5_inject+loomed
loomed = loomed+bootjs_inject #puts bootstrap at the bottom of the file, so it loads after the twine native jquery
return loomed
def get_user_view_stories(user):
all_twines = root_db.get_all_stories()
view_twines = []
for story in all_twines:
user_story_client = current_user.get_client(story["story_id"])
if current_user.admin:
view_twines.append( {"story":story, "admin":True} )
elif story["auth_scheme"] != "invite":
if user_story_client is not None:
view_twines.append( {"story": story, "admin":user_story_client["admin"]} )
else:
view_twines.append( {"story": story, "admin":False} )
else:
if user_story_client != None:
view_twines.append( {"story": story, "admin":user_story_client["admin"]} )
return view_twines
######################
# main server functs #
######################
@app.route('/static/<path>')
def send_static(path):
return send_from_directory("static",path)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == "GET":
with open("templates/login.html") as login_loc:
login = login_loc.read()
return render_template_string(login)
elif request.method == "POST":
request.get_data()
data = parse_qs(request.data.decode("utf-8"))
user = root_db.get_user(data["user"][0])
if user is None:
return {"Status":"NOT OK"}
else:
if user.validate_pass(data["pass"][0]):
root_db.save_user(user)
login_user(user)
return {"Status":"OK"}
else:
return {"Status":"NOT OK"}
@app.route("/logout")
def logout():
logout_user()
return redirect("/login")
@app.route('/')
@login_required
def landing():
with open("templates/landing.html") as landing_loc:
landing = landing_loc.read()
all_twines = root_db.get_all_stories()
view_twines = []
for story in all_twines:
user_story_client = current_user.get_client(story["story_id"])
if current_user.admin:
view_twines.append( {"story":story, "admin":True} )
elif story["auth_scheme"] != "invite":
if user_story_client is not None:
view_twines.append( {"story": story, "admin":user_story_client["admin"]} )
else:
view_twines.append( {"story": story, "admin":False} )
else:
if user_story_client != None:
view_twines.append( {"story": story, "admin":user_story_client["admin"]} )
if current_user.admin:
users = root_db.get_all_users()
else:
users = None
return render_template_string(landing, twines=view_twines, users = users)
#Blunt but effective
@app.route('/twine/<twine_name>')
def serve_twine(twine_name):
if(twine_name.split(".")[-1]=="map"):
return "no", 404
story = root_db.get_story(twine_name)
if story["auth_scheme"]=="login":
if current_user.is_authenticated:
return get_loomed_twine(story)
if story["auth_scheme"]=="none":
return get_loomed_twine(story)
if story["auth_scheme"]=="invite":
if current_user.is_authenticated and current_user.get_client(twine_name) is not None:
return get_loomed_twine(story)
return redirect("/")
@app.route("/log", methods=["POST"])
def exit_event():
request.get_data()
log_data = json.loads(request.data)
story_id = log_data["story_id"]
story_dbs[story_id].add_event(log_data)
client_locations = story_dbs[story_id].get_all_current_client_location_events()
emit("clients_present", client_locations, namespace="/{}".format(story_id), broadcast="true")
return "OK"
@app.route('/twine/<twine_id>/admin')
@login_required
def admin(twine_id):
print(twine_id)
user_story_doc = current_user.get_client(twine_id)
if user_story_doc is not None and user_story_doc["admin"]:
with open('templates/admin.html') as admin_loc:
admin = admin_loc.read()
twine = root_db.get_story(twine_id)
return render_template_string(admin, twine=twine)
else:
return redirect("/")
#######################
# socketio functions #
#######################
####################
## site-wide sockets
@socketio.on("create_user")
def create_user(user_create_doc):
all_users = [u.to_json() for u in root_db.get_all_users()]
if user_create_doc["uname"] in [doc["username"] for doc in all_users]:
emit("create_user_response", {"status":"ERROR", "error":"User already exists with that username"})
else:
root_db.new_user(user_create_doc["uname"], user_create_doc["pass"])
emit("create_user_response",{
"status":"OK",
"users":[u.to_json() for u in root_db.get_all_users()]
})
@socketio.on("user_admin_toggle")
def user_admin_toggle(event):
user = root_db.get_user(event["user"])
user.admin = event["admin"]
root_db.save_user(user)
emit("user_admin_toggle_response")
@socketio.on("validate_code")
def validate_code(code):
code_doc = root_db.get_code(code)
if code_doc is None:
emit("validate_code_response", {"message":"No Such Code Found", "stories":None})
elif code_doc["story_id"] in current_user.story_dict.keys() and current_user.story_dict[code_doc["story_id"]] is not None:
emit("validate_code_response", {"message":"You've already joined this story", "stories":None})
elif code_doc["used"]:
emit("validate_code_response", {"message":"This code has already been used", "stories":None})
else:
current_user.story_dict[code_doc["story_id"]] = {"story_id":code_doc["story_id"], "client_id":None, "admin":False}
code_doc["used"] = True
code_doc["used_by"] = current_user.username
code_doc["user_by_id"] = current_user.user_id
root_db.save_user(current_user)
root_db.update_code(code_doc)
#Send all stories again
emit("validate_code_response", {"message":"Success","stories":get_user_view_stories(current_user)})
@socketio.on("new_twine")
def create_new_twine(create_event):
story_id = create_event["story_id"]
twine_raw = create_event["twine_raw"]
###SAINITY CHECK PLS
twine_checks = process_twine.validate_raw(twine_raw)
all_story_ids = [s["story_id"] for s in root_db.get_all_stories()]
if story_id in all_story_ids:
twine_checks["Story name must be unique"] = False
#if any of the validations flag are false, we cannot accept this item /karen/
flags = [k for k, v in twine_checks.items() if not v]
if len(flags) > 0:
emit("new_twine_response", {"message":flags, "stories":None})
else:
##create and register the new twine
twine_structure = process_twine.process_raw(twine_raw)
story_doc = {
"story_id":story_id,
"title":twine_structure["title"],
"auth_scheme":create_event["auth_scheme"],
"raw":twine_raw,
"jitsi_default":"default_off"}
root_db.add_story(story_doc)
##add one new story_db interface to memory
story_dbs[story_id] = StoryCollection(db, story_id)
for passage in twine_structure["passages"]:
story_dbs[story_id].add_passage(passage)
##register all the new story-specific sockets
for name, function in all_socket_handlers.items():
socketio.on_event(name, function, namespace="/{}".format(story_id))
##add admin access for uploader
current_user.story_dict[story_id] = {"story_id":story_id, "client_id":None, "admin":True}
root_db.save_user(current_user)
#send all stories again
emit("new_twine_response", {"message":"Success", "stories":get_user_view_stories(current_user)})
######################
## story-bound sockets
#this particular function is due for a ol refactor sometime.
def connect_socket(connect_event):
story_id = connect_event["story_id"]
story = root_db.get_story(story_id)
all_clients = story_dbs[story_id].get_all_clients()
if story["auth_scheme"] == "login":
if(current_user.is_authenticated): #this should be a redundant clause but just in case...
user_story_doc = current_user.get_client(story_id)
#if this user already has an entry for this story and this client, just grab it and send it
if user_story_doc is not None and user_story_doc["client_id"] is not None:
connect_event["client_id"] = user_story_doc["client_id"]
client_doc = story_dbs[story_id].get_client(user_story_doc["client_id"])
emit("client_connect_ok", client_doc, namespace="/{}".format(story_id))
#otherwise: create one, add it to the userdoc, and send it
else:
if user_story_doc is not None:
admin_val = user_story_doc['admin']
else:
admin_val = False
user_twine_doc = {
"story_id":story_id,
"client_id":connect_event["client_id"],
"admin":admin_val
}
current_user.story_dict[story_id] = user_twine_doc
root_db.save_user(current_user)
username = get_random_username()
client_doc = {"client_id":connect_event["client_id"], "username":username, "user_id":current_user.user_id}
story_dbs[story_id].add_client(client_doc)
del client_doc["_id"]
emit("client_connect_ok", client_doc, namespace="/{}".format(story_id))
#none auth should still associate the client to a user if there is a user...
elif story["auth_scheme"] == "none":
if(current_user.is_authenticated): #this should be a redundant clause but just in case...
user_story_doc = current_user.get_client(story_id)
#if this user already has an entry for this story and this client, just grab it and send it
if user_story_doc is not None and user_story_doc["client_id"] is not None:
connect_event["client_id"] = user_story_doc["client_id"]
client_doc = story_dbs[story_id].get_client(user_story_doc["client_id"])
emit("client_connect_ok", client_doc, namespace="/{}".format(story_id))
else:
if user_story_doc is not None:
admin_val = user_story_doc['admin']
else:
admin_val = False
user_twine_doc = {
"story_id":story_id,
"client_id":connect_event["client_id"],
"admin":admin_val
}
current_user.story_dict[story_id] = user_twine_doc
root_db.save_user(current_user)
username = get_random_username()
client_doc = {"client_id":connect_event["client_id"], "username":username, "user_id":current_user.user_id}
story_dbs[story_id].add_client(client_doc)
del client_doc["_id"]
emit("client_connect_ok", client_doc, namespace="/{}".format(story_id))
else:
if connect_event["client_id"] in [c["client_id"] for c in all_clients]:
client_doc = story_dbs[story_id].get_client(connect_event["client_id"])
emit("client_connect_ok", client_doc, namespace="/{}".format(story_id))
else:
username = get_random_username()
client_doc = {"client_id":connect_event["client_id"], "username":username}
story_dbs[story_id].add_client(client_doc)
del client_doc["_id"]
emit("client_connect_ok", client_doc, namespace="/{}".format(story_id))
if story["auth_scheme"] == "invite":
if(current_user.is_authenticated): #this should be a redundant clause but just in case...
user_story_doc = current_user.get_client(story_id)
if user_story_doc is not None and user_story_doc["client_id"] is not None:
connect_event["client_id"] = user_story_doc["client_id"]
client_doc = story_dbs[story_id].get_client(user_story_doc["client_id"])
emit("client_connect_ok", client_doc, namespace="/{}".format(story_id))
#otherwise: create one, add it to the userdoc, and send it
elif user_story_doc is not None:
user_twine_doc = {
"story_id":story_id,
"client_id":connect_event["client_id"],
"admin":user_story_doc['admin']
}
current_user.story_dict[story_id] = user_twine_doc
root_db.save_user(current_user)
username = get_random_username()
client_doc = {"client_id":connect_event["client_id"], "username":username, "user_id":current_user.user_id}
story_dbs[story_id].add_client(client_doc)
del client_doc["_id"]
emit("client_connect_ok", client_doc, namespace="/{}".format(story_id))
story_dbs[story_id].add_event(connect_event)
client_locations = story_dbs[story_id].get_all_current_client_location_events()
emit("clients_present", client_locations, namespace="/{}".format(story_id), broadcast="true")
def nav_event(nav_event):
story_id = nav_event["story_id"]
story_dbs[story_id].add_event(nav_event)
client_locations = story_dbs[story_id].get_all_current_client_location_events()
emit("clients_present", client_locations, namespace="/{}".format(story_id), broadcast="true")
def get_story_structure(story_id):
story = root_db.get_story(story_id)
passages = story_dbs[story_id].get_all_passages()
emit("story_structure", {"structure":passages, "story":story, "current_user":current_user.username}, namespace="/{}".format(story_id))
def get_client_locations(story_id):
client_locations = story_dbs[story_id].get_all_current_client_location_events()
emit("clients_present", client_locations, namespace="/{}".format(story_id) )
def update_client(client_update_doc):
story_id = client_update_doc["story_id"]
client_doc = client_update_doc["client"]
story_dbs[story_id].update_client(client_doc)
emit("did_client_update", client_doc)
client_locations = story_dbs[story_id].get_all_current_client_location_events()
emit("clients_present", client_locations, namespace="/{}".format(story_id), broadcast="true")
def update_story(story_update_doc):
root_db.update_story(story_update_doc)
emit("story_updated")
#the complexity of this function could be greatly reduced by a more model oriented storage system
def get_admin_clients(story_id):
final_table = {} #this will be client_id :: client+user doc
anon_count = 0 #count up an identifier for
#we'll iterate over the users and transform them into one of three forms
#(and add them to a final table, indexed by client id (or other!))
for user in root_db.get_all_users():
user_story_client = user.get_client(story_id)
if user_story_client is not None:
if user_story_client["client_id"] is not None:
client_key = user_story_client["client_id"]
doc = {
"user_id":user.user_id,
"username":user.username,
"added_to_story":True,
"loom_admin":user.admin,
"client_id":client_key,
"story_admin":user_story_client["admin"]
}
else:
client_key = anon_count
anon_count += 1
doc = {
"user_id":user.user_id,
"username":user.username,
"added_to_story":True,
"client_id":None,
"story_admin":False,
"client_name":None,
"location":None
}
else:
client_key = anon_count
anon_count += 1
doc = {
"user_id":user.user_id,
"username":user.username,
"loom_admin":user.admin,
"added_to_story":False,
"client_id":None,
"story_admin":False,
"client_name":None,
"location":None
}
final_table[client_key] = doc
#Then, get all of the clients in the database for this story
#and, if there is already an entry matching that client_id, fill out the appropreate information
#otherwise, just add a new doc to the final table
clients = [{"client":doc["client"], "loc":doc["event"]["passage_id"]} for doc in
story_dbs[story_id].get_all_current_client_location_events(get_exited=True)]
for doc in clients:
client_id = doc["client"]["client_id"]
if client_id in final_table.keys():
existing_doc = final_table[client_id]
existing_doc["client_name"] = doc["client"]["username"]
existing_doc["location"] = doc["loc"]
final_table[client_id] = existing_doc
else:
doc = {
"user_id":None,
"username":None,
"added_to_story":True,
"loom_admin":False,
"client_id":client_id,
"story_admin":False,
"client_name":doc["client"]["username"],
"location":doc["loc"]
}
final_table[client_id] = doc
emit("admin_clients", [doc for doc in final_table.values()])
def client_admin_toggle(event):
#give a user admin rights on a story
client = story_dbs[event["story_id"]].get_client(event["client_id"])
user = root_db.get_user_by_id(client["user_id"])
user_story_doc = user.get_client(event["story_id"])
user_story_doc["admin"] = event["admin"]
user.story_dict[event["story_id"]] = user_story_doc
root_db.save_user(user)
emit("client_admin_toggle_response")
def client_added_toggle(event):
#add a user to the story
user = root_db.get_user_by_id(event["user_id"])
if event["added"]:
story_doc = {"story_id":event["story_id"], "client_id":None, "admin":None}
user.story_dict[event["story_id"]] = story_doc
root_db.save_user(user)
else:
user.story_dict[event["story_id"]] = None
root_db.save_user(user)
emit("client_added_toggle_response")
def generate_codes(event):
codes = [get_invite_code() for i in range(int(event["number"]))]
for code in codes:
root_db.add_code(code, event["story_id"])
all_story_codes = root_db.get_story_codes(event["story_id"])
emit("invite_codes", all_story_codes)
def get_codes(story_id):
all_story_codes = root_db.get_story_codes(story_id)
emit("invite_codes", all_story_codes)
def update_twine_text(update_event):
story_id = update_event["story_id"]
twine_raw = update_event["twine_raw"]
###SAINITY CHECK PLS
twine_checks = process_twine.validate_raw(twine_raw)
all_story_ids = [s["story_id"] for s in root_db.get_all_stories()]
#if any of the validations flag are false, we cannot accept this item /karen/
flags = [k for k, v in twine_checks.items() if not v]
if len(flags) > 0:
emit("update_twine_text_response", {"message":flags, "status":"FAILED"})
else:
story = root_db.get_story(story_id)
story["raw"] = twine_raw
root_db.update_story(story)
emit("update_twine_text_response", {"message":"Success", "status":"OK"})
def delete_story(story_id):
#grab it and delete it
pass
##user story toggle
all_socket_handlers = {
"confirm_connected": connect_socket,
"nav_event": nav_event,
"get_story_structure": get_story_structure,
"get_client_locations": get_client_locations,
"get_admin_clients":get_admin_clients,
"update_client":update_client,
"update_story":update_story,
"client_admin_toggle":client_admin_toggle,
"client_added_toggle":client_added_toggle,
"generate_codes":generate_codes,
"get_codes":get_codes,
"update_twine_text":update_twine_text
}
##############
# data setup #
##############
def setup():
print("Setting up")
twines = [fname.split(".")[0] for fname in filter(lambda x: x[0]!=".", os.listdir("twines"))]
already_twines = [story['story_id'] for story in root_db.get_all_stories()]
for story_id in twines:
print("setup {}".format(story_id))
#this setup should only happen once per twine per database reset
if story_id not in already_twines:
story_dbs[story_id] = StoryCollection(db, story_id)
for name, function in all_socket_handlers.items():
socketio.on_event(name, function, namespace="/{}".format(story_id))
twine_structure = process_twine.process_file("twines/{}.html", story_id)
with open("twines/{}.html".format(story_id), "r") as twine_file:
twine_raw = twine_file.read()
story_doc = {"story_id":twine_structure["story_id"],"title":twine_structure["title"], "auth_scheme":"none", "raw":twine_raw, "jitsi_default":"default_off"}
root_db.add_story(story_doc)
for passage in twine_structure["passages"]:
story_dbs[story_id].add_passage(passage)
for story_id in already_twines:
story_dbs[story_id] = StoryCollection(db, story_id)
for name, function in all_socket_handlers.items():
socketio.on_event(name, function, namespace="/{}".format(story_id))
print("finished story setup")
new, admin_user = root_db.new_user("admin", os.environ["ADMIN_PASS"], admin=True)
if new:
for story_id in twines:
admin_user.story_dict[story_id] = {"story_id":story_id, "client_id":None, "admin":True}
root_db.save_user(admin_user)
new, test_user = root_db.new_user("test", "test")
setup()