2013-08-19 14:09:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
This file must compile in EcmaScript 3 and work in IE7.
|
|
|
|
|
|
|
|
Prerequisites to use this module:
|
|
|
|
- load the im_common.xml qweb template into openerp.qweb
|
|
|
|
- implement all the stuff defined later
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
function declare($, _, openerp) {
|
|
|
|
/* jshint es3: true */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var im_common = {};
|
|
|
|
|
|
|
|
/*
|
|
|
|
All of this must be defined to use this module
|
|
|
|
*/
|
|
|
|
_.extend(im_common, {
|
|
|
|
notification: function(message) {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
},
|
|
|
|
connection: null
|
|
|
|
});
|
|
|
|
|
|
|
|
var _t = openerp._t;
|
|
|
|
|
|
|
|
var ERROR_DELAY = 5000;
|
|
|
|
|
|
|
|
im_common.ImUser = openerp.Class.extend(openerp.PropertiesMixin, {
|
|
|
|
init: function(parent, user_rec) {
|
|
|
|
openerp.PropertiesMixin.init.call(this, parent);
|
2013-08-26 08:52:12 +00:00
|
|
|
|
|
|
|
user_rec.image_url = im_common.connection.url('/web/binary/image', {model:'im.user', field: 'image', id: user_rec.id});
|
2013-08-20 14:15:38 +00:00
|
|
|
|
2013-08-19 14:09:08 +00:00
|
|
|
this.set(user_rec);
|
|
|
|
this.set("watcher_count", 0);
|
|
|
|
this.on("change:watcher_count", this, function() {
|
|
|
|
if (this.get("watcher_count") === 0)
|
|
|
|
this.destroy();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
destroy: function() {
|
|
|
|
this.trigger("destroyed");
|
|
|
|
openerp.PropertiesMixin.destroy.call(this);
|
|
|
|
},
|
|
|
|
add_watcher: function() {
|
|
|
|
this.set("watcher_count", this.get("watcher_count") + 1);
|
|
|
|
},
|
|
|
|
remove_watcher: function() {
|
|
|
|
this.set("watcher_count", this.get("watcher_count") - 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
im_common.ConversationManager = openerp.Class.extend(openerp.PropertiesMixin, {
|
2013-08-20 12:15:22 +00:00
|
|
|
init: function(parent, options) {
|
2013-08-19 14:09:08 +00:00
|
|
|
openerp.PropertiesMixin.init.call(this, parent);
|
2013-08-20 14:15:38 +00:00
|
|
|
this.options = _.clone(options) || {};
|
|
|
|
_.defaults(this.options, {
|
|
|
|
inputPlaceholder: _t("Say something..."),
|
|
|
|
defaultMessage: null,
|
|
|
|
userName: _t("Anonymous"),
|
|
|
|
anonymous_mode: false
|
|
|
|
});
|
2013-08-19 14:09:08 +00:00
|
|
|
this.set("right_offset", 0);
|
2013-08-20 15:06:07 +00:00
|
|
|
this.set("bottom_offset", 0);
|
2013-08-19 14:09:08 +00:00
|
|
|
this.conversations = [];
|
|
|
|
this.on("change:right_offset", this, this.calc_positions);
|
2013-08-20 15:06:07 +00:00
|
|
|
this.on("change:bottom_offset", this, this.calc_positions);
|
2013-08-19 14:09:08 +00:00
|
|
|
this.set("window_focus", true);
|
|
|
|
this.set("waiting_messages", 0);
|
|
|
|
this.focus_hdl = _.bind(function() {
|
|
|
|
this.set("window_focus", true);
|
|
|
|
}, this);
|
|
|
|
$(window).bind("focus", this.focus_hdl);
|
|
|
|
this.blur_hdl = _.bind(function() {
|
|
|
|
this.set("window_focus", false);
|
|
|
|
}, this);
|
|
|
|
$(window).bind("blur", this.blur_hdl);
|
|
|
|
this.on("change:window_focus", this, this.window_focus_change);
|
|
|
|
this.window_focus_change();
|
|
|
|
this.on("change:waiting_messages", this, this.messages_change);
|
|
|
|
this.messages_change();
|
|
|
|
this.create_ting();
|
|
|
|
this.activated = false;
|
|
|
|
this.users_cache = {};
|
|
|
|
this.last = null;
|
|
|
|
this.unload_event_handler = _.bind(this.unload, this);
|
|
|
|
},
|
|
|
|
start_polling: function() {
|
|
|
|
var self = this;
|
2013-08-26 12:39:47 +00:00
|
|
|
var def = $.when();
|
|
|
|
var uuid = false;
|
2013-08-19 14:09:08 +00:00
|
|
|
|
2013-08-20 14:15:38 +00:00
|
|
|
if (this.options.anonymous_mode) {
|
2013-08-26 12:39:47 +00:00
|
|
|
uuid = localStorage["oe_livesupport_uuid"] || false;
|
2013-08-20 14:15:38 +00:00
|
|
|
|
|
|
|
if (! uuid) {
|
2013-08-26 12:39:47 +00:00
|
|
|
def = im_common.connection.rpc("/longpolling/im/gen_uuid", {}).then(function(my_uuid) {
|
|
|
|
uuid = my_uuid;
|
|
|
|
localStorage["oe_livesupport_uuid"] = uuid;
|
|
|
|
});
|
2013-08-20 14:15:38 +00:00
|
|
|
}
|
2013-08-26 12:52:54 +00:00
|
|
|
def = def.then(function() {
|
|
|
|
return im_common.connection.model("im.user").call("assign_name", [uuid, self.options.userName]);
|
|
|
|
});
|
2013-08-19 14:09:08 +00:00
|
|
|
}
|
2013-08-20 14:15:38 +00:00
|
|
|
|
2013-08-26 12:39:47 +00:00
|
|
|
return def.then(function() {
|
|
|
|
return im_common.connection.model("im.user").call("get_my_id", [uuid]);
|
|
|
|
}).then(function(my_user_id) {
|
|
|
|
self.my_id = my_user_id;
|
2013-08-19 14:09:08 +00:00
|
|
|
return self.ensure_users([self.my_id]);
|
|
|
|
}).then(function() {
|
|
|
|
var me = self.users_cache[self.my_id];
|
|
|
|
delete self.users_cache[self.my_id];
|
|
|
|
self.me = me;
|
2013-08-20 14:15:38 +00:00
|
|
|
me.set("name", _t("You"));
|
|
|
|
return im_common.connection.rpc("/longpolling/im/activated", {}, {shadow: true});
|
2013-08-19 14:09:08 +00:00
|
|
|
}).then(function(activated) {
|
|
|
|
if (activated) {
|
|
|
|
self.activated = true;
|
|
|
|
$(window).on("unload", self.unload_event_handler);
|
|
|
|
self.poll();
|
|
|
|
} else {
|
|
|
|
return $.Deferred().reject();
|
|
|
|
}
|
2013-08-20 14:15:38 +00:00
|
|
|
}, function(a, e) {
|
|
|
|
e.preventDefault();
|
2013-08-19 14:09:08 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
unload: function() {
|
2013-08-20 14:15:38 +00:00
|
|
|
return im_common.connection.model("im.user").call("im_disconnect", [], {uuid: this.me.get("uuid"), context: {}});
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
|
|
|
ensure_users: function(user_ids) {
|
|
|
|
var no_cache = {};
|
|
|
|
_.each(user_ids, function(el) {
|
|
|
|
if (! this.users_cache[el])
|
|
|
|
no_cache[el] = el;
|
|
|
|
}, this);
|
|
|
|
var self = this;
|
2013-09-02 17:29:23 +00:00
|
|
|
var def;
|
2013-08-19 14:09:08 +00:00
|
|
|
if (_.size(no_cache) === 0)
|
2013-09-02 17:29:23 +00:00
|
|
|
def = $.when();
|
2013-08-19 14:09:08 +00:00
|
|
|
else
|
2013-09-02 17:29:23 +00:00
|
|
|
def = im_common.connection.model("im.user").call("read", [_.values(no_cache), []]).then(function(users) {
|
2013-08-19 14:09:08 +00:00
|
|
|
self.add_to_user_cache(users);
|
|
|
|
});
|
2013-09-02 17:29:23 +00:00
|
|
|
return def.then(function() {
|
|
|
|
return _.map(user_ids, function(id) { return self.get_user(id); });
|
|
|
|
});
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
|
|
|
add_to_user_cache: function(user_recs) {
|
|
|
|
_.each(user_recs, function(user_rec) {
|
|
|
|
if (! this.users_cache[user_rec.id]) {
|
|
|
|
var user = new im_common.ImUser(this, user_rec);
|
|
|
|
this.users_cache[user_rec.id] = user;
|
|
|
|
user.on("destroyed", this, function() {
|
|
|
|
delete this.users_cache[user_rec.id];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
get_user: function(user_id) {
|
|
|
|
return this.users_cache[user_id];
|
|
|
|
},
|
|
|
|
poll: function() {
|
|
|
|
var self = this;
|
|
|
|
var user_ids = _.map(this.users_cache, function(el) {
|
|
|
|
return el.get("id");
|
|
|
|
});
|
|
|
|
im_common.connection.rpc("/longpolling/im/poll", {
|
|
|
|
last: this.last,
|
|
|
|
users_watch: user_ids,
|
|
|
|
uuid: self.me.get("uuid")
|
2013-08-20 14:15:38 +00:00
|
|
|
}, {shadow: true}).then(function(result) {
|
2013-08-19 14:09:08 +00:00
|
|
|
_.each(result.users_status, function(el) {
|
|
|
|
if (self.get_user(el.id))
|
|
|
|
self.get_user(el.id).set(el);
|
|
|
|
});
|
|
|
|
self.last = result.last;
|
2013-09-02 09:31:04 +00:00
|
|
|
self.received_messages(result.res).then(function() {
|
2013-08-19 14:09:08 +00:00
|
|
|
self.poll();
|
|
|
|
});
|
2013-08-20 14:15:38 +00:00
|
|
|
}, function(unused, e) {
|
|
|
|
e.preventDefault();
|
2013-08-19 14:09:08 +00:00
|
|
|
setTimeout(_.bind(self.poll, self), ERROR_DELAY);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
get_activated: function() {
|
|
|
|
return this.activated;
|
|
|
|
},
|
|
|
|
create_ting: function() {
|
|
|
|
if (typeof(Audio) === "undefined") {
|
|
|
|
this.ting = {play: function() {}};
|
|
|
|
return;
|
|
|
|
}
|
2013-08-20 14:15:38 +00:00
|
|
|
var kitten = jQuery.deparam !== undefined && jQuery.deparam(jQuery.param.querystring()).kitten !== undefined;
|
2013-08-20 14:48:43 +00:00
|
|
|
this.ting = new Audio(im_common.connection.url(
|
|
|
|
"/im/static/src/audio/" +
|
2013-08-20 14:15:38 +00:00
|
|
|
(kitten ? "purr" : "Ting") +
|
|
|
|
(new Audio().canPlayType("audio/ogg; codecs=vorbis") ? ".ogg": ".mp3")
|
|
|
|
));
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
|
|
|
window_focus_change: function() {
|
|
|
|
if (this.get("window_focus")) {
|
|
|
|
this.set("waiting_messages", 0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
messages_change: function() {
|
2013-08-20 14:15:38 +00:00
|
|
|
if (! openerp.webclient || !openerp.webclient.set_title_part)
|
|
|
|
return;
|
|
|
|
openerp.webclient.set_title_part("im_messages", this.get("waiting_messages") === 0 ? undefined :
|
|
|
|
_.str.sprintf(_t("%d Messages"), this.get("waiting_messages")));
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
2013-09-02 17:29:23 +00:00
|
|
|
chat_with_users: function(users) {
|
|
|
|
var self = this;
|
|
|
|
return im_common.connection.model("im.session").call("session_get", [_.map(users, function(user) {return user.get("id");}),
|
|
|
|
self.me.get("uuid")]).then(function(session) {
|
|
|
|
return self.activate_session(session.id, true);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
chat_with_all_users: function() {
|
|
|
|
var self = this;
|
|
|
|
return im_common.connection.model("im.user").call("search", [[["uuid", "=", false]]]).then(function(user_ids) {
|
|
|
|
return self.ensure_users(_.without(user_ids, self.me.get("id")));
|
|
|
|
}).then(function(users) {
|
|
|
|
return self.chat_with_users(users);
|
|
|
|
});
|
|
|
|
},
|
2013-09-02 09:31:04 +00:00
|
|
|
activate_session: function(session_id, focus) {
|
|
|
|
var conv = _.find(this.conversations, function(conv) {return conv.session_id == session_id;});
|
|
|
|
var def = $.when();
|
2013-08-19 14:09:08 +00:00
|
|
|
if (! conv) {
|
2013-09-02 09:31:04 +00:00
|
|
|
conv = new im_common.Conversation(this, this, session_id, this.options);
|
|
|
|
def = conv.appendTo($("body")).then(_.bind(function() {
|
|
|
|
conv.on("destroyed", this, function() {
|
|
|
|
this.conversations = _.without(this.conversations, conv);
|
|
|
|
this.calc_positions();
|
|
|
|
});
|
|
|
|
this.conversations.push(conv);
|
2013-08-19 14:09:08 +00:00
|
|
|
this.calc_positions();
|
2013-09-02 15:14:04 +00:00
|
|
|
this.trigger("new_conversation", conv);
|
2013-09-02 09:31:04 +00:00
|
|
|
}, this));
|
|
|
|
}
|
|
|
|
if (focus) {
|
|
|
|
def = def.then(function() {
|
|
|
|
conv.focus();
|
2013-08-19 14:09:08 +00:00
|
|
|
});
|
|
|
|
}
|
2013-09-02 09:31:04 +00:00
|
|
|
return def.then(function() {return conv});
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
2013-09-02 09:31:04 +00:00
|
|
|
received_messages: function(messages) {
|
|
|
|
var self = this;
|
|
|
|
var defs = [];
|
2013-09-04 09:57:46 +00:00
|
|
|
var received = false;
|
2013-09-02 09:31:04 +00:00
|
|
|
_.each(messages, function(message) {
|
2013-09-02 17:02:52 +00:00
|
|
|
if (! message.technical) {
|
|
|
|
defs.push(self.activate_session(message.session_id[0]).then(function(conv) {
|
2014-01-06 13:50:56 +00:00
|
|
|
received = self.my_id[0] !== message.from_id[0];
|
2013-09-02 17:02:52 +00:00
|
|
|
return conv.received_message(message);
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
var json = JSON.parse(message.message);
|
2013-09-04 09:57:46 +00:00
|
|
|
message.json = json;
|
2013-09-02 17:02:52 +00:00
|
|
|
defs.push($.when(im_common.technical_messages_handlers[json.type](self, message)));
|
|
|
|
}
|
2013-09-02 09:31:04 +00:00
|
|
|
});
|
2014-01-06 13:50:56 +00:00
|
|
|
return $.when.apply($, defs).then(function(){
|
|
|
|
if (! self.get("window_focus") && received) {
|
|
|
|
self.set("waiting_messages", self.get("waiting_messages") + messages.length);
|
|
|
|
self.ting.play();
|
|
|
|
self.create_ting();
|
|
|
|
}
|
|
|
|
});
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
|
|
|
calc_positions: function() {
|
|
|
|
var current = this.get("right_offset");
|
|
|
|
_.each(_.range(this.conversations.length), function(i) {
|
2013-08-20 15:06:07 +00:00
|
|
|
this.conversations[i].set("bottom_position", this.get("bottom_offset"));
|
2013-08-19 14:09:08 +00:00
|
|
|
this.conversations[i].set("right_position", current);
|
|
|
|
current += this.conversations[i].$().outerWidth(true);
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
destroy: function() {
|
|
|
|
$(window).off("unload", this.unload_event_handler);
|
|
|
|
$(window).unbind("blur", this.blur_hdl);
|
|
|
|
$(window).unbind("focus", this.focus_hdl);
|
|
|
|
openerp.PropertiesMixin.destroy.call(this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
im_common.Conversation = openerp.Widget.extend({
|
|
|
|
className: "openerp_style oe_im_chatview",
|
|
|
|
events: {
|
2013-09-02 17:02:52 +00:00
|
|
|
"keydown input": "keydown",
|
2013-09-04 13:17:53 +00:00
|
|
|
"click .oe_im_chatview_close": "close",
|
2013-08-19 14:09:08 +00:00
|
|
|
"click .oe_im_chatview_header": "show_hide"
|
|
|
|
},
|
2013-09-02 09:31:04 +00:00
|
|
|
init: function(parent, c_manager, session_id, options) {
|
2013-08-19 14:09:08 +00:00
|
|
|
this._super(parent);
|
2013-09-02 09:31:04 +00:00
|
|
|
this.c_manager = c_manager;
|
|
|
|
this.options = options || {};
|
|
|
|
this.session_id = session_id;
|
2013-08-19 14:09:08 +00:00
|
|
|
this.set("right_position", 0);
|
2013-08-20 15:06:07 +00:00
|
|
|
this.set("bottom_position", 0);
|
2013-08-19 14:09:08 +00:00
|
|
|
this.shown = true;
|
|
|
|
this.set("pending", 0);
|
2013-08-20 12:15:22 +00:00
|
|
|
this.inputPlaceholder = this.options.defaultInputPlaceholder;
|
2013-09-02 16:08:11 +00:00
|
|
|
this.set("users", []);
|
|
|
|
this.set("disconnected", false);
|
2013-09-02 09:31:04 +00:00
|
|
|
this.others = [];
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
|
|
|
start: function() {
|
2013-09-02 09:31:04 +00:00
|
|
|
var self = this;
|
2013-09-02 16:08:11 +00:00
|
|
|
|
|
|
|
self.$().append(openerp.qweb.render("im_common.conversation", {widget: self}));
|
2013-09-02 17:02:52 +00:00
|
|
|
this.$().hide();
|
2013-09-02 16:08:11 +00:00
|
|
|
|
|
|
|
var change_status = function() {
|
|
|
|
var disconnected = _.every(this.get("users"), function(u) { return u.get("im_status") === false; });
|
|
|
|
self.set("disconnected", disconnected);
|
|
|
|
this.$(".oe_im_chatview_users").html(openerp.qweb.render("im_common.conversation.header",
|
|
|
|
{widget: self, to_url: _.bind(im_common.connection.url, im_common.connection)}));
|
|
|
|
};
|
|
|
|
this.on("change:users", this, function(unused, ev) {
|
|
|
|
_.each(ev.oldValue, function(user) {
|
|
|
|
user.off("change:im_status", self, change_status);
|
|
|
|
});
|
|
|
|
_.each(ev.newValue, function(user) {
|
|
|
|
user.on("change:im_status", self, change_status);
|
|
|
|
});
|
|
|
|
change_status.call(self);
|
2013-09-02 17:02:52 +00:00
|
|
|
_.each(ev.oldValue, function(user) {
|
|
|
|
if (! _.contains(ev.newValue, user)) {
|
|
|
|
user.remove_watcher();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_.each(ev.newValue, function(user) {
|
|
|
|
if (! _.contains(ev.oldValue, user)) {
|
|
|
|
user.add_watcher();
|
|
|
|
}
|
|
|
|
});
|
2013-09-02 16:08:11 +00:00
|
|
|
});
|
|
|
|
this.on("change:disconnected", this, function() {
|
|
|
|
self.$().toggleClass("oe_im_chatview_disconnected_status", this.get("disconnected"));
|
|
|
|
self._go_bottom();
|
|
|
|
});
|
|
|
|
|
2013-09-02 17:02:52 +00:00
|
|
|
self.on("change:right_position", self, self.calc_pos);
|
|
|
|
self.on("change:bottom_position", self, self.calc_pos);
|
|
|
|
self.full_height = self.$().height();
|
|
|
|
self.calc_pos();
|
|
|
|
self.on("change:pending", self, _.bind(function() {
|
|
|
|
if (self.get("pending") === 0) {
|
|
|
|
self.$(".oe_im_chatview_nbr_messages").text("");
|
|
|
|
} else {
|
|
|
|
self.$(".oe_im_chatview_nbr_messages").text("(" + self.get("pending") + ")");
|
|
|
|
}
|
|
|
|
}, self));
|
|
|
|
|
|
|
|
return this.refresh_users().then(function() {
|
|
|
|
self.$().show();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
refresh_users: function() {
|
|
|
|
var self = this;
|
2013-09-02 09:31:04 +00:00
|
|
|
var user_ids;
|
|
|
|
return im_common.connection.model("im.session").call("read", [self.session_id]).then(function(session) {
|
|
|
|
user_ids = _.without(session.user_ids, self.c_manager.me.get("id"));
|
2013-09-02 17:29:23 +00:00
|
|
|
return self.c_manager.ensure_users(user_ids);
|
|
|
|
}).then(function(users) {
|
2013-09-02 16:08:11 +00:00
|
|
|
self.set("users", users);
|
2013-09-02 09:31:04 +00:00
|
|
|
});
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
|
|
|
show_hide: function() {
|
|
|
|
if (this.shown) {
|
|
|
|
this.$().animate({
|
|
|
|
height: this.$(".oe_im_chatview_header").outerHeight()
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.$().animate({
|
|
|
|
height: this.full_height
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.shown = ! this.shown;
|
|
|
|
if (this.shown) {
|
|
|
|
this.set("pending", 0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
calc_pos: function() {
|
|
|
|
this.$().css("right", this.get("right_position"));
|
2013-08-20 15:06:07 +00:00
|
|
|
this.$().css("bottom", this.get("bottom_position"));
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
|
|
|
received_message: function(message) {
|
|
|
|
if (this.shown) {
|
|
|
|
this.set("pending", 0);
|
|
|
|
} else {
|
|
|
|
this.set("pending", this.get("pending") + 1);
|
|
|
|
}
|
2013-09-02 17:29:23 +00:00
|
|
|
this.c_manager.ensure_users([message.from_id[0]]).then(_.bind(function(users) {
|
|
|
|
var user = users[0];
|
2013-09-02 16:08:11 +00:00
|
|
|
if (! _.contains(this.get("users"), user) && ! _.contains(this.others, user)) {
|
2013-09-02 09:31:04 +00:00
|
|
|
this.others.push(user);
|
|
|
|
user.add_watcher();
|
|
|
|
}
|
|
|
|
this._add_bubble(user, message.message, openerp.str_to_datetime(message.date));
|
|
|
|
}, this));
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
2013-09-02 17:02:52 +00:00
|
|
|
keydown: function(e) {
|
2013-08-19 14:09:08 +00:00
|
|
|
if(e && e.which !== 13) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var mes = this.$("input").val();
|
|
|
|
if (! mes.trim()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.$("input").val("");
|
2013-09-02 17:02:52 +00:00
|
|
|
this.send_message(mes);
|
|
|
|
},
|
|
|
|
send_message: function(message, technical) {
|
|
|
|
technical = technical || false;
|
2013-08-19 14:09:08 +00:00
|
|
|
var send_it = _.bind(function() {
|
|
|
|
var model = im_common.connection.model("im.message");
|
2013-09-02 17:02:52 +00:00
|
|
|
return model.call("post", [message, this.session_id, technical], {uuid: this.c_manager.me.get("uuid"), context: {}});
|
2013-08-19 14:09:08 +00:00
|
|
|
}, this);
|
|
|
|
var tries = 0;
|
2013-09-02 09:31:04 +00:00
|
|
|
send_it().then(_.bind(function() {}, function(error, e) {
|
2013-08-20 14:15:38 +00:00
|
|
|
e.preventDefault();
|
2013-08-19 14:09:08 +00:00
|
|
|
tries += 1;
|
|
|
|
if (tries < 3)
|
|
|
|
return send_it();
|
2013-09-02 09:31:04 +00:00
|
|
|
}));
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
|
|
|
_add_bubble: function(user, item, date) {
|
|
|
|
var items = [item];
|
|
|
|
if (user === this.last_user) {
|
|
|
|
this.last_bubble.remove();
|
|
|
|
items = this.last_items.concat(items);
|
|
|
|
}
|
|
|
|
this.last_user = user;
|
|
|
|
this.last_items = items;
|
|
|
|
var zpad = function(str, size) {
|
|
|
|
str = "" + str;
|
|
|
|
return new Array(size - str.length + 1).join('0') + str;
|
|
|
|
};
|
|
|
|
date = "" + zpad(date.getHours(), 2) + ":" + zpad(date.getMinutes(), 2);
|
2013-09-03 13:59:40 +00:00
|
|
|
var to_show = _.map(items, im_common.escape_keep_url);
|
|
|
|
this.last_bubble = $(openerp.qweb.render("im_common.conversation_bubble", {"items": to_show, "user": user, "time": date}));
|
2013-08-19 14:09:08 +00:00
|
|
|
$(this.$(".oe_im_chatview_content").children()[0]).append(this.last_bubble);
|
|
|
|
this._go_bottom();
|
|
|
|
},
|
|
|
|
_go_bottom: function() {
|
|
|
|
this.$(".oe_im_chatview_content").scrollTop($(this.$(".oe_im_chatview_content").children()[0]).height());
|
|
|
|
},
|
2013-09-02 15:14:04 +00:00
|
|
|
add_user: function(user) {
|
2013-09-02 16:08:11 +00:00
|
|
|
if (user === this.me || _.contains(this.get("users"), user))
|
2013-09-02 15:14:04 +00:00
|
|
|
return;
|
|
|
|
im_common.connection.model("im.session").call("add_to_session",
|
|
|
|
[this.session_id, user.get("id"), this.c_manager.me.get("uuid")]).then(_.bind(function() {
|
2013-09-04 09:57:46 +00:00
|
|
|
this.send_message(JSON.stringify({"type": "session_modified", "action": "added", "user_id": user.get("id")}), true);
|
2013-09-02 15:14:04 +00:00
|
|
|
}, this));
|
|
|
|
},
|
2013-08-19 14:09:08 +00:00
|
|
|
focus: function() {
|
|
|
|
this.$(".oe_im_chatview_input").focus();
|
2013-08-20 14:15:38 +00:00
|
|
|
if (! this.shown)
|
|
|
|
this.show_hide();
|
2013-08-19 14:09:08 +00:00
|
|
|
},
|
2013-09-04 13:17:53 +00:00
|
|
|
close: function() {
|
|
|
|
var def = $.when();
|
|
|
|
if (this.get("users").length > 1) {
|
|
|
|
def = im_common.connection.model("im.session").call("remove_me_from_session",
|
|
|
|
[this.session_id, this.c_manager.me.get("uuid")]).then(_.bind(function() {
|
|
|
|
return this.send_message(JSON.stringify({"type": "session_modified", "action": "removed",
|
|
|
|
"user_id": this.c_manager.me.get("id")}), true)
|
|
|
|
}, this))
|
|
|
|
}
|
|
|
|
|
|
|
|
return def.then(_.bind(function() {
|
|
|
|
this.destroy();
|
|
|
|
}, this));
|
|
|
|
},
|
2013-08-19 14:09:08 +00:00
|
|
|
destroy: function() {
|
2013-09-02 16:08:11 +00:00
|
|
|
_.each(this.get("users"), function(user) {
|
2013-09-02 09:31:04 +00:00
|
|
|
user.remove_watcher();
|
|
|
|
})
|
|
|
|
_.each(this.others, function(user) {
|
|
|
|
user.remove_watcher();
|
|
|
|
})
|
2013-08-19 14:09:08 +00:00
|
|
|
this.trigger("destroyed");
|
|
|
|
return this._super();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-09-02 17:02:52 +00:00
|
|
|
im_common.technical_messages_handlers = {};
|
|
|
|
|
|
|
|
im_common.technical_messages_handlers.session_modified = function(c_manager, message) {
|
2013-09-04 09:57:46 +00:00
|
|
|
var def = $.when();
|
|
|
|
if (message.json.action === "added" && message.json.user_id === c_manager.me.get("id")) {
|
|
|
|
def = c_manager.activate_session(message.session_id[0], true);
|
|
|
|
}
|
|
|
|
return def.then(function() {
|
|
|
|
var conv = _.find(c_manager.conversations, function(conv) {return conv.session_id == message.session_id[0];});
|
|
|
|
if (conv)
|
|
|
|
return conv.refresh_users();
|
|
|
|
return undefined;
|
2013-09-02 17:02:52 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-03 13:59:40 +00:00
|
|
|
var url_regex = /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/gi;
|
|
|
|
|
|
|
|
im_common.escape_keep_url = function(str) {
|
|
|
|
var last = 0;
|
|
|
|
var txt = "";
|
|
|
|
while (true) {
|
|
|
|
var result = url_regex.exec(str);
|
|
|
|
if (! result)
|
|
|
|
break;
|
|
|
|
txt += _.escape(str.slice(last, result.index));
|
|
|
|
last = url_regex.lastIndex;
|
|
|
|
var url = _.escape(result[0]);
|
|
|
|
txt += '<a href="' + url + '">' + url + '</a>';
|
|
|
|
}
|
2013-09-11 16:34:52 +00:00
|
|
|
txt += _.escape(str.slice(last, str.length));
|
2013-09-03 13:59:40 +00:00
|
|
|
return txt;
|
|
|
|
};
|
|
|
|
|
2013-08-19 14:09:08 +00:00
|
|
|
return im_common;
|
|
|
|
}
|
|
|
|
|
2013-08-20 14:15:38 +00:00
|
|
|
if (typeof(define) !== "undefined") {
|
2013-08-19 14:09:08 +00:00
|
|
|
define(["jquery", "underscore", "openerp"], declare);
|
|
|
|
} else {
|
|
|
|
window.im_common = declare($, _, openerp);
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|