1 # -*- coding: utf-8 -*-
12 from openerp.http import request
13 from openerp.osv import osv, fields
14 from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
15 from openerp.addons.bus.bus import TIMEOUT
17 _logger = logging.getLogger(__name__)
19 DISCONNECTION_TIMER = TIMEOUT + 5
20 AWAY_TIMER = 600 # 10 minutes
22 #----------------------------------------------------------
24 #----------------------------------------------------------
25 class im_chat_conversation_state(osv.Model):
26 """ Adds a state on the m2m between user and session. """
27 _name = 'im_chat.conversation_state'
28 _table = "im_chat_session_res_users_rel"
31 "state" : fields.selection([('open', 'Open'), ('folded', 'Folded'), ('closed', 'Closed')]),
32 "session_id" : fields.many2one('im_chat.session', 'Session', required=True, ondelete="cascade"),
33 "user_id" : fields.many2one('res.users', 'Users', required=True, ondelete="cascade"),
39 class im_chat_session(osv.Model):
42 _name = 'im_chat.session'
46 'uuid': fields.char('UUID', size=50, select=True),
47 'message_ids': fields.one2many('im_chat.message', 'to_id', 'Messages'),
48 'user_ids': fields.many2many('res.users', 'im_chat_session_res_users_rel', 'session_id', 'user_id', "Session Users"),
49 'session_res_users_rel': fields.one2many('im_chat.conversation_state', 'session_id', 'Relation Session Users'),
52 'uuid': lambda *args: '%s' % uuid.uuid4(),
55 def is_in_session(self, cr, uid, uuid, user_id, context=None):
56 """ return if the given user_id is in the session """
57 sids = self.search(cr, uid, [('uuid', '=', uuid)], context=context, limit=1)
58 for session in self.browse(cr, uid, sids, context=context):
59 return user_id and user_id in [u.id for u in session.user_ids]
62 def users_infos(self, cr, uid, ids, context=None):
63 """ get the user infos for all the user in the session """
64 for session in self.pool["im_chat.session"].browse(cr, uid, ids, context=context):
65 users_infos = self.pool["res.users"].read(cr, uid, [u.id for u in session.user_ids], ['id','name', 'im_status'], context=context)
68 def is_private(self, cr, uid, ids, context=None):
69 for session_id in ids:
70 """ return true if the session is private between users no external messages """
71 mess_ids = self.pool["im_chat.message"].search(cr, uid, [('to_id','=',session_id),('from_id','=',None)], context=context)
72 return len(mess_ids) == 0
74 def session_info(self, cr, uid, ids, context=None):
75 """ get the session info/header of a given session """
76 for session in self.browse(cr, uid, ids, context=context):
79 'users': session.users_infos(),
82 # add uid_state if available
84 domain = [('user_id','=',uid), ('session_id','=',session.id)]
85 uid_state = self.pool['im_chat.conversation_state'].search_read(cr, uid, domain, ['state'], context=context)
87 info['state'] = uid_state[0]['state']
90 def session_get(self, cr, uid, user_to, context=None):
91 """ returns the canonical session between 2 users, create it if needed """
94 sids = self.search(cr, uid, [('user_ids','in', user_to),('user_ids', 'in', [uid])], context=context, limit=1)
95 for sess in self.browse(cr, uid, sids, context=context):
96 if len(sess.user_ids) == 2 and sess.is_private():
100 session_id = self.create(cr, uid, { 'user_ids': [(6,0, (user_to, uid))] }, context=context)
101 return self.session_info(cr, uid, [session_id], context=context)
103 def update_state(self, cr, uid, uuid, state=None, context=None):
104 """ modify the fold_state of the given session, and broadcast to himself (e.i. : to sync multiple tabs) """
105 domain = [('user_id','=',uid), ('session_id.uuid','=',uuid)]
106 ids = self.pool['im_chat.conversation_state'].search(cr, uid, domain, context=context)
107 for sr in self.pool['im_chat.conversation_state'].browse(cr, uid, ids, context=context):
110 if sr.state == 'open':
114 self.pool['im_chat.conversation_state'].write(cr, uid, ids, {'state': state}, context=context)
115 self.pool['bus.bus'].sendone(cr, uid, (cr.dbname, 'im_chat.session', uid), sr.session_id.session_info())
117 def add_user(self, cr, uid, uuid, user_id, context=None):
118 """ add the given user to the given session """
119 sids = self.search(cr, uid, [('uuid', '=', uuid)], context=context, limit=1)
120 for session in self.browse(cr, uid, sids, context=context):
121 if user_id not in [u.id for u in session.user_ids]:
122 self.write(cr, uid, [session.id], {'user_ids': [(4, user_id)]}, context=context)
123 # notify the all the channel users and anonymous channel
125 for channel_user_id in session.user_ids:
126 info = self.session_info(cr, channel_user_id.id, [session.id], context=context)
127 notifications.append([(cr.dbname, 'im_chat.session', channel_user_id.id), info])
128 # Anonymous are not notified when a new user is added : cannot exec session_info as uid = None
129 info = self.session_info(cr, openerp.SUPERUSER_ID, [session.id], context=context)
130 notifications.append([session.uuid, info])
131 self.pool['bus.bus'].sendmany(cr, uid, notifications)
132 # send a message to the conversation
133 user = self.pool['res.users'].read(cr, uid, user_id, ['name'], context=context)
134 self.pool["im_chat.message"].post(cr, uid, uid, session.uuid, "meta", user['name'] + " joined the conversation.", context=context)
136 def get_image(self, cr, uid, uuid, user_id, context=None):
137 """ get the avatar of a user in the given session """
139 image_b64 = 'R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
142 session_id = self.pool["im_chat.session"].search(cr, uid, [('uuid','=',uuid), ('user_ids','in', user_id)])
144 # get the image of the user
145 res = self.pool["res.users"].read(cr, uid, [user_id], ["image_small"])[0]
146 if res["image_small"]:
147 image_b64 = res["image_small"]
150 class im_chat_message(osv.Model):
151 """ Sessions messsages type can be 'message' or 'meta'.
152 For anonymous message, the from_id is False.
153 Messages are sent to a session not to users.
155 _name = 'im_chat.message'
158 'create_date': fields.datetime('Create Date', required=True, select=True),
159 'from_id': fields.many2one('res.users', 'Author'),
160 'to_id': fields.many2one('im_chat.session', 'Session To', required=True, select=True, ondelete='cascade'),
161 'type': fields.selection([('message','Message'), ('meta','Meta')], 'Type'),
162 'message': fields.char('Message'),
168 def init_messages(self, cr, uid, context=None):
169 """ get unread messages and old messages received less than AWAY_TIMER
170 ago and the session_info for open or folded window
172 # get the message since the AWAY_TIMER
173 threshold = datetime.datetime.now() - datetime.timedelta(seconds=AWAY_TIMER)
174 threshold = threshold.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
175 domain = [('to_id.user_ids', 'in', [uid]), ('create_date','>',threshold)]
177 # get the message since the last poll of the user
178 presence_ids = self.pool['im_chat.presence'].search(cr, uid, [('user_id', '=', uid)], context=context)
180 presence = self.pool['im_chat.presence'].browse(cr, uid, presence_ids, context=context)[0]
181 threshold = presence.last_poll
182 domain.append(('create_date','>',threshold))
183 messages = self.search_read(cr, uid, domain, ['from_id','to_id','create_date','type','message'], order='id asc', context=context)
185 # get the session of the messages and the not-closed ones
186 session_ids = map(lambda m: m['to_id'][0], messages)
187 domain = [('user_id','=',uid), '|', ('state','!=','closed'), ('session_id', 'in', session_ids)]
188 session_rels_ids = self.pool['im_chat.conversation_state'].search(cr, uid, domain, context=context)
189 # re-open the session where a message have been recieve recently
190 session_rels = self.pool['im_chat.conversation_state'].browse(cr, uid, session_rels_ids, context=context)
192 reopening_session = []
194 for sr in session_rels:
195 si = sr.session_id.session_info()
196 si['state'] = sr.state
197 if sr.state == 'closed':
198 si['state'] = 'folded'
199 reopening_session.append(sr.id)
200 notifications.append([(cr.dbname,'im_chat.session', uid), si])
202 notifications.append([(cr.dbname,'im_chat.session', uid), m])
203 self.pool['im_chat.conversation_state'].write(cr, uid, reopening_session, {'state': 'folded'}, context=context)
206 def post(self, cr, uid, from_uid, uuid, message_type, message_content, context=None):
207 """ post and broadcast a message, return the message id """
209 Session = self.pool['im_chat.session']
210 session_ids = Session.search(cr, uid, [('uuid','=',uuid)], context=context)
212 for session in Session.browse(cr, uid, session_ids, context=context):
213 # build the new message
217 "type": message_type,
218 "message": message_content,
221 message_id = self.create(cr, uid, vals, context=context)
222 # broadcast it to channel (anonymous users) and users_ids
223 data = self.read(cr, uid, [message_id], ['from_id','to_id','create_date','type','message'], context=context)[0]
224 notifications.append([uuid, data])
225 for user in session.user_ids:
226 notifications.append([(cr.dbname, 'im_chat.session', user.id), data])
227 self.pool['bus.bus'].sendmany(cr, uid, notifications)
230 def get_messages(self, cr, uid, uuid, last_id=False, limit=20, context=None):
231 """ get messages (id desc) from given last_id in the given session """
232 Session = self.pool['im_chat.session']
233 if Session.is_in_session(cr, uid, uuid, uid, context=context):
234 domain = [("to_id.uuid", "=", uuid)]
236 domain.append(("id", "<", last_id));
237 return self.search_read(cr, uid, domain, ['id', 'create_date','to_id','from_id', 'type', 'message'], limit=limit, context=context)
241 class im_chat_presence(osv.Model):
242 """ im_chat_presence status can be: online, away or offline.
243 This model is a one2one, but is not attached to res_users to avoid database concurrence errors
245 _name = 'im_chat.presence'
248 'user_id' : fields.many2one('res.users', 'Users', required=True, select=True, ondelete="cascade"),
249 'last_poll': fields.datetime('Last Poll'),
250 'last_presence': fields.datetime('Last Presence'),
251 'status' : fields.selection([('online','Online'), ('away','Away'), ('offline','Offline')], 'IM Status'),
254 'last_poll' : fields.datetime.now,
255 'last_presence' : fields.datetime.now,
258 _sql_constraints = [('im_chat_user_status_unique','unique(user_id)', 'A user can only have one IM status.')]
260 def update(self, cr, uid, presence=True, context=None):
261 """ register the poll, and change its im status if necessary. It also notify the Bus if the status has changed. """
262 presence_ids = self.search(cr, uid, [('user_id', '=', uid)], context=context)
263 presences = self.browse(cr, uid, presence_ids, context=context)
264 # set the default values
265 send_notification = True
267 'last_poll': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
268 'status' : presences and presences[0].status or 'offline'
270 # update the user or a create a new one
272 vals['status'] = 'online'
273 vals['user_id'] = uid
274 self.create(cr, uid, vals, context=context)
277 vals['last_presence'] = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
278 vals['status'] = 'online'
280 threshold = datetime.datetime.now() - datetime.timedelta(seconds=AWAY_TIMER)
281 if datetime.datetime.strptime(presences[0].last_presence, DEFAULT_SERVER_DATETIME_FORMAT) < threshold:
282 vals['status'] = 'away'
283 send_notification = presences[0].status != vals['status']
284 # write only if the last_poll is passed TIMEOUT, or if the status has changed
285 delta = datetime.datetime.now() - datetime.datetime.strptime(presences[0].last_poll, DEFAULT_SERVER_DATETIME_FORMAT)
286 if (delta > datetime.timedelta(seconds=TIMEOUT) or send_notification):
287 self.write(cr, uid, presence_ids, vals, context=context)
288 # avoid TransactionRollbackError
290 # notify if the status has changed
291 if send_notification:
292 self.pool['bus.bus'].sendone(cr, uid, (cr.dbname,'im_chat.presence'), {'id': uid, 'im_status': vals['status']})
293 # gc : disconnect the users having a too old last_poll. 1 on 100 chance to do it.
294 if random.random() < 0.01:
295 self.check_users_disconnection(cr, uid, context=context)
298 def check_users_disconnection(self, cr, uid, context=None):
299 """ disconnect the users having a too old last_poll """
300 dt = (datetime.datetime.now() - datetime.timedelta(0, DISCONNECTION_TIMER)).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
301 presence_ids = self.search(cr, uid, [('last_poll', '<', dt), ('status' , '!=', 'offline')], context=context)
302 self.write(cr, uid, presence_ids, {'status': 'offline'}, context=context)
303 presences = self.browse(cr, uid, presence_ids, context=context)
305 for presence in presences:
306 notifications.append([(cr.dbname,'im_chat.presence'), {'id': presence.user_id.id, 'im_status': presence.status}])
307 self.pool['bus.bus'].sendmany(cr, uid, notifications)
310 class res_users(osv.Model):
311 _inherit = "res.users"
313 def _get_im_status(self, cr, uid, ids, fields, arg, context=None):
314 """ function computing the im_status field of the users """
315 r = dict((i, 'offline') for i in ids)
316 status_ids = self.pool['im_chat.presence'].search(cr, uid, [('user_id', 'in', ids)], context=context)
317 status = self.pool['im_chat.presence'].browse(cr, uid, status_ids, context=context)
319 r[s.user_id.id] = s.status
323 'im_status' : fields.function(_get_im_status, type="char", string="IM Status"),
326 def im_search(self, cr, uid, name, limit=20, context=None):
327 """ search users with a name and return its id, name and im_status """
329 # find the employee group
330 group_employee = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base', 'group_user')[1]
332 where_clause_base = " U.active = 't' "
335 where_clause_base += " AND P.name ILIKE %s "
336 query_params = query_params + ('%'+name+'%',)
338 # first query to find online employee
339 cr.execute('''SELECT U.id as id, P.name as name, COALESCE(S.status, 'offline') as im_status
340 FROM im_chat_presence S
341 JOIN res_users U ON S.user_id = U.id
342 JOIN res_partner P ON P.id = U.partner_id
343 WHERE '''+where_clause_base+'''
345 AND EXISTS (SELECT 1 FROM res_groups_users_rel G WHERE G.gid = %s AND G.uid = U.id)
346 AND S.status = 'online'
349 ''', query_params + (uid, group_employee, limit))
350 result = result + cr.dictfetchall()
352 # second query to find other online people
353 if(len(result) < limit):
354 cr.execute('''SELECT U.id as id, P.name as name, COALESCE(S.status, 'offline') as im_status
355 FROM im_chat_presence S
356 JOIN res_users U ON S.user_id = U.id
357 JOIN res_partner P ON P.id = U.partner_id
358 WHERE '''+where_clause_base+'''
360 AND S.status = 'online'
363 ''', query_params + (tuple([u["id"] for u in result]) + (uid,), limit-len(result)))
364 result = result + cr.dictfetchall()
366 # third query to find all other people
367 if(len(result) < limit):
368 cr.execute('''SELECT U.id as id, P.name as name, COALESCE(S.status, 'offline') as im_status
370 LEFT JOIN im_chat_presence S ON S.user_id = U.id
371 LEFT JOIN res_partner P ON P.id = U.partner_id
372 WHERE '''+where_clause_base+'''
376 ''', query_params + (tuple([u["id"] for u in result]) + (uid,), limit-len(result)))
377 result = result + cr.dictfetchall()
380 #----------------------------------------------------------
382 #----------------------------------------------------------
383 class Controller(openerp.addons.bus.bus.Controller):
384 def _poll(self, dbname, channels, last, options):
385 if request.session.uid:
386 registry, cr, uid, context = request.registry, request.cr, request.session.uid, request.context
387 registry.get('im_chat.presence').update(cr, uid, options.get('im_presence', False), context=context)
388 ## For performance issue, the real time status notification is disabled. This means a change of status are still braoadcasted
389 ## but not received by anyone. Otherwise, all listening user restart their longpolling at the same time and cause a 'ConnectionPool Full Error'
390 ## since there is not enought cursors for everyone. Now, when a user open his list of users, an RPC call is made to update his user status list.
391 ##channels.append((request.db,'im_chat.presence'))
392 # channel to receive message
393 channels.append((request.db,'im_chat.session', request.uid))
394 return super(Controller, self)._poll(dbname, channels, last, options)
396 @openerp.http.route('/im_chat/init', type="json", auth="none")
398 registry, cr, uid, context = request.registry, request.cr, request.session.uid, request.context
399 notifications = registry['im_chat.message'].init_messages(cr, uid, context=context)
402 @openerp.http.route('/im_chat/post', type="json", auth="none")
403 def post(self, uuid, message_type, message_content):
404 registry, cr, uid, context = request.registry, request.cr, request.session.uid, request.context
405 # execute the post method as SUPERUSER_ID
406 message_id = registry["im_chat.message"].post(cr, openerp.SUPERUSER_ID, uid, uuid, message_type, message_content, context=context)
409 @openerp.http.route(['/im_chat/image/<string:uuid>/<string:user_id>'], type='http', auth="none")
410 def image(self, uuid, user_id):
411 registry, cr, context, uid = request.registry, request.cr, request.context, request.session.uid
413 Session = registry.get("im_chat.session")
414 image_b64 = Session.get_image(cr, openerp.SUPERUSER_ID, uuid, simplejson.loads(user_id), context)
416 image_data = base64.b64decode(image_b64)
417 headers = [('Content-Type', 'image/png')]
418 headers.append(('Content-Length', len(image_data)))
419 return request.make_response(image_data, headers)
421 @openerp.http.route(['/im_chat/history'], type="json", auth="none")
422 def history(self, uuid, last_id=False, limit=20):
423 registry, cr, uid, context = request.registry, request.cr, request.session.uid or openerp.SUPERUSER_ID, request.context
424 return registry["im_chat.message"].get_messages(cr, uid, uuid, last_id, limit, context=context)