<commit_before>const mongoose = require('mongoose');
const moment = require('moment');

const SubscriptionsSchema = new mongoose.Schema({
    type: {
        type: String,
        default: 'subscriptions'
    },
    parentUser: { // reference to parentUser
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'  
    },
    chatChannels: [{ // Array of messages (items in this channel)
        type: mongoose.Schema.Types.ObjectId,
        ref: 'ChatChannel'  
    }],
    lastRetrieved: {
        type: Date,
        default: new Date()
    }
});

SubscriptionsSchema.statics.getSubscriptionsForUser = function (userId, callback) {
    const Subscriptions = mongoose.model('Subscriptions');
    Subscriptions.findOne({parentUser: userId}).exec(function (err, res) {
        if (err) console.log(err);
        callback(err, res);
    });
};


SubscriptionsSchema.statics.subscribe = function (body, user, callback) { // requires body.chatChannelId
    const Subscriptions = mongoose.model('Subscriptions');
    const ChatChannel = mongoose.model('ChatChannel');

    if (body.type && body.id && user._id) {

        ChatChannel.findOne({_id: body.id}).exec(function (err, doc) {
            if (err) {
                callback(err, undefined);
            } else {
                Subscriptions.findOneAndUpdate({parentUser: user._id}, {$addToSet:{chatChannels: doc._id}}, {upsert: true, new: true},
                    (err, subscriptions) => {
                        if (err) console.log(err);
                        console.log(`added channel subscription ${doc._id} to user ${user._id}`);
                        callback(err, subscriptions);
                    });
            }
            if (err) console.log(err);
        });
    } else {
        callback(undefined, 'invalid input');
    }
};

SubscriptionsSchema.statics.unsubscribe = function (someId, user, callback) { // requires body.chatChannelId<commit_msg>fix crash on invalid subscription<commit_after>const mongoose = require('mongoose');
const moment = require('moment');

const SubscriptionsSchema = new mongoose.Schema({
    type: {
        type: String,
        default: 'subscriptions'
    },
    parentUser: { // reference to parentUser
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'  
    },
    chatChannels: [{ // Array of messages (items in this channel)
        type: mongoose.Schema.Types.ObjectId,
        ref: 'ChatChannel'  
    }],
    lastRetrieved: {
        type: Date,
        default: new Date()
    }
});

SubscriptionsSchema.statics.getSubscriptionsForUser = function (userId, callback) {
    const Subscriptions = mongoose.model('Subscriptions');
    Subscriptions.findOne({parentUser: userId}).exec(function (err, res) {
        if (err) console.log(err);
        callback(err, res);
    });
};


SubscriptionsSchema.statics.subscribe = function (body, user, callback) { // requires body.chatChannelId
    const Subscriptions = mongoose.model('Subscriptions');
    const ChatChannel = mongoose.model('ChatChannel');

    if (body.type && body.id && user._id) {

        ChatChannel.findOne({_id: body.id}).exec(function (err, doc) {
            if (err) {
                callback(err, undefined);
            } else if (!doc) {
                callback('chatChannel not found', undefined);
            } else {
                Subscriptions.findOneAndUpdate({parentUser: user._id}, {$addToSet:{chatChannels: doc._id}}, {upsert: true, new: true},
                    (err, subscriptions) => {
                        if (err) console.log(err);
                        console.log(`added channel subscription ${doc._id} to user ${user._id}`);
                        callback(err, subscriptions);
                    });
            }
            if (err) console.log(err);
        });
    } else {
        callback(undefined, 'invalid input');
    }
};

SubscriptionsSchema.statics.unsubscribe = function (someId, user, callback) { // requires body.chatChannelId
{'fraction_non_alphanumeric': 0.13104485091455775, 'fraction_numerical': 0.0, 'mean_word_length': 2.3433835845896147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7631872', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1100, 'n_words': 248}