diff options
Diffstat (limited to 'addons/im_livechat/controllers/main.py')
| -rw-r--r-- | addons/im_livechat/controllers/main.py | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/addons/im_livechat/controllers/main.py b/addons/im_livechat/controllers/main.py index c8006fd2df1..43e547e2fb1 100644 --- a/addons/im_livechat/controllers/main.py +++ b/addons/im_livechat/controllers/main.py @@ -29,12 +29,36 @@ class LivechatController(http.Controller): def loader(self, channel_id, **kwargs): username = kwargs.get("username", _("Visitor")) channel = request.env['im_livechat.channel'].sudo().browse(channel_id) - info = request.env['im_livechat.channel'].with_context(from_external_page=True).match_rules(request, channel.id, username=username) - return request.render('im_livechat.loader', {'info': info, 'web_session_required': True}) if info else False + info = request.env['im_livechat.channel'].get_livechat_info(channel.id, username=username) + return request.render('im_livechat.loader', {'info': info, 'web_session_required': True}) if info['available'] else False - @http.route('/im_livechat/available', type='json', auth="public") - def available(self, channel_id): - return len(request.env['im_livechat.channel'].browse(channel_id).get_available_users()) + @http.route('/im_livechat/init', type='json', auth="public") + def livechat_init(self, channel_id): + LivechatChannel = request.env['im_livechat.channel'] + available = len(LivechatChannel.browse(channel_id).get_available_users()) + rule = {} + if available: + # find the country from the request + country_id = False + country_code = request.session.geoip and request.session.geoip.get('country_code') or False + if country_code: + country_ids = request.env['res.country'].sudo().search([('code', '=', country_code)]) + if country_ids: + country_id = country_ids[0].id + # extract url + url = request.httprequest.headers.get('Referer') + # find the first matching rule for the given country and url + matching_rule = request.env['im_livechat.channel.rule'].sudo().match_rule(channel_id, url, country_id) + if matching_rule: + rule = { + 'action': matching_rule.action, + 'auto_popup_timer': matching_rule.auto_popup_timer, + 'regex_url': matching_rule.regex_url, + } + return { + 'available_for_me': available and (not rule or rule['action'] != 'hide_button'), + 'rule': rule, + } @http.route('/im_livechat/get_session', type="json", auth='public') def get_session(self, channel_id, anonymous_name, **kwargs): |
