mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
* feat(incentives): login bennies WIP * feat(content): incentive prize content WIP * fix(content): placeholders pass tests * WIP(content): Bard instrument placeholder * feat(content): Incentives build * chore(sprites): compile and fix some strings * WIP(incentives): quests and backgrounds * fix(quests): correct buy/launch handling * [WIP] Incentives rewarding (#8226) * Added login incentive rewards * Updated incentive rewards * Added incentive modal and updated notification structure * Added analytics to sleeping * Added login incentives to user analytics * Fixed unit tests and ensured that prizes are incremented and not replaced * Updated style of daily login incentive modal * Added rewards modal * Added translations * Added loigin incentive ui elements to profile * Updated login incentives structure and abstracted to common.content * Added dynamic display for login incentives on profile * Added purple potion image * Updated daily login modal * Fixed progress calculation * Added bard gear * Updated login incentive rewards * Fixed styles and text * Added multiple read for notifications * Fixed lint issues * Fixed styles and added 50 limit * Updated quest keys * Added login incentives reward page * Fixed tests * Fixed linting and tests * Read named notifications route. Add image for backgrounds * Fixed style issues and added tranlsations to login incentive notification * Hided abiltiy to purchase incentive backgrounds and added message to detail how to unlock * Updated awarded message * Fixed text and updated progress counter to display better * Fixed purple potion reward text * Fixed check in backgrouns reward text * fix(quest): pass tests * Added display of multiple rewards * Updated modal styles * Fixed neagtive 50 issue * Remvoed total count from daily login incentives modal * Fixed magic paw display * fix(awards): give bunnies again * WIP(incentives): more progress on BG shop * fix(incentives): actually award backgrounds * fix(incentives): more BG fixy * fix(backgrounds): don't gem-buy checkin bgs * Added dust bunny notification * fix(incentives): don't redisplay bunny award * chore(news): Bailey and different promo sprite
129 lines
5.1 KiB
JavaScript
129 lines
5.1 KiB
JavaScript
'use strict';
|
|
|
|
angular.module('habitrpg')
|
|
.config(['$httpProvider', function($httpProvider){
|
|
$httpProvider.interceptors.push(['$q', '$rootScope', function($q, $rootScope){
|
|
var resyncNumber = 0;
|
|
var lastResync = 0;
|
|
|
|
// Verify that the user was not updated from another browser/app/client
|
|
// If it was, sync
|
|
function verifyUserUpdated (response) {
|
|
var isApiCall = response.config.url.indexOf('api/v3') !== -1;
|
|
var isUserAvailable = $rootScope.appLoaded === true;
|
|
var hasUserV = response.data && response.data.userV;
|
|
var isNotSync = response.config.url.indexOf('/api/v3/user') !== 0 || response.config.method !== 'GET';
|
|
var isNotMarkChatSeen = response.config.url.indexOf('/chat/seen') === -1; // exclude chat seen requests because with real time chat they would be too many
|
|
|
|
if (isApiCall && isUserAvailable && hasUserV) {
|
|
var oldUserV = $rootScope.User.user._v;
|
|
$rootScope.User.user._v = response.data.userV;
|
|
|
|
// Something has changed on the user object that was not tracked here, sync the user
|
|
if (isNotMarkChatSeen && isNotSync && ($rootScope.User.user._v - oldUserV) > 1) {
|
|
$rootScope.User.sync();
|
|
}
|
|
}
|
|
}
|
|
|
|
function verifyNewNotifications (response) {
|
|
// Ignore CRON notifications for manual syncs
|
|
var isUserLoaded = $rootScope.appLoaded === true;
|
|
|
|
if (response && response.data && response.data.notifications && response.data.notifications.length > 0) {
|
|
$rootScope.userNotifications = response.data.notifications.filter(function (notification) {
|
|
if (isUserLoaded && notification.type === 'CRON') {
|
|
// If the user is already loaded, do not show the notification, syncing will show it
|
|
// (the user will be synced automatically)
|
|
// $rootScope.User.readNotifications([notification.id]);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
}
|
|
|
|
return {
|
|
request: function (config) {
|
|
var url = config.url;
|
|
|
|
if (url.indexOf('api/v3') !== -1) {
|
|
if ($rootScope.User && $rootScope.User.user) {
|
|
if (url.indexOf('?') !== -1) {
|
|
config.url += '&userV=' + $rootScope.User.user._v;
|
|
} else {
|
|
config.url += '?userV=' + $rootScope.User.user._v;
|
|
}
|
|
}
|
|
}
|
|
|
|
return config;
|
|
},
|
|
response: function(response) {
|
|
verifyUserUpdated(response);
|
|
verifyNewNotifications(response);
|
|
return response;
|
|
},
|
|
responseError: function(response) {
|
|
var mobileApp = !!window.env.appVersion;
|
|
|
|
// Offline
|
|
if (response.status == 0 ||
|
|
// don't know why we're getting 404 here, should be 0
|
|
(response.status == 404 && _.isEmpty(response.data))) {
|
|
|
|
if (!mobileApp) // skip mobile, queue actions
|
|
$rootScope.$broadcast('responseText', window.env.t('serverUnreach'));
|
|
|
|
// Needs refresh
|
|
} else if (response.needRefresh) {
|
|
if (!mobileApp) // skip mobile for now
|
|
$rootScope.$broadcast('responseError', "The site has been updated and the page needs to refresh. The last action has not been recorded, please refresh and try again.");
|
|
|
|
} else if (response.data && response.data.code && response.data.code === 'ACCOUNT_SUSPENDED') {
|
|
confirm(response.data.err);
|
|
localStorage.clear();
|
|
window.location.href = mobileApp ? '/app/login' : '/logout'; //location.reload()
|
|
|
|
// 400 range
|
|
} else if (response.status < 400) {
|
|
// never triggered because we're in responseError
|
|
$rootScope.$broadcast('responseText', response.data && response.data.message);
|
|
} else if (response.status < 500) {
|
|
if (response.status === 400 && response.data && response.data.errors && _.isArray(response.data.errors)) { // bad requests with more info
|
|
response.data.errors.forEach(function (err) {
|
|
$rootScope.$broadcast('responseError', err.message);
|
|
});
|
|
} else {
|
|
$rootScope.$broadcast('responseError', response.data && response.data.message);
|
|
}
|
|
|
|
if ($rootScope.User && $rootScope.User.sync) {
|
|
if (resyncNumber < 100 && (Date.now() - lastResync) > 500) { // avoid thousands of requests when user is not found
|
|
$rootScope.User.sync();
|
|
resyncNumber++;
|
|
lastResync = Date.now();
|
|
}
|
|
}
|
|
|
|
// Need to reject the prompse so the error is handled correctly
|
|
if (response.status === 401) {
|
|
return $q.reject(response);
|
|
}
|
|
// Error
|
|
} else {
|
|
var error = window.env.t('requestError') + '<br><br>"' +
|
|
window.env.t('error') + ' ' + (response.data.message || response.data.error || response.data || 'something went wrong') +
|
|
'" <br><br>' + window.env.t('seeConsole');
|
|
if (mobileApp) error = 'Error contacting the server. Please try again in a few minutes.';
|
|
$rootScope.$broadcast('responseError500', error);
|
|
console.error(response);
|
|
}
|
|
|
|
return $q.reject(response);
|
|
}
|
|
};
|
|
}]);
|
|
}]);
|