ozserver
Version:
API for OZ
255 lines (239 loc) • 8.7 kB
JavaScript
var EventEmitter, Feed, Post, User, exports, formidable, mongoose, sha1,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
mongoose = require('mongoose');
sha1 = require('sha1');
formidable = require('formidable');
EventEmitter = require('events').EventEmitter;
User = mongoose.model('User', require(global.home + '/script/views/db/user'));
Post = mongoose.model('Post', require(global.home + '/script/views/db/post'));
Feed = (function(_super) {
__extends(Feed, _super);
function Feed(req, res) {
var _this = this;
this.req = req;
this.res = res;
this.auth = require(global.home + '/script/controllers/auth')(this.req, this.res);
this.on('send', function(user, post) {
_this.emit('response', {
model: _this.mdl.model,
user: user != null ? user : {},
post: post != null ? post : {},
sessid: _this.req.sessionID != null ? _this.req.sessionID : null,
aid: _this.req.body.aid != null ? _this.req.body.aid : null
});
if ((_this.req.body.model != null) || (_this.req.body.sessid != null)) {
_this.res.set({
'Content-Type': (req.headers.accept || '').indexOf('application/json') !== -1 ? 'application/json; charset=utf-8' : 'text/plain; charset=utf-8'
});
return _this.res.json(_this.mdl.model);
} else {
return _this.res.jsonp(_this.mdl.model);
}
});
this.on('userNotFound', function() {
_this.mdl.emit('userNotFound');
return _this.emit('send');
});
this.on('postNotFound', function() {
_this.mdl.emit('postNotFound');
return _this.emit('send');
});
this.on('postSuccess', function(user, post) {
_this.mdl.emit('success');
return _this.emit('send', user, post);
});
this.on('editSuccess', function(user, post) {
_this.mdl.emit('success');
return _this.emit('send', user, post);
});
this.on('deleteSuccess', function(user, post) {
_this.mdl.emit('success');
return _this.emit('send', user, post);
});
this.on('post', function() {
var model, _ref, _ref1;
if (((_ref = _this.req.query) != null ? _ref.model : void 0) != null) {
model = JSON.parse(_this.req.query.model);
}
if (((_ref1 = _this.req.body) != null ? _ref1.model : void 0) != null) {
model = JSON.parse(_this.req.body.model);
}
if (model == null) {
model = {};
}
_this.mdl = require(global.home + '/script/models/feed/post')(model);
if (_this.mdl.check() === true) {
return _this.auth.user(function(err, user) {
var post;
if ((err != null) || (user == null)) {
return _this.emit('userNotFound');
} else {
post = _this.post(user);
post.save();
return _this.emit('postSuccess', user.toJSON(), post.toJSON());
}
});
} else {
return _this.emit('send');
}
});
this.on('get', function() {
return _this.get(function(posts) {
var post, seria, _i, _len;
posts = JSON.parse(JSON.stringify(posts));
seria = sha1(JSON.stringify(posts));
for (_i = 0, _len = posts.length; _i < _len; _i++) {
post = posts[_i];
post.seria = seria;
}
_this.mdl = require(global.home + '/script/models/feed/get')(posts);
return _this.emit('send');
});
});
this.on('edit', function() {
var model, _ref, _ref1, _ref2;
model = ((_ref = _this.req.query) != null ? _ref.model : void 0) != null ? JSON.parse(_this.req.query.model) : {};
if (((_ref1 = _this.req.query) != null ? _ref1.model : void 0) != null) {
model = JSON.parse(_this.req.query.model);
}
if (((_ref2 = _this.req.body) != null ? _ref2.model : void 0) != null) {
model = JSON.parse(_this.req.body.model);
}
if (model == null) {
model = {};
}
_this.mdl = require(global.home + '/script/models/feed/edit')(model);
if (_this.mdl.check() === true) {
return _this.auth.user(function(err, user) {
if ((err != null) || (user == null)) {
return _this.emit('userNotFound');
} else {
return _this.findPost(function(post) {
if (user.id.toString() === post.author.id.toString()) {
post.message.text = _this.mdl.model.message.text;
post.save();
return _this.emit('editSuccess', user.toJSON(), post.toJSON());
} else {
return _this.emit('userNotFound');
}
});
}
});
} else {
return _this.emit('send');
}
});
this.on('update', function() {
var model, _ref;
model = ((_ref = _this.req.query) != null ? _ref.model : void 0) != null ? JSON.parse(_this.req.query.model) : {};
_this.mdl = require(global.home + '/script/models/feed/get')(model);
if (_this.req.route.params.seria != null) {
return _this.get(function(posts) {
var seria;
seria = sha1(JSON.stringify(posts));
if (seria !== _this.req.route.params.seria) {
_this.mdl.needUpdate();
}
return _this.emit('send');
});
} else {
return _this.emit('send');
}
});
this.on('delete', function() {
_this.mdl = require(global.home + '/script/models/feed/delete')({});
return _this.auth.user(function(err, user) {
if ((err != null) || (user == null)) {
return _this.emit('userNotFound');
} else {
return _this.findPost(function(post) {
if (user.id.toString() === post.author.id.toString()) {
post.disabled = true;
post.save();
return _this.emit('deleteSuccess', user.toJSON(), post.toJSON());
} else {
return _this.emit('userNotFound');
}
});
}
});
});
this.on('upload', function() {
var form;
form = new formidable.IncomingForm();
return form.parse(_this.req, function(err, fields, files) {
console.log('err', err);
console.log('fields', fields);
return console.log('files', files);
});
});
}
Feed.prototype.get = function(callback) {
var _ref,
_this = this;
return Post.find({
'region.name': this.req.route.params.region != null ? this.req.route.params.region : '',
'disabled': false
}, null, {
limit: ((_ref = this.req.query) != null ? _ref.limit : void 0) != null ? this.req.query.limit : 100,
sort: {
post_dt: -1
}
}, function(err, posts) {
if (err) {
throw err;
}
if (posts == null) {
posts = [];
}
if (callback != null) {
return callback(posts);
}
});
};
Feed.prototype.post = function(user) {
var ObjectId, post, _ref, _ref1, _ref2, _ref3;
ObjectId = mongoose.Types.ObjectId;
post = new Post({
id: new ObjectId,
author: {
id: user.id,
firstname: user.firstname,
lastname: user.lastname,
email: user.email,
company: user.company
},
message: {
text: ((_ref = this.mdl.model.message) != null ? _ref.text : void 0) != null ? this.mdl.model.message.text : ''
},
attachments: {
files: ((_ref1 = this.mdl.model.attachments) != null ? _ref1.files : void 0) != null ? this.mdl.model.attachments.files : []
},
region: {
caption: ((_ref2 = this.mdl.model.region) != null ? _ref2.caption : void 0) != null ? this.mdl.model.region.caption : '',
name: ((_ref3 = this.mdl.model.region) != null ? _ref3.name : void 0) != null ? this.mdl.model.region.name : ''
}
});
return post;
};
Feed.prototype.findPost = function(callback) {
var _this = this;
return Post.findOne({
id: this.req.route.params.id != null ? this.req.route.params.id : this.mdl.model.id
}, function(err, post) {
if (err || (post == null)) {
return _this.emit('postNotFound');
} else {
if (callback != null) {
return callback(post);
}
}
});
};
return Feed;
})(EventEmitter);
exports = module.exports = function(req, res) {
return new Feed(req, res);
};
exports.Feed = Feed;