comparison libervia/web/pages/_browser/invitation.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents libervia/pages/_browser/invitation.py@5ea06e8b06ed
children d7c78722e4f8
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 from browser import document, window, timer
2 from bridge import Bridge
3 from template import Template
4 import dialog
5 from cache import cache
6 import javascript
7
8 bridge = Bridge()
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
11 jid_re = javascript.RegExp.new(r"^\w+@\w+\.\w+")
12
13
14 class InvitationManager:
15
16 def __init__(self, invitation_type, invitation_data):
17 self.invitation_type = invitation_type
18 self.invitation_data = invitation_data
19 manager_panel_tpl = Template('invitation/manager.html')
20 self.manager_panel_elt = manager_panel_tpl.get_elt()
21 self.invite_by_email_tpl = Template('invitation/invite_by_email.html')
22 self.affiliation_tpl = Template('invitation/affiliation_item.html')
23 self.new_item_tpl = Template('invitation/new_item.html')
24 # list of item passing filter when adding a new contact
25 self._filtered_new_items = {}
26 self._active_new_item = None
27 self._idx = 0
28
29 def attach(self, affiliations=None):
30 if affiliations is None:
31 affiliations = {}
32 self.affiliations = affiliations
33 self.side_panel = self.manager_panel_elt.select_one(
34 '.invitation_manager_side_panel')
35 self.open()
36 for close_elt in self.manager_panel_elt.select('.click_to_close'):
37 close_elt.bind("click", self.on_manager_close)
38 self.side_panel.bind("click", lambda evt: evt.stopPropagation())
39
40 cache.fill_identities(affiliations.keys(), callback=self._set_affiliations)
41
42 contact_elt = self.manager_panel_elt.select_one('input[name="contact"]')
43 contact_elt.bind("input", self.on_contact_input)
44 contact_elt.bind("keydown", self.on_contact_keydown)
45 contact_elt.bind("focus", self.on_contact_focus)
46 contact_elt.bind("blur", self.on_contact_blur)
47 document['invite_email'].bind('click', self.on_invite_email_click)
48
49 def _set_affiliations(self):
50 for entity_jid, affiliation in self.affiliations.items():
51 self.set_affiliation(entity_jid, affiliation)
52
53 def open(self):
54 """Re-attach and show a closed panel"""
55 self._body_ori_style = document.body.style.height, document.body.style.overflow
56 document.body.style.height = '100vh'
57 document.body.style.overflow = 'hidden'
58 document.body <= self.manager_panel_elt
59 timer.set_timeout(lambda: self.side_panel.classList.add("open"), 0)
60
61 def _on_close_transition_end(self, evt):
62 self.manager_panel_elt.remove()
63 # FIXME: not working with Brython, to report upstream
64 # self.side_panel.unbind("transitionend", self._on_close_transition_end)
65 self.side_panel.unbind("transitionend")
66
67 def close(self):
68 """Hide the panel"""
69 document.body.style.height, document.body.style.overflow = self._body_ori_style
70 self.side_panel.classList.remove('open')
71 self.side_panel.bind("transitionend", self._on_close_transition_end)
72
73 def _invite_jid(self, entity_jid, callback, errback=None):
74 if errback is None:
75 errback = lambda e: dialog.notification.show(f"invitation failed: {e}", "error")
76 if self.invitation_type == 'photos':
77 service = self.invitation_data["service"]
78 path = self.invitation_data["path"]
79 album_name = path.rsplit('/')[-1]
80 print(f"inviting {entity_jid}")
81 bridge.fis_invite(
82 entity_jid,
83 service,
84 "photos",
85 "",
86 path,
87 album_name,
88 '',
89 callback=callback,
90 errback=errback
91 )
92 elif self.invitation_type == 'pubsub':
93 service = self.invitation_data["service"]
94 node = self.invitation_data["node"]
95 name = self.invitation_data.get("name")
96 namespace = self.invitation_data.get("namespace")
97 extra = {}
98 if namespace:
99 extra["namespace"] = namespace
100 print(f"inviting {entity_jid}")
101 bridge.ps_invite(
102 entity_jid,
103 service,
104 node,
105 '',
106 name,
107 javascript.JSON.stringify(extra),
108 callback=callback,
109 errback=errback
110 )
111 else:
112 print(f"error: unknown invitation type: {self.invitation_type}")
113
114 def invite_by_jid(self, entity_jid):
115 self._invite_jid(
116 entity_jid,
117 callback=lambda entity_jid=entity_jid: self._on_jid_invitation_success(entity_jid),
118 )
119
120 def on_manager_close(self, evt):
121 self.close()
122
123 def _on_jid_invitation_success(self, entity_jid):
124 form_elt = document['invitation_form']
125 contact_elt = form_elt.select_one('input[name="contact"]')
126 contact_elt.value = ""
127 contact_elt.dispatchEvent(window.Event.new('input'))
128 dialog.notification.show(
129 f"{entity_jid} has been invited",
130 level="success",
131 )
132 if entity_jid not in self.affiliations:
133 self.set_affiliation(entity_jid, "member")
134
135 def on_contact_invite(self, evt, entity_jid):
136 """User is adding a contact"""
137 form_elt = document['invitation_form']
138 contact_elt = form_elt.select_one('input[name="contact"]')
139 contact_elt.value = ""
140 contact_elt.dispatchEvent(window.Event.new('input'))
141 self.invite_by_jid(entity_jid)
142
143 def on_contact_keydown(self, evt):
144 if evt.key == "Escape":
145 evt.target.value = ""
146 evt.target.dispatchEvent(window.Event.new('input'))
147 elif evt.key == "ArrowDown":
148 evt.stopPropagation()
149 evt.preventDefault()
150 content_elt = document['invitation_contact_search'].select_one(
151 ".search_dialog__content")
152 if self._active_new_item == None:
153 self._active_new_item = content_elt.firstElementChild
154 self._active_new_item.classList.add('selected')
155 else:
156 next_item = self._active_new_item.nextElementSibling
157 if next_item is not None:
158 self._active_new_item.classList.remove('selected')
159 self._active_new_item = next_item
160 self._active_new_item.classList.add('selected')
161 elif evt.key == "ArrowUp":
162 evt.stopPropagation()
163 evt.preventDefault()
164 content_elt = document['invitation_contact_search'].select_one(
165 ".search_dialog__content")
166 if self._active_new_item == None:
167 self._active_new_item = content_elt.lastElementChild
168 self._active_new_item.classList.add('selected')
169 else:
170 previous_item = self._active_new_item.previousElementSibling
171 if previous_item is not None:
172 self._active_new_item.classList.remove('selected')
173 self._active_new_item = previous_item
174 self._active_new_item.classList.add('selected')
175 elif evt.key == "Enter":
176 evt.stopPropagation()
177 evt.preventDefault()
178 if self._active_new_item is not None:
179 entity_jid = self._active_new_item.dataset.entityJid
180 self.invite_by_jid(entity_jid)
181 else:
182 if jid_re.exec(evt.target.value):
183 self.invite_by_jid(evt.target.value)
184 evt.target.value = ""
185
186 def on_contact_focus(self, evt):
187 search_dialog = document['invitation_contact_search']
188 search_dialog.classList.add('open')
189 self._active_new_item = None
190 evt.target.dispatchEvent(window.Event.new('input'))
191
192 def on_contact_blur(self, evt):
193 search_dialog = document['invitation_contact_search']
194 search_dialog.classList.remove('open')
195 for elt in self._filtered_new_items.values():
196 elt.remove()
197 self._filtered_new_items.clear()
198
199
200 def on_contact_input(self, evt):
201 text = evt.target.value.strip().lower()
202 search_dialog = document['invitation_contact_search']
203 content_elt = search_dialog.select_one(".search_dialog__content")
204 for (entity_jid, identity) in cache.identities.items():
205 if not cache.match_identity(entity_jid, text, identity):
206 # if the entity was present in last pass, we remove it
207 try:
208 filtered_item = self._filtered_new_items.pop(entity_jid)
209 except KeyError:
210 pass
211 else:
212 filtered_item.remove()
213 continue
214 if entity_jid not in self._filtered_new_items:
215 # we only create a new element if the item was not already there
216 new_item_elt = self.new_item_tpl.get_elt({
217 "entity_jid": entity_jid,
218 "identities": cache.identities,
219 })
220 content_elt <= new_item_elt
221 self._filtered_new_items[entity_jid] = new_item_elt
222 for elt in new_item_elt.select('.click_to_ok'):
223 # we use mousedown instead of click because otherwise it would be
224 # ignored due to "blur" event manager (see
225 # https://stackoverflow.com/a/9335401)
226 elt.bind(
227 "mousedown",
228 lambda evt, entity_jid=entity_jid: self.on_contact_invite(
229 evt, entity_jid),
230 )
231
232 if ((self._active_new_item is not None
233 and not self._active_new_item.parentElement)):
234 # active item has been filtered out
235 self._active_new_item = None
236
237 def _on_email_invitation_success(self, invitee_jid, email, name):
238 self.set_affiliation(invitee_jid, "member")
239 dialog.notification.show(
240 f"{name} has been invited, he/she has received an email with a link",
241 level="success",
242 )
243
244 def invitation_simple_create_cb(self, invitation_data, email, name):
245 invitee_jid = invitation_data['jid']
246 self._invite_jid(
247 invitee_jid,
248 callback=lambda: self._on_email_invitation_success(invitee_jid, email, name),
249 errback=lambda e: dialog.notification.show(
250 f"invitation failed for {email}: {e}",
251 "error"
252 )
253 )
254
255 # we update identities to have the name instead of the invitation jid in
256 # affiliations
257 cache.identities[invitee_jid] = {'nicknames': [name]}
258 cache.update()
259
260 def invite_by_email(self, email, name):
261 guest_url_tpl = f'{window.URL.new("/g", document.baseURI).href}/{{uuid}}'
262 bridge.invitation_simple_create(
263 email,
264 name,
265 guest_url_tpl,
266 '',
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}")
269 )
270
271 def on_invite_email_submit(self, evt, invite_email_elt):
272 evt.stopPropagation()
273 evt.preventDefault()
274 form = document['email_invitation_form']
275 try:
276 reportValidity = form.reportValidity
277 except AttributeError:
278 print("reportValidity is not supported by this browser!")
279 else:
280 if not reportValidity():
281 return
282 email = form.select_one('input[name="email"]').value
283 name = form.select_one('input[name="name"]').value
284 self.invite_by_email(email, name)
285 invite_email_elt.remove()
286 self.open()
287
288 def on_invite_email_close(self, evt, invite_email_elt):
289 evt.stopPropagation()
290 evt.preventDefault()
291 invite_email_elt.remove()
292 self.open()
293
294 def on_invite_email_click(self, evt):
295 evt.stopPropagation()
296 evt.preventDefault()
297 invite_email_elt = self.invite_by_email_tpl.get_elt()
298 document.body <= invite_email_elt
299 document['email_invitation_submit'].bind(
300 'click', lambda evt: self.on_invite_email_submit(evt, invite_email_elt)
301 )
302 for close_elt in invite_email_elt.select('.click_to_close'):
303 close_elt.bind(
304 "click", lambda evt: self.on_invite_email_close(evt, invite_email_elt))
305 self.close()
306
307 ## affiliations
308
309 def _add_affiliation_bindings(self, entity_jid, affiliation_elt):
310 for elt in affiliation_elt.select(".click_to_delete"):
311 elt.bind(
312 "click",
313 lambda evt, entity_jid=entity_jid, affiliation_elt=affiliation_elt:
314 self.on_affiliation_remove(entity_jid, affiliation_elt)
315 )
316 for elt in affiliation_elt.select(".click_to_set_publisher"):
317 try:
318 name = cache.identities[entity_jid]["nicknames"][0]
319 except (KeyError, IndexError):
320 name = entity_jid
321 elt.bind(
322 "click",
323 lambda evt, entity_jid=entity_jid, name=name,
324 affiliation_elt=affiliation_elt:
325 self.on_affiliation_set(
326 entity_jid, name, affiliation_elt, "publisher"
327 ),
328 )
329 for elt in affiliation_elt.select(".click_to_set_member"):
330 try:
331 name = cache.identities[entity_jid]["nicknames"][0]
332 except (KeyError, IndexError):
333 name = entity_jid
334 elt.bind(
335 "click",
336 lambda evt, entity_jid=entity_jid, name=name,
337 affiliation_elt=affiliation_elt:
338 self.on_affiliation_set(
339 entity_jid, name, affiliation_elt, "member"
340 ),
341 )
342
343 def set_affiliation(self, entity_jid, affiliation):
344 if affiliation not in ('owner', 'member', 'publisher'):
345 raise NotImplementedError(
346 f'{affiliation} affiliation can not be set with this method for the '
347 'moment')
348 if entity_jid not in self.affiliations:
349 self.affiliations[entity_jid] = affiliation
350 affiliation_elt = self.affiliation_tpl.get_elt({
351 "entity_jid": entity_jid,
352 "affiliation": affiliation,
353 "identities": cache.identities,
354 })
355 document['affiliations'] <= affiliation_elt
356 self._add_affiliation_bindings(entity_jid, affiliation_elt)
357
358 def _on_affiliation_remove_success(self, affiliation_elt, entity_jid):
359 affiliation_elt.remove()
360 del self.affiliations[entity_jid]
361
362 def on_affiliation_remove(self, entity_jid, affiliation_elt):
363 if self.invitation_type == 'photos':
364 path = self.invitation_data["path"]
365 service = self.invitation_data["service"]
366 bridge.fis_affiliations_set(
367 service,
368 "",
369 path,
370 {entity_jid: "none"},
371 callback=lambda: self._on_affiliation_remove_success(
372 affiliation_elt, entity_jid),
373 errback=lambda e: dialog.notification.show(
374 f"can't remove affiliation: {e}", "error")
375 )
376 elif self.invitation_type == 'pubsub':
377 service = self.invitation_data["service"]
378 node = self.invitation_data["node"]
379 bridge.ps_node_affiliations_set(
380 service,
381 node,
382 {entity_jid: "none"},
383 callback=lambda: self._on_affiliation_remove_success(
384 affiliation_elt, entity_jid),
385 errback=lambda e: dialog.notification.show(
386 f"can't remove affiliation: {e}", "error")
387 )
388 else:
389 dialog.notification.show(
390 f"error: unknown invitation type: {self.invitation_type}",
391 "error"
392 )
393
394 def _on_affiliation_set_success(self, entity_jid, name, affiliation_elt, affiliation):
395 dialog.notification.show(f"permission updated for {name}")
396 self.affiliations[entity_jid] = affiliation
397 new_affiliation_elt = self.affiliation_tpl.get_elt({
398 "entity_jid": entity_jid,
399 "affiliation": affiliation,
400 "identities": cache.identities,
401 })
402 affiliation_elt.replaceWith(new_affiliation_elt)
403 self._add_affiliation_bindings(entity_jid, new_affiliation_elt)
404
405 def _on_affiliation_set_ok(self, entity_jid, name, affiliation_elt, affiliation):
406 if self.invitation_type == 'pubsub':
407 service = self.invitation_data["service"]
408 node = self.invitation_data["node"]
409 bridge.ps_node_affiliations_set(
410 service,
411 node,
412 {entity_jid: affiliation},
413 callback=lambda: self._on_affiliation_set_success(
414 entity_jid, name, affiliation_elt, affiliation
415 ),
416 errback=lambda e: dialog.notification.show(
417 f"can't set affiliation: {e}", "error")
418 )
419 else:
420 dialog.notification.show(
421 f"error: unknown invitation type: {self.invitation_type}",
422 "error"
423 )
424
425 def _on_affiliation_set_cancel(self, evt, notif_elt):
426 notif_elt.remove()
427 self.open()
428
429 def on_affiliation_set(self, entity_jid, name, affiliation_elt, affiliation):
430 if affiliation == "publisher":
431 message = f"Give autorisation to publish to {name}?"
432 elif affiliation == "member":
433 message = f"Remove autorisation to publish from {name}?"
434 else:
435 dialog.notification.show(f"unmanaged affiliation: {affiliation}", "error")
436 return
437 dialog.Confirm(message).show(
438 ok_cb=lambda evt, notif_elt:
439 self._on_affiliation_set_ok(
440 entity_jid, name, affiliation_elt, affiliation
441 ),
442 cancel_cb=self._on_affiliation_set_cancel
443 )
444 self.close()