comparison libervia/pages/_browser/invitation.py @ 1509:106bae41f5c8

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:44:11 +0200
parents 5a132b85e1ac
children 5ea06e8b06ed
comparison
equal deleted inserted replaced
1508:ec3ad9abf9f9 1509:106bae41f5c8
1 from browser import document, window, timer 1 from browser import document, window, timer
2 from bridge import Bridge 2 from bridge import bridge
3 from template import Template 3 from template import Template
4 import dialog 4 import dialog
5 from cache import cache 5 from cache import cache
6 import javascript 6 import javascript
7 7
8 bridge = Bridge() 8 bridge = bridge()
9 # we use JS RegExp because Python's re is really long to import in Brython 9 # we use JS RegExp because Python's re is really long to import in Brython
10 # FIXME: this is a naive JID regex, a more accurate should be used instead 10 # FIXME: this is a naive JID regex, a more accurate should be used instead
11 jid_re = javascript.RegExp.new(r"^\w+@\w+\.\w+") 11 jid_re = javascript.RegExp.new(r"^\w+@\w+\.\w+")
12 12
13 13
76 if self.invitation_type == 'photos': 76 if self.invitation_type == 'photos':
77 service = self.invitation_data["service"] 77 service = self.invitation_data["service"]
78 path = self.invitation_data["path"] 78 path = self.invitation_data["path"]
79 album_name = path.rsplit('/')[-1] 79 album_name = path.rsplit('/')[-1]
80 print(f"inviting {entity_jid}") 80 print(f"inviting {entity_jid}")
81 bridge.FISInvite( 81 bridge.fis_invite(
82 entity_jid, 82 entity_jid,
83 service, 83 service,
84 "photos", 84 "photos",
85 "", 85 "",
86 path, 86 path,
96 namespace = self.invitation_data.get("namespace") 96 namespace = self.invitation_data.get("namespace")
97 extra = {} 97 extra = {}
98 if namespace: 98 if namespace:
99 extra["namespace"] = namespace 99 extra["namespace"] = namespace
100 print(f"inviting {entity_jid}") 100 print(f"inviting {entity_jid}")
101 bridge.psInvite( 101 bridge.ps_invite(
102 entity_jid, 102 entity_jid,
103 service, 103 service,
104 node, 104 node,
105 '', 105 '',
106 name, 106 name,
239 dialog.notification.show( 239 dialog.notification.show(
240 f"{name} has been invited, he/she has received an email with a link", 240 f"{name} has been invited, he/she has received an email with a link",
241 level="success", 241 level="success",
242 ) 242 )
243 243
244 def invitationSimpleCreateCb(self, invitation_data, email, name): 244 def invitation_simple_create_cb(self, invitation_data, email, name):
245 invitee_jid = invitation_data['jid'] 245 invitee_jid = invitation_data['jid']
246 self._invite_jid( 246 self._invite_jid(
247 invitee_jid, 247 invitee_jid,
248 callback=lambda: self._on_email_invitation_success(invitee_jid, email, name), 248 callback=lambda: self._on_email_invitation_success(invitee_jid, email, name),
249 errback=lambda e: dialog.notification.show( 249 errback=lambda e: dialog.notification.show(
257 cache.identities[invitee_jid] = {'nicknames': [name]} 257 cache.identities[invitee_jid] = {'nicknames': [name]}
258 cache.update() 258 cache.update()
259 259
260 def invite_by_email(self, email, name): 260 def invite_by_email(self, email, name):
261 guest_url_tpl = f'{window.URL.new("/g", document.baseURI).href}/{{uuid}}' 261 guest_url_tpl = f'{window.URL.new("/g", document.baseURI).href}/{{uuid}}'
262 bridge.invitationSimpleCreate( 262 bridge.invitation_simple_create(
263 email, 263 email,
264 name, 264 name,
265 guest_url_tpl, 265 guest_url_tpl,
266 '', 266 '',
267 callback=lambda data: self.invitationSimpleCreateCb(data, email, name), 267 callback=lambda data: self.invitation_simple_create_cb(data, email, name),
268 errback=lambda e: window.alert(f"can't send email invitation: {e}") 268 errback=lambda e: window.alert(f"can't send email invitation: {e}")
269 ) 269 )
270 270
271 def on_invite_email_submit(self, evt, invite_email_elt): 271 def on_invite_email_submit(self, evt, invite_email_elt):
272 evt.stopPropagation() 272 evt.stopPropagation()
304 "click", lambda evt: self.on_invite_email_close(evt, invite_email_elt)) 304 "click", lambda evt: self.on_invite_email_close(evt, invite_email_elt))
305 self.close() 305 self.close()
306 306
307 ## affiliations 307 ## affiliations
308 308
309 def _addAffiliationBindings(self, entity_jid, affiliation_elt): 309 def _add_affiliation_bindings(self, entity_jid, affiliation_elt):
310 for elt in affiliation_elt.select(".click_to_delete"): 310 for elt in affiliation_elt.select(".click_to_delete"):
311 elt.bind( 311 elt.bind(
312 "click", 312 "click",
313 lambda evt, entity_jid=entity_jid, affiliation_elt=affiliation_elt: 313 lambda evt, entity_jid=entity_jid, affiliation_elt=affiliation_elt:
314 self.on_affiliation_remove(entity_jid, affiliation_elt) 314 self.on_affiliation_remove(entity_jid, affiliation_elt)
351 "entity_jid": entity_jid, 351 "entity_jid": entity_jid,
352 "affiliation": affiliation, 352 "affiliation": affiliation,
353 "identities": cache.identities, 353 "identities": cache.identities,
354 }) 354 })
355 document['affiliations'] <= affiliation_elt 355 document['affiliations'] <= affiliation_elt
356 self._addAffiliationBindings(entity_jid, affiliation_elt) 356 self._add_affiliation_bindings(entity_jid, affiliation_elt)
357 357
358 def _on_affiliation_remove_success(self, affiliation_elt, entity_jid): 358 def _on_affiliation_remove_success(self, affiliation_elt, entity_jid):
359 affiliation_elt.remove() 359 affiliation_elt.remove()
360 del self.affiliations[entity_jid] 360 del self.affiliations[entity_jid]
361 361
362 def on_affiliation_remove(self, entity_jid, affiliation_elt): 362 def on_affiliation_remove(self, entity_jid, affiliation_elt):
363 if self.invitation_type == 'photos': 363 if self.invitation_type == 'photos':
364 path = self.invitation_data["path"] 364 path = self.invitation_data["path"]
365 service = self.invitation_data["service"] 365 service = self.invitation_data["service"]
366 bridge.FISAffiliationsSet( 366 bridge.fis_affiliations_set(
367 service, 367 service,
368 "", 368 "",
369 path, 369 path,
370 {entity_jid: "none"}, 370 {entity_jid: "none"},
371 callback=lambda: self._on_affiliation_remove_success( 371 callback=lambda: self._on_affiliation_remove_success(
374 f"can't remove affiliation: {e}", "error") 374 f"can't remove affiliation: {e}", "error")
375 ) 375 )
376 elif self.invitation_type == 'pubsub': 376 elif self.invitation_type == 'pubsub':
377 service = self.invitation_data["service"] 377 service = self.invitation_data["service"]
378 node = self.invitation_data["node"] 378 node = self.invitation_data["node"]
379 bridge.psNodeAffiliationsSet( 379 bridge.ps_node_affiliations_set(
380 service, 380 service,
381 node, 381 node,
382 {entity_jid: "none"}, 382 {entity_jid: "none"},
383 callback=lambda: self._on_affiliation_remove_success( 383 callback=lambda: self._on_affiliation_remove_success(
384 affiliation_elt, entity_jid), 384 affiliation_elt, entity_jid),
398 "entity_jid": entity_jid, 398 "entity_jid": entity_jid,
399 "affiliation": affiliation, 399 "affiliation": affiliation,
400 "identities": cache.identities, 400 "identities": cache.identities,
401 }) 401 })
402 affiliation_elt.replaceWith(new_affiliation_elt) 402 affiliation_elt.replaceWith(new_affiliation_elt)
403 self._addAffiliationBindings(entity_jid, new_affiliation_elt) 403 self._add_affiliation_bindings(entity_jid, new_affiliation_elt)
404 404
405 def _on_affiliation_set_ok(self, entity_jid, name, affiliation_elt, affiliation): 405 def _on_affiliation_set_ok(self, entity_jid, name, affiliation_elt, affiliation):
406 if self.invitation_type == 'pubsub': 406 if self.invitation_type == 'pubsub':
407 service = self.invitation_data["service"] 407 service = self.invitation_data["service"]
408 node = self.invitation_data["node"] 408 node = self.invitation_data["node"]
409 bridge.psNodeAffiliationsSet( 409 bridge.ps_node_affiliations_set(
410 service, 410 service,
411 node, 411 node,
412 {entity_jid: affiliation}, 412 {entity_jid: affiliation},
413 callback=lambda: self._on_affiliation_set_success( 413 callback=lambda: self._on_affiliation_set_success(
414 entity_jid, name, affiliation_elt, affiliation 414 entity_jid, name, affiliation_elt, affiliation