<commit_before>// user.js
//
// A local user; distinct from a person
//
// Copyright 2011,2012 StatusNet Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var databank = require("databank"),
    _ = require("underscore"),
    DatabankObject = databank.DatabankObject,
    Stamper = require("../stamper").Stamper,
    bcrypt  = require("bcrypt"),
    Step = require("step"),
    Person = require("./person").Person,
    Stream = require("./stream").Stream,
    Activity = require("./activity").Activity,
    ActivityObject = require("./activityobject").ActivityObject,
    Edge = require("./edge").Edge,
    Favorite = require("./favorite").Favorite,
    URLMaker = require("../urlmaker").URLMaker,
    NoSuchThingError = databank.NoSuchThingError;

var User = DatabankObject.subClass("user");

exports.User = User;

// for updating

User.prototype.beforeUpdate = function(props, callback) {

    // XXX: required, but immutable. Boooooo.

    if (!_(props).has("nickname")) {
        callback(new Error("'nickname' property is required"), null);
        return;
    } else if (props.nickname !== this.nickname) {
        callback(new Error("'nickname' is immutable"), null);
        return;
    } else {
        delete props.nickname;
    }

    // XXX: required. Seems not strictly necessary, but whatever.

    if (!_(props).has("password")) {
        callback(new Error("'password' property is required"), null);
        return;
    }

    // Callers must omit or leave equal

    if (_(props).has("published")) {
        if (props.published !== this.published) {
            callback(new Error("'published' is autogenerated and immutable"), null);
            return;
        } else {
            delete props.published;
        }
    }

    // Callers must omit or leave equal

    if (_(props).has("profile")) {
        // XXX: we should probably do some deep-equality check
        if (!_(props.profile).has("id") || props.profile.id !== this.profile.id ||
            !_(props.profile).has("objectType") || props.profile.objectType !== this.profile.objectType ||
            !_(props.profile).has("displayName") || props.profile.displayName !== this.profile.displayName) {
            callback(new Error("'profile' is immutable"), null);
            return;
        } else {
            delete props.profile;
        }
    }

    // Callers must omit or leave equal

    if (_(props).has("updated") && props.updated !== this.updated) {
        callback(new Error("'updated' is autogenerated"), null);
        return;
    }

    props.updated = Stamper.stamp();

    Step(
        function() {
            bcrypt.genSalt(10, this);
        },
        function(err, salt) {
            if (err) throw err;
            bcrypt.hash(props.password, salt, this);
        },
        function(err, hash) {
            if (err) {
                callback(err, null);
            } else {
                props.passwordHash = hash;
                delete props.password;
                callback(null, props);
            }
        }
    );
};

// For creating

User.beforeCreate = function(props, callback) {

    if (!props.nickname || !props.password) {
        callback(new Error("Gotta have a nickname and a password."), null);
        return;
    }

    var now = Stamper.stamp();

    props.published = props.updated = now;

    Step(
        function() {
            bcrypt.genSalt(10, this);
        },
        function(err, salt) {
            if (err) throw err;
            bcrypt.hash(props.password, salt, this);
        },
        function(err, hash) {
            if (err) throw err;
            props.passwordHash = hash;
            delete props.password;
            Person.create({"preferredUsername": props.nickname,
                           url: URLMaker.makeURL(props.nickname),
                           displayName: props.nickname}, this);
        },
        function(err, person) {
            if (err) throw err;
            props.profile = {
                objectType: "person",
                id: person.id
            };
            callback(null, props);
        }
    );
};

User.prototype.afterCreate = function(callback) {

    var user = this;

    Step(
        function() {
            var i,
                streams = ["inbox", "outbox", "followers", "following", "favorites", "lists"],
                group = this.group();
            
            for (i = 0; i < streams.length; i++) {
                Stream.create({name: "user:" + user.nickname + ":" + streams[i]}, group());
            }
        },
        function(err, streams) {
            if (err) {
                callback(err);<commit_msg>add default lists after user create<commit_after>// user.js
//
// A local user; distinct from a person
//
// Copyright 2011,2012 StatusNet Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var databank = require("databank"),
    _ = require("underscore"),
    DatabankObject = databank.DatabankObject,
    Stamper = require("../stamper").Stamper,
    bcrypt  = require("bcrypt"),
    Step = require("step"),
    Person = require("./person").Person,
    Stream = require("./stream").Stream,
    Collection = require("./collection").Collection,
    Activity = require("./activity").Activity,
    ActivityObject = require("./activityobject").ActivityObject,
    Edge = require("./edge").Edge,
    Favorite = require("./favorite").Favorite,
    URLMaker = require("../urlmaker").URLMaker,
    NoSuchThingError = databank.NoSuchThingError;

var User = DatabankObject.subClass("user");

exports.User = User;

// for updating

User.prototype.beforeUpdate = function(props, callback) {

    // XXX: required, but immutable. Boooooo.

    if (!_(props).has("nickname")) {
        callback(new Error("'nickname' property is required"), null);
        return;
    } else if (props.nickname !== this.nickname) {
        callback(new Error("'nickname' is immutable"), null);
        return;
    } else {
        delete props.nickname;
    }

    // XXX: required. Seems not strictly necessary, but whatever.

    if (!_(props).has("password")) {
        callback(new Error("'password' property is required"), null);
        return;
    }

    // Callers must omit or leave equal

    if (_(props).has("published")) {
        if (props.published !== this.published) {
            callback(new Error("'published' is autogenerated and immutable"), null);
            return;
        } else {
            delete props.published;
        }
    }

    // Callers must omit or leave equal

    if (_(props).has("profile")) {
        // XXX: we should probably do some deep-equality check
        if (!_(props.profile).has("id") || props.profile.id !== this.profile.id ||
            !_(props.profile).has("objectType") || props.profile.objectType !== this.profile.objectType ||
            !_(props.profile).has("displayName") || props.profile.displayName !== this.profile.displayName) {
            callback(new Error("'profile' is immutable"), null);
            return;
        } else {
            delete props.profile;
        }
    }

    // Callers must omit or leave equal

    if (_(props).has("updated") && props.updated !== this.updated) {
        callback(new Error("'updated' is autogenerated"), null);
        return;
    }

    props.updated = Stamper.stamp();

    Step(
        function() {
            bcrypt.genSalt(10, this);
        },
        function(err, salt) {
            if (err) throw err;
            bcrypt.hash(props.password, salt, this);
        },
        function(err, hash) {
            if (err) {
                callback(err, null);
            } else {
                props.passwordHash = hash;
                delete props.password;
                callback(null, props);
            }
        }
    );
};

// For creating

User.beforeCreate = function(props, callback) {

    if (!props.nickname || !props.password) {
        callback(new Error("Gotta have a nickname and a password."), null);
        return;
    }

    var now = Stamper.stamp();

    props.published = props.updated = now;

    Step(
        function() {
            bcrypt.genSalt(10, this);
        },
        function(err, salt) {
            if (err) throw err;
            bcrypt.hash(props.password, salt, this);
        },
        function(err, hash) {
            if (err) throw err;
            props.passwordHash = hash;
            delete props.password;
            Person.create({"preferredUsername": props.nickname,
                           url: URLMaker.makeURL(props.nickname),
                           displayName: props.nickname}, this);
        },
        function(err, person) {
            if (err) throw err;
            props.profile = {
                objectType: "person",
                id: person.id
            };
            callback(null, props);
        }
    );
};

User.prototype.afterCreate = function(callback) {

    var user = this;

    Step(
        function() {
            var i,
                streams = ["inbox", "outbox", "followers", "following", "favorites", "lists"],
                group = this.group();
            
            for (i = 0; i < streams.length; i++) {
                Stream.create({name: "user:" + user.nickname + ":" + streams[i]}, group());
            }
        },
        function(err, streams) {
            var i,
                lists = ["Friends", "Family", "Acquaintances", "Coworkers", "Following"],
                group = this.group();

            if (err) throw err;

            for (i = 0; i < lists.length; i++) {
                Collection.create({author: user.profile, displayName: lists[i], objectTypes: ["person"]}, group());
            }
        },
        function(err, groups) {
            if (err) {
                callback(err);
{'fraction_non_alphanumeric': 0.13708781030011116, 'fraction_numerical': 0.003241941459799926, 'mean_word_length': 1.9916874480465503, 'pattern_counts': {'":': 4, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3552932', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2926, 'n_words': 785}