2013-08-29 02:08:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Services that persists and retrieves user from localStorage.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
angular.module('groupServices', ['ngResource']).
|
2014-09-15 19:31:39 +00:00
|
|
|
factory('Groups', ['ApiUrlService', '$resource', '$q', '$http', 'User',
|
|
|
|
|
function(ApiUrlService, $resource, $q, $http, User) {
|
|
|
|
|
var API_URL = ApiUrlService.getApiUrl();
|
|
|
|
|
|
2013-12-16 00:22:35 +00:00
|
|
|
var Group = $resource(API_URL + '/api/v2/groups/:gid',
|
2013-09-08 15:22:03 +00:00
|
|
|
{gid:'@_id', messageId: '@_messageId'},
|
2013-08-31 23:14:58 +00:00
|
|
|
{
|
2013-10-30 21:34:59 +00:00
|
|
|
//query: {method: "GET", isArray:false},
|
2013-12-16 00:22:35 +00:00
|
|
|
postChat: {method: "POST", url: API_URL + '/api/v2/groups/:gid/chat'},
|
|
|
|
|
deleteChatMessage: {method: "DELETE", url: API_URL + '/api/v2/groups/:gid/chat/:messageId'},
|
|
|
|
|
join: {method: "POST", url: API_URL + '/api/v2/groups/:gid/join'},
|
|
|
|
|
leave: {method: "POST", url: API_URL + '/api/v2/groups/:gid/leave'},
|
|
|
|
|
invite: {method: "POST", url: API_URL + '/api/v2/groups/:gid/invite'},
|
2013-12-28 19:54:53 +00:00
|
|
|
removeMember: {method: "POST", url: API_URL + '/api/v2/groups/:gid/removeMember'},
|
2013-12-20 19:42:36 +00:00
|
|
|
questAccept: {method: "POST", url: API_URL + '/api/v2/groups/:gid/questAccept'},
|
2013-12-23 09:56:55 +00:00
|
|
|
questReject: {method: "POST", url: API_URL + '/api/v2/groups/:gid/questReject'},
|
2014-09-08 08:46:11 +00:00
|
|
|
questCancel: {method: "POST", url: API_URL + '/api/v2/groups/:gid/questCancel'},
|
2013-12-23 09:56:55 +00:00
|
|
|
questAbort: {method: "POST", url: API_URL + '/api/v2/groups/:gid/questAbort'}
|
2013-08-31 23:14:58 +00:00
|
|
|
});
|
|
|
|
|
|
2013-11-02 03:15:04 +00:00
|
|
|
// Defer loading everything until they're requested
|
2014-08-18 11:48:01 +00:00
|
|
|
var data = {party: undefined, myGuilds: undefined, publicGuilds: undefined, tavern: undefined};
|
2013-09-02 02:08:01 +00:00
|
|
|
|
|
|
|
|
return {
|
2013-11-02 03:32:43 +00:00
|
|
|
party: function(cb){
|
2014-08-18 11:48:01 +00:00
|
|
|
if (!data.party) return (data.party = Group.get({gid: 'party'}, cb));
|
|
|
|
|
return (cb) ? cb(party) : data.party;
|
2013-11-02 03:15:04 +00:00
|
|
|
},
|
|
|
|
|
publicGuilds: function(){
|
2013-10-30 22:14:08 +00:00
|
|
|
//TODO combine these as {type:'guilds,public'} and create a $filter() to separate them
|
2014-08-18 11:48:01 +00:00
|
|
|
if (!data.publicGuilds) data.publicGuilds = Group.query({type:'public'});
|
|
|
|
|
return data.publicGuilds;
|
2013-11-02 03:15:04 +00:00
|
|
|
},
|
|
|
|
|
myGuilds: function(){
|
2014-08-18 11:48:01 +00:00
|
|
|
if (!data.myGuilds) data.myGuilds = Group.query({type:'guilds'});
|
|
|
|
|
return data.myGuilds;
|
2013-11-02 03:15:04 +00:00
|
|
|
},
|
|
|
|
|
tavern: function(){
|
2014-08-18 11:48:01 +00:00
|
|
|
if (!data.tavern) data.tavern = Group.get({gid:'habitrpg'});
|
|
|
|
|
return data.tavern;
|
2013-11-02 03:15:04 +00:00
|
|
|
},
|
|
|
|
|
|
2014-02-13 04:20:42 +00:00
|
|
|
// On enter, set chat message to "seen"
|
|
|
|
|
seenMessage: function(gid){
|
|
|
|
|
$http.post('/api/v2/groups/'+gid+'/chat/seen');
|
2014-08-23 04:51:34 +00:00
|
|
|
if (User.user.newMessages) delete User.user.newMessages[gid];
|
2014-02-13 04:20:42 +00:00
|
|
|
},
|
|
|
|
|
|
2014-08-18 11:48:01 +00:00
|
|
|
// Pass reference to party, myGuilds, publicGuilds, tavern; inside data in order to
|
|
|
|
|
// be able to modify them directly (otherwise will be stick with cached version)
|
|
|
|
|
data: data,
|
|
|
|
|
|
2013-11-02 03:15:04 +00:00
|
|
|
Group: Group
|
2013-09-02 02:08:01 +00:00
|
|
|
}
|
2013-08-29 02:08:59 +00:00
|
|
|
}
|
2014-01-06 05:10:34 +00:00
|
|
|
])
|
|
|
|
|
/**
|
|
|
|
|
* TODO Get this working. Make ChatService it's own ngResource, so we can update chat without having to sync the whole
|
|
|
|
|
* group object (expensive). Also so we can add chat-specific routes
|
|
|
|
|
*/
|
|
|
|
|
// .factory('Chat', ['API_URL', '$resource',
|
|
|
|
|
// function(API_URL, $resource) {
|
|
|
|
|
// var Chat = $resource(API_URL + '/api/v2/groups/:gid/chat/:mid',
|
|
|
|
|
// //{gid:'@_id', mid: '@_messageId'},
|
|
|
|
|
// {
|
|
|
|
|
// like: {method: 'POST', url: API_URL + '/api/v2/groups/:gid/chat/:mid'}
|
|
|
|
|
// //postChat: {method: "POST", url: API_URL + '/api/v2/groups/:gid/chat'},
|
|
|
|
|
// //deleteChatMessage: {method: "DELETE", url: API_URL + '/api/v2/groups/:gid/chat/:messageId'},
|
|
|
|
|
// });
|
|
|
|
|
// return {Chat:Chat};
|
|
|
|
|
// }
|
|
|
|
|
// ]);
|