<commit_before>import { Meteor } from 'meteor/meteor';
import { OnlineUsersSchema } from './onlineusers.schema';
import moment from 'moment/moment';

if (Meteor.isServer) {
    Meteor.publish('onlineUsersForRoute', function (route) {
        return OnlineUsersSchema.find({activeRoute: route});
    });
}

const checkRouteParamAndAuthorization = (route, userId) => {
    check(route, String);
    if (!userId) {
        throw new Meteor.Error('not-authorized');
    }
};

Meteor.methods({
    'onlineUsers.enterRoute'(route) {
        const userId = Meteor.userId();
        checkRouteParamAndAuthorization(route, userId);

        const doc = {
            userId: userId,
            activeRoute:route,
            updatedAt: new Date()
        };
        const selector = { userId: userId, activeRoute:route };
        const existingDoc = OnlineUsersSchema.findOne(selector);

        if (existingDoc) {
            OnlineUsersSchema.update(selector, doc);
        } else {
            OnlineUsersSchema.insert(doc);
        }

        // remove outdated entries
        const aMinAgo = moment().add(-1,'minutes').toDate();
        OnlineUsersSchema.remove({updatedAt: {"$lt" : aMinAgo}});
    },
    'onlineUsers.leaveRoute'(route) {
        const userId = Meteor.userId();
        checkRouteParamAndAuthorization(route, userId);
        OnlineUsersSchema.remove({userId: userId, activeRoute:route});
    }
});<commit_msg>Use `upsert` method of schema class<commit_after>import { Meteor } from 'meteor/meteor';
import { OnlineUsersSchema } from './onlineusers.schema';
import moment from 'moment/moment';

if (Meteor.isServer) {
    Meteor.publish('onlineUsersForRoute', function (route) {
        return OnlineUsersSchema.find({activeRoute: route});
    });
}

const checkRouteParamAndAuthorization = (route, userId) => {
    check(route, String);
    if (!userId) {
        throw new Meteor.Error('not-authorized');
    }
};

Meteor.methods({
    'onlineUsers.enterRoute'(route) {
        const userId = Meteor.userId();
        checkRouteParamAndAuthorization(route, userId);

        OnlineUsersSchema.upsert(
            { userId: userId, activeRoute:route },
            { updatedAt: new Date() }
        );

        // remove outdated entries
        const aMinAgo = moment().add(-1,'minutes').toDate();
        OnlineUsersSchema.remove({updatedAt: {"$lt" : aMinAgo}});
    },
    'onlineUsers.leaveRoute'(route) {
        const userId = Meteor.userId();
        checkRouteParamAndAuthorization(route, userId);
        OnlineUsersSchema.remove({userId: userId, activeRoute:route});
    }
});
{'fraction_non_alphanumeric': 0.13335895465026903, 'fraction_numerical': 0.0007686395080707148, 'mean_word_length': 2.9679878048780486, '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': '5000498', 'n_tokens_mistral': 742, 'n_tokens_neox': 746, 'n_words': 146}