@dongdev/fca-unofficial 2.0.7 → 2.0.10

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.
Files changed (80) hide show
  1. package/DOCS.md +1699 -1434
  2. package/README.md +250 -168
  3. package/package.json +54 -28
  4. package/src/api/action/addExternalModule.js +5 -5
  5. package/src/api/action/changeAvatar.js +11 -10
  6. package/src/api/action/changeBio.js +7 -8
  7. package/src/api/action/getCurrentUserID.js +1 -1
  8. package/src/api/action/handleFriendRequest.js +5 -5
  9. package/src/api/action/logout.js +9 -8
  10. package/src/api/action/refreshFb_dtsg.js +17 -12
  11. package/src/api/action/setPostReaction.js +10 -11
  12. package/src/api/action/unfriend.js +3 -4
  13. package/src/api/http/httpGet.js +7 -8
  14. package/src/api/http/httpPost.js +7 -8
  15. package/src/api/http/postFormData.js +6 -5
  16. package/src/api/messaging/addUserToGroup.js +0 -1
  17. package/src/api/messaging/changeAdminStatus.js +108 -89
  18. package/src/api/messaging/changeArchivedStatus.js +6 -6
  19. package/src/api/messaging/changeBlockedStatus.js +3 -4
  20. package/src/api/messaging/changeGroupImage.js +72 -117
  21. package/src/api/messaging/changeNickname.js +59 -48
  22. package/src/api/messaging/changeThreadColor.js +61 -47
  23. package/src/api/messaging/changeThreadEmoji.js +106 -0
  24. package/src/api/messaging/createNewGroup.js +5 -5
  25. package/src/api/messaging/createPoll.js +36 -63
  26. package/src/api/messaging/deleteMessage.js +4 -4
  27. package/src/api/messaging/deleteThread.js +4 -4
  28. package/src/api/messaging/forwardAttachment.js +38 -47
  29. package/src/api/messaging/getFriendsList.js +5 -6
  30. package/src/api/messaging/getMessage.js +4 -9
  31. package/src/api/messaging/handleMessageRequest.js +5 -5
  32. package/src/api/messaging/markAsDelivered.js +5 -5
  33. package/src/api/messaging/markAsRead.js +7 -7
  34. package/src/api/messaging/markAsReadAll.js +3 -4
  35. package/src/api/messaging/markAsSeen.js +7 -7
  36. package/src/api/messaging/muteThread.js +3 -4
  37. package/src/api/messaging/removeUserFromGroup.js +82 -56
  38. package/src/api/messaging/resolvePhotoUrl.js +2 -3
  39. package/src/api/messaging/searchForThread.js +2 -3
  40. package/src/api/messaging/sendMessage.js +171 -101
  41. package/src/api/messaging/sendMessageMqtt.js +14 -12
  42. package/src/api/messaging/sendTypingIndicator.js +11 -11
  43. package/src/api/messaging/setMessageReaction.js +68 -82
  44. package/src/api/messaging/setTitle.js +77 -48
  45. package/src/api/messaging/shareContact.js +2 -4
  46. package/src/api/messaging/threadColors.js +0 -3
  47. package/src/api/messaging/unsendMessage.js +74 -37
  48. package/src/api/messaging/uploadAttachment.js +11 -9
  49. package/src/api/socket/core/connectMqtt.js +180 -0
  50. package/src/api/socket/core/getSeqID.js +25 -0
  51. package/src/api/socket/core/getTaskResponseData.js +22 -0
  52. package/src/api/socket/core/markDelivery.js +12 -0
  53. package/src/api/socket/core/parseDelta.js +351 -0
  54. package/src/api/socket/detail/buildStream.js +176 -68
  55. package/src/api/socket/detail/constants.js +24 -0
  56. package/src/api/socket/listenMqtt.js +80 -1005
  57. package/src/api/{messaging → threads}/getThreadHistory.js +5 -22
  58. package/src/api/threads/getThreadInfo.js +35 -248
  59. package/src/api/threads/getThreadList.js +20 -20
  60. package/src/api/threads/getThreadPictures.js +3 -4
  61. package/src/api/users/getUserID.js +5 -6
  62. package/src/api/users/getUserInfo.js +305 -73
  63. package/src/api/users/getUserInfoV2.js +134 -0
  64. package/src/database/models/user.js +32 -0
  65. package/src/database/userData.js +89 -0
  66. package/src/utils/constants.js +12 -2
  67. package/src/utils/format.js +1051 -0
  68. package/src/utils/request.js +75 -7
  69. package/CHANGELOG.md +0 -52
  70. package/LICENSE-MIT +0 -21
  71. package/func/checkUpdate.js +0 -58
  72. package/func/logger.js +0 -112
  73. package/func/login.js +0 -0
  74. package/index.d.ts +0 -618
  75. package/module/config.js +0 -34
  76. package/module/login.js +0 -47
  77. package/module/loginHelper.js +0 -635
  78. package/module/options.js +0 -49
  79. package/src/api/threads/changeThreadEmoji.js +0 -55
  80. package/src/utils/index.js +0 -1497
@@ -1,103 +1,122 @@
1
1
  "use strict";
2
2
 
3
- const utils = require("../../utils");
4
- const log = require("npmlog");
3
+ const { generateOfflineThreadingID, getType } = require("../../utils/format");
5
4
 
6
- module.exports = function(defaultFuncs, api, ctx) {
7
- return function changeAdminStatus(threadID, adminIDs, adminStatus, callback) {
8
- if (utils.getType(threadID) !== "String") {
9
- throw new utils.CustomError({
10
- error: "changeAdminStatus: threadID must be a string"
5
+ module.exports = function (defaultFuncs, api, ctx) {
6
+ function changeAdminStatusNoMqtt(threadID, adminID, adminStatus) {
7
+ if (getType(threadID) !== "String") throw { error: "changeAdminStatus: threadID must be a string" };
8
+ if (getType(adminID) !== "String" && getType(adminID) !== "Array") throw { error: "changeAdminStatus: adminID must be a string or an array" };
9
+ if (getType(adminStatus) !== "Boolean") throw { error: "changeAdminStatus: adminStatus must be true or false" };
10
+ let wsContent = {
11
+ request_id: 1,
12
+ type: 3,
13
+ payload: {
14
+ version_id: '3816854585040595',
15
+ tasks: [],
16
+ epoch_id: generateOfflineThreadingID(),
17
+ data_trace_id: null
18
+ },
19
+ app_id: '772021112871879'
20
+ }
21
+ if (getType(adminID) === "Array") {
22
+ for (let i = 0; i < adminID.length; i++) {
23
+ wsContent.payload.tasks.push({
24
+ label: '25',
25
+ payload: JSON.stringify({ thread_key: threadID, contact_id: adminID[i], is_admin: adminStatus }),
26
+ queue_name: 'admin_status',
27
+ task_id: i + 1,
28
+ failure_count: null
29
+ });
30
+ }
31
+ } else {
32
+ wsContent.payload.tasks.push({
33
+ label: '25',
34
+ payload: JSON.stringify({ thread_key: threadID, contact_id: adminID, is_admin: adminStatus }),
35
+ queue_name: 'admin_status',
36
+ task_id: 1,
37
+ failure_count: null
11
38
  });
12
39
  }
13
-
14
- if (utils.getType(adminIDs) === "String") {
15
- adminIDs = [adminIDs];
40
+ wsContent.payload = JSON.stringify(wsContent.payload);
41
+ return new Promise((resolve, reject) => ctx.mqttClient && ctx.mqttClient.publish('/ls_req', JSON.stringify(wsContent), {}, (err, _packet) => err ? reject(err) : resolve()));
42
+ };
43
+ function changeAdminStatusMqtt(threadID, adminID, adminStatus) {
44
+ if (!ctx.mqttClient) {
45
+ throw new Error("Not connected to MQTT");
16
46
  }
17
-
18
- if (utils.getType(adminIDs) !== "Array") {
19
- throw new utils.CustomError({
20
- error: "changeAdminStatus: adminIDs must be an array or string"
21
- });
47
+ if (getType(threadID) !== "String") {
48
+ throw { error: "changeAdminStatus: threadID must be a string" };
22
49
  }
23
-
24
- if (utils.getType(adminStatus) !== "Boolean") {
25
- throw new utils.CustomError({
26
- error: "changeAdminStatus: adminStatus must be a string"
27
- });
50
+ if (getType(adminID) !== "String" && getType(adminID) !== "Array") {
51
+ throw { error: "changeAdminStatus: adminID must be a string or an array" };
28
52
  }
29
-
30
- let resolveFunc = function() {};
31
- let rejectFunc = function() {};
32
- const returnPromise = new Promise(function(resolve, reject) {
33
- resolveFunc = resolve;
34
- rejectFunc = reject;
35
- });
36
-
37
- if (!callback) {
38
- callback = function(err) {
39
- if (err) {
40
- return rejectFunc(err);
41
- }
42
- resolveFunc();
43
- };
53
+ if (getType(adminStatus) !== "Boolean") {
54
+ throw { error: "changeAdminStatus: adminStatus must be true or false" };
44
55
  }
45
-
46
- if (
47
- utils.getType(callback) !== "Function" &&
48
- utils.getType(callback) !== "AsyncFunction"
49
- ) {
50
- throw new utils.CustomError({
51
- error: "changeAdminStatus: callback is not a function"
56
+ const tasks = [];
57
+ const isAdmin = adminStatus ? 1 : 0;
58
+ const epochID = generateOfflineThreadingID();
59
+ if (getType(adminID) === "Array") {
60
+ adminID.forEach((id, index) => {
61
+ tasks.push({
62
+ failure_count: null,
63
+ label: "25",
64
+ payload: JSON.stringify({
65
+ thread_key: threadID,
66
+ contact_id: id,
67
+ is_admin: isAdmin
68
+ }),
69
+ queue_name: "admin_status",
70
+ task_id: index + 1
71
+ });
72
+ });
73
+ } else {
74
+ tasks.push({
75
+ failure_count: null,
76
+ label: "25",
77
+ payload: JSON.stringify({
78
+ thread_key: threadID,
79
+ contact_id: adminID,
80
+ is_admin: isAdmin
81
+ }),
82
+ queue_name: "admin_status",
83
+ task_id: 1
52
84
  });
53
85
  }
54
-
55
- const form = {
56
- thread_fbid: threadID
57
- };
58
-
59
- let i = 0;
60
- for (const u of adminIDs) {
61
- form[`admin_ids[${i++}]`] = u;
62
- }
63
- form["add"] = adminStatus;
64
-
65
- defaultFuncs
66
- .post(
67
- "https://www.facebook.com/messaging/save_admins/?dpr=1",
68
- ctx.jar,
69
- form
70
- )
71
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
72
- .then(function(resData) {
73
- if (resData.error) {
74
- switch (resData.error) {
75
- case 1976004:
76
- throw new utils.CustomError({
77
- error: "Cannot alter admin status: you are not an admin.",
78
- rawResponse: resData
79
- });
80
- case 1357031:
81
- throw new utils.CustomError({
82
- error:
83
- "Cannot alter admin status: this thread is not a group chat.",
84
- rawResponse: resData
85
- });
86
- default:
87
- throw new utils.CustomError({
88
- error: "Cannot alter admin status: unknown error.",
89
- rawResponse: resData
90
- });
86
+ let count_req = 0
87
+ const form = JSON.stringify({
88
+ app_id: "2220391788200892",
89
+ payload: JSON.stringify({
90
+ epoch_id: epochID,
91
+ tasks: tasks,
92
+ version_id: "8798795233522156"
93
+ }),
94
+ request_id: ++count_req,
95
+ type: 3
96
+ });
97
+ return new Promise((resolve, reject) => {
98
+ if (ctx.mqttClient) {
99
+ ctx.mqttClient.publish("/ls_req", form, {}, (err, _packet) => {
100
+ if (err) {
101
+ reject(err);
102
+ } else {
103
+ resolve();
91
104
  }
92
- }
93
-
94
- callback();
95
- })
96
- .catch(function(err) {
97
- log.error("changeAdminStatus", err);
98
- return callback(err);
99
- });
100
-
101
- return returnPromise;
105
+ });
106
+ } else {
107
+ reject(new Error("MQTT client is not available"));
108
+ }
109
+ });
110
+ };
111
+ return function changeAdminStatus(threadID, adminID, adminStatus) {
112
+ if (ctx.mqttClient) {
113
+ try {
114
+ changeAdminStatusMqtt(threadID, adminID, adminStatus);
115
+ } catch (e) {
116
+ changeAdminStatusNoMqtt(threadID, adminID, adminStatus);
117
+ }
118
+ } else {
119
+ changeAdminStatusNoMqtt(threadID, adminID, adminStatus);
120
+ }
102
121
  };
103
122
  };
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
 
3
- const utils = require("../../utils");
4
3
  const log = require("npmlog");
5
-
4
+ const { formatID } = require("../../utils/format");
5
+ const { parseAndCheckLogin } = require("../../utils/client");
6
6
  module.exports = function(defaultFuncs, api, ctx) {
7
7
  return function changeArchivedStatus(threadOrThreads, archive, callback) {
8
8
  let resolveFunc = function() {};
@@ -23,12 +23,12 @@ module.exports = function(defaultFuncs, api, ctx) {
23
23
 
24
24
  const form = {};
25
25
 
26
- if (utils.getType(threadOrThreads) === "Array") {
26
+ if (Array.isArray(threadOrThreads)) {
27
27
  for (let i = 0; i < threadOrThreads.length; i++) {
28
- form["ids[" + threadOrThreads[i] + "]"] = archive;
28
+ form["ids[" + formatID(threadOrThreads[i]) + "]"] = archive;
29
29
  }
30
30
  } else {
31
- form["ids[" + threadOrThreads + "]"] = archive;
31
+ form["ids[" + formatID(threadOrThreads) + "]"] = archive;
32
32
  }
33
33
 
34
34
  defaultFuncs
@@ -37,7 +37,7 @@ module.exports = function(defaultFuncs, api, ctx) {
37
37
  ctx.jar,
38
38
  form
39
39
  )
40
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
40
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
41
41
  .then(function(resData) {
42
42
  if (resData.error) {
43
43
  throw resData;
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
2
 
3
- const utils = require("../../utils");
4
3
  const log = require("npmlog");
5
-
4
+ const { parseAndCheckLogin, saveCookies } = require("../../utils/client");
6
5
  module.exports = function(defaultFuncs, api, ctx) {
7
6
  return function changeBlockedStatus(userID, block, callback) {
8
7
  let resolveFunc = function() {};
@@ -31,8 +30,8 @@ module.exports = function(defaultFuncs, api, ctx) {
31
30
  fbid: userID
32
31
  }
33
32
  )
34
- .then(utils.saveCookies(ctx.jar))
35
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
33
+ .then(saveCookies(ctx.jar))
34
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
36
35
  .then(function(resData) {
37
36
  if (resData.error) {
38
37
  throw resData;
@@ -1,135 +1,90 @@
1
1
  "use strict";
2
2
 
3
- const utils = require("../../utils");
3
+ const { generateOfflineThreadingID } = require("../../utils/format.js");
4
4
  const log = require("npmlog");
5
5
 
6
- module.exports = function(defaultFuncs, api, ctx) {
7
- function handleUpload(image, callback) {
8
- const uploads = [];
9
-
6
+ module.exports = function (defaultFuncs, api, ctx) {
7
+ function handleUpload(image) {
10
8
  const form = {
11
9
  images_only: "true",
12
10
  "attachment[]": image
13
11
  };
14
-
15
- uploads.push(
16
- defaultFuncs
17
- .postFormData(
18
- "https://upload.facebook.com/ajax/mercury/upload.php",
19
- ctx.jar,
20
- form,
21
- {}
22
- )
23
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
24
- .then(function(resData) {
25
- if (resData.error) {
26
- throw resData;
27
- }
28
-
29
- return resData.payload.metadata[0];
30
- })
31
- );
32
-
33
- // resolve all promises
34
- Promise.all(uploads)
35
- .then(function(resData) {
36
- callback(null, resData);
37
- })
38
- .catch(function(err) {
39
- log.error("handleUpload", err);
40
- return callback(err);
12
+ return defaultFuncs
13
+ .postFormData("https://upload.facebook.com/ajax/mercury/upload.php", ctx.jar, form, {})
14
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
15
+ .then(resData => {
16
+ if (resData.error) throw resData;
17
+ return resData.payload.metadata[0];
41
18
  });
42
19
  }
43
-
44
20
  return function changeGroupImage(image, threadID, callback) {
45
- if (
46
- !callback &&
47
- (utils.getType(threadID) === "Function" ||
48
- utils.getType(threadID) === "AsyncFunction")
49
- ) {
50
- throw { error: "please pass a threadID as a second argument." };
51
- }
52
-
53
- if (!utils.isReadableStream(image)) {
54
- throw { error: "please pass a readable stream as a first argument." };
55
- }
56
-
57
- let resolveFunc = function() {};
58
- let rejectFunc = function() {};
59
- const returnPromise = new Promise(function(resolve, reject) {
60
- resolveFunc = resolve;
61
- rejectFunc = reject;
62
- });
63
-
64
- if (!callback) {
65
- callback = function(err) {
66
- if (err) {
67
- return rejectFunc(err);
21
+ return new Promise((resolve, reject) => {
22
+ if (!ctx.mqttClient) {
23
+ const err = new Error("Not connected to MQTT");
24
+ callback?.(err);
25
+ return reject(err);
26
+ }
27
+ if (!threadID || typeof threadID !== "string") {
28
+ const err = new Error("Invalid threadID");
29
+ callback?.(err);
30
+ return reject(err);
31
+ }
32
+ const reqID = ++ctx.wsReqNumber;
33
+ const taskID = ++ctx.wsTaskNumber;
34
+ const onResponse = (topic, message) => {
35
+ if (topic !== "/ls_resp") return;
36
+ let jsonMsg;
37
+ try {
38
+ jsonMsg = JSON.parse(message.toString());
39
+ jsonMsg.payload = JSON.parse(jsonMsg.payload);
40
+ } catch (err) {
41
+ return;
68
42
  }
69
- resolveFunc();
43
+ if (jsonMsg.request_id !== reqID) return;
44
+ ctx.mqttClient.removeListener("message", onResponse);
45
+ callback?.(null, { success: true, response: jsonMsg.payload });
46
+ return resolve({ success: true, response: jsonMsg.payload });
70
47
  };
71
- }
72
-
73
- const messageAndOTID = utils.generateOfflineThreadingID();
74
- const form = {
75
- client: "mercury",
76
- action_type: "ma-type:log-message",
77
- author: "fbid:" + (ctx.i_userID || ctx.userID),
78
- author_email: "",
79
- ephemeral_ttl_mode: "0",
80
- is_filtered_content: false,
81
- is_filtered_content_account: false,
82
- is_filtered_content_bh: false,
83
- is_filtered_content_invalid_app: false,
84
- is_filtered_content_quasar: false,
85
- is_forward: false,
86
- is_spoof_warning: false,
87
- is_unread: false,
88
- log_message_type: "log:thread-image",
89
- manual_retry_cnt: "0",
90
- message_id: messageAndOTID,
91
- offline_threading_id: messageAndOTID,
92
- source: "source:chat:web",
93
- "source_tags[0]": "source:chat",
94
- status: "0",
95
- thread_fbid: threadID,
96
- thread_id: "",
97
- timestamp: Date.now(),
98
- timestamp_absolute: "Today",
99
- timestamp_relative: utils.generateTimestampRelative(),
100
- timestamp_time_passed: "0"
101
- };
102
-
103
- handleUpload(image, function(err, payload) {
104
- if (err) {
105
- return callback(err);
106
- }
107
-
108
- form["thread_image_id"] = payload[0]["image_id"];
109
- form["thread_id"] = threadID;
110
-
111
- defaultFuncs
112
- .post(
113
- "https://www.facebook.com/messaging/set_thread_image/",
114
- ctx.jar,
115
- form
116
- )
117
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
118
- .then(function(resData) {
119
- // check for errors here
120
-
121
- if (resData.error) {
122
- throw resData;
123
- }
124
-
125
- return callback();
48
+ ctx.mqttClient.on("message", onResponse);
49
+ handleUpload(image)
50
+ .then(payload => {
51
+ const imageID = payload.image_id;
52
+ const taskPayload = {
53
+ thread_key: threadID,
54
+ image_id: imageID,
55
+ sync_group: 1
56
+ };
57
+
58
+ const mqttPayload = {
59
+ epoch_id: generateOfflineThreadingID(),
60
+ tasks: [
61
+ {
62
+ failure_count: null,
63
+ label: "37",
64
+ payload: JSON.stringify(taskPayload),
65
+ queue_name: "thread_image",
66
+ task_id: taskID
67
+ }
68
+ ],
69
+ version_id: "8798795233522156"
70
+ };
71
+ const request = {
72
+ app_id: "2220391788200892",
73
+ payload: JSON.stringify(mqttPayload),
74
+ request_id: reqID,
75
+ type: 3
76
+ };
77
+ ctx.mqttClient.publish("/ls_req", JSON.stringify(request), {
78
+ qos: 1,
79
+ retain: false
80
+ });
126
81
  })
127
- .catch(function(err) {
128
- log.error("changeGroupImage", err);
129
- return callback(err);
82
+ .catch(err => {
83
+ ctx.mqttClient.removeListener("message", onResponse);
84
+ log.error("changeGroupImageMqtt", err);
85
+ callback?.(err);
86
+ reject(err);
130
87
  });
131
88
  });
132
-
133
- return returnPromise;
134
89
  };
135
90
  };
@@ -1,59 +1,70 @@
1
1
  "use strict";
2
2
 
3
- const utils = require("../../utils");
3
+ const { generateOfflineThreadingID } = require("../../utils/format.js");
4
4
  const log = require("npmlog");
5
5
 
6
- module.exports = function(defaultFuncs, api, ctx) {
6
+ module.exports = function (defaultFuncs, api, ctx) {
7
7
  return function changeNickname(nickname, threadID, participantID, callback) {
8
- let resolveFunc = function() {};
9
- let rejectFunc = function() {};
10
- const returnPromise = new Promise(function(resolve, reject) {
11
- resolveFunc = resolve;
12
- rejectFunc = reject;
13
- });
14
- if (!callback) {
15
- callback = function(err) {
16
- if (err) {
17
- return rejectFunc(err);
18
- }
19
- resolveFunc();
8
+ return new Promise((resolve, reject) => {
9
+ if (!ctx.mqttClient) {
10
+ const err = new Error("Not connected to MQTT");
11
+ callback?.(err);
12
+ return reject(err);
13
+ }
14
+ if (!threadID || !participantID) {
15
+ const err = new Error("Missing required parameters");
16
+ callback?.(err);
17
+ return reject(err);
18
+ }
19
+ const reqID = ++ctx.wsReqNumber;
20
+ const taskID = ++ctx.wsTaskNumber;
21
+ const payload = {
22
+ epoch_id: generateOfflineThreadingID(),
23
+ tasks: [
24
+ {
25
+ failure_count: null,
26
+ label: "44",
27
+ payload: JSON.stringify({
28
+ thread_key: threadID,
29
+ contact_id: participantID,
30
+ nickname: nickname || "",
31
+ sync_group: 1
32
+ }),
33
+ queue_name: "thread_participant_nickname",
34
+ task_id: taskID
35
+ }
36
+ ],
37
+ version_id: "8798795233522156"
20
38
  };
21
- }
22
-
23
- const form = {
24
- nickname: nickname,
25
- participant_id: participantID,
26
- thread_or_other_fbid: threadID
27
- };
28
-
29
- defaultFuncs
30
- .post(
31
- "https://www.facebook.com/messaging/save_thread_nickname/?source=thread_settings&dpr=1",
32
- ctx.jar,
33
- form
34
- )
35
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
36
- .then(function(resData) {
37
- if (resData.error === 1545014) {
38
- throw { error: "Trying to change nickname of user isn't in thread" };
39
- }
40
- if (resData.error === 1357031) {
41
- throw {
42
- error:
43
- "Trying to change user nickname of a thread that doesn't exist. Have at least one message in the thread before trying to change the user nickname."
44
- };
39
+ const request = {
40
+ app_id: "2220391788200892",
41
+ payload: JSON.stringify(payload),
42
+ request_id: reqID,
43
+ type: 3
44
+ };
45
+ const onResponse = (topic, message) => {
46
+ if (topic !== "/ls_resp") return;
47
+ let jsonMsg;
48
+ try {
49
+ jsonMsg = JSON.parse(message.toString());
50
+ jsonMsg.payload = JSON.parse(jsonMsg.payload);
51
+ } catch (err) {
52
+ return;
45
53
  }
46
- if (resData.error) {
47
- throw resData;
54
+ if (jsonMsg.request_id !== reqID) return;
55
+ ctx.mqttClient.removeListener("message", onResponse);
56
+ callback?.(null, { success: true, response: jsonMsg.payload });
57
+ return resolve({ success: true, response: jsonMsg.payload });
58
+ };
59
+ ctx.mqttClient.on("message", onResponse);
60
+ ctx.mqttClient.publish("/ls_req", JSON.stringify(request), { qos: 1, retain: false }, (err) => {
61
+ if (err) {
62
+ ctx.mqttClient.removeListener("message", onResponse);
63
+ log.error("changeNicknameMqtt", err);
64
+ callback?.(err);
65
+ return reject(err);
48
66
  }
49
-
50
- return callback();
51
- })
52
- .catch(function(err) {
53
- log.error("changeNickname", err);
54
- return callback(err);
55
67
  });
56
-
57
- return returnPromise;
68
+ });
58
69
  };
59
70
  };