Skip to content
This repository was archived by the owner on Jul 3, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 102 additions & 8 deletions examples/group-chat-manager/demo.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -477,7 +493,7 @@ function chatManagerReady(chatController, isOwner) {

}

function processMessage(message) {
function processMessage(message,chatController) {

console.log('[GroupChatManager - processMessage] - msg ', message);

Expand All @@ -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 = `<li class="collection-item avatar">
let list = `<li class="collection-item avatar" id="`+ message.childId +`">
<img src="` + avatar + `" alt="" class="circle">
<span class="title">` + from + `</span>
<p>` + message.value.content.replace(/\n/g, '<br>') + `</p>
<div class="secondary-content reactions">
<a href="#!" data-msg-id="`+ message.childId + `" class="thumb_up_button tooltipped"
data-position="bottom" data-delay="50" data-tooltip="">
<i class="material-icons">thumb_up</i></a>
<span class="thumb_up_count reaction_count tooltipped"
data-position="bottom" data-delay="50" data-tooltip="" style="font-size:15pt;vertical-align:top"></span>
<a href="#!" data-msg-id="`+ message.childId + `" class="thumb_down_button">
<i class="material-icons">thumb_down</i></a>
<span class="thumb_down_count reaction_count tooltipped"
data-position="bottom" data-delay="50" data-tooltip="" style="font-size:15pt;vertical-align:top"></span>
</div>
</li>`;

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');
}
}

Expand Down
74 changes: 64 additions & 10 deletions src/group-chat-manager/ChatController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ');
Expand All @@ -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) => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -238,7 +247,7 @@ class ChatController {
//resolve(dataObjectChild);

let msg = {
childId: dataObjectChild._childId,
childId: dataObjectChild._url,
from: dataObjectChild._owner,
value: dataObjectChild.data,
type: 'create',
Expand All @@ -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]
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/group-chat-manager/GroupChatManager.hy.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"signature": "",
"configuration": {},
"hypertyType": [
"chat"
"chat","chatobject"
],
"constraints": {
"browser": true
Expand Down