[IMP] mail: cache messages for each fetched domain
This is kind of a fix as well as it permits again to use filters on channels.
This commit is contained in:
@@ -177,9 +177,13 @@ function make_channel (data, options) {
|
||||
uuid: data.uuid,
|
||||
is_detached: data.is_minimized,
|
||||
is_folded: data.state === "folded",
|
||||
loaded: false,
|
||||
autoswitch: 'autoswitch' in options ? options.autoswitch : true,
|
||||
hidden: options.hidden,
|
||||
cache: {'[]': {
|
||||
all_history_loaded: false,
|
||||
loaded: false,
|
||||
message_ids: [],
|
||||
}},
|
||||
};
|
||||
if (data.public === "private") {
|
||||
channel.type = "private";
|
||||
@@ -192,6 +196,27 @@ function make_channel (data, options) {
|
||||
return channel;
|
||||
}
|
||||
|
||||
function get_channel_cache (channel_id, domain) {
|
||||
var stringified_domain = JSON.stringify(domain || []);
|
||||
var channel = _.findWhere(channels, {id: channel_id});
|
||||
if (!channel.cache[stringified_domain]) {
|
||||
channel.cache[stringified_domain] = {
|
||||
all_history_loaded: false,
|
||||
loaded: false,
|
||||
message_ids: [],
|
||||
};
|
||||
}
|
||||
return channel.cache[stringified_domain];
|
||||
}
|
||||
|
||||
function remove_message_from_channel (channel_id, message) {
|
||||
message.channel_ids = _.without(message.channel_ids, channel_id);
|
||||
var channel = _.findWhere(channels, { id: channel_id });
|
||||
_.each(channel.cache, function (cache) {
|
||||
cache.message_ids = _.without(cache.message_ids, message.id);
|
||||
});
|
||||
}
|
||||
|
||||
// options: domain, load_more
|
||||
function fetch_from_channel (channel_id, options) {
|
||||
var domain =
|
||||
@@ -214,17 +239,19 @@ function fetch_from_channel (channel_id, options) {
|
||||
}
|
||||
|
||||
return MessageModel.call('message_fetch', [domain], {limit: LIMIT}).then(function (msgs) {
|
||||
var channel = _.findWhere(channels, {id: channel_id});
|
||||
channel.loaded = true;
|
||||
if (!channel.all_history_loaded) {
|
||||
channel.all_history_loaded = msgs.length < LIMIT;
|
||||
var cache = get_channel_cache(channel_id, options.domain);
|
||||
|
||||
cache.message_ids = _.uniq(cache.message_ids.concat(_.pluck(msgs, 'id')));
|
||||
if (!cache.all_history_loaded) {
|
||||
cache.all_history_loaded = msgs.length < LIMIT;
|
||||
}
|
||||
cache.loaded = true;
|
||||
|
||||
_.each(msgs, function (msg) {
|
||||
add_message(msg, {channel_id: channel_id, silent: true});
|
||||
});
|
||||
return _.filter(messages, function (m) {
|
||||
return _.contains(m.channel_ids, channel_id);
|
||||
return _.contains(cache.message_ids, m.id);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -254,6 +281,7 @@ function fetch_document_messages (ids, options) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Public interface
|
||||
//----------------------------------------------------------------------------------
|
||||
var chat_manager = {
|
||||
@@ -262,10 +290,10 @@ var chat_manager = {
|
||||
|
||||
get_messages: function (options) {
|
||||
if ('channel_id' in options) { // channel message
|
||||
var channel = _.findWhere(channels, {id: options.channel_id});
|
||||
if (channel.loaded) {
|
||||
var channel_cache = get_channel_cache(options.channel_id, options.domain);
|
||||
if (channel_cache.loaded) {
|
||||
return $.when(_.filter(messages, function (message) {
|
||||
return _.contains(message.channel_ids, channel.id);
|
||||
return _.contains(message.channel_ids, options.channel_id);
|
||||
}));
|
||||
} else {
|
||||
return fetch_from_channel(options.channel_id);
|
||||
@@ -290,13 +318,16 @@ var chat_manager = {
|
||||
|
||||
return MessageModel.call('set_message_starred', [[message_id], !msg.is_starred]).then(function () {
|
||||
msg.is_starred = !msg.is_starred;
|
||||
if (!msg.is_starred) {
|
||||
remove_message_from_channel("channel_starred", msg);
|
||||
}
|
||||
chat_manager.bus.trigger('update_message', msg);
|
||||
});
|
||||
},
|
||||
mark_as_read: function (message_id) {
|
||||
return MessageModel.call('set_message_done', [[message_id]]).then(function () {
|
||||
var message = _.findWhere(messages, { id: message_id });
|
||||
message.channel_ids = _.without(message.channel_ids, "channel_inbox");
|
||||
remove_message_from_channel("channel_inbox", message);
|
||||
chat_manager.bus.trigger('update_message', message);
|
||||
needaction_counter = needaction_counter - 1;
|
||||
chat_manager.bus.trigger('update_needaction', needaction_counter);
|
||||
@@ -311,8 +342,8 @@ var chat_manager = {
|
||||
return _.findWhere(channels, {id: id}) || channels[0];
|
||||
},
|
||||
|
||||
all_history_loaded: function (channel_id) {
|
||||
return this.get_channel(channel_id).all_history_loaded;
|
||||
all_history_loaded: function (channel_id, domain) {
|
||||
return get_channel_cache(channel_id, domain).all_history_loaded;
|
||||
},
|
||||
|
||||
get_emojis: function() {
|
||||
|
||||
@@ -324,7 +324,8 @@ var ChatAction = Widget.extend(ControlPanelMixin, {
|
||||
fetch_and_render_thread: function () {
|
||||
var self = this;
|
||||
return chat_manager.fetch(this.channel_id, this.domain).then(function(result) {
|
||||
self.thread.render(result, {display_load_more: !chat_manager.all_history_loaded(self.channel_id)});
|
||||
var history_loaded = chat_manager.all_history_loaded(self.channel_id, self.domain);
|
||||
self.thread.render(result, {display_load_more: !history_loaded});
|
||||
});
|
||||
},
|
||||
|
||||
@@ -336,7 +337,8 @@ var ChatAction = Widget.extend(ControlPanelMixin, {
|
||||
return chat_manager
|
||||
.fetch_more(this.channel_id, this.domain)
|
||||
.then(function(result) {
|
||||
self.thread.render(result, {display_load_more: !chat_manager.all_history_loaded(self.channel_id)});
|
||||
var history_loaded = chat_manager.all_history_loaded(self.channel_id, self.domain);
|
||||
self.thread.render(result, {display_load_more: !history_loaded});
|
||||
offset += framework.getPosition(document.querySelector(oldest_msg_selector)).top;
|
||||
self.thread.scroll_to({offset: offset});
|
||||
});
|
||||
@@ -416,8 +418,8 @@ var ChatAction = Widget.extend(ControlPanelMixin, {
|
||||
if ((this.channel_id === "channel_starred" && !message.is_starred) ||
|
||||
(this.channel_id === "channel_inbox" && !message.is_needaction)) {
|
||||
chat_manager.fetch(this.channel_id, this.domain).then(function (messages) {
|
||||
var options = {display_load_more: !chat_manager.all_history_loaded(self.channel_id)};
|
||||
self.thread.remove_message_and_render(message.id, messages, options);
|
||||
var history_loaded = chat_manager.all_history_loaded(self.channel_id, self.domain);
|
||||
self.thread.remove_message_and_render(message.id, messages, {display_load_more: !history_loaded});
|
||||
});
|
||||
} else if (_.contains(message.channel_ids, this.channel_id)) {
|
||||
this.fetch_and_render_thread();
|
||||
|
||||
@@ -108,7 +108,10 @@ var Thread = Widget.extend({
|
||||
scroll_to: function (target) {
|
||||
target = target || {};
|
||||
if (target.id !== undefined) {
|
||||
this.$el.scrollTo(this.$('.o_thread_message[data-message-id=' + target.id + ']'));
|
||||
var $target = this.$('.o_thread_message[data-message-id=' + target.id + ']');
|
||||
if ($target.length) {
|
||||
this.$el.scrollTo($target);
|
||||
}
|
||||
} else if (target.offset !== undefined) {
|
||||
this.$el.scrollTop(target.offset);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user