<commit_before>'use strict';

var Promise = require('bluebird'),
    winston = require('winston'),
    logger = winston.loggers.get('webapi');

var User = function (storage) {
    this.storage = storage;
};

User.prototype = {

    findUser: function (jid) {
        var storage = this.storage;
        return new Promise(function (resolve, reject) {
            storage.User.find({
                where: {
                    jid: jid
                }
            }).success(function (user) {
                resolve(user);
            }).error(function () {
                reject();
            });
        });
    },

    delRoom: function (room) {
        return new Promise(function (resolve, reject) {
            room.destroy().success(function () {
                resolve();
            }).error(function (err) {
                reject(err);
            });
        });
    },

    getRoom: function (owner, roomname) {
        var storage = this.storage;
        return new Promise(function (resolve, reject) {
            if (owner) {
                var affiliation = [];
                affiliation.push(storage.RoomMembers.Affiliation.Owner);

                // Owner as default affiliation
                owner.getRooms({
                    where: {
                        name: roomname,
                        affiliation: affiliation
                    }
                }).success(function (ownerRooms) {
                    if (ownerRooms && ownerRooms.length >= 1) {
                        resolve(ownerRooms[0]);
                    } else {
                        reject();
                    }
                }).error(function () {
                    reject();
                });
            } else {
                reject();
            }
        });
    },

    getRooms: function (user, type) {
        var storage = this.storage;
        return new Promise(function (resolve, reject) {
            if (user) {

                type = type || 'all';

                var affiliation = [];

                switch (type) {
                case 'owner':
                    affiliation.push(storage.RoomMembers.Affiliation.Owner);
                    break;
                case 'member':
                    affiliation.push(storage.RoomMembers.Affiliation.Member);
                    break;
                default: // all 
                    affiliation.push(storage.RoomMembers.Affiliation.Owner);
                    affiliation.push(storage.RoomMembers.Affiliation.Member);
                    break;
                }

                // Owner as default affiliation
                user.getRooms({
                    where: {
                        affiliation: affiliation
                    }
                }).success(function (ownerRooms) {
                    resolve(ownerRooms);
                }).error(function () {
                    reject();
                });
            } else {
                reject();
            }

        });
    },

    addRoom: function (user, data) {
        var storage = this.storage;
        return new Promise(function (resolve, reject) {

            if (user) {
                logger.debug('user' + JSON.stringify(user));

                storage.Room.create({
                    name: data.name
                }).success(function (room) {

                    logger.debug('room:' + JSON.stringify(room));
                    user.addRoom(room, {
                        role: storage.RoomMembers.Role.Moderator,
                        affiliation: storage.RoomMembers.Affiliation.Owner,
                        nickname: 'sdfdsf'
                    }).success(function () {<commit_msg>store subject and description for rooms<commit_after>'use strict';

var Promise = require('bluebird'),
    winston = require('winston'),
    logger = winston.loggers.get('webapi');

var User = function (storage) {
    this.storage = storage;
};

User.prototype = {

    findUser: function (jid) {
        var storage = this.storage;
        return new Promise(function (resolve, reject) {
            storage.User.find({
                where: {
                    jid: jid
                }
            }).success(function (user) {
                resolve(user);
            }).error(function () {
                reject();
            });
        });
    },

    delRoom: function (room) {
        return new Promise(function (resolve, reject) {
            room.destroy().success(function () {
                resolve();
            }).error(function (err) {
                reject(err);
            });
        });
    },

    getRoom: function (owner, roomname) {
        var storage = this.storage;
        return new Promise(function (resolve, reject) {
            if (owner) {
                var affiliation = [];
                affiliation.push(storage.RoomMembers.Affiliation.Owner);

                // Owner as default affiliation
                owner.getRooms({
                    where: {
                        name: roomname,
                        affiliation: affiliation
                    }
                }).success(function (ownerRooms) {
                    if (ownerRooms && ownerRooms.length >= 1) {
                        resolve(ownerRooms[0]);
                    } else {
                        reject();
                    }
                }).error(function () {
                    reject();
                });
            } else {
                reject();
            }
        });
    },

    getRooms: function (user, type) {
        var storage = this.storage;
        return new Promise(function (resolve, reject) {
            if (user) {

                type = type || 'all';

                var affiliation = [];

                switch (type) {
                case 'owner':
                    affiliation.push(storage.RoomMembers.Affiliation.Owner);
                    break;
                case 'member':
                    affiliation.push(storage.RoomMembers.Affiliation.Member);
                    break;
                default: // all 
                    affiliation.push(storage.RoomMembers.Affiliation.Owner);
                    affiliation.push(storage.RoomMembers.Affiliation.Member);
                    break;
                }

                // Owner as default affiliation
                user.getRooms({
                    where: {
                        affiliation: affiliation
                    }
                }).success(function (ownerRooms) {
                    resolve(ownerRooms);
                }).error(function () {
                    reject();
                });
            } else {
                reject();
            }

        });
    },

    addRoom: function (user, data) {
        var storage = this.storage;
        return new Promise(function (resolve, reject) {

            if (user) {
                logger.debug('user' + JSON.stringify(user));

                storage.Room.create({
                    name: data.name,
                    subject : data.subject,
                    description: data.description
                }).success(function (room) {

                    logger.debug('room:' + JSON.stringify(room));
                    user.addRoom(room, {
                        role: storage.RoomMembers.Role.Moderator,
                        affiliation: storage.RoomMembers.Affiliation.Owner,
                        nickname: ''
                    }).success(function () {
{'fraction_non_alphanumeric': 0.10710003996270148, 'fraction_numerical': 0.0005328360197149328, 'mean_word_length': 1.0931140228603289, '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': '2919289', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1648, 'n_words': 346}