mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
improve music feed endpoints
This commit is contained in:
parent
47032ea0ff
commit
7661611c66
@ -1,7 +1,8 @@
|
|||||||
import { Controller } from "linebridge/dist/server"
|
import { Controller } from "linebridge/dist/server"
|
||||||
|
|
||||||
import getPosts from "./services/getPosts"
|
import getPosts from "./services/getPosts"
|
||||||
import getPlaylists from "./services/getPlaylists"
|
import getPlaylistsFromFollowing from "./services/getPlaylistsFromFollowing"
|
||||||
|
import getPlaylistsFromGlobal from "./services/getPlaylistsFromGlobal"
|
||||||
|
|
||||||
export default class FeedController extends Controller {
|
export default class FeedController extends Controller {
|
||||||
static refName = "FeedController"
|
static refName = "FeedController"
|
||||||
@ -28,7 +29,7 @@ export default class FeedController extends Controller {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// fetch playlists
|
// fetch playlists
|
||||||
let playlists = await getPlaylists({
|
let playlists = await getPlaylistsFromFollowing({
|
||||||
for_user_id,
|
for_user_id,
|
||||||
limit: req.query?.limit,
|
limit: req.query?.limit,
|
||||||
skip: req.query?.trim,
|
skip: req.query?.trim,
|
||||||
@ -61,6 +62,64 @@ export default class FeedController extends Controller {
|
|||||||
return res.json(feed)
|
return res.json(feed)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/music/global": {
|
||||||
|
middlewares: ["withAuthentication"],
|
||||||
|
fn: async (req, res) => {
|
||||||
|
const for_user_id = req.user?._id.toString()
|
||||||
|
|
||||||
|
if (!for_user_id) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: "Invalid user id"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch playlists from global
|
||||||
|
const result = await getPlaylistsFromGlobal({
|
||||||
|
for_user_id,
|
||||||
|
limit: req.query?.limit,
|
||||||
|
skip: req.query?.trim,
|
||||||
|
})
|
||||||
|
|
||||||
|
return res.json(result)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/music": {
|
||||||
|
middlewares: ["withAuthentication"],
|
||||||
|
fn: async (req, res) => {
|
||||||
|
const for_user_id = req.user?._id.toString()
|
||||||
|
|
||||||
|
if (!for_user_id) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: "Invalid user id"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let feed = {
|
||||||
|
followingArtists: [],
|
||||||
|
global: [],
|
||||||
|
mayLike: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch playlists from following
|
||||||
|
const followingArtistsPlaylists = await getPlaylistsFromFollowing({
|
||||||
|
for_user_id,
|
||||||
|
limit: req.query?.limit,
|
||||||
|
skip: req.query?.trim,
|
||||||
|
})
|
||||||
|
|
||||||
|
// fetch playlists from global
|
||||||
|
const globalPlaylists = await getPlaylistsFromGlobal({
|
||||||
|
for_user_id,
|
||||||
|
limit: req.query?.limit,
|
||||||
|
skip: req.query?.trim,
|
||||||
|
})
|
||||||
|
|
||||||
|
feed.followingArtists = followingArtistsPlaylists
|
||||||
|
feed.global = globalPlaylists
|
||||||
|
|
||||||
|
return res.json(feed)
|
||||||
|
}
|
||||||
|
},
|
||||||
"/posts": {
|
"/posts": {
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
@ -100,7 +159,7 @@ export default class FeedController extends Controller {
|
|||||||
let feed = []
|
let feed = []
|
||||||
|
|
||||||
// fetch playlists
|
// fetch playlists
|
||||||
const playlists = await getPlaylists({
|
const playlists = await getPlaylistsFromFollowing({
|
||||||
for_user_id,
|
for_user_id,
|
||||||
limit: req.query?.limit,
|
limit: req.query?.limit,
|
||||||
skip: req.query?.trim,
|
skip: req.query?.trim,
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
import { User, Playlist } from "@models"
|
||||||
|
import getTrackDataById from "../../TracksController/services/getTrackDataById"
|
||||||
|
|
||||||
|
export default async (payload) => {
|
||||||
|
const {
|
||||||
|
limit = 20,
|
||||||
|
skip = 0,
|
||||||
|
} = payload
|
||||||
|
|
||||||
|
let playlists = await Playlist.find()
|
||||||
|
.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)
|
||||||
|
|
||||||
|
playlist.list = await Promise.all(playlist.list.map(async (track_id) => {
|
||||||
|
return await getTrackDataById(track_id)
|
||||||
|
})).catch((err) => {
|
||||||
|
return []
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
...playlist.toObject(),
|
||||||
|
user: {
|
||||||
|
username: user.username,
|
||||||
|
avatar: user.avatar,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
return playlists
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user