habitica/website/server/models/userNotification.js

156 lines
5 KiB
JavaScript
Raw Permalink Normal View History

Onboarding guide and initial achievements refactoring (#11536) * add achievements to user * add placeholder strings * add to achievements to common script * add onboarding achievements category * add notifications * more notifications * award achievements * wip notification panel * add achievements icons and copy * do not count onboarding tasks for the created task achievement * add notes * sprites, fixes and completion status and reward * add onboarding panel * add toggle * fix toggle size * fix tests * fix typo * add notification * start adding modal * fix remove button positionin, timeout, progress bar * modal + fixes * disable broken social links from level up modal * change toggle icon color on hover * add border bottom to onboarding guide panel * add collapse animation * expanded onboarding on first open * onboarding: flip toggle colors * onboarding: show progress bar all the time * onboarding: fix panel closing on click * onboarding modal: add close icon and fix padding * wip: add migration for existing users * fix titles in guide * fix achievements copy * do not award completed task achievement when direction is down * start implementing new achievements * start migrating client * remove social links from achievements modals * prevent skipping tutorial + fix achievement notification * sync fixes * start redesign achievement modal * misc fixes to achievements, polish generic achievement modal and hatched pet modal * add special badge for onboarding * fix badge condition * modals fixes * hatched pet modal: add close icon * fix badge typo * fix justin button * new scrolling behavior for dropdowns * fix strings capitalization * add common tests * add api unit tests * add date check * achievements modal polishing * typos * add toggle for achievements categories * typo * fix test * fix edit avatar modal cannot be closed * finish migration and correct launch date * fix migration * migration fixes * fix tests
2019-12-16 16:20:47 +00:00
import _ from 'lodash';
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';
const NOTIFICATION_TYPES = [
// general notifications
'CARD_RECEIVED',
'CRON',
'DROP_CAP_REACHED',
'DROPS_ENABLED', // unused
'FIRST_DROPS',
'GIFT_ONE_GET_ONE',
'GROUP_INVITE_ACCEPTED',
'GROUP_TASK_APPROVAL',
'GROUP_TASK_APPROVED',
'GROUP_TASK_ASSIGNED',
'GROUP_TASK_CLAIMED',
2018-01-20 16:28:50 +00:00
'GROUP_TASK_NEEDS_WORK',
'GUILD_PROMPT',
'LEVELED_UP', // not in use
'LOGIN_INCENTIVE',
'NEW_CHAT_MESSAGE',
'NEW_CONTRIBUTOR_LEVEL',
2018-01-20 16:28:50 +00:00
'NEW_INBOX_MESSAGE',
'NEW_MYSTERY_ITEMS',
2018-01-20 16:28:50 +00:00
'NEW_STUFF',
Onboarding guide and initial achievements refactoring (#11536) * add achievements to user * add placeholder strings * add to achievements to common script * add onboarding achievements category * add notifications * more notifications * award achievements * wip notification panel * add achievements icons and copy * do not count onboarding tasks for the created task achievement * add notes * sprites, fixes and completion status and reward * add onboarding panel * add toggle * fix toggle size * fix tests * fix typo * add notification * start adding modal * fix remove button positionin, timeout, progress bar * modal + fixes * disable broken social links from level up modal * change toggle icon color on hover * add border bottom to onboarding guide panel * add collapse animation * expanded onboarding on first open * onboarding: flip toggle colors * onboarding: show progress bar all the time * onboarding: fix panel closing on click * onboarding modal: add close icon and fix padding * wip: add migration for existing users * fix titles in guide * fix achievements copy * do not award completed task achievement when direction is down * start implementing new achievements * start migrating client * remove social links from achievements modals * prevent skipping tutorial + fix achievement notification * sync fixes * start redesign achievement modal * misc fixes to achievements, polish generic achievement modal and hatched pet modal * add special badge for onboarding * fix badge condition * modals fixes * hatched pet modal: add close icon * fix badge typo * fix justin button * new scrolling behavior for dropdowns * fix strings capitalization * add common tests * add api unit tests * add date check * achievements modal polishing * typos * add toggle for achievements categories * typo * fix test * fix edit avatar modal cannot be closed * finish migration and correct launch date * fix migration * migration fixes * fix tests
2019-12-16 16:20:47 +00:00
'ONBOARDING_COMPLETE',
'REBIRTH_ENABLED',
'SCORED_TASK',
'UNALLOCATED_STATS_POINTS',
'WON_CHALLENGE',
'ITEM_RECEIVED', // notify user when they've got goodies via migration
// achievement notifications
Onboarding guide and initial achievements refactoring (#11536) * add achievements to user * add placeholder strings * add to achievements to common script * add onboarding achievements category * add notifications * more notifications * award achievements * wip notification panel * add achievements icons and copy * do not count onboarding tasks for the created task achievement * add notes * sprites, fixes and completion status and reward * add onboarding panel * add toggle * fix toggle size * fix tests * fix typo * add notification * start adding modal * fix remove button positionin, timeout, progress bar * modal + fixes * disable broken social links from level up modal * change toggle icon color on hover * add border bottom to onboarding guide panel * add collapse animation * expanded onboarding on first open * onboarding: flip toggle colors * onboarding: show progress bar all the time * onboarding: fix panel closing on click * onboarding modal: add close icon and fix padding * wip: add migration for existing users * fix titles in guide * fix achievements copy * do not award completed task achievement when direction is down * start implementing new achievements * start migrating client * remove social links from achievements modals * prevent skipping tutorial + fix achievement notification * sync fixes * start redesign achievement modal * misc fixes to achievements, polish generic achievement modal and hatched pet modal * add special badge for onboarding * fix badge condition * modals fixes * hatched pet modal: add close icon * fix badge typo * fix justin button * new scrolling behavior for dropdowns * fix strings capitalization * add common tests * add api unit tests * add date check * achievements modal polishing * typos * add toggle for achievements categories * typo * fix test * fix edit avatar modal cannot be closed * finish migration and correct launch date * fix migration * migration fixes * fix tests
2019-12-16 16:20:47 +00:00
'ACHIEVEMENT', // generic achievement notification, details inside `notification.data`
'CHALLENGE_JOINED_ACHIEVEMENT',
'GUILD_JOINED_ACHIEVEMENT',
'ACHIEVEMENT_PARTY_ON',
'ACHIEVEMENT_PARTY_UP',
'INVITED_FRIEND_ACHIEVEMENT',
'REBIRTH_ACHIEVEMENT',
'STREAK_ACHIEVEMENT',
'ULTIMATE_GEAR_ACHIEVEMENT',
'ACHIEVEMENT_STABLE',
'ACHIEVEMENT_QUESTS',
'ACHIEVEMENT_ANIMAL_SET',
'ACHIEVEMENT_PET_COLOR',
'ACHIEVEMENT_MOUNT_COLOR',
'ACHIEVEMENT_PET_SET_COMPLETE',
// Deprecated notification types. Can be removed once old data is cleaned out
'BOSS_DAMAGE', // deprecated
'ACHIEVEMENT_ALL_YOUR_BASE', // deprecated
'ACHIEVEMENT_BACK_TO_BASICS', // deprecated
'ACHIEVEMENT_JUST_ADD_WATER', // deprecated
'ACHIEVEMENT_LOST_MASTERCLASSER', // deprecated
'ACHIEVEMENT_MIND_OVER_MATTER', // deprecated
'ACHIEVEMENT_DUST_DEVIL', // deprecated
'ACHIEVEMENT_ARID_AUTHORITY', // deprecated
'ACHIEVEMENT_PARTY_UP', // deprecated
'ACHIEVEMENT_PARTY_ON', // deprecated
'ACHIEVEMENT_BEAST_MASTER', // deprecated
'ACHIEVEMENT_MOUNT_MASTER', // deprecated
'ACHIEVEMENT_TRIAD_BINGO', // deprecated
'ACHIEVEMENT_MONSTER_MAGUS', // deprecated
'ACHIEVEMENT_UNDEAD_UNDERTAKER', // deprecated
'ACHIEVEMENT_PRIMED_FOR_PAINTING', // deprecated
'ACHIEVEMENT_PEARLY_PRO', // deprecated
'ACHIEVEMENT_TICKLED_PINK', // deprecated
'ACHIEVEMENT_ROSY_OUTLOOK', // deprecated
'ACHIEVEMENT_BUG_BONANZA', // deprecated
'ACHIEVEMENT_BARE_NECESSITIES', // deprecated
'ACHIEVEMENT_FRESHWATER_FRIENDS', // deprecated
'ACHIEVEMENT_GOOD_AS_GOLD', // deprecated
'ACHIEVEMENT_ALL_THAT_GLITTERS', // deprecated
'ACHIEVEMENT_BONE_COLLECTOR', // deprecated
'ACHIEVEMENT_SKELETON_CREW', // deprecated
'ACHIEVEMENT_SEEING_RED', // deprecated
'ACHIEVEMENT_RED_LETTER_DAY', // deprecated
'ACHIEVEMENT_LEGENDARY_BESTIARY', // deprecated
'ACHIEVEMENT_SEASONAL_SPECIALIST', // deprecated
'ACHIEVEMENT_VIOLETS_ARE_BLUE', // deprecated
'ACHIEVEMENT_WILD_BLUE_YONDER', // deprecated
'ACHIEVEMENT_DOMESTICATED', // deprecated
'ACHIEVEMENT_SHADY_CUSTOMER', // deprecated
'ACHIEVEMENT_SHADE_OF_IT_ALL', // deprecated
'ACHIEVEMENT_ZODIAC_ZOOKEEPER', // deprecated
'ACHIEVEMENT_BIRDS_OF_A_FEATHER', // deprecated
];
2019-10-08 14:57:10 +00:00
const { Schema } = mongoose;
2019-10-08 14:57:10 +00:00
export const schema = new Schema({
id: {
$type: String,
default: uuid,
validate: [v => validator.isUUID(v), 'Invalid uuid for userNotification.'],
2020-03-01 12:30:22 +00:00
required: true,
},
type: {
$type: String,
2020-03-01 12:30:22 +00:00
required: true,
enum: NOTIFICATION_TYPES,
},
2019-10-08 14:57:10 +00:00
data: {
$type: Schema.Types.Mixed,
default: () => ({}),
},
Notifications v2 and Bailey API (#9716) * Added initial bailey api * wip * implement new panel header * Fixed lint * add ability to mark notification as seen * add notification count, remove top badge from user and add ability to mark multiple notifications as seen * add support dismissall and mark all as read * do not dismiss actionable notif * mark as seen when menu is opened instead of closed * implement ordering, list of actionable notifications * add groups messages and fix badges count * add notifications for received cards * send card received notification to target not sender * rename notificaion field * fix integration tests * mark cards notifications as read and update tests * add mystery items notifications * add unallocated stats points notifications * fix linting * simplify code * refactoring and fixes * fix dropdown opening * start splitting notifications into their own component * add notifications for inbox messages * fix unit tests * fix default buttons styles * add initial bailey support * add title and tests to new stuff notification * add notification if a group task needs more work * add tests and fixes for marking a task as needing more work * make sure user._v is updated * remove console.log * notification: hover status and margins * start styling notifications, add separate files and basic functionalities * fix tests * start adding mystery items notification * wip card notification * fix cards text * initial implementation inbox messages * initial implementation group messages * disable inbox notifications until mobile is ready * wip group chat messages * finish mystery and card notifications * add bailey notification and fix a lot of stuff * start adding guilds and parties invitations * misc invitation fixes * fix lint issues * remove old code and add key to notifications * fix tests * remove unused code * add link for public guilds invite * starts to implement needs work notification design and feature * fixes to needs work, add group task approved notification * finish needs work feature * lots of fixes * implement quest notification * bailey fixes and static page * routing fixes * fixes # this.$store.dispatch(guilds:join, {groupId: group.id, type: party}); * read notifications on click * chat notifications * fix tests for chat notifications * fix chat notification test * fix tests * fix tests (again) * try awaiting * remove only * more sleep * add bailey tests * fix icons alignment * fix issue with multiple points notifications * remove merge code * fix rejecting guild invitation * make remove area bigger * fix error with notifications and add migration * fix migration * fix typos * add cleanup migration too * notifications empty state, new counter color, fix marking messages as seen in guilds * fixes * add image and install correct packages * fix mongoose version * update bailey * typo * make sure chat is marked as read after other requests
2018-01-31 10:55:39 +00:00
// A field to mark the notification as seen without deleting it, optional use
seen: {
$type: Boolean,
// required: true,
default: () => false,
},
}, {
strict: true,
minimize: false, // So empty objects are returned
_id: false, // use id instead of _id,
typeKey: '$type', // So that we can use fields named `type`
});
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;
});
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
filteredNotifications = _.uniqWith(filteredNotifications, (val, otherVal) => {
2020-03-01 19:06:24 +00:00
if (val.type === 'NEW_CHAT_MESSAGE' && val.type === otherVal.type) {
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
};
schema.plugin(baseModel, {
noSet: ['_id', 'id'],
// timestamps: true, // Temporarily removed to debug a possible bug
_id: false, // use id instead of _id
});
2019-10-08 14:57:10 +00:00
export const model = mongoose.model('UserNotification', schema);