Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a contact add/remove button in chat #151

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
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
49 changes: 48 additions & 1 deletion static/js/directives/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"use strict";
define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'], function($, _, templateChat, templateChatroom) {

return ["$compile", "safeDisplayName", "mediaStream", "safeApply", "desktopNotify", "translation", "playSound", "fileUpload", "randomGen", "buddyData", "appData", "$timeout", "geolocation", function($compile, safeDisplayName, mediaStream, safeApply, desktopNotify, translation, playSound, fileUpload, randomGen, buddyData, appData, $timeout, geolocation) {
return ["$compile", "safeDisplayName", "mediaStream", "safeApply", "desktopNotify", "translation", "playSound", "fileUpload", "randomGen", "buddyData", "appData", "$timeout", "geolocation", "contacts", function($compile, safeDisplayName, mediaStream, safeApply, desktopNotify, translation, playSound, fileUpload, randomGen, buddyData, appData, $timeout, geolocation, contacts) {

var displayName = safeDisplayName;
var groupChatId = "";
Expand Down Expand Up @@ -197,13 +197,15 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro
var index = controller.visibleRooms.length;
if (!subscope) {
console.log("Create new chatroom", [id]);
var buddy = buddyData.lookup(id);
if (settings.group) {
controller.visibleRooms.unshift(id);
} else {
controller.visibleRooms.push(id);
}
subscope = controller.rooms[id] = scope.$new();
translation.inject(subscope);
subscope.contact = {};
subscope.id = id;
subscope.isgroupchat = !!settings.group;
subscope.index = index;
Expand All @@ -216,6 +218,31 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro
subscope.p2pstate = false;
subscope.active = false;
subscope.pending = 0;
var handleContactFunctionality = function(buddy) {
var buddyId = buddy && buddy.session && buddy.session.Userid;
var myId = appData.get().userid;
var isAnotherUser = buddyId && myId !== buddyId;
subscope.contact.isContact = !!(buddy && buddy.contact);
subscope.contact.disableContact = !myId || !isAnotherUser;
subscope.contact.showContact = !subscope.isgroupchat && buddyId;
safeApply(subscope);
};
handleContactFunctionality(buddy);
// Update chatter
appData.e.on('authenticationChanged', function() {
handleContactFunctionality(buddy);
});
// Update chattee
mediaStream.api.e.on("received.status", function(event, data) {
if (!id && !data) {
return;
}
// Chattee status changed - logged in
var bd = buddyData.lookup(id);
if ((bd && bd.session.Userid) === data.Userid) {
handleContactFunctionality(buddyData.lookup(data.Id));
}
});
if (!subscope.isgroupchat) {
buddyData.push(id);
}
Expand Down Expand Up @@ -332,6 +359,24 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro
subscope.doClear = function() {
subscope.$broadcast("clear");
};
subscope.addContact = function() {
subscope.$emit("requestcontact", subscope.id, {
restore: true
});
};
subscope.removeContact = function() {
contacts.remove(buddyData.lookup(id).contact.Userid);
};
subscope.updateContactStatus = function(event, data) {
var userid = buddy && buddy.session && buddy.session.Userid;
if (userid !== data.Userid) {
return;
}
subscope.contact.isContact = event.type === "contactadded";
safeApply(subscope);
};
contacts.e.on("contactadded", subscope.updateContactStatus);
contacts.e.on("contactremoved", subscope.updateContactStatus);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make sense and needs fixing. This events trigger for all contacts and need to be limited/matched to the user in this subscope what so ever. In general i would like it if this two events could be avoided alltogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am filtering now by userid to correctly handle this. Not sure what else I would use to handle this, other then the contacts api.

//console.log("Creating new chat room", controller, subscope, index);
subscope.$on("submit", function(event, input) {
subscope.seen();
Expand Down Expand Up @@ -515,6 +560,8 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro
return;
}
delete controller.rooms[id];
contacts.e.off("contactadded", subscope.updateContactStatus);
contacts.e.off("contactremoved", subscope.updateContactStatus);
$timeout(function() {
subscope.$destroy();
}, 0);
Expand Down
2 changes: 2 additions & 0 deletions static/partials/chatroom.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<button ng-if="!isgroupchat" class="btn btn-sm btn-default" title="{{_('Start video call')}}" ng-click="doCall()"><i class="fa fa-phone fa-fw"></i></button>
<button class="btn btn-sm btn-default btn-fileupload" title="{{_('Upload files')}}"><i class="fa fa-upload fa-fw"></i></button>
<button class="btn btn-sm btn-default btn-locationshare" title="{{_('Share my location')}}" ng-click="shareGeolocation()"><i class="fa fa-location-arrow fa-fw"></i></button>
<button ng-if="contact.showContact && !contact.isContact" class="btn btn-sm btn-default" ng-class="{'disabled': contact.disableContact}" title="{{_('Add to contacts')}}" ng-click="addContact()"><i class="fa fa-star-o"></i></button>
<button ng-if="contact.showContact && contact.isContact" class="btn btn-sm btn-default" title="{{_('Remove from contacts')}}" ng-click="removeContact()"><i class="fa fa-star"></i></button>
</div>
<div class="btn-group pull-right">
<button class="btn btn-sm btn-default" title="{{_('Clear chat')}}" ng-click="doClear()"><i class="fa fa-eraser fa-fw"></i></button>
Expand Down