From c41799dd5847392d96f57a87892ff9ac74701921 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 15 Nov 2012 18:49:02 +0100 Subject: [PATCH] First version, not working already bzr revid: nicolas.vanhoren@openerp.com-20121115174902-utga3vfpozc42fde --- addons/web_chat/__init__.py | 2 + addons/web_chat/__openerp__.py | 19 ++++ addons/web_chat/chat.py | 117 ++++++++++++++++++++++++ addons/web_chat/static/src/css/chat.css | 6 ++ addons/web_chat/static/src/js/chat.js | 46 ++++++++++ addons/web_chat/static/src/xml/chat.xml | 11 +++ 6 files changed, 201 insertions(+) create mode 100644 addons/web_chat/__init__.py create mode 100644 addons/web_chat/__openerp__.py create mode 100644 addons/web_chat/chat.py create mode 100644 addons/web_chat/static/src/css/chat.css create mode 100644 addons/web_chat/static/src/js/chat.js create mode 100644 addons/web_chat/static/src/xml/chat.xml diff --git a/addons/web_chat/__init__.py b/addons/web_chat/__init__.py new file mode 100644 index 00000000000..e61dd23b4cf --- /dev/null +++ b/addons/web_chat/__init__.py @@ -0,0 +1,2 @@ + +import chat diff --git a/addons/web_chat/__openerp__.py b/addons/web_chat/__openerp__.py new file mode 100644 index 00000000000..dd3f3cd0a07 --- /dev/null +++ b/addons/web_chat/__openerp__.py @@ -0,0 +1,19 @@ +{ + 'name' : 'Chat', + 'version': '1.0', + 'category': 'Tools', + 'complexity': 'easy', + 'description': + """ +OpenERP Chat module +=================== +Allows users to chat with each other. + """, + 'data': [], + 'depends' : [], + 'js': ['static/src/js/*.js'], + 'css': ['static/src/css/*.css'], + 'qweb': ['static/src/xml/*.xml'], + 'installable': True, + 'auto_install': False, +} diff --git a/addons/web_chat/chat.py b/addons/web_chat/chat.py new file mode 100644 index 00000000000..3b17ea0db3e --- /dev/null +++ b/addons/web_chat/chat.py @@ -0,0 +1,117 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2010 Tiny SPRL (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +import openerp +import openerp.modules.registry +from osv import osv, fields +import gevent +import gevent.event + +class Watcher: + watchers = {} + + @staticmethod + def get_watcher(db_name): + if not Watcher.watchers.get(db_name): + Watcher(db_name) + return Watcher.watchers[db_name] + + def __init__(self, db_name): + self.db_name = db_name + Watcher.watchers[db_name] = self + self.posted = gevent.event.Event() + self.waiting = 0 + gevent.spawn(self.loop) + + def loop(self): + try: + while True: + if self.waiting == 0: + return + registry = openerp.modules.registry.RegistryManager.get(self.db_name) + with registry.cursor() as c: + conn = c._cnx + try: + c.execute("listen received_message;") + c.commit(); + if select.select([conn], [], [], 60) == ([],[],[]): + pass + else: + conn.poll() + while conn.notifies: + notify = conn.notifies.pop() + print "Got NOTIFY:", notify.pid, notify.channel, notify.payload + self.posted.set() + self.posted.clear() + finally: + try: + c.execute("unlisten received_message;") + c.commit() + except: + pass # can't do anything if that fails + finally: + del Watcher.watchers[self.db_name] + self.posted.set() + self.posted = None + + def stop(self, timeout=None): + self.waiting += 1 + self.posted.wait(timeout) + self.waiting -= 1 + + + +class chat_message(osv.osv): + _name = 'chat.message' + _columns = { + 'message': fields.char(string="Message", size=200), + } + + def poll(self, cr, uid, last=None, context=None): + num = 0 + while True: + if not last: + tmp = self.search(cr, uid, [], context=context) + last = 0 + for i in tmp: + last = i if i > last else last + res = self.search(cr, uid, [['id', '>', last]], order="id", context=context) + res = self.read(cr, uid, res, ["id", "message"], context=context) + lst = [x["message"] for x in res] + if len(lst) > 0: + plast = last + last = res[-1]["id"] + if plast is not None: + return {"res": lst, "last": last} + num += 1 + if num == 2: + return {"res": [], "last": last} + import pudb + pudb.set_trace() + Watcher.get_watcher(cr.name).stop(30) + print "waking up" + + def post(self, cr, uid, message, context=None): + self.create({"message": message}, context=context) + cr.commit() + session.execute("notify received_message, '"+ message + "'") + cr.commit() + return False diff --git a/addons/web_chat/static/src/css/chat.css b/addons/web_chat/static/src/css/chat.css new file mode 100644 index 00000000000..299cad4d5cc --- /dev/null +++ b/addons/web_chat/static/src/css/chat.css @@ -0,0 +1,6 @@ + +.openerp .oe_chat { + position: fixed; + bottom: 0px; + right: 0px; +} diff --git a/addons/web_chat/static/src/js/chat.js b/addons/web_chat/static/src/js/chat.js new file mode 100644 index 00000000000..2d19f254385 --- /dev/null +++ b/addons/web_chat/static/src/js/chat.js @@ -0,0 +1,46 @@ + +openerp.web_chat = function(instance) { + + instance.web.Menu = instance.web.Menu.extend({ + start: function() { + new instance.web_chat.Chat(instance.client).appendTo(instance.client.$el); + return this._super(); + } + }); + + instance.web_chat.Chat = instance.web.Widget.extend({ + template: "Chat", + start: function() { + var self = this; + self.poll(); + self.last = null; + self.$(".oe_chat_input").keypress(function(e) { + if(e.which != 13) { + return; + } + var mes = self.$(".oe_chat_input").val(); + self.$(".oe_chat_input").val(""); + var model = new instance.web.Model("chat.message"); + model.call("post", [mes], {context: new instance.web.CompoundContext()}).then(function() { + console.log("pushed message"); + }); + }).focus(); + }, + poll: function() { + var self = this; + var model = new instance.web.Model("chat.message"); + model.call("poll", [this.last], {context: new instance.web.CompoundContext()}).then(function(result) { + console.log("got it", result); + self.last = result.last; + _.each(result.res, function(mes) { + $("
").text(mes).appendTo(self.$(".oe_chat_content")); + }); + //self.poll(); + }, function(unused, e) { + e.preventDefault(); + //setTimeout(_.bind(self.poll, self), 5000); + }); + } + }); + +} \ No newline at end of file diff --git a/addons/web_chat/static/src/xml/chat.xml b/addons/web_chat/static/src/xml/chat.xml new file mode 100644 index 00000000000..8d364a1e48b --- /dev/null +++ b/addons/web_chat/static/src/xml/chat.xml @@ -0,0 +1,11 @@ + + + + +
+
+ +
+
+
\ No newline at end of file