Files
habitica/website/server/models/userNotification.js
Matteo Pagliazzi 5f0ef2d8f0 Webhooks v2 (and other fixes) (#10265)
* begin implementing global webhooks

* add checklist item scored webhook

* add pet hatched and mount raised webhooks (no tests)

* fix typo

* add lvl up webhooks, remove corrupt notifications and reorganize pre-save hook

* fix typo

* add some tests, globalActivity webhook

* fix bug in global activiy webhook and add more tests

* add tests and fix typo for petHatched and mountRaised webhooks

* fix errors and add tests for level up webhook

* wip: add default data to all webhooks, change signature for WebhookSender.send (missing tests)

* remove unused code

* fix unit tests

* fix chat webhooks

* remove console

* fix lint

* add and fix webhook tests

* add questStarted webhook and questActivity type

* add unit tests

* add finial tests and features
2018-04-29 20:07:14 +02:00

95 lines
2.6 KiB
JavaScript

import mongoose from 'mongoose';
import baseModel from '../libs/baseModel';
import { v4 as uuid } from 'uuid';
import validator from 'validator';
const NOTIFICATION_TYPES = [
'DROPS_ENABLED',
'REBIRTH_ENABLED',
'WON_CHALLENGE',
'STREAK_ACHIEVEMENT',
'ULTIMATE_GEAR_ACHIEVEMENT',
'REBIRTH_ACHIEVEMENT',
'NEW_CONTRIBUTOR_LEVEL',
'CRON',
'GROUP_TASK_APPROVAL',
'GROUP_TASK_APPROVED',
'GROUP_TASK_NEEDS_WORK',
'LOGIN_INCENTIVE',
'GROUP_INVITE_ACCEPTED',
'SCORED_TASK',
'BOSS_DAMAGE', // Not used currently but kept to avoid validation errors
'GUILD_PROMPT',
'GUILD_JOINED_ACHIEVEMENT',
'CHALLENGE_JOINED_ACHIEVEMENT',
'INVITED_FRIEND_ACHIEVEMENT',
'CARD_RECEIVED',
'NEW_MYSTERY_ITEMS',
'UNALLOCATED_STATS_POINTS',
'NEW_INBOX_MESSAGE',
'NEW_STUFF',
'NEW_CHAT_MESSAGE',
'LEVELED_UP',
];
const Schema = mongoose.Schema;
export let schema = new Schema({
id: {
type: String,
default: uuid,
validate: [validator.isUUID, 'Invalid uuid.'],
// @TODO: Add these back once we figure out the issue with notifications
// See Fix for https://github.com/HabitRPG/habitica/issues/9923
// required: true,
},
type: {
type: String,
// @TODO: Add these back once we figure out the issue with notifications
// See Fix for https://github.com/HabitRPG/habitica/issues/9923
// required: true,
enum: NOTIFICATION_TYPES,
},
data: {type: Schema.Types.Mixed, default: () => {
return {};
}},
// 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
});
/**
* Convert notifications to JSON making sure to return only valid data.
* Fix for https://github.com/HabitRPG/habitica/issues/9923#issuecomment-362869881
* @TODO Remove once https://github.com/HabitRPG/habitica/issues/9923
* is fixed
*/
schema.statics.convertNotificationsToSafeJson = function convertNotificationsToSafeJson (notifications) {
if (!notifications) return notifications;
return notifications.filter(n => {
// Exclude notifications with a nullish value
if (!n) return false;
// Exclude notifications without an id or a type
if (!n.id || !n.type) return false;
return true;
}).map(n => {
return n.toJSON();
});
};
schema.plugin(baseModel, {
noSet: ['_id', 'id'],
// timestamps: true, // Temporarily removed to debug a possible bug
_id: false, // use id instead of _id
});
export let model = mongoose.model('UserNotification', schema);