[FIX] im_livechat: don't remove empty sessions created recently

The 'cron_remove_empty_session' is performed every day and removes livechat
channels that contain no message.

The problem was that the cron might have been executed between the moment a
public user clicked on the livechat button, creating a new empty channel, and
the moment this user sent the first message through that channel. If that
happenned, the message was sent in an unexisting channel, and obviously never
received by the operator.

This fix makes sure that removed empty channels haven't been created or
modified within the last hour.
This commit is contained in:
Aaron Bohy
2015-12-03 11:21:47 +01:00
parent 8017a9569c
commit 5fdfd38b7e
+4 -2
View File
@@ -58,6 +58,7 @@ class MailChannel(models.Model):
@api.model
def cron_remove_empty_session(self):
hours = 1 # never remove empty session created within the last hour
self.env.cr.execute("""
SELECT id as id
FROM mail_channel C
@@ -65,7 +66,8 @@ class MailChannel(models.Model):
SELECT *
FROM mail_message_mail_channel_rel R
WHERE R.mail_channel_id = C.id
) AND C.channel_type = 'livechat' AND livechat_channel_id IS NOT NULL;
""")
) AND C.channel_type = 'livechat' AND livechat_channel_id IS NOT NULL AND
COALESCE(write_date, create_date, (now() at time zone 'UTC'))::timestamp
< ((now() at time zone 'UTC') - interval %s)""", ("%s hours" % hours,))
empty_channel_ids = [item['id'] for item in self.env.cr.dictfetchall()]
self.browse(empty_channel_ids).unlink()