implement getUserFollowers

This commit is contained in:
SrGooglo 2023-01-05 16:56:54 +00:00
parent 5e67a1f578
commit 18a5f7d229
2 changed files with 61 additions and 0 deletions

View File

@ -146,6 +146,44 @@ export default class User {
return data
}
static async getUserFollowers({
user_id,
username,
limit = 20,
offset = 0,
}) {
if (!User.bridge) {
return false
}
// if user_id or username is not provided, set with current user
if (!user_id && !username) {
const token = await Session.decodedToken()
if (token) {
username = token.username
} else {
throw new Error("username or user_id is required")
}
}
// TODO: if user_id is not provided, get it from username
if (!user_id) {
}
const { data } = await app.api.customRequest("main", {
method: "GET",
url: `/user/${user_id}/followers`,
params: {
limit,
offset,
}
})
return data
}
static async getConnectedUsersFollowing() {
if (!User.bridge) {
return false

View File

@ -149,6 +149,29 @@ export default class FollowerController extends Controller {
}
get = {
"/user/:user_id/followers": async (req, res) => {
const { limit = 30, offset } = req.query
let followers = []
const follows = await UserFollow.find({
to: req.params.user_id,
})
.limit(limit)
.skip(offset)
for await (const follow of follows) {
const user = await User.findById(follow.user_id)
if (!user) {
continue
}
followers.push(user.toObject())
}
return res.json(followers)
},
"/followers": Schematized({
required: ["user_id"],
select: ["user_id"],