Updated chat service to use api v3 (#7113)

* Updated chat service to use api v3

* Removed , added back User functions, added todo
This commit is contained in:
Keith Holliday
2016-04-27 08:59:10 -05:00
committed by Matteo Pagliazzi
parent ea490c9a1f
commit 2619b34c65
6 changed files with 201 additions and 65 deletions

View File

@@ -0,0 +1,73 @@
'use strict';
describe('chatServices', function() {
var $httpBackend, $http, chat, user;
var apiV3Prefix = '/api/v3';
beforeEach(function() {
module(function($provide) {
$provide.value('User', {user:user});
});
inject(function(_$httpBackend_, Chat, User) {
$httpBackend = _$httpBackend_;
chat = Chat;
user = User;
user.sync = function(){};
});
});
it('calls get chat endpoint', function() {
var groupId = 1;
$httpBackend.expectGET(apiV3Prefix + '/groups/' + groupId + '/chat').respond({});
chat.getChat(groupId);
$httpBackend.flush();
});
it('calls get chat endpoint', function() {
var groupId = 1;
var message = "test message";
$httpBackend.expectPOST(apiV3Prefix + '/groups/' + groupId + '/chat').respond({});
chat.postChat(groupId, message);
$httpBackend.flush();
});
it('calls delete chat endpoint', function() {
var groupId = 1;
var chatId = 2;
$httpBackend.expectDELETE(apiV3Prefix + '/groups/' + groupId + '/chat/' + chatId).respond({});
chat.deleteChat(groupId, chatId);
$httpBackend.flush();
});
it('calls like chat endpoint', function() {
var groupId = 1;
var chatId = 2;
$httpBackend.expectPOST(apiV3Prefix + '/groups/' + groupId + '/chat/' + chatId + '/like').respond({});
chat.like(groupId, chatId);
$httpBackend.flush();
});
it('calls flag chat endpoint', function() {
var groupId = 1;
var chatId = 2;
$httpBackend.expectPOST(apiV3Prefix + '/groups/' + groupId + '/chat/' + chatId + '/flag').respond({});
chat.flagChatMessage(groupId, chatId);
$httpBackend.flush();
});
it('calls clearflags chat endpoint', function() {
var groupId = 1;
var chatId = 2;
$httpBackend.expectPOST(apiV3Prefix + '/groups/' + groupId + '/chat/' + chatId + '/clearflags').respond({});
chat.clearFlagCount(groupId, chatId);
$httpBackend.flush();
});
it('calls chat seen endpoint', function() {
var groupId = 1;
$httpBackend.expectPOST(apiV3Prefix + '/groups/' + groupId + '/chat/seen').respond({});
chat.markChatSeen(groupId);
$httpBackend.flush();
});
});

View File

@@ -155,7 +155,7 @@ window.habitrpg = angular.module('habitrpg',
Groups.Group.get($stateParams.gid) Groups.Group.get($stateParams.gid)
.then(function (response) { .then(function (response) {
$scope.group = response.data.data; $scope.group = response.data.data;
Chat.seenMessage($scope.group._id); Chat.markChatSeen($scope.group._id);
}); });
}] }]
}) })

View File

@@ -27,37 +27,38 @@ habitrpg.controller('ChatCtrl', ['$scope', 'Groups', 'Chat', 'User', '$http', 'A
if (_.isEmpty(message) || $scope._sending) return; if (_.isEmpty(message) || $scope._sending) return;
$scope._sending = true; $scope._sending = true;
var previousMsg = (group.chat && group.chat[0]) ? group.chat[0].id : false; var previousMsg = (group.chat && group.chat[0]) ? group.chat[0].id : false;
Chat.utils.postChat({gid: group._id, message:message, previousMsg: previousMsg}, undefined, function(data){ Chat.postChat(group._id, message, previousMsg)
if(data.chat){ .then(function(response) {
group.chat = data.chat; var message = response.data.data.message;
}else if(data.message){
group.chat.unshift(data.message); group.chat.unshift(message);
}
$scope.message.content = ''; $scope.message.content = '';
$scope._sending = false; $scope._sending = false;
if (group.type == 'party') {
Analytics.updateUser({'partyID':group.id,'partySize':group.memberCount}); if (group.type == 'party') {
} Analytics.updateUser({'partyID': group.id, 'partySize': group.memberCount});
if (group.privacy == 'public'){ }
Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'group chat','groupType':group.type,'privacy':group.privacy,'groupName':group.name});
} else { if (group.privacy == 'public'){
Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'group chat','groupType':group.type,'privacy':group.privacy}); Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'group chat','groupType':group.type,'privacy':group.privacy,'groupName':group.name});
} } else {
}, function(err){ Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'group chat','groupType':group.type,'privacy':group.privacy});
$scope._sending = false; }
}); }, function(err){
$scope._sending = false;
});
} }
$scope.deleteChatMessage = function(group, message){ $scope.deleteChatMessage = function(group, message){
if(message.uuid === User.user.id || (User.user.backer && User.user.contributor.admin)){ if(message.uuid === User.user.id || (User.user.backer && User.user.contributor.admin)){
var previousMsg = (group.chat && group.chat[0]) ? group.chat[0].id : false; var previousMsg = (group.chat && group.chat[0]) ? group.chat[0].id : false;
if(confirm('Are you sure you want to delete this message?')){ if (confirm('Are you sure you want to delete this message?')) {
Chat.utils.deleteChatMessage({gid:group._id, messageId:message.id, previousMsg:previousMsg}, undefined, function(data){ Chat.deleteChat(group._id, message.id, previousMsg)
if(data.chat) group.chat = data.chat; .then(function (response) {
var i = _.findIndex(group.chat, {id: message.id});
var i = _.findIndex(group.chat, {id: message.id}); if(i !== -1) group.chat.splice(i, 1);
if(i !== -1) group.chat.splice(i, 1); });
});
} }
} }
} }
@@ -65,28 +66,33 @@ habitrpg.controller('ChatCtrl', ['$scope', 'Groups', 'Chat', 'User', '$http', 'A
$scope.likeChatMessage = function(group,message) { $scope.likeChatMessage = function(group,message) {
if (message.uuid == User.user._id) if (message.uuid == User.user._id)
return Notification.text(window.env.t('foreverAlone')); return Notification.text(window.env.t('foreverAlone'));
if (!message.likes) message.likes = {}; if (!message.likes) message.likes = {};
if (message.likes[User.user._id]) { if (message.likes[User.user._id]) {
delete message.likes[User.user._id]; delete message.likes[User.user._id];
} else { } else {
message.likes[User.user._id] = true; message.likes[User.user._id] = true;
} }
Chat.utils.like({ gid:group._id, messageId:message.id }, undefined);
Chat.like(group._id, message.id);
} }
$scope.flagChatMessage = function(groupId,message) { $scope.flagChatMessage = function(groupId,message) {
if(!message.flags) message.flags = {}; if(!message.flags) message.flags = {};
if(message.flags[User.user._id])
if (message.flags[User.user._id]) {
Notification.text(window.env.t('abuseAlreadyReported')); Notification.text(window.env.t('abuseAlreadyReported'));
else { } else {
$scope.abuseObject = message; $scope.abuseObject = message;
$scope.groupId = groupId; $scope.groupId = groupId;
Members.selectMember(message.uuid, function(){ Members.selectMember(message.uuid)
$rootScope.openModal('abuse-flag',{ .then(function () {
controller:'MemberModalCtrl', $rootScope.openModal('abuse-flag',{
scope: $scope controller:'MemberModalCtrl',
scope: $scope
});
}); });
});
} }
}; };
@@ -116,7 +122,7 @@ habitrpg.controller('ChatCtrl', ['$scope', 'Groups', 'Chat', 'User', '$http', 'A
} }
// When the user clicks fetch recent messages we need to update // When the user clicks fetch recent messages we need to update
// that the user has seen the new messages // that the user has seen the new messages
Chat.seenMessage(group._id); Chat.markChatSeen(group._id);
} }
// List of Ordering options for the party members list // List of Ordering options for the party members list

View File

@@ -26,7 +26,7 @@ angular.module('habitrpg')
} }
} }
$scope.clearMessages = Chat.seenMessage; $scope.clearMessages = Chat.markChatSeen;
$scope.clearCards = Chat.clearCards; $scope.clearCards = Chat.clearCards;
$scope.iconClasses = function() { $scope.iconClasses = function() {

View File

@@ -46,7 +46,9 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User','
} }
} }
$scope.create = function (group) { Chat.markChatSeen($scope.group._id);
$scope.create = function(group) {
if (!group.name) group.name = env.t('possessiveParty', {name: User.user.profile.name}); if (!group.name) group.name = env.t('possessiveParty', {name: User.user.profile.name});
Groups.Group.create(group, function() { Groups.Group.create(group, function() {
Analytics.track({'hitType':'event', 'eventCategory':'behavior', 'eventAction':'join group', 'owner':true, 'groupType':'party', 'privacy':'private'}); Analytics.track({'hitType':'event', 'eventCategory':'behavior', 'eventAction':'join group', 'owner':true, 'groupType':'party', 'privacy':'private'});

View File

@@ -1,33 +1,88 @@
'use strict'; 'use strict';
angular.module('habitrpg').factory('Chat', angular.module('habitrpg')
['$resource', '$http', 'ApiUrl', 'User', .factory('Chat', ['$http', 'ApiUrl', 'User',
function($resource, $http, ApiUrl, User) { function($http, ApiUrl, User) {
var utils = $resource(ApiUrl.get() + '/api/v2/groups/:gid', var apiV3Prefix = '/api/v3';
{gid:'@_id', messageId: '@_messageId'},
{
postChat: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/chat'},
like: {method: 'POST', isArray: true, url: ApiUrl.get() + '/api/v2/groups/:gid/chat/:messageId/like'},
deleteChatMessage: {method: "DELETE", url: ApiUrl.get() + '/api/v2/groups/:gid/chat/:messageId'},
flagChatMessage: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/chat/:messageId/flag'},
clearFlagCount: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/chat/:messageId/clearflags'},
});
var chatService = { function getChat (groupId) {
seenMessage: seenMessage, return $http({
clearCards: clearCards, method: 'GET',
utils: utils url: apiV3Prefix + '/groups/' + groupId + '/chat',
}; });
}
return chatService; function postChat (groupId, message, previousMsg) {
var url = apiV3Prefix + '/groups/' + groupId + '/chat';
function clearCards() { if (previousMsg) {
User.user.ops.update && User.set({'flags.cardReceived':false}); url += '?previousMsg=' + previousMsg;
} }
function seenMessage(gid) { return $http({
// On enter, set chat message to "seen" method: 'POST',
$http.post(ApiUrl.get() + '/api/v2/groups/'+gid+'/chat/seen'); url: url,
if (User.user.newMessages) delete User.user.newMessages[gid]; data: {
} message: message,
}]); }
});
}
function deleteChat (groupId, chatId, previousMsg) {
var url = apiV3Prefix + '/groups/' + groupId + '/chat/' + chatId;
if (previousMsg) {
url += '?previousMsg=' + previousMsg;
}
return $http({
method: 'DELETE',
url: url,
});
}
function like (groupId, chatId) {
return $http({
method: 'POST',
url: apiV3Prefix + '/groups/' + groupId + '/chat/' + chatId + '/like',
});
}
function flagChatMessage (groupId, chatId) {
return $http({
method: 'POST',
url: apiV3Prefix + '/groups/' + groupId + '/chat/' + chatId + '/flag',
});
}
function clearFlagCount (groupId, chatId) {
return $http({
method: 'POST',
url: apiV3Prefix + '/groups/' + groupId + '/chat/' + chatId + '/clearflags',
});
}
function markChatSeen (groupId) {
if (User.user.newMessages) delete User.user.newMessages[gid];
return $http({
method: 'POST',
url: apiV3Prefix + '/groups/' + groupId + '/chat/seen',
});
}
return {
getChat: getChat,
postChat: postChat,
deleteChat: deleteChat,
like: like,
flagChatMessage: flagChatMessage,
clearFlagCount: clearFlagCount,
markChatSeen: markChatSeen,
clearCards: clearCards,
}
//@TOOD: Port when User service is updated
function clearCards() {
User.user.ops.update && User.set({'flags.cardReceived':false});
}
}]);