Files
habitica/website/server/models/user/methods.js
Matteo Pagliazzi 45c31a2bcf Deprecate API v2 (#7761)
* deprecate api-v2

* remove v2 test helpers

* remove unused string
2016-07-16 19:56:18 +02:00

43 lines
1.3 KiB
JavaScript

import shared from '../../../../common';
import Bluebird from 'bluebird';
import {
chatDefaults,
TAVERN_ID,
} from '../group';
import { defaults } from 'lodash';
import schema from './schema';
schema.methods.isSubscribed = function isSubscribed () {
return !!this.purchased.plan.customerId; // eslint-disable-line no-implicit-coercion
};
// Get an array of groups ids the user is member of
schema.methods.getGroups = function getUserGroups () {
let userGroups = this.guilds.slice(0); // clone user.guilds so we don't modify the original
if (this.party._id) userGroups.push(this.party._id);
userGroups.push(TAVERN_ID);
return userGroups;
};
schema.methods.sendMessage = async function sendMessage (userToReceiveMessage, message) {
let sender = this;
shared.refPush(userToReceiveMessage.inbox.messages, chatDefaults(message, sender));
userToReceiveMessage.inbox.newMessages++;
userToReceiveMessage._v++;
userToReceiveMessage.markModified('inbox.messages');
shared.refPush(sender.inbox.messages, defaults({sent: true}, chatDefaults(message, userToReceiveMessage)));
sender.markModified('inbox.messages');
let promises = [userToReceiveMessage.save(), sender.save()];
await Bluebird.all(promises);
};
schema.methods.addNotification = function addUserNotification (type, data = {}) {
this.notifications.push({
type,
data,
});
};