use new routes

This commit is contained in:
SrGooglo 2023-02-13 13:27:59 +00:00
parent 82dc237edf
commit 22f1cbfb1d
8 changed files with 282 additions and 250 deletions

View 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
}
}

View File

@ -36,4 +36,21 @@ export default class FeedModel {
return data 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
}
} }

View 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
}
}

View File

@ -1,2 +1,3 @@
export { default as Session } from "./session" export { default as SessionModel } from "./session"
export { default as User } from "./user" export { default as UserModel } from "./user"
export { default as FollowsModel } from "./follows"

View File

@ -1,36 +1,45 @@
export default class Livestream { export default class Livestream {
static get bridge() {
return window.app?.api.withEndpoints("main")
}
static async getStreamingKey() { 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() { 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) { static async updateLivestreamInfo(payload) {
const { data } = await app.api.customRequest("main", { const request = await app.api.customRequest("main", {
method: "POST", method: "POST",
url: `/tv/streaming/update_info`, url: `/tv/stream/info`,
data: { data: {
...payload ...payload
}, },
}) })
return data return request.data
} }
static async getCategories() { static async getCategories(key) {
const request = await Livestream.bridge.get.tvStreamingCategories() const request = await app.api.customRequest("main", {
method: "GET",
url: `/tv/streaming/categories`,
params: {
key,
}
})
return request return request.data
} }
static async getStreamInfo(payload) { static async getStreamInfo(payload) {
@ -40,7 +49,7 @@ export default class Livestream {
username = app.userData.username username = app.userData.username
} }
const { data } = await app.api.customRequest("main", { const request = await app.api.customRequest("main", {
method: "GET", method: "GET",
url: `/tv/stream/info`, url: `/tv/stream/info`,
params: { params: {
@ -48,7 +57,7 @@ export default class Livestream {
} }
}) })
return data return request.data
} }
static async getLivestream({ username }) { static async getLivestream({ username }) {
@ -58,23 +67,30 @@ export default class Livestream {
let request = await app.api.customRequest("main", { let request = await app.api.customRequest("main", {
method: "GET", method: "GET",
url: `/tv/streaming/${username}`, url: `/tv/streams`,
params: {
username,
}
}) })
request = request.data return request.data
return request
} }
static async getAddresses() { 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() { 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
} }
} }

View File

@ -18,7 +18,7 @@ export default class Post {
const { data } = await app.api.customRequest("main", { const { data } = await app.api.customRequest("main", {
method: "GET", method: "GET",
url: `/posts/${post_id}`, url: `/posts/post/${post_id}`,
}) })
return data return data
@ -31,7 +31,7 @@ export default class Post {
const { data } = await app.api.customRequest("main", { const { data } = await app.api.customRequest("main", {
method: "GET", method: "GET",
url: `/posts/${post_id}/comments`, url: `/comments/post/${post_id}`,
}) })
return data return data
@ -44,7 +44,7 @@ export default class Post {
const request = await app.api.customRequest("main", { const request = await app.api.customRequest("main", {
method: "POST", method: "POST",
url: `/posts/${post_id}/comment`, url: `/comments/post/${post_id}`,
data: { data: {
message: comment, message: comment,
}, },
@ -60,7 +60,7 @@ export default class Post {
const request = await app.api.customRequest("main", { const request = await app.api.customRequest("main", {
method: "DELETE", method: "DELETE",
url: `/posts/${post_id}/comment/${comment_id}`, url: `/comments/post/${post_id}/${comment_id}`,
}) })
return request return request

View File

@ -3,136 +3,112 @@ import jwt_decode from "jwt-decode"
import config from "config" import config from "config"
export default class Session { export default class Session {
static get bridge() { static storageTokenKey = config.app?.storage?.token ?? "token"
return window.app?.api.withEndpoints("main")
}
static tokenKey = config.app?.storage?.token ?? "token"
static get token() { static get token() {
return cookies.get(this.tokenKey) return cookies.get(this.storageTokenKey)
} }
static set token(token) { static set token(token) {
return cookies.set(this.tokenKey, token) return cookies.set(this.storageTokenKey, token)
} }
static get user_id() { static get user_id() {
return this.decodedToken()?.user_id return this.getDecodedToken()?.user_id
} }
static get session_uuid() { static get session_uuid() {
return this.decodedToken()?.session_uuid return this.getDecodedToken()?.session_uuid
} }
static delToken() { static getDecodedToken() {
return cookies.remove(Session.tokenKey)
}
static decodedToken() {
const token = this.token const token = this.token
return token && jwt_decode(token) return token && jwt_decode(token)
} }
static async getAllSessions() { static async getAllSessions() {
return await Session.bridge.get.sessions() const response = await app.api.customRequest("main", {
} method: "get",
url: "/session/all"
//* 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
}) })
if (typeof callback === "function") { return response.data
callback(request.error, request.response)
}
return request
} }
//* GETTERS static async getCurrentSession() {
getAllSessions = async () => { const response = await app.api.customRequest("main", {
return await Session.bridge.get.sessions() method: "get",
url: "/session/current"
})
return response.data
} }
getTokenInfo = async () => { static async getTokenValidation() {
const session = await Session.token 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 () => { static removeToken() {
return await Session.bridge.get.currentSession() return cookies.remove(Session.storageTokenKey)
} }
isCurrentTokenValid = async () => { static async destroyCurrentSession() {
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 () => {
const token = await Session.token const token = await Session.token
const session = await Session.decodedToken() const session = await Session.getDecodedToken()
if (!session || !token) { if (!session || !token) {
return false return false
} }
const result = await Session.bridge.delete.session({ user_id: session.user_id, token: token }) const response = await app.api.customRequest("main", {
this.forgetLocalSession() method: "delete",
url: "/session/current"
}).catch((error) => {
console.error(error)
return false
})
Session.removeToken()
window.app.eventBus.emit("session.destroyed") 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
} }
} }

View File

@ -1,66 +1,66 @@
import Session from "../session" import SessionModel from "../session"
export default class User { export default class User {
static get bridge() { static async data(payload = {}) {
return window.app?.api.withEndpoints("main") let {
} username,
user_id,
} = payload
static async data(payload) { if (!username && !user_id) {
const token = await Session.decodedToken() user_id = SessionModel.user_id
if (!token || !User.bridge) {
return false
} }
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) { user_id = resolveResponse.data.user_id
if (!username) {
throw new Error("username is required")
} }
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) { static async updateData(payload) {
if (!user_id) { const response = await app.api.customRequest("main", {
throw new Error("user_id is required") 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 = {}) { static async unsetFullName() {
if (!User.bridge) { const response = await app.api.customRequest("main", {
throw new Error("Bridge is not available") method: "DELETE",
} url: "/user/self/public_name",
})
if (!payload.username && !payload.user_id) { return response.data
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 })
} }
static async roles() { static async selfRoles() {
const token = await Session.decodedToken() const response = await app.api.customRequest("main", {
method: "GET",
url: "/roles/self",
})
if (!token || !User.bridge) { return response.data
return false
}
return User.bridge.get.userRoles(undefined, { username: token.username })
} }
static async hasRole(role) { static async haveRole(role) {
const roles = await User.roles() const roles = await User.selfRoles()
if (!roles) { if (!roles) {
return false return false
@ -69,69 +69,24 @@ export default class User {
return Array.isArray(roles) && roles.includes(role) return Array.isArray(roles) && roles.includes(role)
} }
static async selfUserId() { static async haveAdmin() {
const token = await Session.decodedToken() return User.haveRole("admin")
if (!token) {
return false
}
return token.user_id
}
static async hasAdmin() {
return User.hasRole("admin")
} }
static async getUserBadges(user_id) { static async getUserBadges(user_id) {
if (!User.bridge) {
return false
}
if (!user_id) { if (!user_id) {
user_id = await User.selfUserId() user_id = SessionModel.user_id
} }
const { data } = await app.api.customRequest("main", { const { data } = await app.api.customRequest("main", {
method: "GET", method: "GET",
url: "/user/badges", url: `/badge/user/${user_id}`,
params: {
user_id: user_id,
}
}) })
return data 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) { static async changePassword(payload) {
if (!User.bridge) {
return false
}
const { currentPassword, newPassword } = payload const { currentPassword, newPassword } = payload
const { data } = await app.api.customRequest("main", { const { data } = await app.api.customRequest("main", {
@ -148,28 +103,12 @@ export default class User {
static async getUserFollowers({ static async getUserFollowers({
user_id, user_id,
username,
limit = 20, limit = 20,
offset = 0, offset = 0,
}) { }) {
if (!User.bridge) {
return false
}
// if user_id or username is not provided, set with current user // if user_id or username is not provided, set with current user
if (!user_id && !username) { if (!user_id && !username) {
const token = await Session.decodedToken() user_id = SessionModel.user_id
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", { const { data } = await app.api.customRequest("main", {
@ -185,27 +124,11 @@ export default class User {
} }
static async getConnectedUsersFollowing() { static async getConnectedUsersFollowing() {
if (!User.bridge) {
return false
}
const { data } = await app.api.customRequest("main", { const { data } = await app.api.customRequest("main", {
method: "GET", method: "GET",
url: "/connected_users_following", url: "/status/connected/following",
}) })
return data 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
}
} }