mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
43 lines
1.0 KiB
JavaScript
43 lines
1.0 KiB
JavaScript
import { User, Playlist, UserFollow } from "../../../models"
|
|
|
|
export default async (payload) => {
|
|
const {
|
|
for_user_id,
|
|
limit = 20,
|
|
skip = 0,
|
|
} = payload
|
|
|
|
// get post from users that the user follows
|
|
const followingUsers = await UserFollow.find({
|
|
user_id: for_user_id
|
|
})
|
|
|
|
const followingUserIds = followingUsers.map((followingUser) => followingUser.to)
|
|
|
|
const fetchFromUserIds = [
|
|
for_user_id,
|
|
...followingUserIds,
|
|
]
|
|
|
|
let playlists = await Playlist.find({
|
|
user_id: { $in: fetchFromUserIds }
|
|
})
|
|
.sort({ created_at: -1 })
|
|
.limit(limit)
|
|
.skip(skip)
|
|
|
|
playlists = await Promise.all(playlists.map(async (playlist) => {
|
|
// get user data
|
|
const user = await User.findById(playlist.user_id)
|
|
|
|
return {
|
|
...playlist.toObject(),
|
|
user: {
|
|
username: user.username,
|
|
avatar: user.avatar,
|
|
},
|
|
}
|
|
}))
|
|
|
|
return playlists
|
|
} |