forked from ALPHACamp/twitter-fullstack-2020
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_services.js
More file actions
84 lines (84 loc) · 2.4 KB
/
_services.js
File metadata and controls
84 lines (84 loc) · 2.4 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
const { Op } = require('sequelize')
const helpers = require('./_helpers')
const db = require('./models')
const User = db.User
const Tweet = db.Tweet
const Like = db.Like
const Reply = db.Reply
const Followship = db.Followship
module.exports = {
// 推薦追蹤用戶
getTopUsers: async (req) => {
const viewUser = helpers.getUser(req)
const followingList = viewUser && viewUser.Followings.map(following => following.id)
const allUsers = await User.findAll({
where: {
role: 'user',
id: { [Op.not]: Number(helpers.getUser(req).id) }
},
include: [{ model: User, as: 'Followers' }],
nest: true
})
const topFollowings = allUsers
.sort((a, b) => {
b.Followers.length - a.Followers.length
})
.slice(0, 10)
.map(topFollowing => {
return {
...topFollowing.toJSON(),
isFollowed: followingList.includes(topFollowing.id)
}
})
return topFollowings
},
// 貼文抓取
getTweets: async (req) => {
const viewUser = helpers.getUser(req)
const UserId = req.params.id || ''
const tweets = await Tweet.findAll({
where: UserId ? { UserId } : {},
order: [['createdAt', 'DESC']],
include: [User, Like, Reply],
nest: true
}) || []
const data = tweets.map(t => ({
...t.toJSON(),
User: t.User.dataValues,
description: t.description.substring(0, 140),
isLiked: t.Likes.some(f => f.UserId === viewUser.id)
}))
return data
},
// 單一貼文回覆抓取
getReplies: async (req) => {
const TweetId = req.params.id || ''
const replies = await Reply.findAll({
where: { TweetId },
order: [['createdAt', 'DESC']],
include: [User],
nest: true,
raw: true
}) || []
return replies
},
// 抓取個人檔案用戶
getUser: async (req) => {
const userId = req.params.id
const viewUser = helpers.getUser(req)
const followingList = viewUser && viewUser.Followings.map(following => following.id)
const findUser = await User.findByPk(userId, {
include: [
Tweet,
{ model: User, as: 'Followings', order: [[Followship, 'createdAt', 'DESC']]},
{ model: User, as: 'Followers', order: [[Followship, 'createdAt', 'DESC']] }
],
nest: true
})
const user = {
...findUser.toJSON(),
isFollowed: followingList.includes(findUser.id)
}
return user
}
}