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,17 +27,19 @@ 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') { if (group.type == 'party') {
Analytics.updateUser({'partyID': group.id, 'partySize': group.memberCount}); Analytics.updateUser({'partyID': group.id, 'partySize': group.memberCount});
} }
if (group.privacy == 'public'){ if (group.privacy == 'public'){
Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'group chat','groupType':group.type,'privacy':group.privacy,'groupName':group.name}); Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'group chat','groupType':group.type,'privacy':group.privacy,'groupName':group.name});
} else { } else {
@@ -52,9 +54,8 @@ habitrpg.controller('ChatCtrl', ['$scope', 'Groups', 'Chat', 'User', '$http', 'A
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,23 +66,28 @@ 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)
.then(function () {
$rootScope.openModal('abuse-flag',{ $rootScope.openModal('abuse-flag',{
controller:'MemberModalCtrl', controller:'MemberModalCtrl',
scope: $scope 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,6 +46,8 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User','
} }
} }
Chat.markChatSeen($scope.group._id);
$scope.create = function(group) { $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() {

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'},
{ function getChat (groupId) {
postChat: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/chat'}, return $http({
like: {method: 'POST', isArray: true, url: ApiUrl.get() + '/api/v2/groups/:gid/chat/:messageId/like'}, method: 'GET',
deleteChatMessage: {method: "DELETE", url: ApiUrl.get() + '/api/v2/groups/:gid/chat/:messageId'}, url: apiV3Prefix + '/groups/' + groupId + '/chat',
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 postChat (groupId, message, previousMsg) {
seenMessage: seenMessage, var url = apiV3Prefix + '/groups/' + groupId + '/chat';
if (previousMsg) {
url += '?previousMsg=' + previousMsg;
}
return $http({
method: 'POST',
url: url,
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, clearCards: clearCards,
utils: utils }
};
return chatService;
//@TOOD: Port when User service is updated
function clearCards() { function clearCards() {
User.user.ops.update && User.set({'flags.cardReceived':false}); User.user.ops.update && User.set({'flags.cardReceived':false});
} }
function seenMessage(gid) {
// On enter, set chat message to "seen"
$http.post(ApiUrl.get() + '/api/v2/groups/'+gid+'/chat/seen');
if (User.user.newMessages) delete User.user.newMessages[gid];
}
}]); }]);