diff --git a/sao/CHANGELOG b/sao/CHANGELOG index af76c99880..fcee2db1f5 100644 --- a/sao/CHANGELOG +++ b/sao/CHANGELOG @@ -1,3 +1,4 @@ +* Allow users to subscribe to chats * Manage user notification * Add search on empty relation field to domain parser * Escape completion content with custom format (issue14363) diff --git a/sao/images/tryton-notification-off.svg b/sao/images/tryton-notification-off.svg new file mode 100644 index 0000000000..f48b825d32 --- /dev/null +++ b/sao/images/tryton-notification-off.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/sao/images/tryton-notification-on.svg b/sao/images/tryton-notification-on.svg new file mode 100644 index 0000000000..1582e4d409 --- /dev/null +++ b/sao/images/tryton-notification-on.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/sao/src/chat.js b/sao/src/chat.js index f8ac11b425..bc27560ae4 100644 --- a/sao/src/chat.js +++ b/sao/src/chat.js @@ -50,6 +50,59 @@ 'class': 'chat', }); + let btn_group = jQuery('
', { + 'class': 'btn-group', + 'role': 'group', + }).appendTo(el); + + let subscribe_btn = jQuery('', { + 'class': 'btn btn-default pull-right', + 'type': 'button', + 'title': Sao.i18n.gettext("Toggle notification"), + }).append(Sao.common.ICONFACTORY.get_icon_img( + 'tryton-notification')) + .appendTo(btn_group); + + Sao.rpc({ + 'method': 'model.ir.chat.channel.get_followers', + 'params': [this.record, {}], + }, Sao.Session.current_session).then((followers) => { + set_subscribe_state(~followers.users.indexOf( + Sao.Session.current_session.login)); + }) + + let set_subscribe_state = (subscribed) => { + let img; + if (subscribed) { + img = 'tryton-notification-on'; + subscribe_btn.addClass('active').off().click(unsubscribe); + } else { + img = 'tryton-notification-off'; + subscribe_btn.removeClass('active').off().click(subscribe); + } + subscribe_btn.html(Sao.common.ICONFACTORY.get_icon_img(img)); + } + + let subscribe = () => { + let session = Sao.Session.current_session; + Sao.rpc({ + 'method': 'model.ir.chat.channel.subscribe', + 'params': [this.record, {}], + }, session).then(() => { + set_subscribe_state(true); + }) + }; + + let unsubscribe = () => { + let session = Sao.Session.current_session; + Sao.rpc({ + 'method': 'model.ir.chat.channel.unsubscribe', + 'params': [this.record, {}], + }, session).then(() => { + set_subscribe_state(false); + }) + }; + this._messages = jQuery('', { 'class': 'chat-messages', }).appendTo(jQuery('', { diff --git a/sao/src/common.js b/sao/src/common.js index 171a700b0a..0f597a97b0 100644 --- a/sao/src/common.js +++ b/sao/src/common.js @@ -3155,6 +3155,8 @@ 'tryton-menu', 'tryton-note', 'tryton-notification', + 'tryton-notification-off', + 'tryton-notification-on', 'tryton-ok', 'tryton-open', 'tryton-print', diff --git a/tryton/CHANGELOG b/tryton/CHANGELOG index 483b036f92..301a814121 100644 --- a/tryton/CHANGELOG +++ b/tryton/CHANGELOG @@ -1,3 +1,4 @@ +* Allow users to subscribe to chats * Manage user notification * Add search on empty relation field to domain parser * Add support for multiple button in the tree view diff --git a/tryton/tryton/chat.py b/tryton/tryton/chat.py index 51ad0f1b3c..86e7ea1f92 100644 --- a/tryton/tryton/chat.py +++ b/tryton/tryton/chat.py @@ -84,9 +84,53 @@ class Chat: 'size-allocate', scroll_to_bottom) def __build(self): + tooltips = common.Tooltips() + widget = Gtk.VBox() widget.set_spacing(3) + hbuttonbox = Gtk.HButtonBox() + hbuttonbox.set_layout(Gtk.ButtonBoxStyle.END) + widget.pack_start(hbuttonbox, expand=False, fill=True, padding=0) + + subscribe_btn = Gtk.ToggleButton() + subscribe_btn.set_image(common.IconFactory.get_image( + 'tryton-notification', Gtk.IconSize.SMALL_TOOLBAR)) + tooltips.set_tip(subscribe_btn, _("Toggle notification")) + subscribe_btn.set_relief(Gtk.ReliefStyle.NONE) + hbuttonbox.pack_start( + subscribe_btn, expand=False, fill=True, padding=0) + hbuttonbox.set_child_non_homogeneous(subscribe_btn, True) + + followers = rpc.execute( + 'model', 'ir.chat.channel', 'get_followers', self.record, + rpc.CONTEXT) + + def set_subscribe_state(subscribed): + if subscribed: + img = 'tryton-notification-on' + else: + img = 'tryton-notification-off' + subscribe_btn.set_image(common.IconFactory.get_image( + img, Gtk.IconSize.SMALL_TOOLBAR)) + + subscribed = rpc._LOGIN in followers['users'] + set_subscribe_state(subscribed) + subscribe_btn.set_active(subscribed) + + def toggle_subscribe(button): + if button.props.active: + rpc.execute( + 'model', 'ir.chat.channel', 'subscribe', self.record, + rpc.CONTEXT) + else: + rpc.execute( + 'model', 'ir.chat.channel', 'unsubscribe', self.record, + rpc.CONTEXT) + set_subscribe_state(button.props.active) + + subscribe_btn.connect('toggled', toggle_subscribe) + def _submit(button): buffer = input_.get_buffer() self.send_message( diff --git a/tryton/tryton/data/pixmaps/tryton/tryton-notification-off.svg b/tryton/tryton/data/pixmaps/tryton/tryton-notification-off.svg new file mode 100644 index 0000000000..f48b825d32 --- /dev/null +++ b/tryton/tryton/data/pixmaps/tryton/tryton-notification-off.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tryton/tryton/data/pixmaps/tryton/tryton-notification-on.svg b/tryton/tryton/data/pixmaps/tryton/tryton-notification-on.svg new file mode 100644 index 0000000000..1582e4d409 --- /dev/null +++ b/tryton/tryton/data/pixmaps/tryton/tryton-notification-on.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tryton/tryton/rpc.py b/tryton/tryton/rpc.py index 6559e0aa67..06abf55082 100644 --- a/tryton/tryton/rpc.py +++ b/tryton/tryton/rpc.py @@ -20,6 +20,7 @@ from tryton.jsonrpc import Fault, ServerPool, ServerProxy logger = logging.getLogger(__name__) CONNECTION = None _USER = None +_LOGIN = None CONTEXT = {} _VIEW_CACHE = {} _TOOLBAR_CACHE = {} @@ -86,7 +87,7 @@ def authentication_services(host, port): def set_service_session(parameters): from tryton import common from tryton.bus import Bus - global CONNECTION, _USER + global CONNECTION, _USER, _LOGIN host = CONFIG['login.host'] hostname = common.get_hostname(host) port = common.get_port(host) @@ -102,6 +103,7 @@ def set_service_session(parameters): if _USER != renew_id: raise ValueError _USER = user_id + _LOGIN = username bus_url_host = parameters.get('bus_url_host', [''])[0] session = ':'.join(map(str, [username, user_id, session])) if CONNECTION is not None: @@ -114,7 +116,7 @@ def set_service_session(parameters): def login(parameters): from tryton import common from tryton.bus import Bus - global CONNECTION, _USER + global CONNECTION, _USER, _LOGIN host = CONFIG['login.host'] hostname = common.get_hostname(host) port = common.get_port(host) @@ -127,6 +129,7 @@ def login(parameters): result = connection.common.db.login(username, parameters, language) logger.debug('%r', result) _USER = result[0] + _LOGIN = username session = ':'.join(map(str, [username] + result[:2])) bus_url_host = result[2] if CONNECTION is not None: @@ -138,7 +141,7 @@ def login(parameters): def logout(): - global CONNECTION, _USER + global CONNECTION, _USER, _LOGIN if CONNECTION is not None: try: logger.info('common.db.logout()') @@ -148,7 +151,7 @@ def logout(): pass CONNECTION.close() CONNECTION = None - _USER = None + _USER = _LOGIN = None def reset_password(): diff --git a/trytond/trytond/ir/chat.py b/trytond/trytond/ir/chat.py index 93563da118..c56782fafb 100644 --- a/trytond/trytond/ir/chat.py +++ b/trytond/trytond/ir/chat.py @@ -44,6 +44,7 @@ class Channel(ModelSQL): unsubscribe=RPC(readonly=False), subscribe_email=RPC(readonly=False), unsubscribe_email=RPC(readonly=False), + get_followers=RPC(), post=RPC(readonly=False, result=int), get_models=RPC(), get=RPC(), @@ -73,19 +74,24 @@ class Channel(ModelSQL): ]) if channels: channel, = channels - else: + elif not Transaction().readonly: channel = cls(resource=str(resource)) channel.save() + else: + return return channel @classmethod - def subscribe(cls, resource, username): + def subscribe(cls, resource, username=None): pool = Pool() Follower = pool.get('ir.chat.follower') User = pool.get('res.user') - user, = User.search([ - ('login', '=', username), - ]) + if username is not None: + user, = User.search([ + ('login', '=', username), + ]) + else: + user = User(Transaction().user) channel = cls._get_channel(resource) Follower.add_user(channel, user) @@ -112,6 +118,26 @@ class Channel(ModelSQL): channel = cls._get_channel(resource) Follower.remove_email(channel, email) + @classmethod + def get_followers(cls, resource): + pool = Pool() + Follower = pool.get('ir.chat.follower') + users, emails = [], [] + channel = cls._get_channel(resource) + if channel: + followers = Follower.search([ + ('channel', '=', channel), + ]) + for follower in followers: + if follower.user: + users.append(follower.user.login) + elif follower.email: + emails.append(follower.email) + return { + 'users': users, + 'emails': emails, + } + @classmethod def post(cls, resource, content, audience='internal'): pool = Pool() diff --git a/trytond/trytond/ir/ui/menu.py b/trytond/trytond/ir/ui/menu.py index 48d7e7a984..83690f8b5b 100644 --- a/trytond/trytond/ir/ui/menu.py +++ b/trytond/trytond/ir/ui/menu.py @@ -61,6 +61,8 @@ CLIENT_ICONS = [(x, x) for x in [ 'tryton-menu', 'tryton-note', 'tryton-notification', + 'tryton-notification-off', + 'tryton-notification-on', 'tryton-ok', 'tryton-open', 'tryton-print',