mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
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:
committed by
Matteo Pagliazzi
parent
ea490c9a1f
commit
2619b34c65
73
test/spec/services/chatServicesSpec.js
Normal file
73
test/spec/services/chatServicesSpec.js
Normal 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();
|
||||
});
|
||||
});
|
||||
@@ -155,7 +155,7 @@ window.habitrpg = angular.module('habitrpg',
|
||||
Groups.Group.get($stateParams.gid)
|
||||
.then(function (response) {
|
||||
$scope.group = response.data.data;
|
||||
Chat.seenMessage($scope.group._id);
|
||||
Chat.markChatSeen($scope.group._id);
|
||||
});
|
||||
}]
|
||||
})
|
||||
|
||||
@@ -27,37 +27,38 @@ habitrpg.controller('ChatCtrl', ['$scope', 'Groups', 'Chat', 'User', '$http', 'A
|
||||
if (_.isEmpty(message) || $scope._sending) return;
|
||||
$scope._sending = true;
|
||||
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){
|
||||
if(data.chat){
|
||||
group.chat = data.chat;
|
||||
}else if(data.message){
|
||||
group.chat.unshift(data.message);
|
||||
}
|
||||
$scope.message.content = '';
|
||||
$scope._sending = false;
|
||||
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 {
|
||||
Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'group chat','groupType':group.type,'privacy':group.privacy});
|
||||
}
|
||||
}, function(err){
|
||||
$scope._sending = false;
|
||||
});
|
||||
Chat.postChat(group._id, message, previousMsg)
|
||||
.then(function(response) {
|
||||
var message = response.data.data.message;
|
||||
|
||||
group.chat.unshift(message);
|
||||
|
||||
$scope.message.content = '';
|
||||
$scope._sending = false;
|
||||
|
||||
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 {
|
||||
Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'group chat','groupType':group.type,'privacy':group.privacy});
|
||||
}
|
||||
}, function(err){
|
||||
$scope._sending = false;
|
||||
});
|
||||
}
|
||||
|
||||
$scope.deleteChatMessage = function(group, message){
|
||||
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;
|
||||
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){
|
||||
if(data.chat) group.chat = data.chat;
|
||||
|
||||
var i = _.findIndex(group.chat, {id: message.id});
|
||||
if(i !== -1) group.chat.splice(i, 1);
|
||||
});
|
||||
if (confirm('Are you sure you want to delete this message?')) {
|
||||
Chat.deleteChat(group._id, message.id, previousMsg)
|
||||
.then(function (response) {
|
||||
var i = _.findIndex(group.chat, {id: message.id});
|
||||
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) {
|
||||
if (message.uuid == User.user._id)
|
||||
return Notification.text(window.env.t('foreverAlone'));
|
||||
|
||||
if (!message.likes) message.likes = {};
|
||||
|
||||
if (message.likes[User.user._id]) {
|
||||
delete message.likes[User.user._id];
|
||||
} else {
|
||||
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) {
|
||||
if(!message.flags) message.flags = {};
|
||||
if(message.flags[User.user._id])
|
||||
|
||||
if (message.flags[User.user._id]) {
|
||||
Notification.text(window.env.t('abuseAlreadyReported'));
|
||||
else {
|
||||
} else {
|
||||
$scope.abuseObject = message;
|
||||
$scope.groupId = groupId;
|
||||
Members.selectMember(message.uuid, function(){
|
||||
$rootScope.openModal('abuse-flag',{
|
||||
controller:'MemberModalCtrl',
|
||||
scope: $scope
|
||||
Members.selectMember(message.uuid)
|
||||
.then(function () {
|
||||
$rootScope.openModal('abuse-flag',{
|
||||
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
|
||||
// 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
|
||||
|
||||
@@ -26,7 +26,7 @@ angular.module('habitrpg')
|
||||
}
|
||||
}
|
||||
|
||||
$scope.clearMessages = Chat.seenMessage;
|
||||
$scope.clearMessages = Chat.markChatSeen;
|
||||
$scope.clearCards = Chat.clearCards;
|
||||
|
||||
$scope.iconClasses = function() {
|
||||
|
||||
@@ -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});
|
||||
Groups.Group.create(group, function() {
|
||||
Analytics.track({'hitType':'event', 'eventCategory':'behavior', 'eventAction':'join group', 'owner':true, 'groupType':'party', 'privacy':'private'});
|
||||
|
||||
@@ -1,33 +1,88 @@
|
||||
'use strict';
|
||||
|
||||
angular.module('habitrpg').factory('Chat',
|
||||
['$resource', '$http', 'ApiUrl', 'User',
|
||||
function($resource, $http, ApiUrl, User) {
|
||||
var utils = $resource(ApiUrl.get() + '/api/v2/groups/:gid',
|
||||
{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'},
|
||||
});
|
||||
angular.module('habitrpg')
|
||||
.factory('Chat', ['$http', 'ApiUrl', 'User',
|
||||
function($http, ApiUrl, User) {
|
||||
var apiV3Prefix = '/api/v3';
|
||||
|
||||
var chatService = {
|
||||
seenMessage: seenMessage,
|
||||
clearCards: clearCards,
|
||||
utils: utils
|
||||
};
|
||||
function getChat (groupId) {
|
||||
return $http({
|
||||
method: 'GET',
|
||||
url: apiV3Prefix + '/groups/' + groupId + '/chat',
|
||||
});
|
||||
}
|
||||
|
||||
return chatService;
|
||||
function postChat (groupId, message, previousMsg) {
|
||||
var url = apiV3Prefix + '/groups/' + groupId + '/chat';
|
||||
|
||||
function clearCards() {
|
||||
User.user.ops.update && User.set({'flags.cardReceived':false});
|
||||
}
|
||||
if (previousMsg) {
|
||||
url += '?previousMsg=' + previousMsg;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}]);
|
||||
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,
|
||||
}
|
||||
|
||||
//@TOOD: Port when User service is updated
|
||||
function clearCards() {
|
||||
User.user.ops.update && User.set({'flags.cardReceived':false});
|
||||
}
|
||||
}]);
|
||||
|
||||
Reference in New Issue
Block a user