19.0 vanilla

This commit is contained in:
Ernad Husremovic 2026-03-09 09:31:39 +01:00
parent 5df8c07b59
commit daa394e8b0
2114 changed files with 564841 additions and 299642 deletions

View file

@ -1,47 +1,77 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models
from odoo.addons.mail.tools.discuss import Store
class MailMessage(models.Model):
_inherit = 'mail.message'
def _message_format(self, fnames, format_reply=True, legacy=False):
"""Override to remove email_from and to return the livechat username if applicable.
A third param is added to the author_id tuple in this case to be able to differentiate it
from the normal name in client code.
def _to_store_defaults(self, target):
return super()._to_store_defaults(target) + ["chatbot_current_step"]
In addition, if we are currently running a chatbot.script, we include the information about
def _to_store(self, store: Store, fields, **kwargs):
"""If we are currently running a chatbot.script, we include the information about
the chatbot.message related to this mail.message.
This allows the frontend display to include the additional features
(e.g: Show additional buttons with the available answers for this step). """
vals_list = super()._message_format(fnames=fnames, format_reply=format_reply, legacy=legacy)
for vals in vals_list:
message_sudo = self.browse(vals['id']).sudo().with_prefetch(self.ids)
mail_channel = self.env['mail.channel'].browse(message_sudo.res_id) if message_sudo.model == 'mail.channel' else self.env['mail.channel']
if mail_channel.channel_type == 'livechat':
if message_sudo.author_id:
vals.pop('email_from')
if message_sudo.author_id.user_livechat_username:
vals['author'] = {
'id': message_sudo.author_id.id,
'user_livechat_username': message_sudo.author_id.user_livechat_username,
(e.g: Show additional buttons with the available answers for this step)."""
super()._to_store(store, [f for f in fields if f != "chatbot_current_step"], **kwargs)
if "chatbot_current_step" not in fields:
return
channel_messages = self.filtered(lambda message: message.channel_id)
channel_by_message = channel_messages._record_by_message()
for message in channel_messages.filtered(
lambda message: channel_by_message[message].channel_type == "livechat"
):
channel = channel_by_message[message]
# sudo: chatbot.script.step - checking whether the current message is from chatbot
chatbot = channel.chatbot_current_step_id.sudo().chatbot_script_id.operator_partner_id
if channel.chatbot_current_step_id and message.author_id == chatbot:
chatbot_message = (
self.env["chatbot.message"]
.sudo()
.search([("mail_message_id", "=", message.id)], limit=1)
)
if step := chatbot_message.script_step_id:
step_data = {
"id": (step.id, message.id),
"message": message.id,
"scriptStep": Store.One(step, ["id", "message", "step_type"]),
"operatorFound": step.is_forward_operator
and channel.livechat_operator_id != chatbot,
}
# sudo: chatbot.script.step - members of a channel can access the current chatbot step
if mail_channel.chatbot_current_step_id \
and message_sudo.author_id == mail_channel.chatbot_current_step_id.sudo().chatbot_script_id.operator_partner_id:
chatbot_message_id = self.env['chatbot.message'].sudo().search([
('mail_message_id', '=', message_sudo.id)], limit=1)
if chatbot_message_id.script_step_id:
vals['chatbot_script_step_id'] = chatbot_message_id.script_step_id.id
if chatbot_message_id.script_step_id.step_type == 'question_selection':
vals['chatbot_step_answers'] = [{
'id': answer.id,
'label': answer.name,
'redirect_link': answer.redirect_link,
} for answer in chatbot_message_id.script_step_id.answer_ids]
if chatbot_message_id.user_script_answer_id:
vals['chatbot_selected_answer_id'] = chatbot_message_id.user_script_answer_id.id
return vals_list
if answer := chatbot_message.user_script_answer_id:
step_data["selectedAnswer"] = {
"id": answer.id,
"label": answer.name,
}
if step.step_type in [
"free_input_multi",
"free_input_single",
"question_email",
"question_phone",
]:
# sudo: chatbot.message - checking the user answer to the step is allowed
user_answer_message = (
self.env["chatbot.message"]
.sudo()
.search(
[
("script_step_id", "=", step.id),
("id", "!=", chatbot_message.id),
("discuss_channel_id", "=", channel.id),
],
limit=1,
)
)
step_data["rawAnswer"] = [
"markup",
user_answer_message.user_raw_answer,
]
store.add_model_values("ChatbotStep", step_data)
store.add(
message, {"chatbotStep": {"scriptStep": step.id, "message": message.id}}
)
def _get_store_partner_name_fields(self):
if self.channel_id.channel_type == "livechat":
return self.env["res.partner"]._get_store_livechat_username_fields()
return super()._get_store_partner_name_fields()