-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticles.js
More file actions
140 lines (115 loc) · 4.3 KB
/
articles.js
File metadata and controls
140 lines (115 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const Article = require('../models/article');
const Feed = require('../models/feed');
const { exposeArticle, exposeFeed } = require('@/utils/exposes');
const ms = require('ms');
async function getArticleById (req, res) {
const { _id: userId } = res.locals.user;
const articleId = req.params.articleId;
const article = await Article.findById(articleId);
if (!article) {
return res.status(404).json({
message: 'Article not found'
});
}
if (!article.readBy.includes(userId)) {
await article.updateOne({ $addToSet: { readBy: userId } });
}
res.status(200).json({
article: exposeArticle(article, userId)
});
}
async function getArticlesByFeedId (req, res) {
const { feedId, limit, offset } = req.query;
const { _id: userId } = res.locals.user;
const feed = await Feed.findById(feedId);
if (!feed) {
return res.status(404).json({
message: 'feed not found'
});
}
const articles = await Article.find({ feeds: feedId }).sort({ published: -1 }).limit(limit).skip(offset).sort({ published: -1 });
const totalArticles = await Article.count({ feeds: feedId });
res.status(200).json({
totalArticles,
articles: articles.map(article => exposeArticle(article, userId, { onlyDescription: true })),
feed: exposeFeed(feed)
});
}
async function getArticlesByTagName (req, res) {
const { tagName, limit, offset } = req.query;
const { _id: userId } = res.locals.user;
const articles = await Article.find({ tags: tagName }).limit(limit).skip(offset).sort({ published: -1 });
const totalArticles = await Article.count({ tags: tagName });
res.status(200).json({
totalArticles,
articles: articles.map(article => exposeArticle(article, userId, { onlyDescription: true }))
});
}
async function getRelatedArticles (req, res) {
const { _id: userId } = res.locals.user;
const articleId = req.query.articleId;
const limitResults = req.query.limit;
const article = await Article.findById(articleId);
if (!article) {
return res.status(404).json({
message: 'Article not found'
});
}
const articles = await Article.find({
tags: { $in: article.tags },
_id: { $ne: articleId },
published: { $gte: new Date(Date.now() - ms('5d')) },
readBy: { $ne: userId }
}).limit(limitResults).sort({ published: -1 });
return res.status(200).json({
articles: articles.sort(() => Math.random() - 0.5).map(article => exposeArticle(article, userId, { onlyDescription: true }))
});
}
async function getTagsList (req, res) {
const tags = await Article.aggregate([
{ $unwind: '$tags' },
{ $group: { _id: '$tags', count: { $sum: 1 } } },
{ $sort: { count: -1 } }
]);
res.status(200).json({
tags: tags.map(tag => {
return {
name: tag._id, count: tag.count
};
}).sort((a, b) => b.count - a.count)
});
}
async function getUnreadArticles (req, res) {
const { _id: userId } = res.locals.user;
const { limit, offset } = req.query;
const userSubscriptions = await Feed.find({ subscribers: userId }).select('_id');
const articles = await Article.find({ readBy: { $ne: userId }, feeds: { $in: userSubscriptions } }).sort({ published: -1 }).limit(limit).skip(offset);
const totalArticles = await Article.count({ readBy: { $ne: userId }, feeds: { $in: userSubscriptions } }).sort({ published: -1 });
res.status(200).json({
totalArticles,
articles: articles.map(article => exposeArticle(article, userId, { onlyDescription: true }))
});
}
async function markArticleAsRead (req, res) {
const { _id: userId } = res.locals.user;
const { articleId } = req.body;
const article = await Article.findById(articleId);
if (!article) {
return res.status(404).json({
message: 'Article not found'
});
}
await article.updateOne({ $addToSet: { readBy: userId } });
res.status(200).json({
article: exposeArticle(article, userId, { onlyDescription: true })
});
}
module.exports = {
getArticlesByFeedId,
getArticleById,
getArticlesByTagName,
getRelatedArticles,
getTagsList,
getUnreadArticles,
markArticleAsRead
};