Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose')
- const Schema = mongoose.Schema
- var UserActivitySchema = new Schema({
- author: {type: mongoose.Schema.Types.ObjectId, required: true, unique: true, ref: 'user'},
- readHistory: [{articleid: {type: mongoose.Schema.Types.ObjectId, ref: 'article'}, date_read: {type: Date, default: Date.now()}}],
- watchHistory: [{videoid: {type: mongoose.Schema.Types.ObjectId, ref: 'video'}, date_watch: {type: Date, default: Date.now()}}],
- searchHistory: [{query: String, date:{type: Date, default: new Date()}}]
- })
- module.exports = mongoose.model('useractivity',UserActivitySchema, 'useractivities')
- if (req.query.page) var page = parseInt(req.query.page);
- else var page = 1;
- UserActivity.findOne({author: req.client.userId}).select('readHistory')
- .populate({path: 'readHistory.articleid', select: 'title description views thumbnailURI', options: { skip: (page-1)*10, limit: 10}, populate: {path: 'author', select: 'displayName -_id'}})
- .exec().then(activity=>{
- if (!activity || activity.readHistory.length <= 0)
- res.json({success: false, message: 'You have not read any article yet.'})
- else {
- res.json({success: true, readHistory: activity.readHistory})
- }
- }).catch(err=>{
- res.json({success: false, message: err.message})
- })
Add Comment
Please, Sign In to add comment