comty.js 0.60.6 → 0.61.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.
@@ -2,190 +2,264 @@
2
2
  var _session = require('../session'); var _session2 = _interopRequireDefault(_session);
3
3
 
4
4
  class AuthModel {
5
- /**
6
- * Async function to handle the login process.
7
- *
8
- * @param {Object} payload - The payload containing username, password, and MFA code.
9
- * @param {Function} callback - Optional callback function to handle further actions.
10
- * @return {Object|boolean} The response data if login successful, false if MFA is required.
11
- */
12
- static async login(payload, callback) {
13
- const response = await _request2.default.call(void 0, {
14
- method: "post",
15
- url: "/auth",
16
- data: payload,
17
- })
18
-
19
- if (response.data.mfa_required) {
20
- __comty_shared_state.eventBus.emit("auth:mfa_required")
21
-
22
- if (typeof callback === "function") {
23
- await callback({
24
- mfa_required: {
25
- method: response.data.method,
26
- sended_to: response.data.sended_to,
27
- },
28
- })
29
- }
30
-
31
- return false
32
- }
33
-
34
- _session2.default.token = response.data.token
35
- _session2.default.refreshToken = response.data.refreshToken
36
-
37
- if (typeof callback === "function") {
38
- await callback()
39
- }
40
-
41
- __comty_shared_state.eventBus.emit("auth:login_success")
42
-
43
- return response.data
44
- }
45
-
46
- /**
47
- * Asynchronously logs out the user by destroying the current session and emitting an event for successful logout.
48
- *
49
- * @return {Promise<void>} A Promise that resolves after the logout process is completed.
50
- */
51
- static async logout() {
52
- await _session2.default.destroyCurrentSession()
53
-
54
- _session2.default.removeToken()
55
-
56
- __comty_shared_state.eventBus.emit("auth:logout_success")
57
- }
58
-
59
- /**
60
- * Registers a new user with the provided payload.
61
- *
62
- * @param {Object} payload - The payload containing the user's information.
63
- * @param {string} payload.username - The username of the user.
64
- * @param {string} payload.password - The password of the user.
65
- * @param {string} payload.email - The email of the user.
66
- * @param {boolean} payload.tos - The acceptance of the terms of service.
67
- * @return {Promise<Object>} A Promise that resolves with the response data if the registration is successful, or false if there was an error.
68
- * @throws {Error} Throws an error if the registration fails.
69
- */
70
- static async register(payload) {
71
- const { username, password, email, tos } = payload
72
-
73
- const response = await _request2.default.call(void 0, {
74
- method: "post",
75
- url: "/register",
76
- data: {
77
- username,
78
- password,
79
- email,
80
- accept_tos: tos,
81
- }
82
- }).catch((error) => {
83
- console.error(error)
84
-
85
- return false
86
- })
87
-
88
- if (!response) {
89
- throw new Error("Unable to register user")
90
- }
91
-
92
- return response
93
- }
94
-
95
- /**
96
- * Verifies the given token and returns the user data associated with it.
97
- *
98
- * @param {string} [token] - The token to verify. If not provided, the stored token is used.
99
- * @return {Promise<Object>} A Promise that resolves with the user data if the token is valid, or false if the token is invalid.
100
- * @throws {Error} Throws an error if there was an issue with the request.
101
- */
102
- static async authToken(token) {
103
- if (!token) {
104
- token = await Session.token
105
- }
106
-
107
- const response = await _request2.default.call(void 0, {
108
- method: "POST",
109
- url: "/auth/token",
110
- data: {
111
- token: token
112
- }
113
- })
114
-
115
- return response.data
116
- }
117
-
118
- /**
119
- * Validates the existence of a username by making a GET request to the `/auth/{username}/exists` endpoint.
120
- *
121
- * @param {string} username - The username to validate.
122
- * @return {Promise<boolean|Object>} A Promise that resolves with the response data if the validation is successful,
123
- * or false if there was an error. Throws an error if the validation fails.
124
- */
125
- static async usernameValidation(username) {
126
- const response = await _request2.default.call(void 0, {
127
- method: "get",
128
- url: `/auth/${username}/exists`,
129
- }).catch((error) => {
130
- console.error(error)
131
-
132
- return false
133
- })
134
-
135
- if (!response) {
136
- throw new Error("Unable to validate user")
137
- }
138
-
139
- return response.data
140
- }
141
-
142
- /**
143
- * Retrieves the availability of a username and email by making a GET request to the `/availability` endpoint.
144
- *
145
- * @param {Object} payload - The payload containing the username and email.
146
- * @param {string} payload.username - The username to check availability for.
147
- * @param {string} payload.email - The email to check availability for.
148
- * @return {Promise<Object|boolean>} A Promise that resolves with the availability data if successful, or false if an error occurred.
149
- */
150
- static async availability(payload) {
151
- const { username, email } = payload
152
-
153
- const response = await _request2.default.call(void 0, {
154
- method: "get",
155
- url: `/availability`,
156
- params: {
157
- username,
158
- email,
159
- }
160
- }).catch((error) => {
161
- console.error(error)
162
-
163
- return false
164
- })
165
-
166
- return response.data
167
- }
168
-
169
- /**
170
- * A function to change the user's password.
171
- *
172
- * @param {Object} payload - An object containing the currentPassword and newPassword.
173
- * @param {string} payload.currentPassword - The current password of the user.
174
- * @param {string} payload.newPassword - The new password to set for the user.
175
- * @return {Promise<Object>} The data response after changing the password.
176
- */
177
- static async changePassword(payload) {
178
- const { currentPassword, newPassword } = payload
179
-
180
- const { data } = await _request2.default.call(void 0, {
181
- method: "put",
182
- url: "/auth/password",
183
- data: {
184
- old_password: currentPassword,
185
- new_password: newPassword,
186
- }
187
- })
188
-
189
- return data
190
- }
191
- } exports.default = AuthModel;
5
+ /**
6
+ * Async function to handle the login process.
7
+ *
8
+ * @param {Object} payload - The payload containing username, password, and MFA code.
9
+ * @param {Function} callback - Optional callback function to handle further actions.
10
+ * @return {Object|boolean} The response data if login successful, false if MFA is required.
11
+ */
12
+ static async login(payload, callback) {
13
+ const response = await _request2.default.call(void 0, {
14
+ method: "post",
15
+ url: "/auth",
16
+ data: payload,
17
+ })
18
+
19
+ if (response.data.mfa_required) {
20
+ __comty_shared_state.eventBus.emit("auth:mfa_required")
21
+
22
+ if (typeof callback === "function") {
23
+ await callback({
24
+ mfa_required: {
25
+ method: response.data.method,
26
+ sended_to: response.data.sended_to,
27
+ },
28
+ })
29
+ }
30
+
31
+ return false
32
+ }
33
+
34
+ _session2.default.token = response.data.token
35
+ _session2.default.refreshToken = response.data.refreshToken
36
+
37
+ if (typeof callback === "function") {
38
+ await callback()
39
+ }
40
+
41
+ __comty_shared_state.eventBus.emit("auth:login_success")
42
+
43
+ return response.data
44
+ }
45
+
46
+ /**
47
+ * Asynchronously logs out the user by destroying the current session and emitting an event for successful logout.
48
+ *
49
+ * @return {Promise<void>} A Promise that resolves after the logout process is completed.
50
+ */
51
+ static async logout() {
52
+ await _session2.default.destroyCurrentSession()
53
+
54
+ await _session2.default.removeToken()
55
+
56
+ __comty_shared_state.eventBus.emit("auth:logout_success")
57
+ }
58
+
59
+ /**
60
+ * Registers a new user with the provided payload.
61
+ *
62
+ * @param {Object} payload - The payload containing the user's information.
63
+ * @param {string} payload.username - The username of the user.
64
+ * @param {string} payload.password - The password of the user.
65
+ * @param {string} payload.email - The email of the user.
66
+ * @param {boolean} payload.tos - The acceptance of the terms of service.
67
+ * @return {Promise<Object>} A Promise that resolves with the response data if the registration is successful, or false if there was an error.
68
+ * @throws {Error} Throws an error if the registration fails.
69
+ */
70
+ static async register(payload) {
71
+ const { username, password, email, tos } = payload
72
+
73
+ const response = await _request2.default.call(void 0, {
74
+ method: "post",
75
+ url: "/register",
76
+ data: {
77
+ username,
78
+ password,
79
+ email,
80
+ accept_tos: tos,
81
+ },
82
+ }).catch((error) => {
83
+ console.error(error)
84
+
85
+ return false
86
+ })
87
+
88
+ if (!response) {
89
+ throw new Error("Unable to register user")
90
+ }
91
+
92
+ return response
93
+ }
94
+
95
+ /**
96
+ * Verifies the given token and returns the user data associated with it.
97
+ *
98
+ * @param {string} [token] - The token to verify. If not provided, the stored token is used.
99
+ * @return {Promise<Object>} A Promise that resolves with the user data if the token is valid, or false if the token is invalid.
100
+ * @throws {Error} Throws an error if there was an issue with the request.
101
+ */
102
+ static async authToken(token) {
103
+ if (!token) {
104
+ token = await _session2.default.token
105
+ }
106
+
107
+ const response = await _request2.default.call(void 0, {
108
+ method: "POST",
109
+ url: "/auth/token",
110
+ data: {
111
+ token: token,
112
+ },
113
+ })
114
+
115
+ return response.data
116
+ }
117
+
118
+ /**
119
+ * Validates the existence of a username by making a GET request to the `/auth/{username}/exists` endpoint.
120
+ *
121
+ * @param {string} username - The username to validate.
122
+ * @return {Promise<boolean|Object>} A Promise that resolves with the response data if the validation is successful,
123
+ * or false if there was an error. Throws an error if the validation fails.
124
+ */
125
+ static async usernameValidation(username) {
126
+ const response = await _request2.default.call(void 0, {
127
+ method: "get",
128
+ url: `/auth/${username}/exists`,
129
+ }).catch((error) => {
130
+ console.error(error)
131
+
132
+ return false
133
+ })
134
+
135
+ if (!response) {
136
+ throw new Error("Unable to validate user")
137
+ }
138
+
139
+ return response.data
140
+ }
141
+
142
+ /**
143
+ * Retrieves the availability of a username and email by making a GET request to the `/availability` endpoint.
144
+ *
145
+ * @param {Object} payload - The payload containing the username and email.
146
+ * @param {string} payload.username - The username to check availability for.
147
+ * @param {string} payload.email - The email to check availability for.
148
+ * @return {Promise<Object|boolean>} A Promise that resolves with the availability data if successful, or false if an error occurred.
149
+ */
150
+ static async availability(payload) {
151
+ const { username, email } = payload
152
+
153
+ const response = await _request2.default.call(void 0, {
154
+ method: "get",
155
+ url: `/availability`,
156
+ params: {
157
+ username,
158
+ email,
159
+ },
160
+ }).catch((error) => {
161
+ console.error(error)
162
+
163
+ return false
164
+ })
165
+
166
+ return response.data
167
+ }
168
+
169
+ /**
170
+ * A function to change the user's password.
171
+ *
172
+ * @param {Object} payload - An object containing the currentPassword, newPassword, and code.
173
+ * @param {string} payload.currentPassword - The current password of the user.
174
+ * @param {string} payload.newPassword - The new password to set for the user.
175
+ * @param {string} [payload.code] - The activation code sent to the user's email, optional if the old password is provided.
176
+ * @return {Promise<Object>} The data response after changing the password.
177
+ */
178
+ static async changePassword(payload) {
179
+ const { currentPassword, newPassword, code, verificationToken } =
180
+ payload
181
+
182
+ const { data } = await _request2.default.call(void 0, {
183
+ method: "put",
184
+ url: "/auth/password",
185
+ data: {
186
+ code: code,
187
+ verificationToken: verificationToken,
188
+ old_password: currentPassword,
189
+ new_password: newPassword,
190
+ },
191
+ })
192
+
193
+ return data
194
+ }
195
+
196
+ /**
197
+ * Activates a user account using the provided activation code.
198
+ *
199
+ * @param {string} user_id - The ID of the user to activate.
200
+ * @param {string} code - The activation code sent to the user's email.
201
+ * @return {Promise<Object>} A promise that resolves with the response data after activation.
202
+ * @throws {Error} Throws an error if the activation process fails.
203
+ */
204
+ static async activateAccount(user_id, code) {
205
+ const { data } = await _request2.default.call(void 0, {
206
+ method: "post",
207
+ url: "/auth/activate",
208
+ data: {
209
+ code: code,
210
+ user_id: user_id,
211
+ },
212
+ })
213
+
214
+ return data
215
+ }
216
+
217
+ /**
218
+ * Resends the activation code to the user.
219
+ *
220
+ * @return {Promise<Object>} A promise that resolves with the response data after sending the activation code.
221
+ * @throws {Error} Throws an error if the resend activation code process fails.
222
+ * @param user_id
223
+ */
224
+ static async resendActivationCode(user_id) {
225
+ const { data } = await _request2.default.call(void 0, {
226
+ method: "post",
227
+ url: "/auth/resend-activation-code",
228
+ data: {
229
+ user_id: user_id,
230
+ },
231
+ })
232
+
233
+ return data
234
+ }
235
+
236
+ static async disableAccount({ confirm = false } = {}) {
237
+ if (!confirm) {
238
+ console.error(
239
+ "In order to disable your account, you must confirm the action.",
240
+ )
241
+ return null
242
+ }
243
+
244
+ const { data } = await _request2.default.call(void 0, {
245
+ method: "post",
246
+ url: "/auth/disable-account",
247
+ })
248
+
249
+ __comty_shared_state.eventBus.emit("auth:disabled_account")
250
+
251
+ return data
252
+ }
253
+
254
+ static async recoverPassword(usernameOrEmail) {
255
+ const { data } = await _request2.default.call(void 0, {
256
+ method: "post",
257
+ url: "/auth/recover-password",
258
+ data: {
259
+ account: usernameOrEmail,
260
+ },
261
+ })
262
+
263
+ return data
264
+ }
265
+ } exports.default = AuthModel;
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _models = require('../../models');
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _session = require('../../models/session'); var _session2 = _interopRequireDefault(_session);
2
2
  var _request = require('../../request'); var _request2 = _interopRequireDefault(_request);
3
3
 
4
4
  class FollowsModel {
@@ -31,7 +31,7 @@ var _request = require('../../request'); var _request2 = _interopRequireDefault(
31
31
  */
32
32
  static async getFollowers(user_id, fetchData) {
33
33
  if (!user_id) {
34
- user_id = _models.SessionModel.user_id
34
+ user_id = _session2.default.user_id
35
35
  }
36
36
 
37
37
  const response = await _request2.default.call(void 0, {
@@ -0,0 +1,42 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _request = require('../../../request'); var _request2 = _interopRequireDefault(_request);
2
+ var _processWithAddons = require('../../../helpers/processWithAddons'); var _processWithAddons2 = _interopRequireDefault(_processWithAddons);
3
+ var _standartListMerge = require('../../../utils/standartListMerge'); var _standartListMerge2 = _interopRequireDefault(_standartListMerge);
4
+
5
+ exports. default = async ({ limit = 100, offset = 0, order = "desc" }) => {
6
+ const addons =
7
+ __comty_shared_state.addons.getByOperation("getFavoriteFolder")
8
+
9
+ const dividedLimit = limit / (addons.length + 1)
10
+
11
+ const { data } = await _request2.default.call(void 0, {
12
+ method: "GET",
13
+ url: "/music/my/folder",
14
+ params: {
15
+ limit: dividedLimit,
16
+ offset: offset,
17
+ order: order,
18
+ },
19
+ })
20
+
21
+ let results = await _processWithAddons2.default.call(void 0, {
22
+ operation: "getFavoriteFolder",
23
+ initialData: data,
24
+ fnArguments: [{ limit: dividedLimit, offset: offset, order: order }],
25
+ normalizeAddonResult: ({ currentData, addonResult }) => {
26
+ return _standartListMerge2.default.call(void 0, currentData, addonResult)
27
+ },
28
+ })
29
+
30
+ // sort by liked_at
31
+ results.tracks.items.sort((a, b) => {
32
+ if (a.liked_at > b.liked_at) {
33
+ return -1
34
+ }
35
+ if (a.liked_at < b.liked_at) {
36
+ return 1
37
+ }
38
+ return 0
39
+ })
40
+
41
+ return results
42
+ }
@@ -1,36 +1,33 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});async function exportObjs() {
2
- if (window) {
3
- let paths = {
4
- ...import.meta.glob("./**.ts"),
5
- ...import.meta.glob("./**.js"),
6
- }
7
-
8
- let fns = {}
9
-
10
- for (const path in paths) {
11
- const name = path.split("/").pop().replace(".ts", "").replace(".js", "")
12
- const fn = await paths[path]()
13
-
14
- fns[name] = fn.default
15
- }
16
-
17
- return fns
18
- } else {
19
- let objs = {}
20
-
21
- const dirs = fs.readdirSync(__dirname).filter(file => file !== "index.js")
22
-
23
- const fs = require("fs")
24
- const path = require("path")
25
-
26
- dirs.forEach((file) => {
27
- const model = require(path.join(__dirname, file)).default
28
-
29
- objs[file.replace(".js", "")] = model
30
- })
31
-
32
- return objs
33
- }
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});function exportObjs() {
2
+ if (typeof window !== "undefined") {
3
+ const paths = {
4
+ ...import.meta.glob("./**.ts", { eager: true, import: "default" }),
5
+ ...import.meta.glob("./**.js", { eager: true, import: "default" }),
6
+ }
7
+
8
+ return Object.entries(paths).reduce((acc, [path, module]) => {
9
+ const name = path
10
+ .split("/")
11
+ .pop()
12
+ .replace(/\.(ts|js)$/, "")
13
+ acc[name] = module
14
+ return acc
15
+ }, {})
16
+ } else {
17
+ const fs = require("fs")
18
+ const path = require("path")
19
+
20
+ return fs
21
+ .readdirSync(__dirname)
22
+ .filter((file) => file !== "index.js" && /\.js$/.test(file))
23
+ .reduce((acc, file) => {
24
+ const name = file.replace(/\.js$/, "")
25
+ acc[name] = require(path.join(__dirname, file)).default
26
+ return acc
27
+ }, {})
28
+ }
34
29
  }
35
30
 
36
- exports. default = await exportObjs()
31
+ const exportedObjs = exportObjs()
32
+
33
+ exports. default = exportedObjs
@@ -0,0 +1,33 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _request = require('../../../request'); var _request2 = _interopRequireDefault(_request);
2
+
3
+ const typeToNamespace = {
4
+ track: "tracks",
5
+ //playlist: "playlists",
6
+ //release: "releases",
7
+ }
8
+
9
+ exports. default = async (type, track_id) => {
10
+ if (!type) {
11
+ throw new Error("type is required")
12
+ }
13
+
14
+ if (!track_id) {
15
+ throw new Error("track_id is required")
16
+ }
17
+
18
+ type = type.toLowerCase()
19
+
20
+ type = typeToNamespace[type]
21
+
22
+ if (!type) {
23
+ throw new Error(`Unsupported type: ${type}`)
24
+ }
25
+
26
+ const response = await _request2.default.call(void 0, {
27
+ method: "GET",
28
+ url: `/music/${type}/${track_id}/is_favourite`,
29
+ })
30
+
31
+ // @ts-ignore
32
+ return response.data
33
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _request = require('../../../request'); var _request2 = _interopRequireDefault(_request);
2
+
3
+ exports. default = async (params) => {
4
+ const response = await _request2.default.call(void 0, {
5
+ method: "GET",
6
+ url: `/music/recently`,
7
+ params: params,
8
+ })
9
+
10
+ return response.data
11
+ }