2019-12-16 16:20:47 +00:00
|
|
|
import _ from 'lodash';
|
2016-06-07 14:14:19 +00:00
|
|
|
import mongoose from 'mongoose';
|
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
import validator from 'validator';
|
2019-10-08 14:57:10 +00:00
|
|
|
import baseModel from '../libs/baseModel';
|
2016-06-07 14:14:19 +00:00
|
|
|
|
|
|
|
|
const NOTIFICATION_TYPES = [
|
2020-03-07 19:03:13 +00:00
|
|
|
'DROPS_ENABLED', // unused
|
2016-06-07 14:14:19 +00:00
|
|
|
'REBIRTH_ENABLED',
|
|
|
|
|
'WON_CHALLENGE',
|
|
|
|
|
'STREAK_ACHIEVEMENT',
|
|
|
|
|
'ULTIMATE_GEAR_ACHIEVEMENT',
|
|
|
|
|
'REBIRTH_ACHIEVEMENT',
|
|
|
|
|
'NEW_CONTRIBUTOR_LEVEL',
|
|
|
|
|
'CRON',
|
2016-10-25 12:56:43 +00:00
|
|
|
'GROUP_TASK_APPROVAL',
|
2016-11-12 22:47:45 +00:00
|
|
|
'GROUP_TASK_APPROVED',
|
2019-04-29 18:38:28 +00:00
|
|
|
'GROUP_TASK_ASSIGNED',
|
2019-09-26 18:49:11 +00:00
|
|
|
'GROUP_TASK_CLAIMED',
|
2018-01-20 16:28:50 +00:00
|
|
|
'GROUP_TASK_NEEDS_WORK',
|
2016-11-24 01:34:09 +00:00
|
|
|
'LOGIN_INCENTIVE',
|
2016-12-01 18:04:57 +00:00
|
|
|
'GROUP_INVITE_ACCEPTED',
|
2016-12-21 19:45:45 +00:00
|
|
|
'SCORED_TASK',
|
2017-01-07 10:47:19 +00:00
|
|
|
'BOSS_DAMAGE', // Not used currently but kept to avoid validation errors
|
2020-02-13 19:12:45 +00:00
|
|
|
'GIFT_ONE_GET_ONE',
|
2017-05-19 19:45:11 +00:00
|
|
|
'GUILD_PROMPT',
|
|
|
|
|
'GUILD_JOINED_ACHIEVEMENT',
|
2017-06-07 16:43:16 +00:00
|
|
|
'CHALLENGE_JOINED_ACHIEVEMENT',
|
2017-07-20 01:39:39 +00:00
|
|
|
'INVITED_FRIEND_ACHIEVEMENT',
|
2018-01-20 16:28:50 +00:00
|
|
|
'CARD_RECEIVED',
|
|
|
|
|
'NEW_MYSTERY_ITEMS',
|
|
|
|
|
'UNALLOCATED_STATS_POINTS',
|
|
|
|
|
'NEW_INBOX_MESSAGE',
|
|
|
|
|
'NEW_STUFF',
|
|
|
|
|
'NEW_CHAT_MESSAGE',
|
Add API Call to bulk score tasks (#11389)
* Add new API call to complete multiple task scorings in one call
* Improve API response
* Improve saving process
* Improve handling for multiple tasks scored at once
* Handle challenge task errors better
* Improve check for alias
* Improve check for task scorings
* Fix merge errors
* make nodemon ignore content_cache
* Fix completing group tasks
* fix test
* fix tests (again)
* typo
* WIP(a11y): task modal updates
* fix(tasks): borders in modal
* fix(tasks): circley locks
* fix(task-modal): placeholders
* WIP(task-modal): disabled states, hide empty options, +/- restyle
* fix(task-modal): box shadows instead of borders, habit control pointer
* fix(task-modal): button states?
* fix(modal): tighten up layout, new spacing utils
* fix(tasks): more stylin
* fix(tasks): habit hovers
* fix(css): checklist labels, a11y colors
* fix(css): one more missed hover issue
* fix(css): lock Challenges, label fixes
* fix(css): scope input/textarea changes
* fix(style): task tweakies
* fix(style): more button fixage
* WIP(component): start select list story
* working example of a templated selectList
* fix(style): more button corrections
* fix(lint): EOL
* fix(buttons): factor btn-secondary to better override Bootstrap
* fix(styles): standardize more buttons
* wip: difficulty select - style fixes
* selectDifficulty works! :tada: - fix styles
* change the dropdown-item sizes only for the selectList ones
* selectTranslatedArray
* changed many label margins
* more correct dropdown style
* fix(modals): button corrections
* input-group styling + datetime picker without today button
* Style/margins for "repeat every" - extract selectTag.vue
* working tag-selection / update - cleanup
* fix stories
* fix svg color on create modal (purple)
* fix task modal bottom padding
* correct dropdown shadow
* update dropdown-toggle caret size / color
* fixed checklist style
* sync checked state
* selectTag padding
* fix spacing between positive/negative streak inputs
* toggle-checkbox + fix some spacings
* disable repeat-on when its a groupTask
* fix new checklist-item
* fix toggle-checkbox style - fix difficulty style
* fix checklist ui
* add tags label , when there arent any tags selected
* WORKING select-tag component :tada:
* fix taglist story
* show max 5 items in tag dropdown + "X more" label
* fix datetime clear button
* replace m-b-xs to mb-1 (bootstrap) - fix input-group-text style
* fix styles of advanced settings
* fix delete task styles
* always show grippy on hover of the item
* extract modal-text-input mixin + fix the borders/dropshadow
* fix(spacing): revert most to Bootstrap
* feat(checklists): make local copy of master checklist non-editable
also aggressively update checklists because they weren't syncing??
* fix(checklists): handle add/remove options better
* feat(teams): manager notes field
* fix select/dropdown styles
* input border + icon colors
* delete task underline color
* fix checklist "delete icon" vertical position
* selectTag fixes - normal open/close toggle working again - remove icon color
* fixing icons:
Trash can - Delete
Little X - Remove
Big X - Close
Block - Block
* fix taglist margins / icon sizes
* wip margin overview (in storybook)
* fix routerlink
* remove unused method
* new selectTag style + add markdown inside tagList + scrollable tag selection
* fix selectTag / selectList active border
* fix difficulty select (svg default color)
* fix input padding-left + fix reset habit streak fullwidth / padding + "repeat every" gray text (no border)
* feat(teams): improved approval request > approve > reward flow
* fix(tests): address failures
* fix(lint): oops only
* fix(tasks): short-circuit group related logic
* fix(tasks): more short circuiting
* fix(tasks): more lines, less lint
* fix(tasks): how do i keep missing these
* feat(teams): provide assigning user summary
* fix(teams): don't attempt to record assiging user if not supplied
* fix advanced-settings styling / margin
* fix merge + hide advanced streak settings when none enabled
* fix styles
* set Roboto font for advanced settings
* Add Challenge flag to the tag list
* add tag with enter, when no other tag is found
* fix styles + tag cancel button
* refactor footer / margin
* split repeat fields into option mt-3 groups
* button all the things
* fix(tasks): style updates
* no hover state for non-editable tasks on team board
* keep assign/claim footer on task after requesting approval
* disable more fields on user copy of team task, and remove hover states
for them
* fix(tasks): functional revisions
* "Claim Rewards" instead of "x" in task approved notif
* Remove default transition supplied by Bootstrap, apply individually to
some elements
* Delete individual tasks and related notifications when master task
deleted from team board
* Manager notes now save when supplied at task initial creation
* Can no longer dismiss rewards from approved task by hitting Dismiss
All
* fix(tasks): clean tasksOrder
also adjust related test expectation
* fix(tests): adjust integration expectations
* fix(test): ratzen fratzen only
* fix lint
* fix tests
* fix(teams): checklist, notes
* handleSharedCompletion: handle error, make sure it is run after the user task has been saved
* fix typo
* correctly handle errors in handleSharedCompletion when approving a task
* fix(teams): improve disabled states
* handleSharedCompletion: do not increase completions by 1 manually to adjust for last approval not saved yet
* revert changes to config.json.example
* fix(teams): more style fixage
* add unit tests for findMultipleByIdOrAlias
* exclude api v4 route from apidocs
* BREAKING(teams): return 202 instead of 401 for approval request
* fix(teams): better taskboard sync
also re-re-fix checklist borders
* scoreTasks: validate body
* fix tests, move string to api errors
* fix(tests): update expectations for breaking change
* start updating api docs, process tasks sequentially to avoid conflicts with user._tmp
* do not crash entire bulk operation in case of errors
* save task only if modified
* fix lint
* undo changes to error handling: either all tasks scoring are successfull or none
* remove stale code
* do not return user._tmp when bulk scoring, it would be the last version only
* make sure user._tmp.leveledUp is not lost when bulk scoring
* rewards tests
* mixed tests
* fix tests, allow scoring the same task multiple times
* finish integration tests
* fix api docs for the bulk score route
* refactor(task-modal): lockable label component
* wip loading spinner
* refactor(teams): move task scoring to mixin
* fix(teams): style corrections
* fix(btn): fix padding to have height of 32px
* implement loading spinner
* remove console.log warnings
* fix(tasks): spacing and wording corrections
* fix(teams): don't bork manager notes
* fix(teams): assignment fix and more approval flow revisions
* WIP(teams): use tag dropdown control for assignment
* finish merge - never throw an error when a group task requires approval (wip - needs tests)
* fix taskModal merge
* fix merge
* fix(task modal): add newline
* fix(column.vue): add newline at end of file
* mvp yesterdaily modal
* fix tests
* fix api docs for bulk scoring group tasks
* separate task scoring and _tmp handling
* handle _tmp when bulk scoring
* rya: close modal before calling cron API, prevents issues with modals
* rya: fix conflicts with other modals
* add sounds, support for group plans, analytics
* use asyncResource for group plans
* fix lint
* streak bonus: add comment about missing in rya
* move yesterdailyModal
* fix issues with level up modals and rya
* add comments for future use, fix level up modals not showing up at levels with a quest drop
* handle errors in rya modal
* bundle quest and crit notifications
Co-authored-by: Phillip Thelen <phillip@habitica.com>
Co-authored-by: Phillip Thelen <viirus@pherth.net>
Co-authored-by: Sabe Jones <sabrecat@gmail.com>
Co-authored-by: negue <eugen.bolz@gmail.com>
2020-08-21 09:46:56 +00:00
|
|
|
'LEVELED_UP', // Not in use
|
2020-03-07 19:03:13 +00:00
|
|
|
'FIRST_DROPS',
|
2019-12-16 16:20:47 +00:00
|
|
|
'ONBOARDING_COMPLETE',
|
2019-06-11 18:06:00 +00:00
|
|
|
'ACHIEVEMENT_ALL_YOUR_BASE',
|
|
|
|
|
'ACHIEVEMENT_BACK_TO_BASICS',
|
|
|
|
|
'ACHIEVEMENT_JUST_ADD_WATER',
|
2019-06-11 19:50:17 +00:00
|
|
|
'ACHIEVEMENT_LOST_MASTERCLASSER',
|
2019-06-11 18:06:00 +00:00
|
|
|
'ACHIEVEMENT_MIND_OVER_MATTER',
|
2019-09-17 21:10:32 +00:00
|
|
|
'ACHIEVEMENT_DUST_DEVIL',
|
|
|
|
|
'ACHIEVEMENT_ARID_AUTHORITY',
|
2019-10-18 15:04:33 +00:00
|
|
|
'ACHIEVEMENT_PARTY_UP',
|
|
|
|
|
'ACHIEVEMENT_PARTY_ON',
|
|
|
|
|
'ACHIEVEMENT_BEAST_MASTER',
|
|
|
|
|
'ACHIEVEMENT_MOUNT_MASTER',
|
|
|
|
|
'ACHIEVEMENT_TRIAD_BINGO',
|
2019-10-23 13:10:43 +00:00
|
|
|
'ACHIEVEMENT_MONSTER_MAGUS',
|
|
|
|
|
'ACHIEVEMENT_UNDEAD_UNDERTAKER',
|
2019-12-10 20:41:39 +00:00
|
|
|
'ACHIEVEMENT_PRIMED_FOR_PAINTING',
|
|
|
|
|
'ACHIEVEMENT_PEARLY_PRO',
|
2020-02-17 17:37:37 +00:00
|
|
|
'ACHIEVEMENT_TICKLED_PINK',
|
|
|
|
|
'ACHIEVEMENT_ROSY_OUTLOOK',
|
2020-03-17 14:11:41 +00:00
|
|
|
'ACHIEVEMENT_BUG_BONANZA',
|
2020-05-19 19:00:25 +00:00
|
|
|
'ACHIEVEMENT_BARE_NECESSITIES',
|
2020-07-27 19:17:43 +00:00
|
|
|
'ACHIEVEMENT_FRESHWATER_FRIENDS',
|
2020-08-18 20:13:06 +00:00
|
|
|
'ACHIEVEMENT_GOOD_AS_GOLD',
|
|
|
|
|
'ACHIEVEMENT_ALL_THAT_GLITTERS',
|
2020-10-19 20:38:12 +00:00
|
|
|
'ACHIEVEMENT_BONE_COLLECTOR',
|
|
|
|
|
'ACHIEVEMENT_SKELETON_CREW',
|
2020-11-24 23:53:15 +00:00
|
|
|
'ACHIEVEMENT_SEEING_RED',
|
|
|
|
|
'ACHIEVEMENT_RED_LETTER_DAY',
|
2021-02-16 23:00:39 +00:00
|
|
|
'ACHIEVEMENT_LEGENDARY_BESTIARY',
|
2021-03-29 22:51:33 +00:00
|
|
|
'ACHIEVEMENT_SEASONAL_SPECIALIST',
|
2021-05-25 19:37:15 +00:00
|
|
|
'ACHIEVEMENT_VIOLETS_ARE_BLUE',
|
|
|
|
|
'ACHIEVEMENT_WILD_BLUE_YONDER',
|
2021-08-24 20:42:40 +00:00
|
|
|
'ACHIEVEMENT_DOMESTICATED',
|
2021-10-21 15:49:22 +00:00
|
|
|
'ACHIEVEMENT_SHADY_CUSTOMER',
|
|
|
|
|
'ACHIEVEMENT_SHADE_OF_IT_ALL',
|
2022-01-28 22:14:50 +00:00
|
|
|
'ACHIEVEMENT_ZODIAC_ZOOKEEPER',
|
2022-03-08 17:43:38 +00:00
|
|
|
'ACHIEVEMENT_BIRDS_OF_A_FEATHER',
|
2019-12-16 16:20:47 +00:00
|
|
|
'ACHIEVEMENT', // generic achievement notification, details inside `notification.data`
|
2020-10-16 17:50:54 +00:00
|
|
|
'DROP_CAP_REACHED',
|
2016-06-07 14:14:19 +00:00
|
|
|
];
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const { Schema } = mongoose;
|
2016-06-07 14:14:19 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
export const schema = new Schema({
|
2016-06-07 14:14:19 +00:00
|
|
|
id: {
|
2018-10-28 14:23:41 +00:00
|
|
|
$type: String,
|
2016-06-07 14:14:19 +00:00
|
|
|
default: uuid,
|
2020-01-29 05:49:55 +00:00
|
|
|
validate: [v => validator.isUUID(v), 'Invalid uuid for userNotification.'],
|
2020-03-01 12:30:22 +00:00
|
|
|
required: true,
|
2018-02-02 19:31:52 +00:00
|
|
|
},
|
|
|
|
|
type: {
|
2018-10-28 14:23:41 +00:00
|
|
|
$type: String,
|
2020-03-01 12:30:22 +00:00
|
|
|
required: true,
|
2018-02-02 19:31:52 +00:00
|
|
|
enum: NOTIFICATION_TYPES,
|
2016-06-07 14:14:19 +00:00
|
|
|
},
|
2019-10-08 14:57:10 +00:00
|
|
|
data: {
|
|
|
|
|
$type: Schema.Types.Mixed,
|
|
|
|
|
default: () => ({}),
|
|
|
|
|
},
|
2018-01-31 10:55:39 +00:00
|
|
|
// A field to mark the notification as seen without deleting it, optional use
|
2018-02-02 19:31:52 +00:00
|
|
|
seen: {
|
2018-10-28 14:23:41 +00:00
|
|
|
$type: Boolean,
|
2018-02-02 19:31:52 +00:00
|
|
|
// required: true,
|
|
|
|
|
default: () => false,
|
|
|
|
|
},
|
2016-06-07 14:14:19 +00:00
|
|
|
}, {
|
|
|
|
|
strict: true,
|
|
|
|
|
minimize: false, // So empty objects are returned
|
2018-10-28 14:23:41 +00:00
|
|
|
_id: false, // use id instead of _id,
|
|
|
|
|
typeKey: '$type', // So that we can use fields named `type`
|
2016-06-07 14:14:19 +00:00
|
|
|
});
|
|
|
|
|
|
2018-02-04 12:28:05 +00:00
|
|
|
/**
|
2020-03-01 19:06:24 +00:00
|
|
|
* Remove invalid data from an array of notifications.
|
|
|
|
|
* Fix for https://github.com/HabitRPG/habitica/issues/9923
|
|
|
|
|
* Called by user's post init hook (models/user/hooks.js)
|
2018-02-04 12:28:05 +00:00
|
|
|
*/
|
2020-03-01 19:06:24 +00:00
|
|
|
schema.statics.cleanupCorruptData = function cleanupCorruptNotificationsData (notifications) {
|
2018-02-04 13:04:41 +00:00
|
|
|
if (!notifications) return notifications;
|
|
|
|
|
|
2020-03-01 19:06:24 +00:00
|
|
|
let filteredNotifications = notifications.filter(notification => {
|
|
|
|
|
// Exclude notifications with a nullish value, no id or no type
|
|
|
|
|
if (!notification || !notification.id || !notification.type) return false;
|
2018-02-04 12:28:05 +00:00
|
|
|
return true;
|
2018-07-30 14:11:56 +00:00
|
|
|
});
|
|
|
|
|
|
2020-03-01 19:06:24 +00:00
|
|
|
// Remove duplicate NEW_CHAT_MESSAGES notifications
|
|
|
|
|
// can be caused by a race condition when adding a new notification of this type
|
|
|
|
|
// in group.sendChat if two messages are posted at the same time
|
2018-07-30 14:11:56 +00:00
|
|
|
filteredNotifications = _.uniqWith(filteredNotifications, (val, otherVal) => {
|
2020-03-01 19:06:24 +00:00
|
|
|
if (val.type === 'NEW_CHAT_MESSAGE' && val.type === otherVal.type) {
|
2018-07-30 14:11:56 +00:00
|
|
|
return val.data.group.id === otherVal.data.group.id;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-01 19:06:24 +00:00
|
|
|
return filteredNotifications;
|
2018-02-04 12:28:05 +00:00
|
|
|
};
|
|
|
|
|
|
2016-06-07 14:14:19 +00:00
|
|
|
schema.plugin(baseModel, {
|
|
|
|
|
noSet: ['_id', 'id'],
|
2016-12-15 16:33:24 +00:00
|
|
|
// timestamps: true, // Temporarily removed to debug a possible bug
|
2016-06-07 14:14:19 +00:00
|
|
|
_id: false, // use id instead of _id
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
export const model = mongoose.model('UserNotification', schema);
|