comty.js 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ import request from "../../handlers/request"
2
+
3
+ export default class Livestream {
4
+ static deleteProfile = async (profile_id) => {
5
+ const response = await request({
6
+ method: "DELETE",
7
+ url: `/tv/streaming/profile`,
8
+ data: {
9
+ profile_id
10
+ }
11
+ })
12
+
13
+ return response.data
14
+ }
15
+
16
+ static postProfile = async (payload) => {
17
+ const response = await request({
18
+ method: "POST",
19
+ url: `/tv/streaming/profile`,
20
+ data: payload,
21
+ })
22
+
23
+ return response.data
24
+ }
25
+
26
+ static getProfiles = async () => {
27
+ const response = await request({
28
+ method: "GET",
29
+ url: `/tv/streaming/profiles`,
30
+ })
31
+
32
+ return response.data
33
+ }
34
+
35
+ static regenerateStreamingKey = async (profile_id) => {
36
+ const response = await request({
37
+ method: "POST",
38
+ url: `/tv/streaming/regenerate_key`,
39
+ data: {
40
+ profile_id
41
+ }
42
+ })
43
+
44
+ return response.data
45
+ }
46
+
47
+ static getCategories = async (key) => {
48
+ const response = await request({
49
+ method: "GET",
50
+ url: `/tv/streaming/categories`,
51
+ params: {
52
+ key,
53
+ }
54
+ })
55
+
56
+ return response.data
57
+ }
58
+
59
+ static getLivestream = async (payload = {}) => {
60
+ if (!payload.username) {
61
+ throw new Error("Username is required")
62
+ }
63
+
64
+ let response = await request({
65
+ method: "GET",
66
+ url: `/tv/streams`,
67
+ params: {
68
+ username: payload.username,
69
+ profile_id: payload.profile_id,
70
+ }
71
+ })
72
+
73
+ return response.data
74
+ }
75
+
76
+ static getLivestreams = async () => {
77
+ const response = await request({
78
+ method: "GET",
79
+ url: `/tv/streams`,
80
+ })
81
+
82
+ return response.data
83
+ }
84
+ }
@@ -0,0 +1,48 @@
1
+ import request from "../../handlers/request"
2
+
3
+ export default class PlaylistsModel {
4
+ static putPlaylist = async (payload) => {
5
+ if (!payload) {
6
+ throw new Error("Payload is required")
7
+ }
8
+
9
+ const { data } = await request({
10
+ method: "PUT",
11
+ url: `/playlist`,
12
+ data: payload,
13
+ })
14
+
15
+ return data
16
+ }
17
+
18
+ static getPlaylist = async (id) => {
19
+ const { data } = await request({
20
+ method: "GET",
21
+ url: `/playlist/data/${id}`,
22
+ })
23
+
24
+ return data
25
+ }
26
+
27
+ static getMyReleases = async () => {
28
+ const { data } = await request({
29
+ method: "GET",
30
+ url: `/playlist/self`,
31
+ })
32
+
33
+ return data
34
+ }
35
+
36
+ static deletePlaylist = async (id) => {
37
+ if (!id) {
38
+ throw new Error("ID is required")
39
+ }
40
+
41
+ const { data } = await request({
42
+ method: "DELETE",
43
+ url: `/playlist/${id}`,
44
+ })
45
+
46
+ return data
47
+ }
48
+ }
@@ -0,0 +1,169 @@
1
+ import request from "../../handlers/request"
2
+ import Settings from "../../helpers/withSettings"
3
+
4
+ export default class Post {
5
+ static get maxPostTextLength() {
6
+ return 3200
7
+ }
8
+
9
+ static get maxCommentLength() {
10
+ return 1200
11
+ }
12
+
13
+ static getPostingPolicy = async () => {
14
+ const { data } = await request({
15
+ method: "GET",
16
+ url: "/posting_policy",
17
+ })
18
+
19
+ return data
20
+ }
21
+
22
+ static getPost = async ({ post_id }) => {
23
+ if (!post_id) {
24
+ throw new Error("Post ID is required")
25
+ }
26
+
27
+ const { data } = await request({
28
+ method: "GET",
29
+ url: `/posts/post/${post_id}`,
30
+ })
31
+
32
+ return data
33
+ }
34
+
35
+ static getPostComments = async ({ post_id }) => {
36
+ if (!post_id) {
37
+ throw new Error("Post ID is required")
38
+ }
39
+
40
+ const { data } = await request({
41
+ method: "GET",
42
+ url: `/comments/post/${post_id}`,
43
+ })
44
+
45
+ return data
46
+ }
47
+
48
+ static sendComment = async ({ post_id, comment }) => {
49
+ if (!post_id || !comment) {
50
+ throw new Error("Post ID and/or comment are required")
51
+ }
52
+
53
+ const request = await request({
54
+ method: "POST",
55
+ url: `/comments/post/${post_id}`,
56
+ data: {
57
+ message: comment,
58
+ },
59
+ })
60
+
61
+ return request
62
+ }
63
+
64
+ static deleteComment = async ({ post_id, comment_id }) => {
65
+ if (!post_id || !comment_id) {
66
+ throw new Error("Post ID and/or comment ID are required")
67
+ }
68
+
69
+ const request = await request({
70
+ method: "DELETE",
71
+ url: `/comments/post/${post_id}/${comment_id}`,
72
+ })
73
+
74
+ return request
75
+ }
76
+
77
+ static getExplorePosts = async ({ trim, limit }) => {
78
+ const { data } = await request({
79
+ method: "GET",
80
+ url: `/posts/explore`,
81
+ params: {
82
+ trim: trim ?? 0,
83
+ limit: limit ?? Settings.get("feed_max_fetch"),
84
+ }
85
+ })
86
+
87
+ return data
88
+ }
89
+
90
+ static getSavedPosts = async ({ trim, limit }) => {
91
+ const { data } = await request({
92
+ method: "GET",
93
+ url: `/posts/saved`,
94
+ params: {
95
+ trim: trim ?? 0,
96
+ limit: limit ?? Settings.get("feed_max_fetch"),
97
+ }
98
+ })
99
+
100
+ return data
101
+ }
102
+
103
+ static getUserPosts = async ({ user_id, trim, limit }) => {
104
+ if (!user_id) {
105
+ // use current user_id
106
+ user_id = app.userData?._id
107
+ }
108
+
109
+ const { data } = await request({
110
+ method: "GET",
111
+ url: `/posts/user/${user_id}`,
112
+ params: {
113
+ trim: trim ?? 0,
114
+ limit: limit ?? Settings.get("feed_max_fetch"),
115
+ }
116
+ })
117
+
118
+ return data
119
+ }
120
+
121
+ static toogleLike = async ({ post_id }) => {
122
+ if (!post_id) {
123
+ throw new Error("Post ID is required")
124
+ }
125
+
126
+ const { data } = await request({
127
+ method: "POST",
128
+ url: `/posts/${post_id}/toogle_like`,
129
+ })
130
+
131
+ return data
132
+ }
133
+
134
+ static toogleSave = async ({ post_id }) => {
135
+ if (!post_id) {
136
+ throw new Error("Post ID is required")
137
+ }
138
+
139
+ const { data } = await request({
140
+ method: "POST",
141
+ url: `/posts/${post_id}/toogle_save`,
142
+ })
143
+
144
+ return data
145
+ }
146
+
147
+ static create = async (payload) => {
148
+ const { data } = await request({
149
+ method: "POST",
150
+ url: `/posts/new`,
151
+ data: payload,
152
+ })
153
+
154
+ return data
155
+ }
156
+
157
+ static deletePost = async ({ post_id }) => {
158
+ if (!post_id) {
159
+ throw new Error("Post ID is required")
160
+ }
161
+
162
+ const { data } = await request({
163
+ method: "DELETE",
164
+ url: `/posts/${post_id}`,
165
+ })
166
+
167
+ return data
168
+ }
169
+ }
@@ -0,0 +1,126 @@
1
+ import jwt_decode from "jwt-decode"
2
+ import request from "../../handlers/request"
3
+ import Storage from "../../helpers/withStorage"
4
+
5
+ export default class Session {
6
+ static storageTokenKey = "token"
7
+
8
+ static get token() {
9
+ return Storage.engine.get(this.storageTokenKey)
10
+ }
11
+
12
+ static set token(token) {
13
+ return Storage.engine.set(this.storageTokenKey, token)
14
+ }
15
+
16
+ static get user_id() {
17
+ return this.getDecodedToken()?.user_id
18
+ }
19
+
20
+ static get session_uuid() {
21
+ return this.getDecodedToken()?.session_uuid
22
+ }
23
+
24
+ static getDecodedToken = () => {
25
+ const token = this.token
26
+
27
+ return token && jwt_decode(token)
28
+ }
29
+
30
+ static getAllSessions = async () => {
31
+ const response = await request({
32
+ method: "get",
33
+ url: "/session/all"
34
+ })
35
+
36
+ return response.data
37
+ }
38
+
39
+ static getCurrentSession = async () => {
40
+ const response = await request({
41
+ method: "get",
42
+ url: "/session/current"
43
+ })
44
+
45
+ return response.data
46
+ }
47
+
48
+ static getTokenValidation = async () => {
49
+ const session = await Session.token
50
+
51
+ const response = await request({
52
+ method: "get",
53
+ url: "/session/validate",
54
+ data: {
55
+ session: session
56
+ }
57
+ })
58
+
59
+ return response.data
60
+ }
61
+
62
+ static removeToken() {
63
+ return Storage.engine.remove(Session.storageTokenKey)
64
+ }
65
+
66
+ static destroyCurrentSession = async () => {
67
+ const token = await Session.token
68
+ const session = await Session.getDecodedToken()
69
+
70
+ if (!session || !token) {
71
+ return false
72
+ }
73
+
74
+ const response = await request({
75
+ method: "delete",
76
+ url: "/session/current"
77
+ }).catch((error) => {
78
+ console.error(error)
79
+
80
+ return false
81
+ })
82
+
83
+ Session.removeToken()
84
+
85
+ __comty_shared_state.eventBus.emit("session.destroyed")
86
+
87
+ return response.data
88
+ }
89
+
90
+ static destroyAllSessions = async () => {
91
+ const session = await Session.getDecodedToken()
92
+
93
+ if (!session) {
94
+ return false
95
+ }
96
+
97
+ const response = await request({
98
+ method: "delete",
99
+ url: "/session/all"
100
+ })
101
+
102
+ Session.removeToken()
103
+
104
+ __comty_shared_state.eventBus.emit("session.destroyed")
105
+
106
+ return response.data
107
+ }
108
+
109
+ static validSession = async (token) => {
110
+ const response = await request({
111
+ method: "POST",
112
+ url: "/session/validate",
113
+ data: {
114
+ token: token
115
+ }
116
+ })
117
+
118
+ return response.data
119
+ }
120
+
121
+ static isCurrentTokenValid = async () => {
122
+ const health = await Session.getTokenValidation()
123
+
124
+ return health.valid
125
+ }
126
+ }
@@ -0,0 +1,87 @@
1
+ export default class SpotifySyncModel {
2
+ static get spotify_redirect_uri() {
3
+ return window.location.origin + "/callbacks/sync/spotify"
4
+ }
5
+
6
+ static get spotify_authorize_endpoint() {
7
+ return "https://accounts.spotify.com/authorize?response_type=code&client_id={{client_id}}&scope={{scope}}&redirect_uri={{redirect_uri}}&response_type=code"
8
+ }
9
+
10
+ static async authorizeAccount() {
11
+ const scopes = [
12
+ "user-read-private",
13
+ "user-modify-playback-state",
14
+ "user-read-currently-playing",
15
+ "user-read-playback-state",
16
+ "streaming",
17
+ ]
18
+
19
+ const { client_id } = await SpotifySyncModel.get_client_id()
20
+
21
+ const parsedUrl = SpotifySyncModel.spotify_authorize_endpoint
22
+ .replace("{{client_id}}", client_id)
23
+ .replace("{{scope}}", scopes.join(" "))
24
+ .replace("{{redirect_uri}}", SpotifySyncModel.spotify_redirect_uri)
25
+
26
+ // open on a new tab
27
+ window.open(parsedUrl, "_blank")
28
+ }
29
+
30
+ static async get_client_id() {
31
+ const { data } = await app.cores.api.customRequest( {
32
+ method: "GET",
33
+ url: `/sync/spotify/client_id`,
34
+ })
35
+
36
+ return data
37
+ }
38
+
39
+ static async syncAuthCode(code) {
40
+ const { data } = await app.cores.api.customRequest( {
41
+ method: "POST",
42
+ url: `/sync/spotify/auth`,
43
+ data: {
44
+ redirect_uri: SpotifySyncModel.spotify_redirect_uri,
45
+ code,
46
+ },
47
+ })
48
+
49
+ return data
50
+ }
51
+
52
+ static async unlinkAccount() {
53
+ const { data } = await app.cores.api.customRequest( {
54
+ method: "POST",
55
+ url: `/sync/spotify/unlink`,
56
+ })
57
+
58
+ return data
59
+ }
60
+
61
+ static async isAuthorized() {
62
+ const { data } = await app.cores.api.customRequest( {
63
+ method: "GET",
64
+ url: `/sync/spotify/is_authorized`,
65
+ })
66
+
67
+ return data
68
+ }
69
+
70
+ static async getData() {
71
+ const { data } = await app.cores.api.customRequest( {
72
+ method: "GET",
73
+ url: `/sync/spotify/data`,
74
+ })
75
+
76
+ return data
77
+ }
78
+
79
+ static async getCurrentPlaying() {
80
+ const { data } = await app.cores.api.customRequest( {
81
+ method: "GET",
82
+ url: `/sync/spotify/currently_playing`,
83
+ })
84
+
85
+ return data
86
+ }
87
+ }
@@ -0,0 +1,11 @@
1
+ import SpotifySyncModel from "./cores/spotifyCore"
2
+
3
+ export default class SyncModel {
4
+ static get bridge() {
5
+ return window.app?.cores.api.withEndpoints()
6
+ }
7
+
8
+ static get spotifyCore() {
9
+ return SpotifySyncModel
10
+ }
11
+ }
@@ -0,0 +1,159 @@
1
+ import SessionModel from "../session"
2
+ import request from "../../handlers/request"
3
+
4
+ export default class User {
5
+ static data = async (payload = {}) => {
6
+ let {
7
+ username,
8
+ user_id,
9
+ } = payload
10
+
11
+ if (!username && !user_id) {
12
+ user_id = SessionModel.user_id
13
+ }
14
+
15
+ if (username && !user_id) {
16
+ // resolve user_id from username
17
+ const resolveResponse = await request({
18
+ method: "GET",
19
+ url: `/user/user_id/${username}`,
20
+ })
21
+
22
+ user_id = resolveResponse.data.user_id
23
+ }
24
+
25
+ const response = await request({
26
+ method: "GET",
27
+ url: `/user/${user_id}/data`,
28
+ })
29
+
30
+ return response.data
31
+ }
32
+
33
+ static updateData = async (payload) => {
34
+ const response = await request({
35
+ method: "POST",
36
+ url: "/user/self/update_data",
37
+ data: {
38
+ update: payload,
39
+ },
40
+ })
41
+
42
+ return response.data
43
+ }
44
+
45
+ static unsetFullName = async () => {
46
+ const response = await request({
47
+ method: "DELETE",
48
+ url: "/user/self/public_name",
49
+ })
50
+
51
+ return response.data
52
+ }
53
+
54
+ static selfRoles = async () => {
55
+ const response = await request({
56
+ method: "GET",
57
+ url: "/roles/self",
58
+ })
59
+
60
+ return response.data
61
+ }
62
+
63
+ static haveRole = async (role) => {
64
+ const roles = await User.selfRoles()
65
+
66
+ if (!roles) {
67
+ return false
68
+ }
69
+
70
+ return Array.isArray(roles) && roles.includes(role)
71
+ }
72
+
73
+ static haveAdmin = async () => {
74
+ return User.haveRole("admin")
75
+ }
76
+
77
+ static getUserBadges = async (user_id) => {
78
+ if (!user_id) {
79
+ user_id = SessionModel.user_id
80
+ }
81
+
82
+ const { data } = await request({
83
+ method: "GET",
84
+ url: `/badge/user/${user_id}`,
85
+ })
86
+
87
+ return data
88
+ }
89
+
90
+ static changePassword = async (payload) => {
91
+ const { currentPassword, newPassword } = payload
92
+
93
+ const { data } = await request({
94
+ method: "POST",
95
+ url: "/self/update_password",
96
+ data: {
97
+ currentPassword,
98
+ newPassword,
99
+ }
100
+ })
101
+
102
+ return data
103
+ }
104
+
105
+ static getUserFollowers = async ({
106
+ user_id,
107
+ limit = 20,
108
+ offset = 0,
109
+ }) => {
110
+ // if user_id or username is not provided, set with current user
111
+ if (!user_id && !username) {
112
+ user_id = SessionModel.user_id
113
+ }
114
+
115
+ const { data } = await request({
116
+ method: "GET",
117
+ url: `/user/${user_id}/followers`,
118
+ params: {
119
+ limit,
120
+ offset,
121
+ }
122
+ })
123
+
124
+ return data
125
+ }
126
+
127
+ static getConnectedUsersFollowing = async () => {
128
+ const { data } = await request({
129
+ method: "GET",
130
+ url: "/status/connected/following",
131
+ })
132
+
133
+ return data
134
+ }
135
+
136
+ static checkUsernameAvailability = async (username) => {
137
+ const { data } = await request({
138
+ method: "GET",
139
+ url: `/user/username_available`,
140
+ params: {
141
+ username,
142
+ }
143
+ })
144
+
145
+ return data
146
+ }
147
+
148
+ static checkEmailAvailability = async (email) => {
149
+ const { data } = await request({
150
+ method: "GET",
151
+ url: `/user/email_available`,
152
+ params: {
153
+ email,
154
+ }
155
+ })
156
+
157
+ return data
158
+ }
159
+ }
@@ -0,0 +1,18 @@
1
+ import request from "../../handlers/request"
2
+
3
+ export default class WidgetModel {
4
+ static browse = async ({ limit, offset, keywords } = {}) => {
5
+ const response = await request({
6
+ instance: globalThis.__comty_shared_state.instances["marketplace"],
7
+ method: "GET",
8
+ url: "/widgets",
9
+ params: {
10
+ limit,
11
+ offset,
12
+ keywords: JSON.stringify(keywords),
13
+ },
14
+ })
15
+
16
+ return response.data
17
+ }
18
+ }