Add ability for plugins to send custom messages

This commit is contained in:
Dmitriy Simushev 2013-04-29 11:04:17 +00:00
parent 00d668a74a
commit 3594485267
9 changed files with 129 additions and 22 deletions

View File

@ -5,6 +5,6 @@
Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php
*/
(function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){this.periodicallyCalled=[];this.periodicallyCalled.push(b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this)))},finalize:function(){for(var a=0;a<this.periodicallyCalled.length;a++)b.Objects.server.stopCallFunctionsPeriodically(this.periodicallyCalled[a])},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});
for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread,c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,
a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_);
(function(c,d,h){c.Collections.Messages=d.Collection.extend({model:c.Models.Message,initialize:function(){this.periodicallyCalled=[];this.periodicallyCalled.push(c.Objects.server.callFunctionsPeriodically(h.bind(this.updateMessagesFunctionBuilder,this),h.bind(this.updateMessages,this)))},finalize:function(){for(var a=0;a<this.periodicallyCalled.length;a++)c.Objects.server.stopCallFunctionsPeriodically(this.periodicallyCalled[a])},updateMessages:function(a){a.lastId&&c.Objects.Models.thread.set({lastId:a.lastId});
for(var j=c.Models.Message.prototype.KIND_PLUGIN,f=[],b,e,g=0,d=a.messages.length;g<d;g++)b=a.messages[g],b.kind!=j?f.push(new c.Models.Message(b)):"object"!=typeof b.message||null===b.message||(e=b.message.plugin||!1,e="process:"+(!1!==e?e+":":"")+"plugin:message",b={messageData:b,model:!1},this.trigger(e,b),b.model&&f.push(b.model));0<f.length&&this.add(f)},updateMessagesFunctionBuilder:function(){var a=c.Objects.Models.thread,d=c.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",
lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!d.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_);

View File

@ -141,9 +141,9 @@ groupId:a.get("groupId"),name:a.get("name"),info:a.get("info"),email:a.get("emai
Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php
*/
(function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){this.periodicallyCalled=[];this.periodicallyCalled.push(b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this)))},finalize:function(){for(var a=0;a<this.periodicallyCalled.length;a++)b.Objects.server.stopCallFunctionsPeriodically(this.periodicallyCalled[a])},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});
for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread,c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,
a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_);
(function(c,d,h){c.Collections.Messages=d.Collection.extend({model:c.Models.Message,initialize:function(){this.periodicallyCalled=[];this.periodicallyCalled.push(c.Objects.server.callFunctionsPeriodically(h.bind(this.updateMessagesFunctionBuilder,this),h.bind(this.updateMessages,this)))},finalize:function(){for(var a=0;a<this.periodicallyCalled.length;a++)c.Objects.server.stopCallFunctionsPeriodically(this.periodicallyCalled[a])},updateMessages:function(a){a.lastId&&c.Objects.Models.thread.set({lastId:a.lastId});
for(var j=c.Models.Message.prototype.KIND_PLUGIN,f=[],b,e,g=0,d=a.messages.length;g<d;g++)b=a.messages[g],b.kind!=j?f.push(new c.Models.Message(b)):"object"!=typeof b.message||null===b.message||(e=b.message.plugin||!1,e="process:"+(!1!==e?e+":":"")+"plugin:message",b={messageData:b,model:!1},this.trigger(e,b),b.model&&f.push(b.model));0<f.length&&this.add(f)},updateMessagesFunctionBuilder:function(){var a=c.Objects.Models.thread,d=c.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",
lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!d.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_);
/*
This file is part of Mibew Messenger project.
http://mibew.org

View File

@ -6,4 +6,4 @@
License: http://mibew.org/license.php
*/
(function(c,d,e){var f={"<":"&lt;",">":"&gt;","&":"&amp;",'"':"&quot;","'":"&#x27;","`":"&#x60;"},g=/[&<>'"`]/g;c.Views.Message=d.Marionette.ItemView.extend({template:e.templates.message,className:"message",modelEvents:{change:"render"},serializeData:function(){var a=this.model.toJSON(),b=this.model.get("kind");a.allowFormatting=b!=this.model.KIND_USER&&b!=this.model.KIND_AGENT;a.kindName=this.kindToString(b);a.message=this.escapeString(a.message);return a},kindToString:function(a){return a==this.model.KIND_USER?
"user":a==this.model.KIND_AGENT?"agent":a==this.model.KIND_FOR_AGENT?"hidden":a==this.model.KIND_INFO?"inf":a==this.model.KIND_CONN?"conn":a==this.model.KIND_EVENTS?"event":""},escapeString:function(a){return a.replace(g,function(a){return f[a]||"&amp;"})}})})(Mibew,Backbone,Handlebars);
"user":a==this.model.KIND_AGENT?"agent":a==this.model.KIND_FOR_AGENT?"hidden":a==this.model.KIND_INFO?"inf":a==this.model.KIND_CONN?"conn":a==this.model.KIND_EVENTS?"event":a==this.model.KIND_PLUGIN?"plugin":""},escapeString:function(a){return a.replace(g,function(a){return f[a]||"&amp;"})}})})(Mibew,Backbone,Handlebars);

View File

@ -5,4 +5,4 @@
Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php
*/
(function(a){a.Models.Message=a.Models.Base.extend({defaults:{kind:null,created:0,name:"",message:""},KIND_USER:1,KIND_AGENT:2,KIND_FOR_AGENT:3,KIND_INFO:4,KIND_CONN:5,KIND_EVENTS:6})})(Mibew);
(function(a){a.Models.Message=a.Models.Base.extend({defaults:{kind:null,created:0,name:"",message:""},KIND_USER:1,KIND_AGENT:2,KIND_FOR_AGENT:3,KIND_INFO:4,KIND_CONN:5,KIND_EVENTS:6,KIND_PLUGIN:7})})(Mibew);

View File

@ -73,7 +73,7 @@ b.Utils.playSound=function(a){c("body").append('<audio autoplay style="display:
Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php
*/
(function(a){a.Models.Message=a.Models.Base.extend({defaults:{kind:null,created:0,name:"",message:""},KIND_USER:1,KIND_AGENT:2,KIND_FOR_AGENT:3,KIND_INFO:4,KIND_CONN:5,KIND_EVENTS:6})})(Mibew);
(function(a){a.Models.Message=a.Models.Base.extend({defaults:{kind:null,created:0,name:"",message:""},KIND_USER:1,KIND_AGENT:2,KIND_FOR_AGENT:3,KIND_INFO:4,KIND_CONN:5,KIND_EVENTS:6,KIND_PLUGIN:7})})(Mibew);
/*
This file is part of Mibew Messenger project.
http://mibew.org
@ -117,7 +117,7 @@ this.$el.removeClass("active"+(a?"-"+a:""))},getDashedControlType:function(){"un
License: http://mibew.org/license.php
*/
(function(c,d,e){var f={"<":"&lt;",">":"&gt;","&":"&amp;",'"':"&quot;","'":"&#x27;","`":"&#x60;"},g=/[&<>'"`]/g;c.Views.Message=d.Marionette.ItemView.extend({template:e.templates.message,className:"message",modelEvents:{change:"render"},serializeData:function(){var a=this.model.toJSON(),b=this.model.get("kind");a.allowFormatting=b!=this.model.KIND_USER&&b!=this.model.KIND_AGENT;a.kindName=this.kindToString(b);a.message=this.escapeString(a.message);return a},kindToString:function(a){return a==this.model.KIND_USER?
"user":a==this.model.KIND_AGENT?"agent":a==this.model.KIND_FOR_AGENT?"hidden":a==this.model.KIND_INFO?"inf":a==this.model.KIND_CONN?"conn":a==this.model.KIND_EVENTS?"event":""},escapeString:function(a){return a.replace(g,function(a){return f[a]||"&amp;"})}})})(Mibew,Backbone,Handlebars);
"user":a==this.model.KIND_AGENT?"agent":a==this.model.KIND_FOR_AGENT?"hidden":a==this.model.KIND_INFO?"inf":a==this.model.KIND_CONN?"conn":a==this.model.KIND_EVENTS?"event":a==this.model.KIND_PLUGIN?"plugin":""},escapeString:function(a){return a.replace(g,function(a){return f[a]||"&amp;"})}})})(Mibew,Backbone,Handlebars);
/*
This file is part of Mibew Messenger project.
http://mibew.org

View File

@ -63,13 +63,69 @@
Mibew.Objects.Models.thread.set({lastId: args.lastId});
}
// Use shortcut for KIND_PLUGIN
var kindPlugin = Mibew.Models.Message.prototype.KIND_PLUGIN;
// Get all new messages
var newMessages = [];
var messageData, pluginName, eventName, eventArgs;
for(var i = 0, length = args.messages.length; i < length; i++) {
// Store message
newMessages.push(
new Mibew.Models.Message(args.messages[i])
);
messageData = args.messages[i];
if (messageData.kind != kindPlugin) {
// Message have one of the core kinds. Just store it.
newMessages.push(
new Mibew.Models.Message(messageData)
);
continue;
}
// Message have KIND_PLUGIN kind and need to be processed
// by plugins to know how to display it.
//
// Message treat as data object with following fields:
// - 'plugin': string, name of the plugin which sent the
// message;
// - 'data': object, some data sent by the plugin.
// Check if message is an real Object
if ((typeof messageData.message != 'object')
|| (messageData.message === null)) {
continue;
}
// Prepare event name.
//
// If plugin name was specified it will be
// 'process:<plugin_name>:plugin:message' and
// 'process:plugin:message' otherwise.
pluginName = messageData.message.plugin || false;
eventName = 'process:'
+ ((pluginName !== false) ? pluginName + ':' : '')
+ 'plugin:message';
// Prepare event arguments.
//
// It is an object with following fields:
// - 'messageData': object which contains message data
// passed from server.
// - 'model': message model initialized by the plugin or
// boolean false if message should not be displayed. By
// default it field equals to boolean false.
eventArgs = {
'messageData': messageData,
'model': false
}
// Trigger event. See description of eventName and eventArgs
// above.
this.trigger(eventName, eventArgs);
if (eventArgs.model) {
// Store custom plugin message in the collection
newMessages.push(
eventArgs.model
);
}
}
// Add new messages to the message collection if there are any

View File

@ -97,6 +97,9 @@
if (kind == this.model.KIND_EVENTS) {
return "event";
}
if (kind == this.model.KIND_PLUGIN) {
return "plugin";
}
return "";
},

View File

@ -61,7 +61,10 @@
KIND_CONN: 5,
/** System message about some events (like rename). */
KIND_EVENTS: 6
KIND_EVENTS: 6,
/** Message sent by a plugin */
KIND_PLUGIN: 7
/** End of message kind constants */
}

View File

@ -86,6 +86,10 @@ Class Thread {
* System message about some events (like rename).
*/
const KIND_EVENTS = 6;
/**
* Message sent by a plugin.
*/
const KIND_PLUGIN = 7;
/**
* Messaging window connection timeout.
@ -658,7 +662,8 @@ Class Thread {
* - 'kind': int, message kind, see Thread::KIND_* for details;
* - 'created': int, unix timestamp when message was created;
* - 'name': string, name of sender;
* - 'message': string, message text.
* - 'message': associative array with message data if kind equals to
* Thread::KIND_PLUGIN and message text string otherwise.
* @see Thread::postMessage()
*/
public function getMessages($is_user, &$last_id) {
@ -668,7 +673,8 @@ Class Thread {
// Load messages
$messages = $db->query(
"select messageid as id, ikind as kind, dtmcreated as created, tname as name, tmessage as message " .
"select messageid as id, ikind as kind, dtmcreated as created, " .
" tname as name, tmessage as message " .
"from {chatmessage} " .
"where threadid = :threadid and messageid > :lastid " .
($is_user ? "and ikind <> " . self::KIND_FOR_AGENT : "") .
@ -681,9 +687,28 @@ Class Thread {
);
foreach ($messages as $key => $msg) {
// Change message fields encoding
$messages[$key]['name'] = myiconv($webim_encoding, "utf-8", $msg['name']);
$messages[$key]['message'] = myiconv($webim_encoding, "utf-8", $msg['message']);
// Change sender name encoding
$messages[$key]['name'] = myiconv(
$webim_encoding,
"utf-8",
$msg['name']
);
// Process message body
if ($messages[$key]['kind'] == self::KIND_PLUGIN) {
// Treat plugin message body as an associative array
$messages[$key]['message'] = unserialize(
$messages[$key]['message']
);
} else {
// Change message body encoding for core messages kinds
$messages[$key]['message'] = myiconv(
$webim_encoding,
"utf-8",
$msg['message']
);
}
// Get last message ID
if ($msg['id'] > $last_id) {
$last_id = $msg['id'];
@ -696,11 +721,24 @@ Class Thread {
/**
* Send the messsage
*
* Message body, passed as $message argument can be either string or
* associative array. It depends on $kind argument value.
* If $kind equals to Thread:KIND_PLUGIN $message must be an associative
* array with following keys:
* - 'plugin': string, name of the plugin which send the message;
* - 'data': associative array, arbitrary message data.
* Message body in this case will automatically serialized and unserialized
* then it needed.
*
* On the other hand, if $kind argument NOT equals to Thread::KIND_PLUGIN
* $message must be a string, representing message text.
*
* @param int $kind Message kind. One of the Thread::KIND_*
* @param string $message Message body
* @param string|array $message Message body
* @param string|null $from Sender name
* @param int|null $opid operator id. Use NULL for system messages
* @param int|null $time unix timestamp of the send time. Use NULL for current time.
* @param int|null $time unix timestamp of the send time. Use NULL for
* current time.
* @return int Message ID
*
* @see Thread::KIND_USER
@ -709,6 +747,7 @@ Class Thread {
* @see Thread::KIND_INFO
* @see Thread::KIND_CONN
* @see Thread::KIND_EVENTS
* @see Thread::KIND_PLUGIN
* @see Thread::getMessages()
*/
public function postMessage($kind, $message, $from = null, $opid = null, $time = null) {
@ -718,6 +757,12 @@ Class Thread {
"(threadid,ikind,tmessage,tname,agentId,dtmcreated) " .
"VALUES (:threadid,:kind,:message,:name,:agentid,:created)";
// Serialize message body for messages with kind equals to
// Thread::KIND_PLUGIN
if ($kind == self::KIND_PLUGIN) {
$message = serialize($message);
}
$values = array(
':threadid' => $this->id,
':kind' => $kind,