Files
odoo_source/addons/mail/controllers/link_preview.py
T
Thibault Delavallée 84e6596fa1 [REF] mail: improve link preview code (and perf)
Improve link preview code

  * remove useless code indirections;
  * improve performance of link creation, cleanly support batch-creation (could
    lead to 98+5 URLs to the same domain creation within default 10 seconds
    instead of 98+1 but we feel it's ok);
  * name methods according to ORM;
  * use tools for html empty;

Also cleanup link preview tests: concatenate tests when possible, improve
setup, use real-life scenarios (using 'message_post' notably), remove useless
data creation.

Task-3557985 (Mail: Message cleanup (shortcode, link))
Prepares Task-36879 (Mail: Support MultiCompany Aliases)

Part-of: odoo/odoo#138938
2023-10-18 07:51:43 +00:00

34 lines
1.5 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import http
from odoo.http import request
from odoo.addons.mail.models.discuss.mail_guest import add_guest_to_context
class LinkPreviewController(http.Controller):
@http.route("/mail/link_preview", methods=["POST"], type="json", auth="public")
@add_guest_to_context
def mail_link_preview(self, message_id, clear=None):
if not request.env["mail.link.preview"]._is_link_preview_enabled():
return
guest = request.env["mail.guest"]._get_guest_from_context()
message = guest.env["mail.message"].search([("id", "=", int(message_id))])
if not message:
return
if not message.is_current_user_or_guest_author and not guest.env.user._is_admin():
return
if clear:
message.sudo().link_preview_ids._unlink_and_notify()
guest.env["mail.link.preview"].sudo()._create_from_message_and_notify(message)
@http.route("/mail/link_preview/delete", methods=["POST"], type="json", auth="public")
@add_guest_to_context
def mail_link_preview_delete(self, link_preview_ids):
guest = request.env["mail.guest"]._get_guest_from_context()
link_preview_sudo = guest.env["mail.link.preview"].sudo().search([("id", "in", link_preview_ids)])
if not link_preview_sudo:
return
if not link_preview_sudo.message_id.is_current_user_or_guest_author and not guest.env.user._is_admin():
return
link_preview_sudo._unlink_and_notify()