Пример #1
0
        function() {
            var User = require("./user").User;
            if (!_(props).getPath(["links", "self", "href"])) {

                props._uuid = IDMaker.makeID();

                if (!props.id) {
                    props.id = ActivityObject.makeURI(type, props._uuid);
                }

                if (!props.published) {
                    props.published = now;
                }

                if (!props.updated) {
                    props.updated = now;
                }

                if (!_.has(props, "links")) {
                    props.links = {};
                }

                props.links.self = {
                    href: URLMaker.makeURL("api/" + type + "/" + props._uuid)
                };

                _.each(["likes", "replies", "shares"], function(feed) {

                    if (!_.has(props, feed)) {
                        props[feed] = {
                            url: URLMaker.makeURL("api/" + type + "/" + props._uuid + "/" + feed)
                        };
                    }
                });

                if (!_.has(props, "url") && 
                    _.has(props, "author") &&
                    _.isObject(props.author)) {
                    if (_.has(props.author, "preferredUsername") &&
                        _.isString(props.author.preferredUsername)) {
                        props.url = URLMaker.makeURL([props.author.preferredUsername, type, props._uuid].join("/"));
                        this(null, null);
                    } else {
                        User.fromPerson(props.author.id, this);
                    }
                } else {
                    this(null, null);
                }

            } else {
                _.each(["likes", "replies", "shares"], function(feed) {
                    // For non-new stuff, clear out volatile data
                    ActivityObject.trimCollection(props, feed);
                });
                this(null, null);
            }
        },
Пример #2
0
ActivityObject.prototype.toString = function() {
    var obj = this;

    if (!_.has(obj, "objectType")) {
        return "[activityobject]";
    } else if (!_.has(obj, "id")) {
        return "["+obj.objectType+"]";
    } else {
        return "["+obj.objectType+" "+obj.id+"]";
    }
};
Пример #3
0
ActivityObject.prototype.isFollowable = function() {
    var obj = this,
        followableTypes = [ActivityObject.PERSON];

    if (_.contains(followableTypes, obj.objectType)) {
        return true;
    } else if (_.has(obj, "links") &&
               _.has(obj.links, "activity-outbox")) {
        return true;
    } else {
        return false;
    }
};
Пример #4
0
                _.each(["likes", "replies", "shares"], function(feed) {

                    if (!_.has(props, feed)) {
                        props[feed] = {
                            url: URLMaker.makeURL("api/" + type + "/" + props._uuid + "/" + feed)
                        };
                    }
                });
Пример #5
0
    _.each(dateprops, function(dateprop) {
        // XXX: validate the date

        if (_.has(props, dateprop)) {
            if (!_.isString(props[dateprop])) {
                throw new TypeError(dateprop + " property is not a string");
            }
        }
    });
Пример #6
0
 _.each(oprops, function(name) {
     if (_.has(props, name)) {
         if (!_.isObject(props[name])) {
             throw new TypeError(name + " property is not an activity object");
         } else {
             ActivityObject.validate(props[name]);
         }
     }
 });
Пример #7
0
    _.each(uriarrayprops, function(uriarrayprop) {
        if (_.has(props, uriarrayprop)) {
            if (!_.isArray(props[uriarrayprop])) {
                throw new TypeError(uriarrayprop + " is not an array");
            }

            if (_.some(props[uriarrayprop], function(str) { return !_.isString(str); })) {
                throw new TypeError(uriarrayprop + " member is not a string");
            }

            // XXX: validate that duplicates are URIs
        }
    });
Пример #8
0
ActivityObject.beforeCreate = function(props, callback) {

    var type = this.type;

    if (!_.has(props, "objectType")) {
        props.objectType = type;
    }

    var now = Stamper.stamp();

    // Keep a timestamp for when we created something

    props._created = now;

    Step(
        function() {
            var User = require("./user").User;
            if (!_(props).getPath(["links", "self", "href"])) {

                props._uuid = IDMaker.makeID();

                if (props.id) {
                    props._foreign_id = true;
                } else {
                    props.id = ActivityObject.makeURI(type, props._uuid);
                }

                if (!props.published) {
                    props.published = now;
                }

                if (!props.updated) {
                    props.updated = now;
                }

                if (!_.has(props, "links")) {
                    props.links = {};
                }

                if (props._foreign_id) {
                    props.links.self = {
                        href: URLMaker.makeURL("api/" + type, {id: props.id})
                    };
                } else {
                    props.links.self = {
                        href: URLMaker.makeURL("api/" + type + "/" + props._uuid)
                    };
                }

                _.each(["likes", "replies", "shares"], function(feed) {

                    if (!_.has(props, feed)) {
                        if (props._foreign_id) {
                            props[feed] = {
                                url: URLMaker.makeURL("api/" + type + "/" + feed, {id: props.id})
                            };
                        } else {
                            props[feed] = {
                                url: URLMaker.makeURL("api/" + type + "/" + props._uuid + "/" + feed)
                            };
                        }
                    }
                });

                if (!_.has(props, "url") &&
                    _.has(props, "author") &&
                    _.isObject(props.author)) {
                    if (_.has(props.author, "preferredUsername") &&
                        _.isString(props.author.preferredUsername)) {
                        props.url = URLMaker.makeURL([props.author.preferredUsername, type, props._uuid].join("/"));
                        this(null, null);
                    } else {
                        User.fromPerson(props.author.id, this);
                    }
                } else {
                    this(null, null);
                }

            } else {
                _.each(["likes", "replies", "shares"], function(feed) {
                    // For non-new stuff, clear out volatile data
                    ActivityObject.trimCollection(props, feed);
                });
                this(null, null);
            }
        },
        function(err, user) {
            if (err) throw err;
            if (user) {
                props.url = URLMaker.makeURL([user.nickname, type, props._uuid].join("/"));
            }
            // Save the author by reference; don't save the whole thing
            ActivityObject.compressProperty(props, "author", this.parallel());
            ActivityObject.compressProperty(props, "inReplyTo", this.parallel());
        },
        function(err) {
            if (err) throw err;
            ActivityObject.validate(props);
            this(null);
        },
        function(err) {
            if (err) {
                callback(err, null);
            } else {
                callback(null, props);
            }
        }
    );
};
Пример #9
0
 _.each(np, function(nprop) {
     if (_.has(props, nprop) && !_.isNumber(props[nprop])) {
         throw new TypeError(nprop + " property of media link is not a number");
     }
 });
Пример #10
0
 _.each(htmlprops, function(name) {
     // XXX: validate HTML
     if (_.has(props, name) && !_.isString(props[name])) {
         throw new TypeError(name + " property is not a string");
     }
 });
Пример #11
0
ActivityObject.validate = function(props) {

    var dateprops = ["published", "updated"],
        uriarrayprops = ["downstreamDuplicates", "upstreamDuplicates"],
        htmlprops = ["content", "summary"],
        oprops = ["inReplyTo", "author"];

    // XXX: validate that id is a really-truly URI

    if (!_.isString(props.id)) {
        throw new TypeError("no id in activity object");
    }

    // XXX: validate that objectType is an URI or in our whitelist

    if (!_.isString(props.objectType)) {
        throw new TypeError("no objectType in activity object");
    }

    // XXX: validate that displayName is not HTML

    if (_.has(props, "displayName") && !_.isString(props.displayName)) {
        throw new TypeError("displayName property is not a string");
    }

    // XXX: validate that url is an URL

    if (_.has(props, "url") && !_.isString(props.url)) {
        throw new TypeError("url property is not a string");
    }

    _.each(oprops, function(name) {
        if (_.has(props, name)) {
            if (!_.isObject(props[name])) {
                throw new TypeError(name + " property is not an activity object");
            } else {
                ActivityObject.validate(props[name]);
            }
        }
    });

    // Validate attachments

    if (_.has(props, "attachments")) {
        if (!_.isArray(props.attachments)) {
            throw new TypeError("attachments is not an array");
        }
        _.each(props.attachments, function(attachment) {
            if (!_.isObject(attachment)) {
                throw new TypeError("attachment is not an object");
            }
            ActivityObject.validate(attachment);
        });
    }

    _.each(uriarrayprops, function(uriarrayprop) {
        if (_.has(props, uriarrayprop)) {
            if (!_.isArray(props[uriarrayprop])) {
                throw new TypeError(uriarrayprop + " is not an array");
            }

            if (_.some(props[uriarrayprop], function(str) { return !_.isString(str); })) {
                throw new TypeError(uriarrayprop + " member is not a string");
            }

            // XXX: validate that duplicates are URIs
        }
    });

    if (_.has(props, "image")) {
        if (!_.isObject(props.image)) {
            throw new TypeError("image property is not an object");
        }

        ActivityObject.validateMediaLink(props.image);
    }

    _.each(dateprops, function(dateprop) {
        // XXX: validate the date

        if (_.has(props, dateprop)) {
            if (!_.isString(props[dateprop])) {
                throw new TypeError(dateprop + " property is not a string");
            }
        }
    });

    _.each(htmlprops, function(name) {
        // XXX: validate HTML
        if (_.has(props, name) && !_.isString(props[name])) {
            throw new TypeError(name + " property is not a string");
        }
    });

    return;
};