This commit is contained in:
SrGooglo 2024-04-25 20:25:26 +00:00
parent 8641d5a00f
commit 5ef49f1588

View File

@ -2,7 +2,15 @@ import SessionModel from "../session"
import request from "../../request" import request from "../../request"
export default class User { export default class User {
static data = async (payload = {}) => { /**
* Retrieves the data of a user.
*
* @param {Object} payload - An object containing the username and user_id.
* @param {string} payload.username - The username of the user.
* @param {string} payload.user_id - The ID of the user.
* @return {Promise<Object>} - A promise that resolves with the data of the user.
*/
static async data(payload = {}) {
let { let {
username, username,
user_id, user_id,
@ -30,7 +38,13 @@ export default class User {
return response.data return response.data
} }
static updateData = async (payload) => { /**
* Updates the user data with the given payload.
*
* @param {Object} payload - The data to update the user with.
* @return {Promise<Object>} - A promise that resolves with the updated user data.
*/
static async updateData(payload) {
const response = await request({ const response = await request({
method: "POST", method: "POST",
url: "/users/self/update", url: "/users/self/update",
@ -42,36 +56,39 @@ export default class User {
return response.data return response.data
} }
static unsetFullName = async () => { /**
* Update the public name to null in the user data.
*
* @return {Promise} A Promise that resolves with the response data after updating the public name
*/
static async unsetPublicName() {
return await User.updateData({ return await User.updateData({
full_name: null, public_name: null,
}) })
} }
static selfRoles = async () => { /**
* Retrieves the roles of a user.
*
* @param {string} user_id - The ID of the user. If not provided, the current user ID will be used.
* @return {Promise<Array>} An array of roles for the user.
*/
static async getRoles(user_id) {
const response = await request({ const response = await request({
method: "GET", method: "GET",
url: "/users/self/roles", url: `/users/${user_id ?? "self"}/roles`,
}) })
return response.data return response.data
} }
static haveRole = async (role) => { /**
const roles = await User.selfRoles() * Retrieves the badges for a given user.
*
if (!roles) { * @param {string} user_id - The ID of the user. If not provided, the current session user ID will be used.
return false * @return {Promise<Array>} An array of badges for the user.
} */
static async getBadges(user_id) {
return Array.isArray(roles) && roles.includes(role)
}
static haveAdmin = async () => {
return User.haveRole("admin")
}
static getUserBadges = async (user_id) => {
if (!user_id) { if (!user_id) {
user_id = SessionModel.user_id user_id = SessionModel.user_id
} }
@ -84,61 +101,6 @@ export default class User {
return data return data
} }
static getUserFollowers = async ({
user_id,
limit = 20,
offset = 0,
}) => {
// if user_id or username is not provided, set with current user
if (!user_id && !username) {
user_id = SessionModel.user_id
}
const { data } = await request({
method: "GET",
url: `/user/${user_id}/followers`,
params: {
limit,
offset,
}
})
return data
}
static getConnectedUsersFollowing = async () => {
const { data } = await request({
method: "GET",
url: "/status/connected/following",
})
return data
}
static checkUsernameAvailability = async (username) => {
const { data } = await request({
method: "GET",
url: `/availability`,
params: {
username,
}
})
return data
}
static checkEmailAvailability = async (email) => {
const { data } = await request({
method: "GET",
url: `/availability`,
params: {
email,
}
})
return data
}
/** /**
* Retrive user config from server * Retrive user config from server
* *