From 11836c05081948efcbc4702809ad0bca3d2d14f8 Mon Sep 17 00:00:00 2001 From: Afonso Oliveira Date: Thu, 30 Nov 2017 16:01:42 +0000 Subject: [PATCH] chat controller with reactions (just add, no remove) --- examples/group-chat-manager/demo.js | 110 ++++++++++++++++-- src/group-chat-manager/ChatController.js | 74 ++++++++++-- .../GroupChatManager.hy.json | 2 +- 3 files changed, 167 insertions(+), 19 deletions(-) mode change 100755 => 100644 examples/group-chat-manager/demo.js diff --git a/examples/group-chat-manager/demo.js b/examples/group-chat-manager/demo.js old mode 100755 new mode 100644 index 6940b95..6794072 --- a/examples/group-chat-manager/demo.js +++ b/examples/group-chat-manager/demo.js @@ -40,6 +40,8 @@ function hypertyReady(result, identity) { $('.join-room-btn').hide();*/ chatGroupManager = result.instance; + chatGroupManager.identity = identity; + chatGroupManager.messages = {}; chatGroupManager.onInvitation((event) => { onInvitation(event); }); @@ -316,15 +318,29 @@ function prepareChat(chatController, isOwner) { Object.keys(msgs).forEach(function(objectKey, index) { var msg = msgs[objectKey]; console.log('ProcessMessage: ', msg); - processMessage({ - value: msgs[objectKey].data, - identity: msgs[objectKey].identity - }); + if(msgs[objectKey].data.type=='chat'){ + processMessage({ + childId: msgs[objectKey]._url, + value: msgs[objectKey].data, + identity: msgs[objectKey].identity + },chatController); + }else if(msgs[objectKey].data.type=='chatobject'){ + processReaction({ + childId: msgs[objectKey]._url, + value: msgs[objectKey].data, + identity: msgs[objectKey].identity + }); + } }); chatController.onMessage(function(message) { console.info('[GroupChatManagerDemo ] new message received: ', message); - processMessage(message); + processMessage(message,chatController); + }); + + chatController.onReaction(function(reaction) { + console.info('[GroupChatManagerDemo ] new reaction received: ', reaction); + processReaction(reaction); }); chatController.onChange(function(event) { @@ -451,7 +467,7 @@ function chatManagerReady(chatController, isOwner) { chatController.send(message).then(function(result) { console.log('message sent', result); - processMessage(result); + processMessage(result,chatController); messageForm[0].reset(); }).catch(function(reason) { console.error('message error', reason); @@ -477,7 +493,7 @@ function chatManagerReady(chatController, isOwner) { } -function processMessage(message) { +function processMessage(message,chatController) { console.log('[GroupChatManager - processMessage] - msg ', message); @@ -490,16 +506,94 @@ function processMessage(message) { avatar = message.identity.userProfile.avatar; from = message.identity.userProfile.cn; } + message.childId = message.childId.replace(/#/g, '-').replace(/.*:\/\/(.*)\//,'$1_'); if (message.value.content) { - let list = `
  • + let list = `
  • ` + from + `

    ` + message.value.content.replace(/\n/g, '
    ') + `

    +
  • `; console.log('[GroupChatManager - processMessage] - ', messagesList, message, list); messagesList.append(list); + chatGroupManager.messages[message.childId]={ + text: message.value.content, + reactions: {} + }; + + $('#'+message.childId + ' .reactions a').on('click',function(event){ + let msgId = $(event.currentTarget).data("msg-id"); + let reactionType = '' + if($(event.currentTarget).hasClass("thumb_up_button")){ + reactionType = "thumb_up"; + console.log("Thumbs up!!!!! :) to ",$(event.currentTarget).data("msg-id")); + }else if($(event.currentTarget).hasClass("thumb_down_button")){ + reactionType = "thumb_down"; + console.log("Thumbs down!!!!! :("); + }else {console.log("no thumbs");} + chatController.sendReaction(reactionType,msgId).then(function(reaction){ + let msgId = reaction.value.content.msgId; + let reactionType = reaction.value.content.reactionType; + //let reaction = { identity: message.identity, content: {msgId: msgId,reactionType: reactionType}}; + processReaction(reaction); + }) + }); + + } +} + +function processReaction(reaction){ + let msgId = reaction.value.content.msgId; + let reactionType = reaction.value.content.reactionType; + let message = chatGroupManager.messages[msgId]; + console.log("cGM messages",chatGroupManager.messages); + if(message.reactions[reactionType] == undefined){ + message.reactions[reactionType] = {}; + } + if(! (reaction.identity.userProfile.userURL in message.reactions[reactionType])){ + message.reactions[reactionType][reaction.identity.userProfile.userURL] = { + name: reaction.identity.userProfile.cn, + id: reaction.childId + }; + updateReaction(reaction); + } +} + +function processReactionDeletion(reaction){ + let msgId = reaction.value.content.msgId; + let reactionType = reaction.value.content.reactionType; + let message = chatGroupManager.messages[msgId]; + + message.reactions[reactionType].delete(reaction.identity.userProfile.userURL); + updateReaction(reaction); +} + +function updateReaction(reaction){ + let msgId = reaction.value.content.msgId; + let reactionType = reaction.value.content.reactionType; + let message = chatGroupManager.messages[msgId]; + console.log("update count", $('#'+msgId+' .reactions .'+reactionType+'_count'), "to",(message.reactions[reactionType].length)); + $('#'+msgId+' .reactions .'+reactionType+'_count').html(Object.keys(message.reactions[reactionType]).length); + $('#'+msgId+' .reactions .'+reactionType+'_count').attr('data-tooltip', + $.map(Object.values(message.reactions[reactionType]),function(user,i){ + return user.name; + }).join(", ") + ); + $('#'+msgId+' .reactions .'+reactionType+'_count').tooltip(); + if(reaction.identity.userProfile.userURL==chatGroupManager.identity.userURL){ + $('#'+msgId+' .reactions .'+reactionType+'_button').css('color','#999'); } } diff --git a/src/group-chat-manager/ChatController.js b/src/group-chat-manager/ChatController.js index 7adfc39..ced48fb 100644 --- a/src/group-chat-manager/ChatController.js +++ b/src/group-chat-manager/ChatController.js @@ -52,6 +52,19 @@ class ChatController { _this._objectDescURL = 'hyperty-catalogue://catalogue.' + domain + '/.well-known/dataschema/Communication'; } + _onAddChildGeneric(child){ + let _this = this; + _this.child_cseq +=1; + console.info('[GroupChatManager.ChatController] - Add Child: ', child); + if (child.value.type == "chat"){ + if (_this._onMessage) _this._onMessage(child); + }else if (child.value.type == "chatobject" && child.value.content.type == "reaction"){ + if (_this._onReaction) _this._onReaction(child); + }else{ + console.info('[GroupChatManager.ChatController] - Add Child: NOTHING DONE!', child); + } + } + set dataObjectReporter(dataObjectReporter) { if (!dataObjectReporter) throw new Error('The data object reporter is necessary parameter '); @@ -67,11 +80,8 @@ class ChatController { } }); - dataObjectReporter.onAddChild(function(child) { - _this.child_cseq +=1; - console.info('[GroupChatManager.ChatController]Reporter - Add Child:', child); - // dataObjectReporter.data.lastModified = new Date().toJSON(); - if (_this._onMessage) _this._onMessage(child); + dataObjectReporter.onAddChild(function(child){ + _this._onAddChildGeneric.call(_this,child) }); dataObjectReporter.onRead((event) => { @@ -112,10 +122,8 @@ class ChatController { }); - dataObjectObserver.onAddChild(function(child) { - _this.child_cseq +=1; - console.info('[GroupChatManager.ChatController]Observer - Add Child: ', child); - if (_this._onMessage) _this._onMessage(child); + dataObjectObserver.onAddChild(function(child){ + _this._onAddChildGeneric.call(_this,child) }); // let childrens = dataObjectObserver.childrens; @@ -203,6 +211,7 @@ class ChatController { console.log('[GroupChatManager.ChatController.onUnsubscribe - this._onUserRemoved] ', this.onUserRemoved); if (this._onUserRemoved) this._onUserRemoved(participant); } + /** * This function is used to send a chat message. * @param {string} message Is the ChatMessage to be sent. @@ -238,7 +247,7 @@ class ChatController { //resolve(dataObjectChild); let msg = { - childId: dataObjectChild._childId, + childId: dataObjectChild._url, from: dataObjectChild._owner, value: dataObjectChild.data, type: 'create', @@ -257,6 +266,41 @@ class ChatController { } + sendReaction(reactionType,msgId){ + let _this = this; + let mode = _this.controllerMode; + let dataObject = mode === 'reporter' ? _this.dataObjectReporter : _this.dataObjectObserver; + + return new Promise(function(resolve,reject){ + let _dataObjectChild; + _this.child_cseq += 1; + let msg = { + type : "chatobject", + content : { + type: 'reaction', + reactionType: reactionType, + msgId: msgId + } + } + + dataObject.addChild('resources', msg).then(function(dataObjectChild) { + let msg = { + childId: dataObjectChild._url, + from: dataObjectChild._owner, + value: dataObjectChild.data, + type: 'create', + identity: { + userProfile: _this.myIdentity + } + }; + resolve(msg); + }).catch(function(reason) { + console.error('Reason:', reason); + reject(reason); + }); + }); + } + /** * [onChange description] * @param {Function} callback [description] @@ -277,6 +321,16 @@ class ChatController { _this._onMessage = callback; } + /** + * This function is used to receive new message reactions. + * @param {Function} callback Function to handle with new message reactions + * @return {Communication.ChatMessage} m + */ + onReaction(callback) { + let _this = this; + _this._onReaction = callback; + } + /** * [onUserAdded description] * @param {Function} callback [description] diff --git a/src/group-chat-manager/GroupChatManager.hy.json b/src/group-chat-manager/GroupChatManager.hy.json index f40b0d9..fb6026c 100644 --- a/src/group-chat-manager/GroupChatManager.hy.json +++ b/src/group-chat-manager/GroupChatManager.hy.json @@ -3,7 +3,7 @@ "signature": "", "configuration": {}, "hypertyType": [ - "chat" + "chat","chatobject" ], "constraints": { "browser": true