Mercurial > libervia-web
comparison libervia/pages/_browser/invitation.py @ 1331:fe353fceec38
browser (invitation, photos/album): invitation manager improvments:
invitation manager has been moved to a separated module, it is generic so it can be used
with other activities.
It has been simplified, and contact to add are dynamically filtered using a text input.
Invitation are done by email using a the new modal.
New notification module is used to indicate when invitation has been done.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 14 Aug 2020 09:31:32 +0200 |
parents | |
children | c74e5a488af6 |
comparison
equal
deleted
inserted
replaced
1330:b525fdcb393b | 1331:fe353fceec38 |
---|---|
1 from browser import document, window, timer | |
2 from bridge import Bridge | |
3 from template import Template | |
4 from dialog import notification | |
5 from cache import cache | |
6 | |
7 bridge = Bridge() | |
8 | |
9 | |
10 class InvitationManager: | |
11 | |
12 def __init__(self, invitation_type, invitation_data): | |
13 self.invitation_type = invitation_type | |
14 self.invitation_data = invitation_data | |
15 manager_panel_tpl = Template('invitation/manager.html') | |
16 self.manager_panel_elt = manager_panel_tpl.get_elt() | |
17 self.invite_by_email_tpl = Template('invitation/invite_by_email.html') | |
18 self.affiliation_tpl = Template('invitation/affiliation_item.html') | |
19 self.new_item_tpl = Template('invitation/new_item.html') | |
20 # list of item passing filter when adding a new contact | |
21 self._filtered_new_items = {} | |
22 self._active_new_item = None | |
23 self._idx = 0 | |
24 | |
25 def attach(self, affiliations=None): | |
26 if affiliations is None: | |
27 affiliations = {} | |
28 self.affiliations = affiliations | |
29 self.side_panel = self.manager_panel_elt.select_one( | |
30 '.invitation_manager_side_panel') | |
31 self.open() | |
32 for close_elt in self.manager_panel_elt.select('.click_to_close'): | |
33 close_elt.bind("click", self.on_manager_close) | |
34 self.side_panel.bind("click", lambda evt: evt.stopPropagation()) | |
35 | |
36 cache.fill_identities(affiliations.keys(), callback=self._set_affiliations) | |
37 | |
38 contact_elt = self.manager_panel_elt.select_one('input[name="contact"]') | |
39 contact_elt.bind("input", self.on_contact_input) | |
40 contact_elt.bind("keydown", self.on_contact_keydown) | |
41 contact_elt.bind("focus", self.on_contact_focus) | |
42 contact_elt.bind("blur", self.on_contact_blur) | |
43 document['invite_email'].bind('click', self.on_invite_email_click) | |
44 | |
45 def _set_affiliations(self): | |
46 for entity_jid, affiliation in self.affiliations.items(): | |
47 self.set_affiliation(entity_jid, affiliation) | |
48 | |
49 def open(self): | |
50 """Re-attach and show a closed panel""" | |
51 self._body_ori_style = document.body.style.height, document.body.style.overflow | |
52 document.body.style.height = '100vh' | |
53 document.body.style.overflow = 'hidden' | |
54 document.body <= self.manager_panel_elt | |
55 timer.set_timeout(lambda: self.side_panel.classList.add("open"), 0) | |
56 | |
57 def _on_close_transition_end(self, evt): | |
58 self.manager_panel_elt.remove() | |
59 # FIXME: not working with Brython, to report upstream | |
60 # self.side_panel.unbind("transitionend", self._on_close_transition_end) | |
61 self.side_panel.unbind("transitionend") | |
62 | |
63 def close(self): | |
64 """Hide the panel""" | |
65 document.body.style.height, document.body.style.overflow = self._body_ori_style | |
66 self.side_panel.classList.remove('open') | |
67 self.side_panel.bind("transitionend", self._on_close_transition_end) | |
68 | |
69 def invite_jid(self, entity_jid): | |
70 if self.invitation_type == 'photos': | |
71 path = self.invitation_data["path"] | |
72 service = self.invitation_data["service"] | |
73 album_name = path.rsplit('/')[-1] | |
74 print(f"inviting {entity_jid}") | |
75 bridge.FISInvite( | |
76 entity_jid, | |
77 service, | |
78 "photos", | |
79 "", | |
80 path, | |
81 album_name, | |
82 '', | |
83 callback=lambda entity_jid=entity_jid: | |
84 self._on_jid_invitation_success(entity_jid), | |
85 errback=lambda e: notification.show(f"invitation failed: {e}", "error") | |
86 ) | |
87 | |
88 def on_manager_close(self, evt): | |
89 self.close() | |
90 | |
91 def _on_jid_invitation_success(self, entity_jid): | |
92 form_elt = document['invitation_form'] | |
93 contact_elt = form_elt.select_one('input[name="contact"]') | |
94 contact_elt.value = "" | |
95 contact_elt.dispatchEvent(window.Event.new('input')) | |
96 notification.show( | |
97 f"{entity_jid} has been invited", | |
98 level="success", | |
99 ) | |
100 if entity_jid not in self.affiliations: | |
101 self.set_affiliation(entity_jid, "member") | |
102 | |
103 def on_contact_invite(self, evt, entity_jid): | |
104 """User is adding a contact""" | |
105 form_elt = document['invitation_form'] | |
106 contact_elt = form_elt.select_one('input[name="contact"]') | |
107 contact_elt.value = "" | |
108 contact_elt.dispatchEvent(window.Event.new('input')) | |
109 self.invite_jid(entity_jid) | |
110 | |
111 def on_contact_keydown(self, evt): | |
112 if evt.key == "Escape": | |
113 evt.target.value = "" | |
114 evt.target.dispatchEvent(window.Event.new('input')) | |
115 elif evt.key == "ArrowDown": | |
116 evt.stopPropagation() | |
117 evt.preventDefault() | |
118 content_elt = document['invitation_contact_search'].select_one( | |
119 ".search_dialog__content") | |
120 if self._active_new_item == None: | |
121 self._active_new_item = content_elt.firstElementChild | |
122 self._active_new_item.classList.add('selected') | |
123 else: | |
124 next_item = self._active_new_item.nextElementSibling | |
125 if next_item is not None: | |
126 self._active_new_item.classList.remove('selected') | |
127 self._active_new_item = next_item | |
128 self._active_new_item.classList.add('selected') | |
129 elif evt.key == "ArrowUp": | |
130 evt.stopPropagation() | |
131 evt.preventDefault() | |
132 content_elt = document['invitation_contact_search'].select_one( | |
133 ".search_dialog__content") | |
134 if self._active_new_item == None: | |
135 self._active_new_item = content_elt.lastElementChild | |
136 self._active_new_item.classList.add('selected') | |
137 else: | |
138 previous_item = self._active_new_item.previousElementSibling | |
139 if previous_item is not None: | |
140 self._active_new_item.classList.remove('selected') | |
141 self._active_new_item = previous_item | |
142 self._active_new_item.classList.add('selected') | |
143 elif evt.key == "Enter": | |
144 evt.stopPropagation() | |
145 evt.preventDefault() | |
146 if self._active_new_item is not None: | |
147 entity_jid = self._active_new_item.dataset.entityJid | |
148 self.invite_jid(entity_jid) | |
149 | |
150 def on_contact_focus(self, evt): | |
151 search_dialog = document['invitation_contact_search'] | |
152 search_dialog.classList.add('open') | |
153 self._active_new_item = None | |
154 evt.target.dispatchEvent(window.Event.new('input')) | |
155 | |
156 def on_contact_blur(self, evt): | |
157 search_dialog = document['invitation_contact_search'] | |
158 search_dialog.classList.remove('open') | |
159 for elt in self._filtered_new_items.values(): | |
160 elt.remove() | |
161 self._filtered_new_items.clear() | |
162 | |
163 | |
164 def on_contact_input(self, evt): | |
165 text = evt.target.value.strip().lower() | |
166 search_dialog = document['invitation_contact_search'] | |
167 content_elt = search_dialog.select_one(".search_dialog__content") | |
168 for (entity_jid, identity) in cache.identities.items(): | |
169 if not cache.match_identity(entity_jid, text, identity): | |
170 # if the entity was present in last pass, we remove it | |
171 try: | |
172 filtered_item = self._filtered_new_items.pop(entity_jid) | |
173 except KeyError: | |
174 pass | |
175 else: | |
176 filtered_item.remove() | |
177 continue | |
178 if entity_jid not in self._filtered_new_items: | |
179 # we only create a new element if the item was not already there | |
180 new_item_elt = self.new_item_tpl.get_elt({ | |
181 "entity_jid": entity_jid, | |
182 "identities": cache.identities, | |
183 }) | |
184 content_elt <= new_item_elt | |
185 self._filtered_new_items[entity_jid] = new_item_elt | |
186 for elt in new_item_elt.select('.click_to_ok'): | |
187 # we use mousedown instead of click because otherwise it would be | |
188 # ignored due to "blur" event manager (see | |
189 # https://stackoverflow.com/a/9335401) | |
190 elt.bind( | |
191 "mousedown", | |
192 lambda evt, entity_jid=entity_jid: self.on_contact_invite( | |
193 evt, entity_jid), | |
194 ) | |
195 | |
196 if ((self._active_new_item is not None | |
197 and not self._active_new_item.parentElement)): | |
198 # active item has been filtered out | |
199 self._active_new_item = None | |
200 | |
201 def _on_email_invitation_success(self, invitee_jid, email, name): | |
202 self.set_affiliation(invitee_jid, "member") | |
203 notification.show( | |
204 f"{name} has been invited, he/she has received an email with a link", | |
205 level="success", | |
206 ) | |
207 | |
208 def invitationSimpleCreateCb(self, invitation_data, email, name): | |
209 if self.invitation_type == 'photos': | |
210 path = self.invitation_data["path"] | |
211 service = self.invitation_data["service"] | |
212 invitee_jid = invitation_data['jid'] | |
213 album_name = path.rsplit('/')[-1] | |
214 bridge.FISInvite( | |
215 invitee_jid, | |
216 service, | |
217 "photos", | |
218 "", | |
219 path, | |
220 album_name, | |
221 '', | |
222 callback=lambda: self._on_email_invitation_success(invitee_jid, email, name), | |
223 errback=lambda e: window.alert(f"invitation failed for {email}: {e}") | |
224 ) | |
225 | |
226 # we update identities to have the name instead of the invitation jid in | |
227 # affiliations | |
228 cache.identities[invitee_jid] = {'nicknames': [name]} | |
229 cache.update() | |
230 | |
231 def invite_by_email(self, email, name): | |
232 guest_url_tpl = f'{window.URL.new("/g", document.baseURI).href}/{{uuid}}' | |
233 bridge.invitationSimpleCreate( | |
234 email, | |
235 name, | |
236 guest_url_tpl, | |
237 '', | |
238 callback=lambda data: self.invitationSimpleCreateCb(data, email, name), | |
239 errback=lambda e: window.alert(f"can't send email invitation: {e}") | |
240 ) | |
241 | |
242 def on_invite_email_submit(self, evt, invite_email_elt): | |
243 evt.stopPropagation() | |
244 evt.preventDefault() | |
245 form = document['email_invitation_form'] | |
246 try: | |
247 reportValidity = form.reportValidity | |
248 except AttributeError: | |
249 print("reportValidity is not supported by this browser!") | |
250 else: | |
251 if not reportValidity(): | |
252 return | |
253 email = form.select_one('input[name="email"]').value | |
254 name = form.select_one('input[name="name"]').value | |
255 self.invite_by_email(email, name) | |
256 invite_email_elt.remove() | |
257 self.open() | |
258 | |
259 def on_invite_email_close(self, evt, invite_email_elt): | |
260 evt.stopPropagation() | |
261 evt.preventDefault() | |
262 invite_email_elt.remove() | |
263 self.open() | |
264 | |
265 def on_invite_email_click(self, evt): | |
266 evt.stopPropagation() | |
267 evt.preventDefault() | |
268 invite_email_elt = self.invite_by_email_tpl.get_elt() | |
269 document.body <= invite_email_elt | |
270 document['email_invitation_submit'].bind( | |
271 'click', lambda evt: self.on_invite_email_submit(evt, invite_email_elt) | |
272 ) | |
273 for close_elt in invite_email_elt.select('.click_to_close'): | |
274 close_elt.bind( | |
275 "click", lambda evt: self.on_invite_email_close(evt, invite_email_elt)) | |
276 self.close() | |
277 | |
278 ## affiliations | |
279 | |
280 def set_affiliation(self, entity_jid, affiliation): | |
281 if affiliation not in ('owner', 'member'): | |
282 raise NotImplementedError( | |
283 f'{affiliation} affiliation can not be set with this method for the ' | |
284 'moment') | |
285 if entity_jid not in self.affiliations: | |
286 self.affiliations[entity_jid] = affiliation | |
287 affiliation_elt = self.affiliation_tpl.get_elt({ | |
288 "entity_jid": entity_jid, | |
289 "affiliation": affiliation, | |
290 "identities": cache.identities, | |
291 }) | |
292 document['affiliations'] <= affiliation_elt | |
293 for elt in affiliation_elt.select(".click_to_delete"): | |
294 elt.bind( | |
295 "click", | |
296 lambda evt, entity_jid=entity_jid, affiliation_elt=affiliation_elt: | |
297 self.on_affiliation_remove(entity_jid, affiliation_elt) | |
298 ) | |
299 | |
300 def _on_affiliation_remove_success(self, affiliation_elt, entity_jid): | |
301 affiliation_elt.remove() | |
302 del self.affiliations[entity_jid] | |
303 | |
304 def on_affiliation_remove(self, entity_jid, affiliation_elt): | |
305 if self.invitation_type == 'photos': | |
306 path = self.invitation_data["path"] | |
307 service = self.invitation_data["service"] | |
308 bridge.FISAffiliationsSet( | |
309 service, | |
310 "", | |
311 path, | |
312 {entity_jid: "none"}, | |
313 callback=lambda: self._on_affiliation_remove_success( | |
314 affiliation_elt, entity_jid), | |
315 errback=lambda e: notification.show( | |
316 f"can't remove affiliation: {e}", "error") | |
317 ) |