<commit_before>// api
Friends = {};

STATUSES     = {};
STATUSES.pending = "pending"; // for the user who sent the request
STATUSES.friend  = "friend";  // when both users are friends
STATUSES.request = "request"; // for the user on the recieving end of the request
STATUSES.empty   = "empty";   // used for denied requests and removed friends

if (Meteor.isServer) {
    var _onCreateUser = Accounts.onCreateUser.bind(Accounts);
    // Since onCreateUser overrides default behavior, and we don't want to restrict package users
    // by removing the onCreateUser function, we override onCreateUser to modify the user document before the regular onCreateUser call.

    // TODO: This doesn't work, I'll comment it out until someone finds a solution http://stackoverflow.com/questions/25583543/overriding-a-package-method-in-meteor
    // Accounts.onCreateUser = function (func) {
    //     console.log('onCreateUser definition');
    //     _onCreateUser(function (options, user) {
    //         console.log('onCreateUser call, the user should now have a profile');
    //         if (!user.profile) {
    //             user.profile = options.profile || {};
    //         }
    //         if (!user.profile.friends) {
    //             user.profile.friends = [];
    //         }
    //         return func(options, user);
    //     });
    // };

    // TODO: using the dirty way here, now people can't use Accounts.onCreateUser or this package will fail to work. This DOES work though
    Accounts.onCreateUser(function (options, user) {
        if (!user.profile) {
            user.profile = options.profile || {};
        }
        if (!user.profile.friends) {
            user.profile.friends = [];
        }
        return user;
    });
}

Meteor.methods({
    addFriend: function (userId, friendId, status) {
        Meteor.users.update({_id: userId},
            {$addToSet: {"profile.friends": {_id: friendId, status: status}}});
    },
    /**
     * Updates the status of two friends.
     * The first user's status will be applied to the second users reference to
     * the first user and the other way around. The second user's status wil be
     * applied to the first user's reference to the second user.
     *
     * This might be a bit confusing so here's an example
     *
     * We have two users, this state resembles that of just after one
     * user sent a friend request to someone else
     * by user1 to user2
     * user1 {profile.friends = [{
     *   userId: user2,
     *   status: "pending"
     * }]}
     * user2 {profile.friends = [{
     *     userId: user1,
     *     status: "request"
     * }]}
     *
     * calling updateFriendStatus ({
     *     userId: user1,
     *     status: "updatedStatus1" // using fake statuses for clarity
     * },{
     *     userId: user2,
     *     status: "updatedStatus2"
     * })
     *
     * will result in
     * user1 {profile.friends = [{
     *   userId: user2,
     *   status: "updatedStatus2"
     * }]}
     * user2 {profile.friends = [{
     *     userId: user1,
     *     status: "updatedStatus1"
     * }]}
     *
     * The userStatus objects are structured like this
     * {
     *     userId: "340usdfh0932ujweh032ur",
     *     status: "friend" // a status from STATUSES
     * }
     */
    updateFriendStatus: function (userStatus, userStatus2) {
        if (!this.isSimulation) {
            var updateStatus = function (userId, friendId, status) {
                Meteor.users.update({_id: userId, "profile.friends._id": friendId}, {$set: {"profile.friends.$.status": status}});
            };
            updateStatus(userStatus.userId, userStatus2.userId, userStatus2.status);
            updateStatus(userStatus2.userId, userStatus.userId, userStatus.status);
        }
    },
    friendRequest: function (userId, friendId) {
        console.dir(STATUSES);
        if (!validations.areRelated(userId, friendId)) {
            Meteor.call('addFriend', userId, friendId, STATUSES.pending);
            Meteor.call('addFriend', friendId, userId, STATUSES.request);
        }
    },
    confirmRequest: function (userId, friendId) {
        var validated = validations.validateUserRelations(
            {userId: userId, status: STATUSES.pending},
            {userId: friendId, status: STATUSES.request}
        );
        if (validated) {
            Meteor.call('updateFriendStatus',<commit_msg>Removed a console.dir call<commit_after>// api
Friends = {};

STATUSES     = {};
STATUSES.pending = "pending"; // for the user who sent the request
STATUSES.friend  = "friend";  // when both users are friends
STATUSES.request = "request"; // for the user on the recieving end of the request
STATUSES.empty   = "empty";   // used for denied requests and removed friends

if (Meteor.isServer) {
    var _onCreateUser = Accounts.onCreateUser.bind(Accounts);
    // Since onCreateUser overrides default behavior, and we don't want to restrict package users
    // by removing the onCreateUser function, we override onCreateUser to modify the user document before the regular onCreateUser call.

    // TODO: This doesn't work, I'll comment it out until someone finds a solution http://stackoverflow.com/questions/25583543/overriding-a-package-method-in-meteor
    // Accounts.onCreateUser = function (func) {
    //     console.log('onCreateUser definition');
    //     _onCreateUser(function (options, user) {
    //         console.log('onCreateUser call, the user should now have a profile');
    //         if (!user.profile) {
    //             user.profile = options.profile || {};
    //         }
    //         if (!user.profile.friends) {
    //             user.profile.friends = [];
    //         }
    //         return func(options, user);
    //     });
    // };

    // TODO: using the dirty way here, now people can't use Accounts.onCreateUser or this package will fail to work. This DOES work though
    Accounts.onCreateUser(function (options, user) {
        if (!user.profile) {
            user.profile = options.profile || {};
        }
        if (!user.profile.friends) {
            user.profile.friends = [];
        }
        return user;
    });
}

Meteor.methods({
    addFriend: function (userId, friendId, status) {
        Meteor.users.update({_id: userId},
            {$addToSet: {"profile.friends": {_id: friendId, status: status}}});
    },
    /**
     * Updates the status of two friends.
     * The first user's status will be applied to the second users reference to
     * the first user and the other way around. The second user's status wil be
     * applied to the first user's reference to the second user.
     *
     * This might be a bit confusing so here's an example
     *
     * We have two users, this state resembles that of just after one
     * user sent a friend request to someone else
     * by user1 to user2
     * user1 {profile.friends = [{
     *   userId: user2,
     *   status: "pending"
     * }]}
     * user2 {profile.friends = [{
     *     userId: user1,
     *     status: "request"
     * }]}
     *
     * calling updateFriendStatus ({
     *     userId: user1,
     *     status: "updatedStatus1" // using fake statuses for clarity
     * },{
     *     userId: user2,
     *     status: "updatedStatus2"
     * })
     *
     * will result in
     * user1 {profile.friends = [{
     *   userId: user2,
     *   status: "updatedStatus2"
     * }]}
     * user2 {profile.friends = [{
     *     userId: user1,
     *     status: "updatedStatus1"
     * }]}
     *
     * The userStatus objects are structured like this
     * {
     *     userId: "340usdfh0932ujweh032ur",
     *     status: "friend" // a status from STATUSES
     * }
     */
    updateFriendStatus: function (userStatus, userStatus2) {
        if (!this.isSimulation) {
            var updateStatus = function (userId, friendId, status) {
                Meteor.users.update({_id: userId, "profile.friends._id": friendId}, {$set: {"profile.friends.$.status": status}});
            };
            updateStatus(userStatus.userId, userStatus2.userId, userStatus2.status);
            updateStatus(userStatus2.userId, userStatus.userId, userStatus.status);
        }
    },
    friendRequest: function (userId, friendId) {
        if (!validations.areRelated(userId, friendId)) {
            Meteor.call('addFriend', userId, friendId, STATUSES.pending);
            Meteor.call('addFriend', friendId, userId, STATUSES.request);
        }
    },
    confirmRequest: function (userId, friendId) {
        var validated = validations.validateUserRelations(
            {userId: userId, status: STATUSES.pending},
            {userId: friendId, status: STATUSES.request}
        );
        if (validated) {
            Meteor.call('updateFriendStatus',
{'fraction_non_alphanumeric': 0.11673107524437372, 'fraction_numerical': 0.008638326892475562, 'mean_word_length': 2.2289908256880735, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, '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': '5640541', 'n_tokens_mistral': 2569, 'n_tokens_neox': 2501, 'n_words': 719}