-
Notifications
You must be signed in to change notification settings - Fork 1
/
feed.js
169 lines (152 loc) · 5.47 KB
/
feed.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const express = require("express");
const router = express.Router();
const Post = require("./models/Post"); //get model
const User = require("./models/User");
const loginRequired = require("./verifyToken"); //verifyToken.js
const middlewares = require("./middlewares");
const serializer = require("./post/postserializer");
async function serializePosts(posts) {
//populate post Objects with clients and author info
let res = (posts.map(post => {
return serializer.serialize(post);
}));
res = await Promise.all(res);
return res;
}
/*****GET FEED******/
//login required
router.get(
"/",
[loginRequired, middlewares.getUserObject],
async (req, res) => {
user = req.user;
let posts = [];
//get posts by searching preferences
let preferencePosts = {};
for (item of user.preferences) {
posts = [];
posts = Post.find(
{ $text: { $search: item } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } });
preferencePosts[item] = await posts;
}
//populate post Objects with clients and author info
promises = [];
for (let item of user.preferences) {
promises.push(serializePosts(preferencePosts[item]));
}
promises = await Promise.all(promises);
let cnt = 0;
for (let item of user.preferences) {
preferencePosts[item] = promises[cnt];
cnt++;
}
res.json(preferencePosts);
}
);
/****GET USER's POSTS ****/
router.get(
"/myPosts/",
[loginRequired, middlewares.getUserObject],
async (req, res) => {
//get the user's own posts
let posts = [];
let ownPosts = {};
ownPosts.unfulfilled = [];
ownPosts.pending = [];
ownPosts.fulfilled = [];
//get unfulfilled
for (item of user.posts) {
posts.push(Post.findOne({ _id: item, fulfilled: 0 }));
}
ownPosts.unfulfilled = (await Promise.all(posts)).filter((v) => v !== null);
posts = [];
//get pending
for (item of user.posts) {
posts.push(Post.findOne({ _id: item, fulfilled: 1 }));
}
ownPosts.pending = (await Promise.all(posts)).filter((v) => v !== null);
posts = [];
//get fulfilled
for (item of user.posts) {
posts.push(Post.findOne({ _id: item, fulfilled: 2 }));
}
ownPosts.fulfilled = (await Promise.all(posts)).filter((v) => v !== null);
ownPosts.unfulfilled = await serializePosts(ownPosts.unfulfilled);
ownPosts.pending = await serializePosts(ownPosts.pending);
ownPosts.fulfilled = await serializePosts(ownPosts.fulfilled);
return res.json(ownPosts);
}
);
/****GET POSTS that user responded to****/
/*
router.get(
"/activities/",
[loginRequired, middlewares.getUserObject],
async (req, res) => {
//get the user's own posts
let posts = [];
let activities = {};
activities.unfulfilled = [];
activities.pending = [];
activities.fulfilled = [];
//get unfulfilled
for (item of user.activities) {
posts.push(Post.findOne({ _id: item, fulfilled: 0 }));
}
activities.unfulfilled = (await (Promise.all(posts))).filter((v) => v !== null);
posts = [];
//get pending
for (item of user.activities) {
posts.push(Post.findOne({ _id: item, fulfilled: 1 }));
}
activities.pending = (await Promise.all(posts)).filter((v) => v !== null);
posts = [];
//get fulfilled
for (item of user.activities) {
post.push(Post.findOne({ _id: item, fulfilled: 2 }));
}
activities.fulfilled = (await Promise.all(posts)).filter((v) => v !== null);
activities.unfulfilled = await serializePosts(activities.unfulfilled);
activities.pending = await serializePosts(activities.pending);
activities.fulfilled = await serializePosts(activities.fulfilled);
return res.json(activities);
}
);
*/
/****GET Followed POSTS****/
router.get(
"/followedPosts/",
[loginRequired, middlewares.getUserObject],
async (req, res) => {
//get the user's own posts
let posts = [];
let followedPosts = {};
followedPosts.unfulfilled = [];
followedPosts.pending = [];
followedPosts.fulfilled = [];
//get unfulfilled
for (item of user.followedPosts) {
posts.push(Post.findOne({ _id: item, fulfilled: 0 }));
}
followedPosts.unfulfilled = (await Promise.all(posts)).filter((v) => v !== null);
posts = [];
//get pending
for (item of user.followedPosts) {
posts.push(Post.findOne({ _id: item, fulfilled: 1 }));
}
followedPosts.pending = (await Promise.all(posts)).filter((v) => v !== null);
posts = [];
//get fulfilled
for (item of user.followedPosts) {
posts.push(Post.findOne({ _id: item, fulfilled: 2 }));
}
followedPosts.fulfilled = (await Promise.all(posts)).filter((v) => v !== null);
followedPosts.unfulfilled = await serializePosts(followedPosts.unfulfilled);
followedPosts.pending = await serializePosts(followedPosts.pending);
followedPosts.fulfilled = await serializePosts(followedPosts.fulfilled);
return res.json(followedPosts);
}
);
module.exports = router;