mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
use new routes
This commit is contained in:
parent
82dc237edf
commit
22f1cbfb1d
52
packages/app/src/models/auth/index.js
Normal file
52
packages/app/src/models/auth/index.js
Normal file
@ -0,0 +1,52 @@
|
||||
import SessionModel from "../session"
|
||||
|
||||
export default class AuthModel {
|
||||
static login = async (payload) => {
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "post",
|
||||
url: "/auth/login",
|
||||
data: {
|
||||
username: payload.username, //window.btoa(payload.username),
|
||||
password: payload.password, //window.btoa(payload.password),
|
||||
},
|
||||
})
|
||||
|
||||
SessionModel.token = response.data.token
|
||||
|
||||
app.eventBus.emit("auth:login_success")
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
static logout = async () => {
|
||||
await SessionModel.destroyCurrentSession()
|
||||
|
||||
SessionModel.removeToken()
|
||||
|
||||
app.eventBus.emit("auth:logout_success")
|
||||
}
|
||||
|
||||
static async register(payload) {
|
||||
if (!User.bridge) {
|
||||
return false
|
||||
}
|
||||
|
||||
const { username, password, email } = payload
|
||||
|
||||
const response = await User.bridge.post.register(undefined, {
|
||||
username,
|
||||
password,
|
||||
email,
|
||||
}).catch((error) => {
|
||||
console.error(error)
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
if (!response) {
|
||||
throw new Error("Unable to register user")
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
}
|
@ -36,4 +36,21 @@ export default class FeedModel {
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
static async search(keywords, params = {}) {
|
||||
if (!FeedModel.bridge) {
|
||||
throw new Error("Bridge is not available")
|
||||
}
|
||||
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/search`,
|
||||
params: {
|
||||
keywords: keywords,
|
||||
params: params
|
||||
}
|
||||
})
|
||||
|
||||
return data
|
||||
}
|
||||
}
|
47
packages/app/src/models/follows/index.js
Normal file
47
packages/app/src/models/follows/index.js
Normal file
@ -0,0 +1,47 @@
|
||||
import { SessionModel } from "models"
|
||||
|
||||
export default class FollowsModel {
|
||||
static async imFollowing(user_id) {
|
||||
if (!user_id) {
|
||||
throw new Error("user_id is required")
|
||||
}
|
||||
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/follow/user/${user_id}`,
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
static async getFollowers(user_id) {
|
||||
if (!user_id) {
|
||||
// set current user_id
|
||||
user_id = SessionModel.user_id
|
||||
}
|
||||
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/follow/user/${user_id}/followers`,
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
static async toogleFollow({ user_id, username }) {
|
||||
if (!user_id && !username) {
|
||||
throw new Error("user_id or username is required")
|
||||
}
|
||||
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "POST",
|
||||
url: "/follow/user/toogle",
|
||||
data: {
|
||||
user_id: user_id,
|
||||
username: username
|
||||
},
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
export { default as Session } from "./session"
|
||||
export { default as User } from "./user"
|
||||
export { default as SessionModel } from "./session"
|
||||
export { default as UserModel } from "./user"
|
||||
export { default as FollowsModel } from "./follows"
|
@ -1,36 +1,45 @@
|
||||
export default class Livestream {
|
||||
static get bridge() {
|
||||
return window.app?.api.withEndpoints("main")
|
||||
}
|
||||
|
||||
static async getStreamingKey() {
|
||||
const request = await Livestream.bridge.get.tvStreamingKey()
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/tv/streaming/key`,
|
||||
})
|
||||
|
||||
return request
|
||||
return request.data
|
||||
}
|
||||
|
||||
static async regenerateStreamingKey() {
|
||||
const request = await Livestream.bridge.post.tvRegenerateStreamingKey()
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "POST",
|
||||
url: `/tv/streaming/key/regenerate`,
|
||||
|
||||
return request
|
||||
})
|
||||
|
||||
return request.data
|
||||
}
|
||||
|
||||
static async updateLivestreamInfo(payload) {
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "POST",
|
||||
url: `/tv/streaming/update_info`,
|
||||
url: `/tv/stream/info`,
|
||||
data: {
|
||||
...payload
|
||||
},
|
||||
})
|
||||
|
||||
return data
|
||||
return request.data
|
||||
}
|
||||
|
||||
static async getCategories() {
|
||||
const request = await Livestream.bridge.get.tvStreamingCategories()
|
||||
static async getCategories(key) {
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/tv/streaming/categories`,
|
||||
params: {
|
||||
key,
|
||||
}
|
||||
})
|
||||
|
||||
return request
|
||||
return request.data
|
||||
}
|
||||
|
||||
static async getStreamInfo(payload) {
|
||||
@ -40,7 +49,7 @@ export default class Livestream {
|
||||
username = app.userData.username
|
||||
}
|
||||
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/tv/stream/info`,
|
||||
params: {
|
||||
@ -48,7 +57,7 @@ export default class Livestream {
|
||||
}
|
||||
})
|
||||
|
||||
return data
|
||||
return request.data
|
||||
}
|
||||
|
||||
static async getLivestream({ username }) {
|
||||
@ -58,23 +67,30 @@ export default class Livestream {
|
||||
|
||||
let request = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/tv/streaming/${username}`,
|
||||
url: `/tv/streams`,
|
||||
params: {
|
||||
username,
|
||||
}
|
||||
})
|
||||
|
||||
request = request.data
|
||||
|
||||
return request
|
||||
return request.data
|
||||
}
|
||||
|
||||
static async getAddresses() {
|
||||
const request = await Livestream.bridge.get.tvStreamingAddresses()
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/tv/streaming/addresses`,
|
||||
})
|
||||
|
||||
return request
|
||||
return request.data
|
||||
}
|
||||
|
||||
static async getLivestreams() {
|
||||
const request = await Livestream.bridge.get.tvStreams()
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/tv/streams`,
|
||||
})
|
||||
|
||||
return request
|
||||
return request.data
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@ export default class Post {
|
||||
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/posts/${post_id}`,
|
||||
url: `/posts/post/${post_id}`,
|
||||
})
|
||||
|
||||
return data
|
||||
@ -31,7 +31,7 @@ export default class Post {
|
||||
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/posts/${post_id}/comments`,
|
||||
url: `/comments/post/${post_id}`,
|
||||
})
|
||||
|
||||
return data
|
||||
@ -44,7 +44,7 @@ export default class Post {
|
||||
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "POST",
|
||||
url: `/posts/${post_id}/comment`,
|
||||
url: `/comments/post/${post_id}`,
|
||||
data: {
|
||||
message: comment,
|
||||
},
|
||||
@ -60,7 +60,7 @@ export default class Post {
|
||||
|
||||
const request = await app.api.customRequest("main", {
|
||||
method: "DELETE",
|
||||
url: `/posts/${post_id}/comment/${comment_id}`,
|
||||
url: `/comments/post/${post_id}/${comment_id}`,
|
||||
})
|
||||
|
||||
return request
|
||||
@ -155,7 +155,7 @@ export default class Post {
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
static async create(payload) {
|
||||
if (!Post.bridge) {
|
||||
throw new Error("Bridge is not available")
|
||||
|
@ -3,136 +3,112 @@ import jwt_decode from "jwt-decode"
|
||||
import config from "config"
|
||||
|
||||
export default class Session {
|
||||
static get bridge() {
|
||||
return window.app?.api.withEndpoints("main")
|
||||
}
|
||||
|
||||
static tokenKey = config.app?.storage?.token ?? "token"
|
||||
static storageTokenKey = config.app?.storage?.token ?? "token"
|
||||
|
||||
static get token() {
|
||||
return cookies.get(this.tokenKey)
|
||||
return cookies.get(this.storageTokenKey)
|
||||
}
|
||||
|
||||
static set token(token) {
|
||||
return cookies.set(this.tokenKey, token)
|
||||
return cookies.set(this.storageTokenKey, token)
|
||||
}
|
||||
|
||||
static get user_id() {
|
||||
return this.decodedToken()?.user_id
|
||||
return this.getDecodedToken()?.user_id
|
||||
}
|
||||
|
||||
static get session_uuid() {
|
||||
return this.decodedToken()?.session_uuid
|
||||
return this.getDecodedToken()?.session_uuid
|
||||
}
|
||||
|
||||
static delToken() {
|
||||
return cookies.remove(Session.tokenKey)
|
||||
}
|
||||
|
||||
static decodedToken() {
|
||||
static getDecodedToken() {
|
||||
const token = this.token
|
||||
|
||||
return token && jwt_decode(token)
|
||||
}
|
||||
|
||||
static async getAllSessions() {
|
||||
return await Session.bridge.get.sessions()
|
||||
}
|
||||
|
||||
//* BASIC HANDLERS
|
||||
login = (payload, callback) => {
|
||||
const body = {
|
||||
username: payload.username, //window.btoa(payload.username),
|
||||
password: payload.password, //window.btoa(payload.password),
|
||||
}
|
||||
|
||||
return this.generateNewToken(body, (err, res) => {
|
||||
if (typeof callback === "function") {
|
||||
callback(err, res)
|
||||
}
|
||||
|
||||
if (!err || res.status === 200) {
|
||||
let token = res.data
|
||||
|
||||
if (typeof token === "object") {
|
||||
token = token.token
|
||||
}
|
||||
|
||||
Session.token = token
|
||||
window.app.eventBus.emit("session.created")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
logout = async () => {
|
||||
await this.destroyCurrentSession()
|
||||
this.forgetLocalSession()
|
||||
}
|
||||
|
||||
//* GENERATORS
|
||||
generateNewToken = async (payload, callback) => {
|
||||
const request = await Session.bridge.post.login(payload, undefined, {
|
||||
parseData: false
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "get",
|
||||
url: "/session/all"
|
||||
})
|
||||
|
||||
if (typeof callback === "function") {
|
||||
callback(request.error, request.response)
|
||||
}
|
||||
|
||||
return request
|
||||
return response.data
|
||||
}
|
||||
|
||||
//* GETTERS
|
||||
getAllSessions = async () => {
|
||||
return await Session.bridge.get.sessions()
|
||||
static async getCurrentSession() {
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "get",
|
||||
url: "/session/current"
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
getTokenInfo = async () => {
|
||||
static async getTokenValidation() {
|
||||
const session = await Session.token
|
||||
|
||||
return await Session.bridge.post.validateSession({ session })
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "get",
|
||||
url: "/session/validate",
|
||||
data: {
|
||||
session: session
|
||||
}
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
getCurrentSession = async () => {
|
||||
return await Session.bridge.get.currentSession()
|
||||
static removeToken() {
|
||||
return cookies.remove(Session.storageTokenKey)
|
||||
}
|
||||
|
||||
isCurrentTokenValid = async () => {
|
||||
const health = await this.getTokenInfo()
|
||||
|
||||
return health.valid
|
||||
}
|
||||
|
||||
forgetLocalSession = () => {
|
||||
return Session.delToken()
|
||||
}
|
||||
|
||||
destroyAllSessions = async () => {
|
||||
const session = await Session.decodedToken()
|
||||
|
||||
if (!session) {
|
||||
return false
|
||||
}
|
||||
|
||||
const result = await Session.bridge.delete.sessions({ user_id: session.user_id })
|
||||
this.forgetLocalSession()
|
||||
window.app.eventBus.emit("session.destroyed")
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
destroyCurrentSession = async () => {
|
||||
static async destroyCurrentSession() {
|
||||
const token = await Session.token
|
||||
const session = await Session.decodedToken()
|
||||
const session = await Session.getDecodedToken()
|
||||
|
||||
if (!session || !token) {
|
||||
return false
|
||||
}
|
||||
|
||||
const result = await Session.bridge.delete.session({ user_id: session.user_id, token: token })
|
||||
this.forgetLocalSession()
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "delete",
|
||||
url: "/session/current"
|
||||
}).catch((error) => {
|
||||
console.error(error)
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
Session.removeToken()
|
||||
|
||||
window.app.eventBus.emit("session.destroyed")
|
||||
|
||||
return result
|
||||
return response.data
|
||||
}
|
||||
|
||||
static async destroyAllSessions() {
|
||||
const session = await Session.getDecodedToken()
|
||||
|
||||
if (!session) {
|
||||
return false
|
||||
}
|
||||
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "delete",
|
||||
url: "/session/all"
|
||||
})
|
||||
|
||||
Session.removeToken()
|
||||
|
||||
window.app.eventBus.emit("session.destroyed")
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
static async isCurrentTokenValid() {
|
||||
const health = await Session.getTokenValidation()
|
||||
|
||||
return health.valid
|
||||
}
|
||||
}
|
@ -1,66 +1,66 @@
|
||||
import Session from "../session"
|
||||
import SessionModel from "../session"
|
||||
|
||||
export default class User {
|
||||
static get bridge() {
|
||||
return window.app?.api.withEndpoints("main")
|
||||
}
|
||||
static async data(payload = {}) {
|
||||
let {
|
||||
username,
|
||||
user_id,
|
||||
} = payload
|
||||
|
||||
static async data(payload) {
|
||||
const token = await Session.decodedToken()
|
||||
|
||||
if (!token || !User.bridge) {
|
||||
return false
|
||||
if (!username && !user_id) {
|
||||
user_id = SessionModel.user_id
|
||||
}
|
||||
|
||||
return User.bridge.get.user(undefined, payload ?? { username: token.username })
|
||||
}
|
||||
if (username && !user_id) {
|
||||
// resolve user_id from username
|
||||
const resolveResponse = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/user/user_id/${username}`,
|
||||
})
|
||||
|
||||
static async dataByUsername(username) {
|
||||
if (!username) {
|
||||
throw new Error("username is required")
|
||||
user_id = resolveResponse.data.user_id
|
||||
}
|
||||
|
||||
return User.bridge.get.user(undefined, { username })
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: `/user/${user_id}/data`,
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
static async dataById(user_id) {
|
||||
if (!user_id) {
|
||||
throw new Error("user_id is required")
|
||||
}
|
||||
static async updateData(payload) {
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "POST",
|
||||
url: "/user/self/update_data",
|
||||
data: {
|
||||
update: payload,
|
||||
},
|
||||
})
|
||||
|
||||
return User.bridge.get.user(undefined, { _id: user_id })
|
||||
return response.data
|
||||
}
|
||||
|
||||
static async publicData(payload = {}) {
|
||||
if (!User.bridge) {
|
||||
throw new Error("Bridge is not available")
|
||||
}
|
||||
static async unsetFullName() {
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "DELETE",
|
||||
url: "/user/self/public_name",
|
||||
})
|
||||
|
||||
if (!payload.username && !payload.user_id) {
|
||||
const token = await Session.decodedToken()
|
||||
|
||||
if (token) {
|
||||
payload.username = token.username
|
||||
} else {
|
||||
throw new Error("username or user_id is required")
|
||||
}
|
||||
}
|
||||
|
||||
return User.bridge.get.userPublicData({ username: payload.username, user_id: payload.user_id })
|
||||
return response.data
|
||||
}
|
||||
|
||||
static async roles() {
|
||||
const token = await Session.decodedToken()
|
||||
static async selfRoles() {
|
||||
const response = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: "/roles/self",
|
||||
})
|
||||
|
||||
if (!token || !User.bridge) {
|
||||
return false
|
||||
}
|
||||
|
||||
return User.bridge.get.userRoles(undefined, { username: token.username })
|
||||
return response.data
|
||||
}
|
||||
|
||||
static async hasRole(role) {
|
||||
const roles = await User.roles()
|
||||
static async haveRole(role) {
|
||||
const roles = await User.selfRoles()
|
||||
|
||||
if (!roles) {
|
||||
return false
|
||||
@ -69,69 +69,24 @@ export default class User {
|
||||
return Array.isArray(roles) && roles.includes(role)
|
||||
}
|
||||
|
||||
static async selfUserId() {
|
||||
const token = await Session.decodedToken()
|
||||
|
||||
if (!token) {
|
||||
return false
|
||||
}
|
||||
|
||||
return token.user_id
|
||||
}
|
||||
|
||||
static async hasAdmin() {
|
||||
return User.hasRole("admin")
|
||||
static async haveAdmin() {
|
||||
return User.haveRole("admin")
|
||||
}
|
||||
|
||||
static async getUserBadges(user_id) {
|
||||
if (!User.bridge) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (!user_id) {
|
||||
user_id = await User.selfUserId()
|
||||
user_id = SessionModel.user_id
|
||||
}
|
||||
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: "/user/badges",
|
||||
params: {
|
||||
user_id: user_id,
|
||||
}
|
||||
url: `/badge/user/${user_id}`,
|
||||
})
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
static async register(payload) {
|
||||
if (!User.bridge) {
|
||||
return false
|
||||
}
|
||||
|
||||
const { username, password, email } = payload
|
||||
|
||||
const response = await User.bridge.post.register(undefined, {
|
||||
username,
|
||||
password,
|
||||
email,
|
||||
}).catch((error) => {
|
||||
console.error(error)
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
if (!response) {
|
||||
throw new Error("Unable to register user")
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
static async changePassword(payload) {
|
||||
if (!User.bridge) {
|
||||
return false
|
||||
}
|
||||
|
||||
const { currentPassword, newPassword } = payload
|
||||
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
@ -148,28 +103,12 @@ export default class User {
|
||||
|
||||
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) {
|
||||
|
||||
user_id = SessionModel.user_id
|
||||
}
|
||||
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
@ -185,27 +124,11 @@ export default class User {
|
||||
}
|
||||
|
||||
static async getConnectedUsersFollowing() {
|
||||
if (!User.bridge) {
|
||||
return false
|
||||
}
|
||||
|
||||
const { data } = await app.api.customRequest("main", {
|
||||
method: "GET",
|
||||
url: "/connected_users_following",
|
||||
url: "/status/connected/following",
|
||||
})
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
getData = async (payload, callback) => {
|
||||
const request = await User.bridge.get.user(undefined, { username: payload.username, _id: payload.user_id }, {
|
||||
parseData: false
|
||||
})
|
||||
|
||||
if (typeof callback === "function") {
|
||||
callback(request.error, request.response)
|
||||
}
|
||||
|
||||
return request.response.data
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user