Question on How @ and # String Trigger Works in Odoo Chatter?

Viewed 85

Hope you guys doin well,

I'm curious about how can a certain string like @ and # can trigger a popup of a user and channels in Odoo Chatter & Discuss. This chatter has mail.thread Models related to it but i can't seem to understand how is it possible to create my own custom trigger in chatter?

Is it belong to certain views and not in models? Any help will be appreciated!

2 Answers

There is another way, and that is using the / or "command" popup. You can add your own item in the popup list and trigger any actions when a user selects your item from the popup.

  1. Inherit from mail.channel
class MailChannel(models.Model):
    _inherit = 'mail.channel'
  1. Add a new method that starts with _define_command_XXXXX. This registers the item in the popup.
def _define_command_sample(self):
    return {'help': _('Here is some sample help')}
  1. Add a new method that starts with _execute_command_XXXXX. This method is executed when the user uses your command.
def _execute_command_sample(self, **kwargs):
    # Send a "temporary" message to the user
    self._send_transient_message(self.env.user.partner_id, f"Hello! You just successfully used the /sample command. You typed {kwargs['body']}.")

Reference: Github where Odoo defines the /lead command

PS: The above is for Odoo V12.0 - V14.0. Odoo V15.0 works a bit different, but you can check this and this for how to do it.

Those are suggestion delimiters for the models used to update the suggestion list.

const suggestionDelimiters = ['@', ':', '#', '/'];

For example @ is used to fetch partners (mail.partner model) matching a given search term. The _updateSuggestionList function will search the suggestions, sort them then will show (update) the popup list

To add a new custom trigger, you need add the delimiter to the suggestionDelimiters list and alter the _computeSuggestionModelName function to return the related model, the model must be defined like the mail.partner model.

If you want to patch an existing model, check the following example (taken from the hr module):

/** @odoo-module **/

import {
    registerInstancePatchModel,
    registerFieldPatchModel,
} from '@mail/model/model_core';
import { attr, one2one } from '@mail/model/model_field';


// Patch model
registerInstancePatchModel('mail.partner', 'hr/static/src/models/partner/partner.js', {

    async checkIsEmployee() {
        await this.async(() => this.messaging.models['hr.employee'].performRpcSearchRead({
            context: { active_test: false },
            domain: [['user_partner_id', '=', this.id]],
            fields: ['user_id', 'user_partner_id'],
        }));
        this.update({ hasCheckedEmployee: true });
    },

});

// Patch fields
registerFieldPatchModel('mail.partner', 'hr/static/src/models/partner/partner.js', {

    hasCheckedEmployee: attr({
        default: false,
    }),

});

Related