Merge branch 'master' of https://github.com/coralproject/talk into post-comment

This commit is contained in:
David Jay
2016-11-07 16:29:06 -05:00
38 changed files with 681 additions and 288 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ const ActionSchema = new Schema({
item_type: String,
item_id: String,
user_id: String
},{
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
+106
View File
@@ -0,0 +1,106 @@
const mongoose = require('../mongoose');
const uuid = require('uuid');
const Schema = mongoose.Schema;
const AssetSchema = new Schema({
id: {
type: String,
default: uuid.v4,
unique: true,
index: true
},
url: {
type: String,
unique: true,
index: true
},
type: {
type: String,
default: 'article'
},
headline: String,
summary: String,
section: String,
subsection: String,
authors: [String],
publication_date: Date
}, {
versionKey: false,
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
});
/**
* Search for assets. Currently only returns all.
*/
AssetSchema.statics.search = function(query) {
return Asset.find(query).exec();
};
/**
* Finds an asset by its id.
* @param {String} id identifier of the asset (uuid).
*/
AssetSchema.statics.findById = function(id) {
return Asset.findOne({id}).exec();
};
/**
* Finds a asset by its url.
* @param {String} url identifier of the asset (uuid).
*/
AssetSchema.statics.findByUrl = function(url) {
return Asset.findOne({'url': url}).exec();
};
/**
* Upserts an asset.
*/
AssetSchema.statics.upsert = function(data) {
// If an id is not sent, create one.
if (typeof data.id === 'undefined') {
data.id = uuid.v4();
}
// Perform the upsert against the id field.
let updatePromise = Asset.update({id: data.id}, data, {upsert: true}).exec()
.then(() => {
// Pull the freshly minted asset out and return.
return Asset.findById(data.id);
})
.catch((err) => {
console.error('Error upserting asset.', err);
//return new Promise(); // ??? what do we return on error?
});
return updatePromise;
};
/**
* Remove assets from the db.
* @param {String} query bson query to identify assets to be removed.
*/
AssetSchema.statics.removeAll = function(query) {
return Asset.remove(query).exec();
};
const Asset = mongoose.model('Asset', AssetSchema);
module.exports = Asset;
+3 -11
View File
@@ -24,7 +24,7 @@ const CommentSchema = new Schema({
default: ''
},
parent_id: String
},{
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
@@ -53,15 +53,7 @@ CommentSchema.statics.findByAssetId = function(asset_id) {
* @param {String} status the new status of the comment
*/
CommentSchema.statics.changeStatus = function(id, status) {
return Comment.update({'id': id}, {$set: {'status': status}}, {upsert: false}).then(() => {
Comment.findById(id).then((comment) => {
return comment;
}).catch((err) => {
console.log('Error updating status for the comment.', err);
});
}).catch((err) => {
console.log('Error updating status for the comment.', err);
});
return Comment.findOneAndUpdate({'id': id}, {$set: {'status': status}}, {upsert: false, new: true});
};
/**
@@ -71,7 +63,7 @@ CommentSchema.statics.changeStatus = function(id, status) {
*/
CommentSchema.statics.addAction = function(id, user_id, action_type) {
// check that the comment exist
var action = new Action({
let action = new Action({
action_type: action_type,
item_type: 'comment',
item_id: id,
+1 -1
View File
@@ -11,7 +11,7 @@ const UserProfileSchema = new Schema({
},
display_name: String,
auth_user_id: String
},{
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'