function wrap(my) { var Reply = createModel("Reply") .attr("updateTimeout", { type: "number", default: 1000 * 60 * 60 }) .attr("id") .attr("title") .attr("body") .attr("authorId") .attr("parentId") .attr("topicId") .attr("updateTime", { type: "date" }) .attr("createTime", { type: "date" }) .on("changed",function(reply,attrs){ my.publish("*.*.update","Reply",reply.id,this.toJSON(reply,Object.keys(attrs))); }) // update timeout .on("changing",function(reply){ if(Date.now() - reply.updateTime.getTime() > reply.updateTimeout){ reply.error("timeout","timeout"); } }) .on("creating",function(reply){ reply.attrs.createTime = reply.attrs.updateTime = new Date(); }) .method("updateInfo",function(title,body){ this.begin(); this.updateTime = new Date(); this.title = title; this.body = body; this.end(); return this.errors; }) Reply.className = "Reply"; return Reply; }
function wrap(my) { var Topic = Model("Column") Topic .attr("id", { readonly: true }) .attr("title", { min: 2, required: true, max: 35, message: "标题2~35字符" }) .attr("body", { min: 2, max: 1000, required: true, message: "内容2~1000字符" }) .attr("authorId", { required: true }) .attr("columnId", { required: true }) .attr("seal", { type: "boolean", default: false }) .attr("accessNum", { type: "number", default: 0 }) .attr("replyNum",{ type: "number", default:0 }) .attr("replyTree", { type: Node }) .attr("updateTime", { type: "date" }) .attr("createTime", { type: "date" }) .attr("top", { type: "boolean", default:false }) .on("creating", function (topic) { topic.attrs.body = escape(topic.attrs.body); topic.attrs.replyTree = new Node(); topic.attrs.createTime = topic.attrs.updateTime = new Date(); }) .on("changing", function (topic, attrs) { attrs.updateTime = new Date(); }) .on("changed", function (topic, attrs) { if(attrs.body){ topic.oattrs.body = escape(topic.oattrs.body); } var jsonObj = this.toJSON(topic, Object.keys(attrs)); my.publish("*.*.update", "Topic", topic.id, jsonObj); }) .method("removeReply", function (replyId) { var tree = this.replyTree; var node = tree.getNode(replyId); if (node) { var ids = node.allChildIds; if (tree === node) { var cids = node.childIds; cids.forEach(function (cid) { node.removeChild(cid); }); } else { tree.removeChild(replyId); } ids.push(replyId); ids.forEach(function (id) { my.repos.Reply.remove(id); }) } this.begin(); this.replyTree = this.replyTree; this.replyNum = this.replyTree.allChildIds.length; this.end(); }) .method("access", function () { var num = this.accessNum; this.accessNum = num + 1; }) .method("addReply", function (parentId, replyId) { var tree = this.replyTree; var parent = tree.getNode(parentId); if (parent) { parent.appendChild(new Node(replyId)); } this.begin(); this.replyTree = tree; this.replyNum = tree.allChildIds.length; this.end(); }) .method("updateInfo", function (title, body, columnId) { var deferred = Q.defer(); var self = this; my.services.existColumn(columnId, function (has) { if (has) { self.begin(); self.title = title; self.body = body; self.columnId = columnId; self.end(); } else { self.result.error("columnId", "没有这个栏目"); } deferred.resolve(self.result); }); return deferred.promise; }) .method("toseal", function () { this.seal = true; }) .method("unseal", function () { this.seal = false; }) Topic.className = "Topic"; return Topic; }
function wrap(my) { var Reply = createModel("Reply") .attr("updateTimeout", { type: "number", default: 1000 * 60 * 60 }) .attr("id", { readonly: true }) .attr("title", { min: 2, max: 35, required: true, message: "标题2~35字符" }) .attr("body", { min: 2, required: true, max: 1000, message: "内容2~1000字符" }) .attr("authorId", { required: true }) .attr("parentId") .attr("topicId", { required: true }) .attr("updateTime", { type: "date" }) .attr("createTime", { type: "date" }) .on("changed", function (reply, attrs) { if (attrs.body) { reply.oattrs.body = escape(reply.oattrs.body); } my.publish("*.*.update", "Reply", reply.id, this.toJSON(reply, Object.keys(attrs))); }) .on("changing", function (reply) { if (Date.now() - reply.updateTime.getTime() > reply.updateTimeout) { reply.result.error("timeout", "timeout"); } }) .on("creating", function (reply) { reply.attrs.body = escape(reply.attrs.body); reply.attrs.createTime = reply.attrs.updateTime = new Date(); }) .method("updateInfo", function (title, body) { this.begin(); this.updateTime = new Date(); this.title = title; this.body = body; this.end(); return this.result; }) Reply.className = "Reply"; return Reply; }
function wrap(my) { var Topic = Model("Column") Topic .attr("id") .attr("title") .attr("body") .attr("authorId") .attr("columnId") .attr("accessNum", { type: "number" ,default:0 }) .attr("replyTree", { type: Node }) .attr("updateTime", { type: "date" }) .attr("createTime", { type: "date" }) .use(attr) // init replyTree .on("creating", function(topic) { topic.attrs.replyTree = new Node(); topic.attrs.createTime = topic.attrs.updateTime = new Date(); }) .on("changed",function(topic,attrs){ my.publish("*.*.update","Topic",topic.id,this.toJSON(topic,Object.keys(attrs))); }) .method("removeReply", function(replyId) { var tree = this.replyTree; var node = tree.getNode(replyId); if (node) { var ids = node.allChildIds; if (tree === node) { var cids = node.childIds; cids.forEach(function(cid) { node.removeChild(cid); }); } else { tree.removeChild(replyId); } ids.push(replyId); ids.forEach(function(id) { my.repos.Reply.remove(id); }) } }) .method("access", function() { var num = this.accessNum; this.accessNum = num + 1; }) .method("addReply", function(parentId, replyId) { var tree = this.replyTree; var parent = tree.getNode(parentId); parent.appendChild(new Node(replyId)); }) .method("updateInfo", function(title, body, columnId) { this.begin(); this.title = title; this.body = body; this.columnId = columnId; this.end(); return this.errors; }) Topic.className = "Topic"; return Topic; }
function wrap(my) { var User = createModel("User"); User.roles = { USER: 0, ADMIN: 1, SEAL: 3 }; User .attr("id", { readonly: true }) .attr("follows", { type: "array", default: [] }) .attr("watchers", { type: "array", default: [] }) .attr("nickname", { min: 2, max: 15, required: true, readonly: true, validator: /^[a-zA-Z0-9\u4E00-\u9FA5]*$/, message: "昵称长度 2~15 字符,中文汉字 a~z A~Z 0-9" }) .attr("sex", { type: "boolean", default: true }) .attr("address", { max: 35, default: "", message: "长度小于35个字符" }) .attr("des", { max: 200, default: "", message: "长度小于200个字符" }) .attr("role", { required: true, type: "number", default: User.roles.USER }) .attr("password", { min: 6, max: 14, validator: /^[a-z0-9]*$/, message: "密码长度 6~14,支持a-z 0-9字符", required: true }) .attr("email", { min: 3, max: 30, message: "email格式错误!", type: "email", required: true, readonly: true }) .attr("fraction", { default: 0, type: "number" }) .attr("createTime", { type: "date" }) .attr("reportTime", { type: "date" }) .attr("isCustomLogo", { type: "boolean", default: true }) .attr("money",{ type:"number", default: 0.0 }) .method("updateInfo", function (data) { data = data || {} this.begin(); if (data.hasOwnProperty("address")) { this.address = data.address; } if (data.hasOwnProperty("des")) { this.des = data.des; } if (data.hasOwnProperty("sex")) { this.sex = data.sex; } if (data.hasOwnProperty("isCustomLogo")) { this.isCustomLogo = data.isCustomLogo; } this.end(); return this.result; }) .method("updatePassword", function (npass) { this.password = npass; return this.result; }) .method("plus", function (num) { this.fraction = this.fraction + num; return this.result; }) .method("recharge",function(money){ this.money = this.money + money; return this.result; }) .method("report", function () { var reportTime = this.reportTime; var nowTime = new Date(); if ("" + reportTime.getFullYear() + reportTime.getMonth() + reportTime.getDate() !== "" + nowTime.getFullYear() + nowTime.getMonth() + nowTime.getDate()) { this.plus(2); this.reportTime = new Date(); } }) .method("follow", function (uid) { var self = this; if (uid === this.id) return; my.repos.User.get(uid, function (err, user) { if (user) { var follows = self.follows; if (follows.indexOf(uid) === -1) { follows.push(uid); self.follows = follows; } var watchers = user.watchers; if (watchers.indexOf(self.id) === -1) { watchers.push(self.id); user.watchers = watchers; } } }) }) .method("unfollow", function (uid) { var self = this; if (uid === this.id) return; my.repos.User.get(uid, function (err, user) { var follows = self.follows; var findex = follows.indexOf(uid); if (findex !== -1) { follows.splice(findex, 1); self.follows = follows; } if (user) { var watchers = user.watchers; var windex = watchers.indexOf(self.id); if (windex !== -1) { watchers.splice(windex, 1); user.watchers = watchers; } } }) }) .method("becomeAdmin", function () { this.role = User.roles.ADMIN; }) .method("becomeUser", function () { this.role = User.roles.USER; }) .method("sealUser", function () { this.role = User.roles.SEAL; }) .on("changed", function (u, attrs) { passTransform(u); my.publish("*.*.update", "User", u.id, this.toJSON(u, Object.keys(attrs))); }) .validate(function (user, keys) { if (keys.indexOf("role") !== -1) { var role = user.attrs["role"]; if ([0, 1, 2, 3].indexOf(role) === -1) { user.result.error("role", "没有这个角色"); } } }) User.on("created", function (user) { passTransform(user); }) User.on("creating", function (user) { user.attrs.createTime = new Date(); user.attrs.reportTime = new Date(0); }) function passTransform(u) { // password transform if (u.attrs.password) { var md5 = crypto.createHash('md5'); u.oattrs.password = md5.update(u.oattrs.password).digest("hex"); } } User.className = "User"; return User; }