[FIX] mail: fix full composer messages
Full composer messages were not loaded in the chatter when sent. We lost that functionality sometimes in the previous months. This commit makes it work properly again. Some refactoring were required, we needed a way to load messages with a given res_id and model.
This commit is contained in:
@@ -177,39 +177,6 @@ function add_channel_to_message (message, channel_id) {
|
||||
message.channel_ids = _.uniq(message.channel_ids);
|
||||
}
|
||||
|
||||
function post_channel_message (data) {
|
||||
return ChannelModel.call('message_post', [data.channel_id], {
|
||||
message_type: 'comment',
|
||||
content_subtype: 'html',
|
||||
partner_ids: data.partner_ids,
|
||||
body: _.str.trim(data.content),
|
||||
subtype: 'mail.mt_comment',
|
||||
attachment_ids: data.attachment_ids,
|
||||
});
|
||||
}
|
||||
|
||||
function post_document_message (model_name, res_id, data) {
|
||||
var values = {
|
||||
attachment_ids: data.attachment_ids,
|
||||
body: _.str.trim(data.content),
|
||||
content_subtype: data.content_subtype,
|
||||
context: data.context,
|
||||
message_type: data.message_type,
|
||||
partner_ids: data.partner_ids,
|
||||
subtype: data.subtype,
|
||||
subtype_id: data.subtype_id,
|
||||
};
|
||||
|
||||
var model = new Model(model_name);
|
||||
return model.call('message_post', [res_id], values).then(function (msg_id) {
|
||||
return MessageModel.call('message_format', [msg_id]).then(function (msgs) {
|
||||
msgs[0].model = model_name;
|
||||
msgs[0].res_id = res_id;
|
||||
add_message(msgs[0]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function add_channel (data, options) {
|
||||
options = typeof options === "object" ? options : {};
|
||||
var channel = chat_manager.get_channel(data.id);
|
||||
@@ -516,35 +483,75 @@ function on_chat_session_notification (chat_session) {
|
||||
// Public interface
|
||||
//----------------------------------------------------------------------------------
|
||||
var chat_manager = {
|
||||
post_message: post_channel_message,
|
||||
post_message_in_document: post_document_message,
|
||||
post_message: function (data, options) {
|
||||
options = options || {};
|
||||
var msg = {
|
||||
partner_ids: data.partner_ids,
|
||||
body: _.str.trim(data.content),
|
||||
attachment_ids: data.attachment_ids,
|
||||
};
|
||||
if ('channel_id' in options) {
|
||||
// post a message in a channel
|
||||
return ChannelModel.call('message_post', [options.channel_id], _.extend(msg, {
|
||||
message_type: 'comment',
|
||||
content_subtype: 'html',
|
||||
subtype: 'mail.mt_comment',
|
||||
}));
|
||||
}
|
||||
if ('model' in options && 'res_id' in options) {
|
||||
// post a message in a chatter
|
||||
_.extend(msg, {
|
||||
content_subtype: data.content_subtype,
|
||||
context: data.context,
|
||||
message_type: data.message_type,
|
||||
subtype: data.subtype,
|
||||
subtype_id: data.subtype_id,
|
||||
});
|
||||
|
||||
var model = new Model(options.model);
|
||||
return model.call('message_post', [options.res_id], msg).then(function (msg_id) {
|
||||
return MessageModel.call('message_format', [msg_id]).then(function (msgs) {
|
||||
msgs[0].model = options.model;
|
||||
msgs[0].res_id = options.res_id;
|
||||
add_message(msgs[0]);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
get_messages: function (options) {
|
||||
if ('channel_id' in options) { // channel message
|
||||
var channel = this.get_channel(options.channel_id);
|
||||
var channel;
|
||||
|
||||
if ('channel_id' in options && options.load_more) {
|
||||
// get channel messages, force load_more
|
||||
channel = this.get_channel(options.channel_id);
|
||||
return fetch_from_channel(channel, {domain: options.domain || {}, load_more: true});
|
||||
}
|
||||
if ('channel_id' in options) {
|
||||
// channel message, check in cache first
|
||||
channel = this.get_channel(options.channel_id);
|
||||
var channel_cache = get_channel_cache(channel, options.domain);
|
||||
if (channel_cache.loaded) {
|
||||
return $.when(channel_cache.messages);
|
||||
} else {
|
||||
return fetch_from_channel(channel, {domain: options.domain});
|
||||
}
|
||||
} else { // chatter message
|
||||
}
|
||||
},
|
||||
fetch: function (channel, domain) {
|
||||
return fetch_from_channel(channel, {domain: domain});
|
||||
},
|
||||
fetch_more: function (channel, domain) {
|
||||
return fetch_from_channel(channel, {domain: domain, load_more: true});
|
||||
},
|
||||
/**
|
||||
* Fetches chatter messages from their ids
|
||||
*/
|
||||
fetch_messages: function (message_ids, options) {
|
||||
return fetch_document_messages(message_ids, options).then(function(result) {
|
||||
chat_manager.mark_as_read(message_ids);
|
||||
return result;
|
||||
});
|
||||
if ('ids' in options) {
|
||||
// get messages from their ids (chatter is the main use case)
|
||||
return fetch_document_messages(options.ids, options).then(function(result) {
|
||||
chat_manager.mark_as_read(options.ids);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
if ('model' in options && 'res_id' in options) {
|
||||
// get messages for a chatter, when it doesn't know the ids (use
|
||||
// case is when using the full composer)
|
||||
var domain = [['model', '=', options.model], ['res_id', '=', options.res_id]];
|
||||
MessageModel.call('message_fetch', [domain], {limit: 30}).then(function (msgs) {
|
||||
return _.map(msgs, add_message);
|
||||
});
|
||||
}
|
||||
},
|
||||
toggle_star_status: function (message_id) {
|
||||
var msg = _.findWhere(messages, { id: message_id });
|
||||
|
||||
@@ -82,10 +82,9 @@ return Widget.extend({
|
||||
content: this.$input.val(),
|
||||
attachment_ids: [],
|
||||
partner_ids: [],
|
||||
channel_id: this.channel_id,
|
||||
};
|
||||
this.$input.val('');
|
||||
this.trigger('post_message', message);
|
||||
this.trigger('post_message', message, this.channel_id);
|
||||
}
|
||||
},
|
||||
on_click_close: function (event) {
|
||||
|
||||
@@ -674,6 +674,8 @@ var ChatterComposer = ChatComposer.extend({
|
||||
}, {
|
||||
on_close: function() {
|
||||
self.trigger('need_refresh');
|
||||
var parent = self.getParent();
|
||||
chat_manager.get_messages({model: parent.model, res_id: parent.res_id});
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -741,15 +743,18 @@ var Chatter = form_common.AbstractField.extend({
|
||||
|
||||
fetch_and_render_thread: function (ids, options) {
|
||||
var self = this;
|
||||
return chat_manager.fetch_messages(ids, options).then(function (raw_messages) {
|
||||
options = options || {};
|
||||
options.ids = ids;
|
||||
return chat_manager.get_messages(options).then(function (raw_messages) {
|
||||
self.thread.render(raw_messages, {display_load_more: raw_messages.length < ids.length});
|
||||
});
|
||||
},
|
||||
|
||||
on_post_message: function (message) {
|
||||
var self = this;
|
||||
var options = {model: this.model, res_id: this.res_id};
|
||||
chat_manager
|
||||
.post_message_in_document(this.model, this.res_id, message)
|
||||
.post_message(message, options)
|
||||
.then(function () {
|
||||
self.close_composer();
|
||||
if (message.partner_ids.length) {
|
||||
|
||||
@@ -452,7 +452,7 @@ var ChatAction = Widget.extend(ControlPanelMixin, {
|
||||
var oldest_msg_selector = '.o_thread_message[data-message-id="' + oldest_msg_id + '"]';
|
||||
var offset = -framework.getPosition(document.querySelector(oldest_msg_selector)).top;
|
||||
return chat_manager
|
||||
.fetch_more(this.channel, this.domain)
|
||||
.get_messages({channel_id: this.channel.id, domain: this.domain, load_more: true})
|
||||
.then(function(result) {
|
||||
if (self.messages_separator_position === 'top') {
|
||||
self.messages_separator_position = undefined; // reset value to re-compute separator position
|
||||
@@ -494,9 +494,9 @@ var ChatAction = Widget.extend(ControlPanelMixin, {
|
||||
},
|
||||
|
||||
on_post_message: function (message) {
|
||||
message.channel_id = this.channel.id;
|
||||
var options = {channel_id: this.channel.id};
|
||||
chat_manager
|
||||
.post_message(message)
|
||||
.post_message(message, options)
|
||||
.fail(function () {
|
||||
// todo: display notification
|
||||
});
|
||||
|
||||
@@ -37,8 +37,8 @@ function open_chat (session) {
|
||||
chat_manager.fold_channel(channel_id);
|
||||
});
|
||||
|
||||
chat_session.window.on("post_message", null, function (message) {
|
||||
chat_manager.post_message(message);
|
||||
chat_session.window.on("post_message", null, function (message, channel_id) {
|
||||
chat_manager.post_message(message, {channel_id: channel_id});
|
||||
});
|
||||
chat_session.window.on("messages_read", null, function () {
|
||||
chat_manager.mark_channel_as_seen(channel);
|
||||
|
||||
Reference in New Issue
Block a user