comparison browser/sat_browser/plugin_xep_0085.py @ 1124:28e3eb3bb217

files reorganisation and installation rework: - files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory) - VERSION file is now used, as for other SàT projects - replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly - removed check for data_dir if it's empty - installation tested working in virtual env - libervia launching script is now in bin/libervia
author Goffi <goffi@goffi.org>
date Sat, 25 Aug 2018 17:59:48 +0200
parents src/browser/sat_browser/plugin_xep_0085.py@f8a7a046ff9c
children
comparison
equal deleted inserted replaced
1123:63a4b8fe9782 1124:28e3eb3bb217
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for Chat State Notifications Protocol (xep-0085)
5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from pyjamas.Timer import Timer
21
22
23 # Copy of the map from sat/src/plugins/plugin_xep_0085
24 TRANSITIONS = {"active": {"next_state": "inactive", "delay": 120},
25 "inactive": {"next_state": "gone", "delay": 480},
26 "gone": {"next_state": "", "delay": 0},
27 "composing": {"next_state": "paused", "delay": 30},
28 "paused": {"next_state": "inactive", "delay": 450}
29 }
30
31
32 class ChatStateMachine:
33 """This is an adapted version of the ChatStateMachine from sat/src/plugins/plugin_xep_0085
34 which manage a timer on the web browser and keep it synchronized with the timer that runs
35 on the backend. This is only needed to avoid calling the bridge method chatStateComposing
36 too often ; accuracy is not needed so we can ignore the delay of the communication between
37 the web browser and the backend (the timer on the web browser always starts a bit before).
38 /!\ Keep this file up to date if you modify the one in the sat plugins directory.
39 """
40 def __init__(self, host, target_s):
41
42 self.host = host
43 self.target_s = target_s
44 self.started = False
45 self.state = None
46 self.timer = None
47
48 def _onEvent(self, state):
49 # Pyjamas callback takes no extra argument so we need this trick
50
51 # Here we should check the value of the parameter "Send chat state notifications"
52 # but this costs two messages. It's even better to call chatStateComposing
53 # with a doubt, it will be checked by the back-end anyway before sending
54 # the actual notifications to the other client.
55 if state == "composing" and not self.started:
56 return
57 self.started = True
58 self.next_state = state
59 self.__onEvent(None)
60
61 def __onEvent(self, timer):
62 state = self.next_state
63
64 assert(state in TRANSITIONS)
65 transition = TRANSITIONS[state]
66 assert("next_state" in transition and "delay" in transition)
67
68 if state != self.state and state == "composing":
69 self.host.bridge.call('chatStateComposing', None, self.target_s)
70
71 self.state = state
72 if self.timer is not None:
73 self.timer.cancel()
74
75 if transition["next_state"] and transition["delay"] > 0:
76 self.next_state = transition["next_state"]
77 self.timer = Timer(transition["delay"] * 1000, self.__onEvent) # pyjamas timer is in milliseconds