Merge pull request #7145 from TheHollidayInn/api-v3-client-user-service

[WIP] Ported User Serivce to client side and to api v3
This commit is contained in:
Matteo Pagliazzi 2016-05-11 15:50:09 +02:00
commit bcb2877f61
42 changed files with 706 additions and 453 deletions

View file

@ -171,5 +171,6 @@
"pushDeviceAlreadyAdded": "The user already has the push device",
"resetComplete": "Reset completed",
"lvl10ChangeClass": "To change class you must be at least level 10.",
"equipmentAlreadyOwned": "You already own that piece of equipment"
"equipmentAlreadyOwned": "You already own that piece of equipment",
"pmsMarkedRead": "Your private messages have been marked as read"
}

View file

@ -147,6 +147,7 @@ import deletePM from './ops/deletePM';
import reroll from './ops/reroll';
import addPushDevice from './ops/addPushDevice';
import reset from './ops/reset';
import markPmsRead from './ops/markPMSRead';
api.ops = {
scoreTask,
@ -187,6 +188,7 @@ api.ops = {
reroll,
addPushDevice,
reset,
markPmsRead,
};
/*
@ -288,6 +290,7 @@ api.wrap = function wrapUser (user, main = true) {
readCard: _.partial(importedOps.readCard, user),
openMysteryItem: _.partial(importedOps.openMysteryItem, user),
score: _.partial(importedOps.scoreTask, user),
markPmsRead: _.partial(importedOps.markPmsRead, user),
};
}

View file

@ -5,6 +5,7 @@ import taskDefaults from '../libs/taskDefaults';
module.exports = function addTask (user, req = {body: {}}) {
let task = taskDefaults(req.body);
user.tasksOrder[`${task.type}s`].unshift(task._id);
user[`${task.type}s`].unshift(task);
if (user.preferences.newTaskEdit) {
task._editing = true;

View file

@ -6,16 +6,17 @@ import _ from 'lodash';
module.exports = function deleteTask (user, req = {}) {
let tid = _.get(req, 'params.id');
let task = user.tasks[tid];
let taskType = _.get(req, 'params.taskType');
if (!task) {
let index = _.findIndex(user[`${taskType}s`], function findById (task) {
return task._id === tid;
});
if (index === -1) {
throw new NotFound(i18n.t('messageTaskNotFound', req.language));
}
let index = user[`${task.type}s`].indexOf(task);
if (index !== -1) {
user[`${task.type}s`].splice(index, 1);
}
user[`${taskType}s`].splice(index, 1);
return {};
};

View file

@ -46,6 +46,8 @@ import allocate from './allocate';
import readCard from './readCard';
import openMysteryItem from './openMysteryItem';
import scoreTask from './scoreTask';
import markPmsRead from './markPMSRead';
module.exports = {
update,
@ -96,4 +98,5 @@ module.exports = {
readCard,
openMysteryItem,
scoreTask,
markPmsRead,
};

View file

@ -0,0 +1,14 @@
import i18n from '../i18n';
module.exports = function markPmsRead (user, req = {}) {
user.inbox.newMessages = 0;
if (req.v2 === true) {
return user;
} else {
return [
user.inbox.newMessages,
i18n.t('pmsMarkedRead'),
];
}
};

View file

@ -8,23 +8,26 @@ import _ from 'lodash';
// TODO used only in client, move there?
module.exports = function sortTag (user, req = {}) {
module.exports = function sortTask (user, req = {}) {
let id = _.get(req, 'params.id');
let to = _.get(req, 'query.to');
let fromParam = _.get(req, 'query.from');
let taskType = _.get(req, 'params.taskType');
let task = user.tasks[id];
let index = _.findIndex(user[`${taskType}s`], function findById (task) {
return task._id === id;
});
if (!task) {
if (index === -1) {
throw new NotFound(i18n.t('messageTaskNotFound', req.language));
}
if (!to && !fromParam) {
throw new BadRequest('?to=__&from=__ are required');
}
let tasks = user[`${task.type}s`];
let tasks = user[`${taskType}s`];
if (task.type === 'todo' && tasks[fromParam] !== task) {
if (taskType === 'todo') {
let preenedTasks = preenTodos(tasks);
if (to !== -1) {
@ -34,10 +37,6 @@ module.exports = function sortTag (user, req = {}) {
fromParam = tasks.indexOf(preenedTasks[fromParam]);
}
if (tasks[fromParam] !== task) {
throw new NotFound(i18n.t('messageTaskNotFound', req.language));
}
let movedTask = tasks.splice(fromParam, 1)[0];
if (to === -1) {

View file

@ -1,268 +0,0 @@
'use strict';
angular.module('habitrpg')
.service('ApiUrl', ['API_URL', function(currentApiUrl){
this.setApiUrl = function(newUrl){
currentApiUrl = newUrl;
};
this.get = function(){
return currentApiUrl;
};
}])
/**
* Services that persists and retrieves user from localStorage.
*/
.factory('User', ['$rootScope', '$http', '$location', '$window', 'STORAGE_USER_ID', 'STORAGE_SETTINGS_ID', 'MOBILE_APP', 'Notification', 'ApiUrl',
function($rootScope, $http, $location, $window, STORAGE_USER_ID, STORAGE_SETTINGS_ID, MOBILE_APP, Notification, ApiUrl) {
var authenticated = false;
var defaultSettings = {
auth: { apiId: '', apiToken: ''},
sync: {
queue: [], //here OT will be queued up, this is NOT call-back queue!
sent: [] //here will be OT which have been sent, but we have not got reply from server yet.
},
fetching: false, // whether fetch() was called or no. this is to avoid race conditions
online: false
};
var settings = {}; //habit mobile settings (like auth etc.) to be stored here
var user = {}; // this is stored as a reference accessible to all controllers, that way updates propagate
var userNotifications = {
// "party.order" : env.t("updatedParty"),
// "party.orderAscending" : env.t("updatedParty")
// party.order notifications are not currently needed because the party avatars are resorted immediately now
}; // this is a list of notifications to send to the user when changes are made, along with the message.
//first we populate user with schema
user.apiToken = user._id = ''; // we use id / apitoken to determine if registered
//than we try to load localStorage
if (localStorage.getItem(STORAGE_USER_ID)) {
_.extend(user, JSON.parse(localStorage.getItem(STORAGE_USER_ID)));
}
user._wrapped = false;
var syncQueue = function (cb) {
if (!authenticated) {
$window.alert("Not authenticated, can't sync, go to settings first.");
return;
}
var queue = settings.sync.queue;
var sent = settings.sync.sent;
if (queue.length === 0) {
// Sync: Queue is empty
return;
}
if (settings.fetching) {
// Sync: Already fetching
return;
}
if (settings.online!==true) {
// Sync: Not online
return;
}
settings.fetching = true;
// move all actions from queue array to sent array
_.times(queue.length, function () {
sent.push(queue.shift());
});
// Save the current filters
var current_filters = user.filters;
$http.post(ApiUrl.get() + '/api/v2/user/batch-update', sent, {params: {data:+new Date, _v:user._v, siteVersion: $window.env && $window.env.siteVersion}})
.success(function (data, status, heacreatingders, config) {
//make sure there are no pending actions to sync. If there are any it is not safe to apply model from server as we may overwrite user data.
if (!queue.length) {
//we can't do user=data as it will not update user references in all other angular controllers.
// the user has been modified from another application, sync up
if(data && data.wasModified) {
delete data.wasModified;
$rootScope.$emit('userUpdated', user);
}
// Update user
_.extend(user, data);
// Preserve filter selections between syncs
_.extend(user.filters,current_filters);
if (!user._wrapped){
// This wraps user with `ops`, which are functions shared both on client and mobile. When performed on client,
// they update the user in the browser and then send the request to the server, where the same operation is
// replicated. We need to wrap each op to provide a callback to send that operation
$window.habitrpgShared.wrap(user);
_.each(user.ops, function(op,k){
user.ops[k] = function(req,cb){
if (cb) return op(req,cb);
op(req,function(err,response) {
for(var updatedItem in req.body) {
var itemUpdateResponse = userNotifications[updatedItem];
if(itemUpdateResponse) Notification.text(itemUpdateResponse.data.message);
}
if (err) {
var message = err.code ? err.data.message : err;
if (MOBILE_APP) Notification.push({type:'text', text: message});
else Notification.text(message);
// In the case of 200s, they're friendly alert messages like "Your pet has hatched!" - still send the op
if ((err.code && err.code >= 400) || !err.code) return;
}
userServices.log({op:k, params: req.params, query:req.query, body:req.body});
});
}
});
}
// Emit event when user is synced
$rootScope.$emit('userSynced');
}
sent.length = 0;
settings.fetching = false;
save();
if (cb) {
cb(false)
}
syncQueue(); // call syncQueue to check if anyone pushed more actions to the queue while we were talking to server.
})
.error(function (data, status, headers, config) {
// (Notifications handled in app.js)
// If we're offline, queue up offline actions so we can send when we're back online
if (status === 0) {
//move sent actions back to queue
_.times(sent.length, function () {
queue.push(sent.shift())
});
settings.fetching = false;
// In the case of errors, discard the corrupt queue
} else {
// Clear the queue. Better if we can hunt down the problem op, but this is the easiest solution
settings.sync.queue = settings.sync.sent = [];
save();
}
});
}
var save = function () {
localStorage.setItem(STORAGE_USER_ID, JSON.stringify(user));
localStorage.setItem(STORAGE_SETTINGS_ID, JSON.stringify(settings));
};
var userServices = {
user: user,
set: function(updates) {
user.ops.update({body:updates});
},
online: function (status) {
if (status===true) {
settings.online = true;
syncQueue();
} else {
settings.online = false;
};
},
authenticate: function (uuid, token, cb) {
if (!!uuid && !!token) {
var offset = moment().zone(); // eg, 240 - this will be converted on server as -(offset/60)
$http.defaults.headers.common['x-api-user'] = uuid;
$http.defaults.headers.common['x-api-key'] = token;
$http.defaults.headers.common['x-user-timezoneOffset'] = offset;
authenticated = true;
settings.auth.apiId = uuid;
settings.auth.apiToken = token;
settings.online = true;
if (user && user._v) user._v--; // shortcut to always fetch new updates on page reload
userServices.log({}, function(){
// If they don't have timezone, set it
if (user.preferences.timezoneOffset !== offset)
userServices.set({'preferences.timezoneOffset': offset});
cb && cb();
});
} else {
alert('Please enter your ID and Token in settings.')
}
},
authenticated: function(){
return this.settings.auth.apiId !== "";
},
getBalanceInGems: function() {
var balance = user.balance || 0;
return balance * 4;
},
log: function (action, cb) {
//push by one buy one if an array passed in.
if (_.isArray(action)) {
action.forEach(function (a) {
settings.sync.queue.push(a);
});
} else {
settings.sync.queue.push(action);
}
save();
syncQueue(cb);
},
sync: function(){
user._v--;
userServices.log({});
},
save: save,
settings: settings
};
//load settings if we have them
if (localStorage.getItem(STORAGE_SETTINGS_ID)) {
//use extend here to make sure we keep object reference in other angular controllers
_.extend(settings, JSON.parse(localStorage.getItem(STORAGE_SETTINGS_ID)));
//if settings were saved while fetch was in process reset the flag.
settings.fetching = false;
//create and load if not
} else {
localStorage.setItem(STORAGE_SETTINGS_ID, JSON.stringify(defaultSettings));
_.extend(settings, defaultSettings);
}
//If user does not have ApiID that forward him to settings.
if (!settings.auth.apiId || !settings.auth.apiToken) {
if (MOBILE_APP) {
$location.path("/login");
} else {
//var search = $location.search(); // FIXME this should be working, but it's returning an empty object when at a root url /?_id=...
var search = $location.search($window.location.search.substring(1)).$$search; // so we use this fugly hack instead
if (search.err) return alert(search.err);
if (search._id && search.apiToken) {
userServices.authenticate(search._id, search.apiToken, function(){
$window.location.href='/';
});
} else {
var isStaticOrSocial = $window.location.pathname.match(/^\/(static|social)/);
if (!isStaticOrSocial){
localStorage.clear();
$window.location.href = '/logout';
}
}
}
} else {
userServices.authenticate(settings.auth.apiId, settings.auth.apiToken)
}
return userServices;
}
]);

View file

@ -0,0 +1,22 @@
import {
generateUser,
} from '../../../../helpers/api-integration/v3';
describe('POST /user/mark-pms-read', () => {
let user;
beforeEach(async () => {
user = await generateUser();
});
// More tests in common code unit tests
it('marks user\'s private messages as read', async () => {
await user.update({
'inbox.newMessages': 1,
});
await user.post('/user/mark-pms-read');
await user.sync();
expect(user.inbox.newMessages).to.equal(0);
});
});

View file

@ -8,6 +8,10 @@ describe('shared.ops.addTask', () => {
beforeEach(() => {
user = generateUser();
user.habits = [];
user.todos = [];
user.dailys = [];
user.rewards = [];
});
it('adds an habit', () => {

View file

@ -25,7 +25,7 @@ describe('Auth Controller', function() {
describe('logging in', function() {
it('should log in users with correct uname / pass', function() {
$httpBackend.expectPOST('/api/v2/user/auth/local').respond({id: 'abc', token: 'abc'});
$httpBackend.expectPOST('/api/v3/user/auth/local/login').respond({data: {id: 'abc', apiToken: 'abc'}});
scope.auth();
$httpBackend.flush();
expect(user.authenticate).to.be.calledOnce;
@ -33,7 +33,7 @@ describe('Auth Controller', function() {
});
it('should not log in users with incorrect uname / pass', function() {
$httpBackend.expectPOST('/api/v2/user/auth/local').respond(404, '');
$httpBackend.expectPOST('/api/v3/user/auth/local/login').respond(404, '');
scope.auth();
$httpBackend.flush();
expect(user.authenticate).to.not.be.called;

View file

@ -4,11 +4,9 @@ describe("CopyMessageModal controller", function() {
var scope, ctrl, user, Notification, $rootScope, $controller;
beforeEach(function() {
module(function($provide) {
$provide.value('User', {});
});
module(function($provide) {});
inject(function($rootScope, _$controller_, _Notification_){
inject(function($rootScope, _$controller_, _Notification_, User){
user = specHelper.newUser();
user._id = "unique-user-id";
user.ops = {
@ -20,10 +18,12 @@ describe("CopyMessageModal controller", function() {
$controller = _$controller_;
// Load RootCtrl to ensure shared behaviors are loaded
$controller('RootCtrl', {$scope: scope, User: {user: user}});
User.setUser(user);
ctrl = $controller('CopyMessageModalCtrl', {$scope: scope, User: {user: user}});
// Load RootCtrl to ensure shared behaviors are loaded
$controller('RootCtrl', {$scope: scope, User: User});
ctrl = $controller('CopyMessageModalCtrl', {$scope: scope, User: User});
Notification = _Notification_;
Notification.text = sandbox.spy();

View file

@ -1,13 +1,16 @@
'use strict';
describe('Filters Controller', function() {
var scope, user;
var scope, user, userService;
beforeEach(inject(function($rootScope, $controller, Shared) {
beforeEach(inject(function($rootScope, $controller, Shared, User) {
user = specHelper.newUser();
Shared.wrap(user);
scope = $rootScope.$new();
$controller('FiltersCtrl', {$scope: scope, User: {user: user}});
// user.filters = {};
User.setUser(user);
userService = User;
$controller('FiltersCtrl', {$scope: scope, User: User});
}));
describe('tags', function(){
@ -22,9 +25,9 @@ describe('Filters Controller', function() {
it('toggles tag filtering', inject(function(Shared){
var tag = {id: Shared.uuid(), name: 'myTag'};
scope.toggleFilter(tag);
expect(user.filters[tag.id]).to.eql(true);
expect(userService.user.filters[tag.id]).to.eql(true);
scope.toggleFilter(tag);
expect(user.filters[tag.id]).to.eql(false);
expect(userService.user.filters[tag.id]).to.eql(false);
}));
});
@ -33,7 +36,7 @@ describe('Filters Controller', function() {
scope.filterQuery = 'task';
scope.updateTaskFilter();
expect(user.filterQuery).to.eql(scope.filterQuery);
expect(userService.user.filterQuery).to.eql(scope.filterQuery);
});
});
});

View file

@ -1,11 +1,17 @@
'use strict';
describe('Footer Controller', function() {
var scope, user;
var scope, user, User;
beforeEach(inject(function($rootScope, $controller) {
user = specHelper.newUser();
var User = {log: sandbox.stub(), set: sandbox.stub(), user: user};
User = {
log: sandbox.stub(),
set: sandbox.stub(),
addTenGems: sandbox.stub(),
addHourglass: sandbox.stub(),
user: user
};
scope = $rootScope.$new();
$controller('FooterCtrl', {$scope: scope, User: User});
}));
@ -39,21 +45,17 @@ describe('Footer Controller', function() {
describe('#addTenGems', function() {
it('posts to /user/addTenGems', inject(function($httpBackend) {
$httpBackend.expectPOST('/api/v2/user/addTenGems').respond({});
scope.addTenGems();
$httpBackend.flush();
expect(User.addTenGems).to.have.been.called;
}));
});
describe('#addHourglass', function() {
it('posts to /user/addHourglass', inject(function($httpBackend) {
$httpBackend.expectPOST('/api/v2/user/addHourglass').respond({});
scope.addHourglass();
$httpBackend.flush();
expect(User.addHourglass).to.have.been.called;
}));
});

View file

@ -4,11 +4,9 @@ describe('Inventory Controller', function() {
var scope, ctrl, user, rootScope;
beforeEach(function() {
module(function($provide) {
$provide.value('User', {});
});
module(function($provide) {});
inject(function($rootScope, $controller, Shared){
inject(function($rootScope, $controller, Shared, User, $location, $window) {
user = specHelper.newUser({
balance: 4,
items: {
@ -26,17 +24,21 @@ describe('Inventory Controller', function() {
Shared.wrap(user);
var mockWindow = {
confirm: function(msg){
confirm: function(msg) {
return true;
}
},
};
scope = $rootScope.$new();
rootScope = $rootScope;
// Load RootCtrl to ensure shared behaviors are loaded
$controller('RootCtrl', {$scope: scope, User: {user: user}, $window: mockWindow});
User.user = user;
User.setUser(user);
ctrl = $controller('InventoryCtrl', {$scope: scope, User: {user: user}, $window: mockWindow});
// Load RootCtrl to ensure shared behaviors are loaded
$controller('RootCtrl', {$scope: scope, User: User, $window: mockWindow});
ctrl = $controller('InventoryCtrl', {$scope: scope, User: User, $window: mockWindow});
});
});

View file

@ -12,6 +12,11 @@ describe('Settings Controller', function () {
user = specHelper.newUser();
User = {
set: sandbox.stub(),
reroll: sandbox.stub(),
rebirth: sandbox.stub(),
releasePets: sandbox.stub(),
releaseMounts: sandbox.stub(),
releaseBoth: sandbox.stub(),
user: user
};
@ -123,7 +128,7 @@ describe('Settings Controller', function () {
scope.reroll(true);
expect(user.ops.reroll).to.be.calledWith({});
expect(User.reroll).to.be.calledWith({});
});
it('navigates to the tasks page when confirmed', function () {
@ -173,7 +178,7 @@ describe('Settings Controller', function () {
scope.rebirth(true);
expect(user.ops.rebirth).to.be.calledWith({});
expect(User.rebirth).to.be.calledWith({});
});
it('navigates to tasks page when confirmed', function () {
@ -216,9 +221,9 @@ describe('Settings Controller', function () {
it('doesn\'t call any release method if type is not provided', function () {
scope.releaseAnimals();
expect(User.user.ops.releasePets).to.not.be.called;
expect(User.user.ops.releaseMounts).to.not.be.called;
expect(User.user.ops.releaseBoth).to.not.be.called;
expect(User.releasePets).to.not.be.called;
expect(User.releaseMounts).to.not.be.called;
expect(User.releaseBoth).to.not.be.called;
});
it('doesn\'t redirect to tasks page if type is not provided', function () {
@ -230,7 +235,7 @@ describe('Settings Controller', function () {
it('calls releasePets when "pets" is provided', function () {
scope.releaseAnimals('pets');
expect(User.user.ops.releasePets).to.be.calledOnce;
expect(User.releasePets).to.be.calledOnce;
});
it('navigates to the tasks page when "pets" is provided', function () {
@ -242,7 +247,7 @@ describe('Settings Controller', function () {
it('calls releaseMounts when "mounts" is provided', function () {
scope.releaseAnimals('mounts');
expect(User.user.ops.releaseMounts).to.be.calledOnce;
expect(User.releaseMounts).to.be.calledOnce;
});
it('navigates to the tasks page when "mounts" is provided', function () {
@ -254,7 +259,7 @@ describe('Settings Controller', function () {
it('calls releaseBoth when "both" is provided', function () {
scope.releaseAnimals('both');
expect(User.user.ops.releaseBoth).to.be.calledOnce;
expect(User.releaseBoth).to.be.calledOnce;
});
it('navigates to the tasks page when "both" is provided', function () {
@ -266,9 +271,9 @@ describe('Settings Controller', function () {
it('does not call release functions when non-applicable argument is passed in', function () {
scope.releaseAnimals('dummy');
expect(User.user.ops.releasePets).to.not.be.called;
expect(User.user.ops.releaseMounts).to.not.be.called;
expect(User.user.ops.releaseBoth).to.not.be.called;
expect(User.releasePets).to.not.be.called;
expect(User.releaseMounts).to.not.be.called;
expect(User.releaseBoth).to.not.be.called;
});
});

View file

@ -8,6 +8,8 @@ describe('Tasks Controller', function() {
User = {
user: user
};
User.deleteTask = sandbox.stub();
User.user.ops = {
deleteTask: sandbox.stub(),
};
@ -51,13 +53,13 @@ describe('Tasks Controller', function() {
it('does not remove task if not confirmed', function() {
window.confirm.returns(false);
scope.removeTask(task);
expect(user.ops.deleteTask).to.not.be.called;
expect(User.deleteTask).to.not.be.called;
});
it('removes task', function() {
window.confirm.returns(true);
scope.removeTask(task);
expect(user.ops.deleteTask).to.be.calledOnce;
expect(User.deleteTask).to.be.calledOnce;
});
});

View file

@ -2,7 +2,7 @@
describe('Tasks Service', function() {
var rootScope, tasks, user, $httpBackend;
var apiV3Prefix = 'api/v3/tasks';
var apiV3Prefix = '/api/v3/tasks';
beforeEach(function() {
module(function($provide) {
@ -151,35 +151,35 @@ describe('Tasks Service', function() {
});
it('toggles the _editing property', function() {
tasks.editTask(task);
tasks.editTask(task, user);
expect(task._editing).to.eql(true);
tasks.editTask(task);
tasks.editTask(task, user);
expect(task._editing).to.eql(false);
});
it('sets _tags to true by default', function() {
tasks.editTask(task);
tasks.editTask(task, user);
expect(task._tags).to.eql(true);
});
it('sets _tags to false if preference for collapsed tags is turned on', function() {
user.preferences.tagsCollapsed = true;
tasks.editTask(task);
tasks.editTask(task, user);
expect(task._tags).to.eql(false);
});
it('sets _advanced to true by default', function(){
user.preferences.advancedCollapsed = true;
tasks.editTask(task);
tasks.editTask(task, user);
expect(task._advanced).to.eql(false);
});
it('sets _advanced to false if preference for collapsed advance menu is turned on', function() {
user.preferences.advancedCollapsed = false;
tasks.editTask(task);
tasks.editTask(task, user);
expect(task._advanced).to.eql(true);
});
@ -187,7 +187,7 @@ describe('Tasks Service', function() {
it('closes task chart if it exists', function() {
rootScope.charts[task.id] = true;
tasks.editTask(task);
tasks.editTask(task, user);
expect(rootScope.charts[task.id]).to.eql(false);
});
});

View file

@ -36,12 +36,12 @@ describe('userServices', function() {
expect(user_id).to.eql(user.user);
});
it('alerts when not authenticated', function(){
xit('alerts when not authenticated', function(){
user.log();
expect($window.alert).to.have.been.calledWith("Not authenticated, can't sync, go to settings first.");
});
it('puts items in que queue', function(){
xit('puts items in que queue', function(){
user.log({});
//TODO where does that null comes from?
expect(user.settings.sync.queue).to.eql([null, {}]);

View file

@ -310,6 +310,7 @@ window.habitrpg = angular.module('habitrpg',
});
var settings = JSON.parse(localStorage.getItem(STORAGE_SETTINGS_ID));
if (settings && settings.auth) {
$httpProvider.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8';
$httpProvider.defaults.headers.common['x-api-user'] = settings.auth.apiId;

View file

@ -28,8 +28,8 @@ angular.module('habitrpg')
$scope.registrationInProgress = false;
if (status === 0) {
$window.alert(window.env.t('noReachServer'));
} else if (!!data && !!data.err) {
$window.alert(data.err);
} else if (!!data && !!data.error) {
$window.alert(data.message);
} else {
$window.alert(window.env.t('errorUpCase') + ' ' + status);
}
@ -46,10 +46,10 @@ angular.module('habitrpg')
$scope.registrationInProgress = true;
var url = ApiUrl.get() + "/api/v2/register";
var url = ApiUrl.get() + "/api/v3/user/auth/local/register";
if($rootScope.selectedLanguage) url = url + '?lang=' + $rootScope.selectedLanguage.code;
$http.post(url, scope.registerVals).success(function(data, status, headers, config) {
runAuth(data.id, data.apiToken);
$http.post(url, scope.registerVals).success(function(res, status, headers, config) {
runAuth(res.data.id, res.data.apiToken);
}).error(errorAlert);
};
@ -58,13 +58,14 @@ angular.module('habitrpg')
username: $scope.loginUsername || $('#loginForm input[name="username"]').val(),
password: $scope.loginPassword || $('#loginForm input[name="password"]').val()
};
$http.post(ApiUrl.get() + "/api/v2/user/auth/local", data)
.success(function(data, status, headers, config) {
runAuth(data.id, data.token);
//@TODO: Move all the $http methods to a service
$http.post(ApiUrl.get() + "/api/v3/user/auth/local/login", data)
.success(function(res, status, headers, config) {
runAuth(res.data.id, res.data.apiToken);
}).error(errorAlert);
};
$scope.playButtonClick = function(){
$scope.playButtonClick = function() {
Analytics.track({'hitType':'event','eventCategory':'button','eventAction':'click','eventLabel':'Play'})
if (User.authenticated()) {
window.location.href = ('/' + window.location.hash);
@ -80,7 +81,7 @@ angular.module('habitrpg')
if(email == null || email.length == 0) {
alert(window.env.t('invalidEmail'));
} else {
$http.post(ApiUrl.get() + '/api/v2/user/reset-password', {email:email})
$http.post(ApiUrl.get() + '/api/v3/user/reset-password', {email:email})
.success(function(){
alert(window.env.t('newPassSent'));
})
@ -98,12 +99,12 @@ angular.module('habitrpg')
$scope.socialLogin = function(network){
hello(network).login({scope:'email'}).then(function(auth){
$http.post(ApiUrl.get() + "/api/v2/user/auth/social", auth)
.success(function(data, status, headers, config) {
runAuth(data.id, data.token);
$http.post(ApiUrl.get() + "/api/v3/user/auth/social", auth)
.success(function(res, status, headers, config) {
runAuth(res.data.id, res.data.apiToken);
}).error(errorAlert);
}, function( e ){
alert("Signin error: " + e.error.message );
alert("Signin error: " + e.message );
});
};

View file

@ -9,7 +9,7 @@ habitrpg.controller("CopyMessageModalCtrl", ['$scope', 'User', 'Notification',
notes: $scope.notes
};
User.user.ops.addTask({body:newTask});
User.addTask({body:newTask});
Notification.text(window.env.t('messageAddedAsToDo'));
$scope.$close();

View file

@ -14,7 +14,7 @@ habitrpg.controller("FiltersCtrl", ['$scope', '$rootScope', 'User', 'Shared',
_.each(User.user.tags, function(tag){
// Send an update op for each changed tag (excluding new tags & deleted tags, this if() packs a punch)
if (tagsSnap[tag.id] && tagsSnap[tag.id].name != tag.name)
User.user.ops.updateTag({params:{id:tag.id},body:{name:tag.name}});
User.updateTag({params:{id:tag.id}, body:{name:tag.name}});
})
$scope._editing = false;
} else {
@ -25,7 +25,11 @@ habitrpg.controller("FiltersCtrl", ['$scope', '$rootScope', 'User', 'Shared',
};
$scope.toggleFilter = function(tag) {
user.filters[tag.id] = !user.filters[tag.id];
if (!user.filters[tag.id]) {
user.filters[tag.id] = true;
} else {
user.filters[tag.id] = !user.filters[tag.id];
}
// no longer persisting this, it was causing a lot of confusion - users thought they'd permanently lost tasks
// Note: if we want to persist for just this computer, easy method is:
// User.save();
@ -37,7 +41,7 @@ habitrpg.controller("FiltersCtrl", ['$scope', '$rootScope', 'User', 'Shared',
$scope.updateTaskFilter();
$scope.createTag = function() {
User.user.ops.addTag({body:{name:$scope._newTag.name, id:Shared.uuid()}});
User.addTag({body:{name: $scope._newTag.name, id: Shared.uuid()}});
$scope._newTag.name = '';
};
}]);

View file

@ -78,6 +78,7 @@ function($scope, $rootScope, User, $http, Notification, ApiUrl, Social) {
});
};
//@TODO: Route?
$scope.addMissedDay = function(numberOfDays){
if (!confirm("Are you sure you want to reset the day by " + numberOfDays + " day(s)?")) return;
var dayBefore = moment(User.user.lastCron).subtract(numberOfDays, 'days').toDate();
@ -86,15 +87,11 @@ function($scope, $rootScope, User, $http, Notification, ApiUrl, Social) {
};
$scope.addTenGems = function(){
$http.post(ApiUrl.get() + '/api/v2/user/addTenGems').success(function(){
User.log({});
})
User.addTenGems();
};
$scope.addHourglass = function(){
$http.post(ApiUrl.get() + '/api/v2/user/addHourglass').success(function(){
User.log({});
})
User.addHourglass();
};
$scope.addGold = function(){
@ -124,6 +121,7 @@ function($scope, $rootScope, User, $http, Notification, ApiUrl, Social) {
};
$scope.addBossQuestProgressUp = function(){
//@TODO: Route?
User.set({
'party.quest.progress.up': User.user.party.quest.progress.up + 1000
});

View file

@ -68,7 +68,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
$scope.deleteAllMessages = function() {
if (confirm(window.env.t('confirmDeleteAllMessages'))) {
User.user.ops.clearPMs({});
User.clearPMs();
}
};

View file

@ -99,7 +99,7 @@ habitrpg.controller("InventoryCtrl",
var selected = $scope.selectedEgg ? 'selectedEgg' : $scope.selectedPotion ? 'selectedPotion' : $scope.selectedFood ? 'selectedFood' : undefined;
if (selected) {
var type = $scope.selectedEgg ? 'eggs' : $scope.selectedPotion ? 'hatchingPotions' : $scope.selectedFood ? 'food' : undefined;
user.ops.sell({params:{type:type, key: $scope[selected].key}});
User.sell({params:{type:type, key: $scope[selected].key}});
if (user.items[type][$scope[selected].key] < 1) {
$scope[selected] = null;
}
@ -118,7 +118,7 @@ habitrpg.controller("InventoryCtrl",
var userHasPet = user.items.pets[egg.key + '-' + potion.key] > 0;
var isPremiumPet = Content.hatchingPotions[potion.key].premium && !Content.dropEggs[egg.key];
user.ops.hatch({params:{egg:egg.key, hatchingPotion:potion.key}});
User.hatch({params:{egg:egg.key, hatchingPotion:potion.key}});
if (!user.preferences.suppressModals.hatchPet && !userHasPet && !isPremiumPet) {
$scope.hatchedPet = {
@ -172,7 +172,7 @@ habitrpg.controller("InventoryCtrl",
} else if (!$window.confirm(window.env.t('feedPet', {name: petDisplayName, article: food.article, text: food.text()}))) {
return;
}
User.user.ops.feed({params:{pet: pet, food: food.key}});
User.feed({params:{pet: pet, food: food.key}});
$scope.selectedFood = null;
_updateDropAnimalCount(user.items);
@ -198,12 +198,12 @@ habitrpg.controller("InventoryCtrl",
// Selecting Pet
} else {
User.user.ops.equip({params:{type: 'pet', key: pet}});
User.equip({params:{type: 'pet', key: pet}});
}
}
$scope.chooseMount = function(egg, potion) {
User.user.ops.equip({params:{type: 'mount', key: egg + '-' + potion}});
User.equip({params:{type: 'mount', key: egg + '-' + potion}});
}
$scope.getSeasonalShopArray = function(set){
@ -230,7 +230,7 @@ habitrpg.controller("InventoryCtrl",
for (item in user.items.gear.equipped){
var itemKey = user.items.gear.equipped[item];
if (user.items.gear.owned[itemKey]) {
user.ops.equip({params: {key: itemKey}});
User.equip({params: {key: itemKey}});
}
}
break;
@ -239,7 +239,7 @@ habitrpg.controller("InventoryCtrl",
for (item in user.items.gear.costume){
var itemKey = user.items.gear.costume[item];
if (user.items.gear.owned[itemKey]) {
user.ops.equip({params: {type:"costume", key: itemKey}});
User.equip({params: {type:"costume", key: itemKey}});
}
}
break;
@ -247,17 +247,17 @@ habitrpg.controller("InventoryCtrl",
case "petMountBackground":
var pet = user.items.currentPet;
if (pet) {
user.ops.equip({params:{type: 'pet', key: pet}});
User.equip({params:{type: 'pet', key: pet}});
}
var mount = user.items.currentMount;
if (mount) {
user.ops.equip({params:{type: 'mount', key: mount}});
User.equip({params:{type: 'mount', key: mount}});
}
var background = user.preferences.background;
if (background) {
User.user.ops.unlock({query:{path:"background."+background}});
User.unlock({query:{path:"background."+background}});
}
break;
@ -310,9 +310,9 @@ habitrpg.controller("InventoryCtrl",
};
$scope.clickTimeTravelItem = function(type,key) {
if (user.purchased.plan.consecutive.trinkets < 1) return user.ops.hourglassPurchase({params:{type:type,key:key}});
if (user.purchased.plan.consecutive.trinkets < 1) return User.hourglassPurchase({params:{type:type,key:key}});
if (!window.confirm(window.env.t('hourglassBuyItemConfirm'))) return;
user.ops.hourglassPurchase({params:{type:type,key:key}});
User.hourglassPurchase({params:{type:type,key:key}});
};
function _updateDropAnimalCount(items) {

View file

@ -29,7 +29,7 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User','
$scope.newGroup = { type: 'party' };
});
}
// Chat.seenMessage($scope.group._id);
function checkForNotifications () {
// Checks if user's party has reached 2 players for the first time.
if(!user.achievements.partyUp

View file

@ -21,7 +21,7 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
if (!!fromState.name) Analytics.track({'hitType':'pageview','eventCategory':'navigation','eventAction':'navigate','page':'/#/'+toState.name});
// clear inbox when entering or exiting inbox tab
if (fromState.name=='options.social.inbox' || toState.name=='options.social.inbox') {
User.user.ops.update && User.set({'inbox.newMessages':0});
User.clearNewMessages();
}
});
@ -218,11 +218,11 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
key: itemKey
};
user.ops.equip({ params: equipParams });
User.equip({ params: equipParams });
}
$rootScope.purchase = function(type, item){
if (type == 'special') return user.ops.buySpecialSpell({params:{key:item.key}});
if (type == 'special') return User.buySpecialSpell({params:{key:item.key}});
var gems = user.balance * 4;
var price = item.value;
@ -248,7 +248,7 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
message += window.env.t('buyThis', {text: itemName, price: price, gems: gems});
if ($window.confirm(message))
user.ops.purchase({params:{type:type,key:item.key}});
User.purchase({params:{type:type,key:item.key}});
};
function _canBuyEquipment(itemKey) {

View file

@ -99,7 +99,7 @@ habitrpg.controller('SettingsCtrl',
$scope.popoverEl.popover('destroy');
if (confirm) {
User.user.ops.reroll({});
User.reroll({});
$rootScope.$state.go('tasks');
}
}
@ -124,7 +124,7 @@ habitrpg.controller('SettingsCtrl',
$scope.popoverEl.popover('destroy');
if (confirm) {
User.user.ops.rebirth({});
User.rebirth({});
$rootScope.$state.go('tasks');
}
}
@ -175,7 +175,7 @@ habitrpg.controller('SettingsCtrl',
}
$scope.reset = function(){
User.user.ops.reset({});
User.reset({});
$rootScope.$state.go('tasks');
}
@ -235,7 +235,7 @@ habitrpg.controller('SettingsCtrl',
var releaseFunction = RELEASE_ANIMAL_TYPES[type];
if (releaseFunction) {
User.user.ops[releaseFunction]({});
User[releaseFunction]({});
$rootScope.$state.go('tasks');
}
}
@ -246,15 +246,15 @@ habitrpg.controller('SettingsCtrl',
$scope.hasWebhooks = _.size(webhooks);
})
$scope.addWebhook = function(url) {
User.user.ops.addWebhook({body:{url:url, id:Shared.uuid()}});
User.addWebhook({body:{url:url, id:Shared.uuid()}});
$scope._newWebhook.url = '';
}
$scope.saveWebhook = function(id,webhook) {
delete webhook._editing;
User.user.ops.updateWebhook({params:{id:id}, body:webhook});
User.updateWebhook({params:{id:id}, body:webhook});
}
$scope.deleteWebhook = function(id) {
User.user.ops.deleteWebhook({params:{id:id}});
User.deleteWebhook({params:{id:id}});
}
$scope.applyCoupon = function(coupon){

View file

@ -24,7 +24,7 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
if (direction === 'down') $rootScope.playSound('Minus_Habit');
else if (direction === 'up') $rootScope.playSound('Plus_Habit');
}
User.user.ops.score({params:{id: task.id, direction:direction}});
User.score({params:{task: task, direction:direction}});
Analytics.updateUser();
Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'score task','taskType':task.type,'direction':direction});
};
@ -33,12 +33,12 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
var newTask = {
text: task,
type: listDef.type,
tags: _.transform(User.user.filters, function(m, v, k) {
if (v) m.push(v);
}),
// tags: _.transform(User.user.filters, function(m, v, k) {
// if (v) m.push(v);
// }),
};
User.user.ops.addTask({body:newTask});
User.addTask({body: newTask});
}
$scope.addTask = function(addTo, listDef) {
@ -80,7 +80,7 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
*/
$scope.pushTask = function(task, index, location) {
var to = (location === 'bottom' || $scope.ctrlPressed) ? -1 : 0;
User.user.ops.sortTask({params:{id:task.id},query:{from:index, to:to}})
User.sortTask({params:{id: task._id, taskType: task.type}, query:{from:index, to:to}})
};
/**
@ -96,22 +96,17 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
$scope.removeTask = function(task) {
if (!confirm(window.env.t('sureDelete', {taskType: window.env.t(task.type), taskText: task.text}))) return;
User.user.ops.deleteTask({params:{id:task.id}})
User.deleteTask({params:{id: task._id, taskType: task.type}})
};
$scope.saveTask = function(task, stayOpen, isSaveAndClose) {
//@TODO: We will need to fix tag saving when user service is ported since tags are attached at the user level
if (task.checklist) {
task.checklist = _.filter(task.checklist, function(i) {return !!i.text});
}
User.user.ops.updateTask({params:{id:task.id},body:task});
if (task.checklist)
task.checklist = _.filter(task.checklist,function(i){return !!i.text});
User.updateTask(task, {body: task});
if (!stayOpen) task._editing = false;
if (isSaveAndClose) {
$("#task-" + task.id).parent().children('.popover').removeClass('in');
$("#task-" + task._id).parent().children('.popover').removeClass('in');
}
if (task.type == 'habit') Guide.goto('intro', 3);
@ -131,7 +126,7 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
};
$scope.unlink = function(task, keep) {
Tasks.unlinkTask(task.id, keep)
Tasks.unlinkTask(task._id, keep)
.success(function () {
User.log({});
});
@ -164,7 +159,7 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
*/
function focusChecklist(task,index) {
window.setTimeout(function(){
$('#task-'+task.id+' .checklist-form input[type="text"]')[index].focus();
$('#task-'+task._id+' .checklist-form input[type="text"]')[index].focus();
});
}
@ -177,10 +172,10 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
if (!task.checklist[$index].text) {
// Don't allow creation of an empty checklist item
// TODO Provide UI feedback that this item is still blank
} else if ($index == task.checklist.length - 1) {
User.user.ops.updateTask({params:{id:task.id},body:task});
task.checklist.push({completed: false, text: ''});
focusChecklist(task, task.checklist.length - 1);
} else if ($index == task.checklist.length-1){
User.updateTask({params:{id:task._id},body:task}); // don't preen the new empty item
task.checklist.push({completed:false,text:''});
focusChecklist(task,task.checklist.length-1);
} else {
$scope.saveTask(task, true);
focusChecklist(task, $index + 1);
@ -190,12 +185,12 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
$scope.removeChecklistItem = function(task, $event, $index, force){
// Remove item if clicked on trash icon
if (force) {
Tasks.removeChecklistItem(task.id, task.checklist[$index]._id);
Tasks.removeChecklistItem(task._id, task.checklist[$index]._id);
task.checklist.splice($index, 1);
} else if (!task.checklist[$index].text) {
// User deleted all the text and is now wishing to delete the item
// saveTask will prune the empty item
Tasks.removeChecklistItem(task.id, task.checklist[$index]._id);
Tasks.removeChecklistItem(task._id, task.checklist[$index]._id);
// Move focus if the list is still non-empty
if ($index > 0)
focusChecklist(task, $index-1);
@ -238,7 +233,7 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
$scope.buy = function(item) {
playRewardSound(item);
User.user.ops.buy({params:{key:item.key}});
User.buy({params:{key:item.key}});
};
/*

View file

@ -17,17 +17,17 @@ habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$
});
$scope.allocate = function(stat){
User.user.ops.allocate({query:{stat:stat}});
User.allocate({query:{stat:stat}});
}
$scope.changeClass = function(klass){
if (!klass) {
if (!confirm(window.env.t('sureReset')))
return;
return User.user.ops.changeClass({});
return User.changeClass({});
}
User.user.ops.changeClass({query:{class:klass}});
User.changeClass({query:{class:klass}});
$scope.selectedClass = undefined;
Shared.updateStore(User.user);
Guide.goto('classes', 0,true);
@ -46,7 +46,7 @@ habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$
}
$scope.acknowledgeHealthWarning = function(){
User.user.ops.update && User.set({'flags.warnedLowHealth':true});
User.set({'flags.warnedLowHealth':true});
}
/**
@ -69,7 +69,7 @@ habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$
if (confirm(window.env.t('purchaseFor',{cost:cost*4})) !== true) return;
if (User.user.balance < cost) return $rootScope.openModal('buyGems');
}
User.user.ops.unlock({query:{path:path}})
User.unlock({query:{path:path}})
}
$scope.ownsSet = function(type,_set) {

View file

@ -16,7 +16,7 @@
ui.item.data('startIndex', ui.item.index());
},
stop: function (event, ui) {
User.user.ops.sortTag({
User.sortTag({
query: {
from: ui.item.data('startIndex'),
to:ui.item.index()

View file

@ -20,8 +20,8 @@
stop: function (event, ui) {
var task = angular.element(ui.item[0]).scope().task;
var startIndex = ui.item.data('startIndex');
User.user.ops.sortTask({
params: { id: task.id },
User.sortTask({
params: { id: task._id, taskType: task.type },
query: {
from: startIndex,
to: ui.item.index()

View file

@ -3,20 +3,20 @@
var TASK_KEYS_TO_REMOVE = ['_id', 'completed', 'date', 'dateCompleted', 'history', 'id', 'streak', 'createdAt'];
angular.module('habitrpg')
.factory('Tasks', ['$rootScope', 'Shared', 'User', '$http',
function tasksFactory($rootScope, Shared, User, $http) {
.factory('Tasks', ['$rootScope', 'Shared', '$http',
function tasksFactory($rootScope, Shared, $http) {
function getUserTasks () {
return $http({
method: 'GET',
url: 'api/v3/tasks/user',
url: '/api/v3/tasks/user',
});
};
function createUserTasks (taskDetails) {
return $http({
method: 'POST',
url: 'api/v3/tasks/user',
url: '/api/v3/tasks/user',
data: taskDetails,
});
};
@ -24,14 +24,14 @@ angular.module('habitrpg')
function getChallengeTasks (challengeId) {
return $http({
method: 'GET',
url: 'api/v3/tasks/challenge/' + challengeId,
url: '/api/v3/tasks/challenge/' + challengeId,
});
};
function createChallengeTasks (challengeId, taskDetails) {
return $http({
method: 'POST',
url: 'api/v3/tasks/challenge/' + challengeId,
url: '/api/v3/tasks/challenge/' + challengeId,
data: taskDetails,
});
};
@ -39,14 +39,14 @@ angular.module('habitrpg')
function getTask (taskId) {
return $http({
method: 'GET',
url: 'api/v3/tasks/' + taskId,
url: '/api/v3/tasks/' + taskId,
});
};
function updateTask (taskId, taskDetails) {
return $http({
method: 'PUT',
url: 'api/v3/tasks/' + taskId,
url: '/api/v3/tasks/' + taskId,
data: taskDetails,
});
};
@ -54,28 +54,28 @@ angular.module('habitrpg')
function deleteTask (taskId) {
return $http({
method: 'DELETE',
url: 'api/v3/tasks/' + taskId,
url: '/api/v3/tasks/' + taskId,
});
};
function scoreTask (taskId, direction) {
return $http({
method: 'POST',
url: 'api/v3/tasks/' + taskId + '/score/' + direction,
url: '/api/v3/tasks/' + taskId + '/score/' + direction,
});
};
function moveTask (taskId, position) {
return $http({
method: 'POST',
url: 'api/v3/tasks/' + taskId + '/move/to/' + position,
url: '/api/v3/tasks/' + taskId + '/move/to/' + position,
});
};
function addChecklistItem (taskId, checkListItem) {
return $http({
method: 'POST',
url: 'api/v3/tasks/' + taskId + '/checklist',
url: '/api/v3/tasks/' + taskId + '/checklist',
data: checkListItem,
});
};
@ -83,14 +83,14 @@ angular.module('habitrpg')
function scoreCheckListItem (taskId, itemId) {
return $http({
method: 'POST',
url: 'api/v3/tasks/' + taskId + '/checklist/' + itemId + '/score',
url: '/api/v3/tasks/' + taskId + '/checklist/' + itemId + '/score',
});
};
function updateChecklistItem (taskId, itemId, itemDetails) {
return $http({
method: 'PUT',
url: 'api/v3/tasks/' + taskId + '/checklist/' + itemId,
url: '/api/v3/tasks/' + taskId + '/checklist/' + itemId,
data: itemDetails,
});
};
@ -98,21 +98,21 @@ angular.module('habitrpg')
function removeChecklistItem (taskId, itemId) {
return $http({
method: 'DELETE',
url: 'api/v3/tasks/' + taskId + '/checklist/' + itemId,
url: '/api/v3/tasks/' + taskId + '/checklist/' + itemId,
});
};
function addTagToTask (taskId, tagId) {
return $http({
method: 'POST',
url: 'api/v3/tasks/' + taskId + '/tags/' + tagId,
url: '/api/v3/tasks/' + taskId + '/tags/' + tagId,
});
};
function removeTagFromTask (taskId, tagId) {
return $http({
method: 'DELETE',
url: 'api/v3/tasks/' + taskId + '/tags/' + tagId,
url: '/api/v3/tasks/' + taskId + '/tags/' + tagId,
});
};
@ -123,21 +123,21 @@ angular.module('habitrpg')
return $http({
method: 'POST',
url: 'api/v3/tasks/unlink/' + taskId + '?keep=' + keep,
url: '/api/v3/tasks/unlink/' + taskId + '?keep=' + keep,
});
};
function clearCompletedTodos () {
return $http({
method: 'POST',
url: 'api/v3/tasks/clearCompletedTodos',
url: '/api/v3/tasks/clearCompletedTodos',
});
};
function editTask(task) {
function editTask(task, user) {
task._editing = !task._editing;
task._tags = !User.user.preferences.tagsCollapsed;
task._advanced = !User.user.preferences.advancedCollapsed;
task._tags = !user.preferences.tagsCollapsed;
task._advanced = !user.preferences.advancedCollapsed;
if($rootScope.charts[task.id]) $rootScope.charts[task.id] = false;
}

View file

@ -0,0 +1,435 @@
'use strict';
angular.module('habitrpg')
.service('ApiUrl', ['API_URL', function(currentApiUrl) {
this.setApiUrl = function(newUrl){
currentApiUrl = newUrl;
};
this.get = function(){
return currentApiUrl;
};
}])
/**
* Services that persists and retrieves user from localStorage.
*/
.factory('User', ['$rootScope', '$http', '$location', '$window', 'STORAGE_USER_ID', 'STORAGE_SETTINGS_ID', 'Notification', 'ApiUrl', 'Tasks', 'Tags',
function($rootScope, $http, $location, $window, STORAGE_USER_ID, STORAGE_SETTINGS_ID, Notification, ApiUrl, Tasks, Tags) {
var authenticated = false;
var defaultSettings = {
auth: { apiId: '', apiToken: ''},
sync: {
queue: [], //here OT will be queued up, this is NOT call-back queue!
sent: [] //here will be OT which have been sent, but we have not got reply from server yet.
},
fetching: false, // whether fetch() was called or no. this is to avoid race conditions
online: false
};
var settings = {}; //habit mobile settings (like auth etc.) to be stored here
var user = {}; // this is stored as a reference accessible to all controllers, that way updates propagate
var userNotifications = {
// "party.order" : env.t("updatedParty"),
// "party.orderAscending" : env.t("updatedParty")
// party.order notifications are not currently needed because the party avatars are resorted immediately now
}; // this is a list of notifications to send to the user when changes are made, along with the message.
//first we populate user with schema
user.apiToken = user._id = ''; // we use id / apitoken to determine if registered
//than we try to load localStorage
if (localStorage.getItem(STORAGE_USER_ID)) {
_.extend(user, JSON.parse(localStorage.getItem(STORAGE_USER_ID)));
}
user._wrapped = false;
function sync() {
$http({
method: "GET",
url: '/api/v3/user/',
})
.then(function (response) {
if (response.data.message) Notification.text(response.data.message);
_.extend(user, response.data.data);
if (!user._wrapped) {
// This wraps user with `ops`, which are functions shared both on client and mobile. When performed on client,
// they update the user in the browser and then send the request to the server, where the same operation is
// replicated. We need to wrap each op to provide a callback to send that operation
$window.habitrpgShared.wrap(user);
_.each(user.ops, function(op,k){
user.ops[k] = function(req,cb){
if (cb) return op(req,cb);
op(req,function(err,response) {
for(var updatedItem in req.body) {
var itemUpdateResponse = userNotifications[updatedItem];
if(itemUpdateResponse) Notification.text(itemUpdateResponse);
}
if (err) {
var message = err.code ? err.message : err;
Notification.text(message);
// In the case of 200s, they're friendly alert messages like "Your pet has hatched!" - still send the op
if ((err.code && err.code >= 400) || !err.code) return;
}
userServices.log({op:k, params: req.params, query:req.query, body:req.body});
});
}
});
}
save();
$rootScope.$emit('userSynced');
return Tasks.getUserTasks();
})
.then(function (response) {
var tasks = response.data.data;
user.habits = [];
user.todos = [];
user.dailys = [];
user.rewards = [];
tasks.forEach(function (element, index, array) {
user[element.type + 's'].push(element)
})
});
}
sync();
var save = function () {
localStorage.setItem(STORAGE_USER_ID, JSON.stringify(user));
localStorage.setItem(STORAGE_SETTINGS_ID, JSON.stringify(settings));
};
function callOpsFunctionAndRequest (opName, endPoint, method, paramString, opData) {
if (!opData) opData = {};
$window.habitrpgShared.ops[opName](user, opData);
var url = '/api/v3/user/' + endPoint;
if (paramString) {
url += '/' + paramString
}
var body = {};
if (opData.body) body = opData.body;
var queryString = '';
if (opData.query) queryString = '?' + $.param(opData.query)
$http({
method: method,
url: url + queryString,
body: body,
})
.then(function (response) {
if (response.data.message) Notification.text(response.data.message);
save();
})
}
function setUser(updates) {
for (var key in updates) {
_.set(user, key, updates[key]);
}
}
var userServices = {
user: user,
//@TODO: WE need a new way to set the user from tests
setUser: function (userInc) {
user = userInc;
},
allocate: function (data) {
callOpsFunctionAndRequest('allocate', 'allocate', "POST",'', data);
},
changeClass: function (data) {
callOpsFunctionAndRequest('changeClass', 'change-class', "POST",'', data);
},
addTask: function (data) {
user.ops.addTask(data);
save();
Tasks.createUserTasks(data.body);
},
score: function (data) {
$window.habitrpgShared.ops.scoreTask({user: user, task: data.params.task, direction: data.params.direction}, data.params);
save();
Tasks.scoreTask(data.params.task._id, data.params.direction);
},
sortTask: function (data) {
user.ops.sortTask(data);
save();
Tasks.moveTask(data.params.id, data.query.to);
},
updateTask: function (task, data) {
$window.habitrpgShared.ops.updateTask(task, data);
save();
Tasks.updateTask(task._id, data.body);
},
deleteTask: function (data) {
user.ops.deleteTask(data);
save();
Tasks.deleteTask(data.params.id);
},
addTag: function(data) {
user.ops.addTag(data);
save();
Tags.createTag(data.body);
},
updateTag: function(data) {
user.ops.updateTag(data);
save();
Tags.updateTag(data.params.id, data.body);
},
addTenGems: function () {
$http({
method: "POST",
url: 'api/v3/debug/add-ten-gems',
})
.then(function (response) {
Notification.text('+10 Gems!');
sync();
})
},
addHourglass: function () {
$http({
method: "POST",
url: 'api/v3/debug/add-hourglass',
})
.then(function (response) {
sync();
})
},
clearNewMessages: function () {
callOpsFunctionAndRequest('markPmsRead', 'mark-pms-read', "POST");
},
clearPMs: function () {
callOpsFunctionAndRequest('clearPMs', 'messages', "DELETE");
},
buy: function (data) {
callOpsFunctionAndRequest('buy', 'buy', "POST", data.params.key, data);
},
purchase: function (data) {
var type = data.params.type;
var key = data.params.key;
callOpsFunctionAndRequest('purchase', 'purchase', "POST", type + '/' + key, data);
},
buySpecialSpell: function (data) {
$window.habitrpgShared.ops['buySpecialSpell'](user, data);
var key = data.params.key;
$http({
method: "POST",
url: '/api/v3/user/' + 'buy-special-spell/' + key,
})
.then(function (response) {
Notification.text(response.data.message);
})
},
sell: function (data) {
var type = data.params.type;
var key = data.params.key;
callOpsFunctionAndRequest('sell', 'sell', "POST", type + '/' + key, data);
},
hatch: function (data) {
var egg = data.params.egg;
var hatchingPotion = data.params.hatchingPotion;
callOpsFunctionAndRequest('hatch', 'hatch', "POST", egg + '/' + hatchingPotion, data);
},
feed: function (data) {
var pet = data.params.pet;
var food = data.params.food;
callOpsFunctionAndRequest('feed', 'feed', "POST", pet + '/' + food, data);
},
equip: function (data) {
var type = data.params.type;
var key = data.params.key;
callOpsFunctionAndRequest('equip', 'equip', "POST", type + '/' + key, data);
},
hourglassPurchase: function (data) {
var type = data.params.type;
var key = data.params.key;
callOpsFunctionAndRequest('hourglassPurchase', 'purchase-hourglass', "POST", type + '/' + key, data);
},
unlock: function (data) {
callOpsFunctionAndRequest('unlock', 'unlock', "POST", '', data);
},
set: function(updates) {
setUser(updates);
$http({
method: "PUT",
url: '/api/v3/user',
data: updates,
});
},
reroll: function () {
callOpsFunctionAndRequest('reroll', 'reroll', "POST");
},
rebirth: function () {
callOpsFunctionAndRequest('rebirth', 'rebirth', "POST");
},
reset: function () {
callOpsFunctionAndRequest('reset', 'reset', "POST");
},
releaseBoth: function () {
callOpsFunctionAndRequest('releaseBoth', 'releaseBoth', "POST");
},
releaseMounts: function () {
callOpsFunctionAndRequest('releaseMounts', 'releaseMounts', "POST");
},
releasePets: function () {
callOpsFunctionAndRequest('releasePets', 'releasePets', "POST");
},
addWebhook: function (data) {
callOpsFunctionAndRequest('addWebhook', 'webhook', "POST", '', data, data.body);
},
updateWebhook: function (data) {
callOpsFunctionAndRequest('updateWebhook', 'webhook', "PUT", data.params.id, data, data.body);
},
deleteWebhook: function (data) {
callOpsFunctionAndRequest('deleteWebhook', 'webhook', "DELETE", data.params.id, data, data.body);
},
sleep: function () {
callOpsFunctionAndRequest('sleep', 'sleep', "POST");
},
online: function (status) {
if (status===true) {
settings.online = true;
// syncQueue();
} else {
settings.online = false;
};
},
authenticate: function (uuid, token, cb) {
if (!!uuid && !!token) {
var offset = moment().zone(); // eg, 240 - this will be converted on server as -(offset/60)
$http.defaults.headers.common['x-api-user'] = uuid;
$http.defaults.headers.common['x-api-key'] = token;
$http.defaults.headers.common['x-user-timezoneOffset'] = offset;
authenticated = true;
settings.auth.apiId = uuid;
settings.auth.apiToken = token;
settings.online = true;
save();
sync();
if (cb) {
cb();
}
//@TODO: Do we need the timezone set?
// userServices.log({}, function(){
// // If they don't have timezone, set it
// if (user.preferences.timezoneOffset !== offset)
// userServices.set({'preferences.timezoneOffset': offset});
// cb && cb();
// });
} else {
alert('Please enter your ID and Token in settings.')
}
},
authenticated: function(){
return this.settings.auth.apiId !== "";
},
getBalanceInGems: function() {
var balance = user.balance || 0;
return balance * 4;
},
log: function (action, cb) {
//push by one buy one if an array passed in.
if (_.isArray(action)) {
action.forEach(function (a) {
settings.sync.queue.push(a);
});
} else {
settings.sync.queue.push(action);
}
save();
},
sync: function(){
userServices.log({});
sync();
},
save: save,
settings: settings
};
//load settings if we have them
if (localStorage.getItem(STORAGE_SETTINGS_ID)) {
//use extend here to make sure we keep object reference in other angular controllers
_.extend(settings, JSON.parse(localStorage.getItem(STORAGE_SETTINGS_ID)));
//if settings were saved while fetch was in process reset the flag.
settings.fetching = false;
//create and load if not
} else {
localStorage.setItem(STORAGE_SETTINGS_ID, JSON.stringify(defaultSettings));
_.extend(settings, defaultSettings);
}
//If user does not have ApiID that forward him to settings.
if (!settings.auth.apiId || !settings.auth.apiToken) {
//var search = $location.search(); // FIXME this should be working, but it's returning an empty object when at a root url /?_id=...
var search = $location.search($window.location.search.substring(1)).$$search; // so we use this fugly hack instead
if (search.err) return alert(search.err);
if (search._id && search.apiToken) {
userServices.authenticate(search._id, search.apiToken, function(){
$window.location.href = '/';
});
} else {
var isStaticOrSocial = $window.location.pathname.match(/^\/(static|social)/);
if (!isStaticOrSocial){
localStorage.clear();
$location.path('/logout');
}
}
} else {
userServices.authenticate(settings.auth.apiId, settings.auth.apiToken)
}
return userServices;
}
]);

View file

@ -33,7 +33,7 @@
"bower_components/jquery-ui/ui/minified/jquery.ui.sortable.min.js",
"bower_components/smart-app-banner/smart-app-banner.js",
"common/dist/scripts/habitrpg-shared.js",
"common/dist/scripts/habitrpg-shared.js",
"js/env.js",
@ -42,7 +42,6 @@
"js/services/sharedServices.js",
"js/services/notificationServices.js",
"common/script/public/userServices.js",
"common/script/public/directives.js",
"js/services/analyticsServices.js",
"js/services/groupServices.js",
@ -50,11 +49,13 @@
"js/services/memberServices.js",
"js/services/guideServices.js",
"js/services/taskServices.js",
"js/services/tagsServices.js",
"js/services/challengeServices.js",
"js/services/paymentServices.js",
"js/services/questServices.js",
"js/services/socialServices.js",
"js/services/statServices.js",
"js/services/userServices.js",
"js/filters/money.js",
"js/filters/roundLargeNumbers.js",
@ -132,7 +133,9 @@
"js/services/sharedServices.js",
"js/services/socialServices.js",
"js/services/statServices.js",
"common/script/public/userServices.js",
"js/services/taskServices.js",
"js/services/tagsServices.js",
"js/services/userServices.js",
"js/controllers/authCtrl.js",
"js/controllers/footerCtrl.js"
],
@ -166,7 +169,9 @@
"js/services/sharedServices.js",
"js/services/socialServices.js",
"js/services/statServices.js",
"common/script/public/userServices.js",
"js/services/taskServices.js",
"js/services/tagsServices.js",
"js/services/userServices.js",
"js/controllers/authCtrl.js",
"js/controllers/footerCtrl.js"
],

View file

@ -1175,6 +1175,26 @@ api.clearMessages = {
},
};
/**
* @api {post} /api/v3/user/mark-pms-read Marks Private Messages as read
* @apiVersion 3.0.0
* @apiName markPmsRead
* @apiGroup User
*
* @apiSuccess {object} data user.inbox.messages
**/
api.markPmsRead = {
method: 'POST',
middlewares: [authWithHeaders()],
url: '/user/mark-pms-read',
async handler (req, res) {
let user = res.locals.user;
let markPmsResponse = common.ops.markPmsRead(user, req);
await user.save();
res.respond(200, markPmsResponse);
},
};
/*
* @api {post} /api/v3/user/reroll Rerolls a user.
* @apiVersion 3.0.0

View file

@ -16,7 +16,7 @@
.popover-content
span(ng-if='!env.worldDmg.tavern') {{user.preferences.sleep ? env.t('innText',{name: user.profile.name}) : env.t('danielText')}}
span(ng-if='env.worldDmg.tavern') {{user.preferences.sleep ? env.t('innTextBroken',{name: user.profile.name}) : env.t('danielTextBroken')}}
button.btn-block.btn.btn-lg.btn-success(ng-click='User.user.ops.sleep({})')
button.btn-block.btn.btn-lg.btn-success(ng-click='User.sleep({})')
| {{user.preferences.sleep ? env.t('innCheckOut') : env.t('innCheckIn')}}
span(ng-if='!user.preferences.sleep && !env.worldDmg.tavern')=env.t('danielText2')
span(ng-if='!user.preferences.sleep && env.worldDmg.tavern')=env.t('danielText2Broken')

View file

@ -8,7 +8,7 @@ div(ng-if='task._editing')
p
a(ng-click='unlink(task, "keep")')=env.t('keepIt')
| &nbsp;&nbsp;
a(ng-click="removeTask(task, obj")=env.t('removeIt')
a(ng-click="removeTask(task, obj)")=env.t('removeIt')
div(ng-if='task.challenge.broken=="CHALLENGE_DELETED"')
p
|&nbsp;

View file

@ -23,7 +23,7 @@ script(id='templates/habitrpg-tasks.html', type="text/ng-template")
i.glyphicon.glyphicon-warning-sign &nbsp;
=env.t('dailiesRestingInInn')
button.btn-block.btn.btn-lg.btn-success(ng-click='User.user.ops.sleep({})')
button.btn-block.btn.btn-lg.btn-success(ng-click='User.sleep({})')
| {{env.t('innCheckOut')}}
+taskColumnTabs('top')

View file

@ -23,15 +23,15 @@
|{{checklistCompletion(task.checklist)}}/{{task.checklist.length}}
span.glyphicon.glyphicon-tags(tooltip='{{Shared.appliedTags(user.tags, task.tags)}}', ng-hide='Shared.noTags(task.tags)')
// edit
a(ng-hide='task._editing', ng-click='editTask(task)', tooltip=env.t('edit'))
a(ng-hide='task._editing', ng-click='editTask(task, user)', tooltip=env.t('edit'))
| &nbsp;
span.glyphicon.glyphicon-pencil(ng-hide='task._editing')
| &nbsp;
a(ng-hide='!task._editing', ng-click='editTask(task)', tooltip=env.t('cancel'))
a(ng-hide='!task._editing', ng-click='editTask(task, user)', tooltip=env.t('cancel'))
span.glyphicon.glyphicon-remove(ng-hide='!task._editing')
| &nbsp;
// save
a(ng-hide='!task._editing', ng-click='editTask(task);saveTask(task)', tooltip=env.t('save'))
a(ng-hide='!task._editing', ng-click='editTask(task, user);saveTask(task)', tooltip=env.t('save'))
span.glyphicon.glyphicon-ok(ng-hide='!task._editing')
| &nbsp;
//challenges