Skip to content

Commit

Permalink
Added new api methods. (#15)
Browse files Browse the repository at this point in the history
* Added "getCurrentTimestamp()"

* Added new methods.

* Added types for new methods.

* Create changeBlockedStatusMqtt.js

* Create createPollMqtt.js

* Create forwardMessage.js

* Create pinMessage.js

* Create sendTypingIndicatorMqtt.js

* Create setMessageReactionMqtt.js

* Create setTheme.js

* Create unsendMessageMqtt.js
  • Loading branch information
Nessie authored Mar 20, 2024
1 parent 8138b18 commit 0cbe674
Show file tree
Hide file tree
Showing 11 changed files with 765 additions and 4 deletions.
10 changes: 9 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,19 @@ declare module '@xaviabot/fca-unofficial' {
changeApprovalMode: (approvalMode: 0 | 1, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
changeArchivedStatus: (threadOrThreads: string | string[], archive: boolean, callback?: (err?: Error) => void) => Promise<void>,
changeBlockedStatus: (userID: string, blocked: boolean, callback?: (err?: Error) => void) => Promise<void>,
changeBlockedStatusMqtt: (userID: string, status: boolean, type: string?, callback?: (err?: Error) => void) => Promise<void>,
changeGroupImage: (image: ReadableStream, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
changeNickname: (nickname: string, threadID: string, pariticipantID: string, callback?: (err?: Error) => void) => Promise<void>,
changeThreadColor: (color: string, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
changeThreadEmoji: (emoji: string | null, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
createNewGroup: (participantIDs: string[], groupTitle?: string, callback?: (err: Error, threadID: string) => void) => Promise<string>,
createPoll: (title: string, threadID: string, options?: { [item: string]: boolean }, callback?: (err?: Error) => void) => Promise<void>,
createPollMqtt: (title: string, options?: { [item: string]: boolean }, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
deleteMessage: (messageOrMessages: string | string[], callback?: (err?: Error) => void) => Promise<void>,
deleteThread: (threadOrThreads: string | string[], callback?: (err?: Error) => void) => Promise<void>,
editMessage: (text: string, messageID: string, callback?: (err?: Error) => void) => Promise<void>,
forwardAttachment: (attachmentID: string, userOrUsers: string | string[], callback?: (err?: Error) => void) => Promise<void>,
forwardMessage: (messageID: string, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
getAppState: () => any,
getCurrentUserID: () => string,
getEmojiUrl: (c: string, size: number, pixelRatio: number) => string,
Expand All @@ -112,15 +116,19 @@ declare module '@xaviabot/fca-unofficial' {
markAsReadAll: (callback?: (err?: Error) => void) => Promise<void>,
markAsSeen(seenTimestamp?: number, callback?: (err?: Error) => void): Promise<void>,
muteThread: (threadID: string, muteSeconds: number, callback?: (err?: Error) => void) => Promise<void>,
pinMessage: (pinMode: boolean, messageID: string, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
removeUserFromGroup: (userID: string, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
resolvePhotoUrl: (photoID: string, callback?: (err: Error | null, url: string) => void) => Promise<string>,
sendMessage: typeof sendMessage,
sendTypingIndicator: (threadID: string, callback?: (err?: Error) => void) => Promise<void>,
sendTypingIndicatorMqtt: (isTyping: boolean, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
setMessageReaction: (reaction: string, messageID: string, callback?: (err?: Error) => void, forceCustomReaction?: boolean) => Promise<void>,
setMessageReactionMqtt: (reaction: string, messageID: string, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
setOptions: (options: Partial<IFCAU_Options>) => void,
setTitle: (newTitle: string, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
setTheme: (themeID?: string, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
unsendMessage: (messageID: string, callback?: (err?: Error) => void) => Promise<void>,
editMessage: (text: string, messageID: string, callback?: (err?: Error) => void) => Promise<void>
unsendMessageMqtt: (messageID: string, threadID: string, callback?: (err?: Error) => void) => Promise<void>,
}

export type IFCAU_ListenMessage =
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,19 @@ function buildAPI(globalOptions, html, jar) {
"changeArchivedStatus",
"changeBio",
"changeBlockedStatus",
"changeBlockedStatusMqtt",
"changeGroupImage",
"changeNickname",
"changeThreadColor",
"changeThreadEmoji",
"createNewGroup",
"createPoll",
"createPollMqtt",
"deleteMessage",
"deleteThread",
"editMessage",
"forwardAttachment",
"forwardMessage",
"getCurrentUserID",
"getEmojiUrl",
"getFriendsList",
Expand All @@ -233,17 +237,21 @@ function buildAPI(globalOptions, html, jar) {
"markAsReadAll",
"markAsSeen",
"muteThread",
"pinMessage",
"removeUserFromGroup",
"resolvePhotoUrl",
"searchForThread",
"sendMessage",
"sendTypingIndicator",
"sendTypingIndicatorMqtt",
"setMessageReaction",
"setMessageReactionMqtt",
"setTitle",
"setTheme",
"threadColors",
"unsendMessage",
"unsendMessageMqtt",
"unfriend",
"editMessage",

// HTTP
"httpGet",
Expand Down
79 changes: 79 additions & 0 deletions src/changeBlockedStatusMqtt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

const { generateOfflineThreadingID, getCurrentTimestamp, getGUID } = require('@xaviabot/fca-unofficial/utils');

function isCallable(func) {
try {
Reflect.apply(func, null, []);
return true;
} catch (error) {
return false;
}
}

module.exports = function (defaultFuncs, api, ctx) {
return function changeBlockedStatusMqtt(userID, status, type, callback) {
if (!ctx.mqttClient) {
throw new Error('Not connected to MQTT');
}

ctx.wsReqNumber += 1;
ctx.wsTaskNumber += 1;

const label = '334';
let userBlockAction = 0;

switch (type) {
case 'messenger':
if (status) {
userBlockAction = 1; // Block
} else {
userBlockAction = 0; // Unblock
}
break;
case 'facebook':
if (status) {
userBlockAction = 3; // Block
} else {
userBlockAction = 2; // Unblock
}
break;
default:
throw new Error('Invalid type');
}

const taskPayload = {
blockee_id: userID,
request_id: getGUID(),
user_block_action: userBlockAction,
};

const payload = JSON.stringify(taskPayload);
const version = '25393437286970779';

const task = {
failure_count: null,
label: label,
payload: payload,
queue_name: 'native_sync_block',
task_id: ctx.wsTaskNumber,
};

const content = {
app_id: '2220391788200892',
payload: JSON.stringify({
tasks: [task],
epoch_id: parseInt(generateOfflineThreadingID()),
version_id: version,
}),
request_id: ctx.wsReqNumber,
type: 3,
};

if (isCallable(callback)) {
ctx.reqCallbacks[ctx.wsReqNumber] = callback;
}

ctx.mqttClient.publish('/ls_req', JSON.stringify(content), { qos: 1, retain: false });
};
};
56 changes: 56 additions & 0 deletions src/createPollMqtt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const { generateOfflineThreadingID, getCurrentTimestamp } = require('@xaviabot/fca-unofficial/utils');

function isCallable(func) {
try {
Reflect.apply(func, null, []);
return true;
} catch (error) {
return false;
}
}

module.exports = function (defaultFuncs, api, ctx) {
return function createPollMqtt(title, options, threadID, callback) {
if (!ctx.mqttClient) {
throw new Error('Not connected to MQTT');
}

ctx.wsReqNumber += 1;
ctx.wsTaskNumber += 1;

const taskPayload = {
question_text: title,
thread_key: threadID,
options: options,
sync_group: 1,
};

const task = {
failure_count: null,
label: '163',
payload: JSON.stringify(taskPayload),
queue_name: 'poll_creation',
task_id: ctx.wsTaskNumber,
};

const content = {
app_id: '2220391788200892',
payload: JSON.stringify({
data_trace_id: null,
epoch_id: parseInt(generateOfflineThreadingID()),
tasks: [task],
version_id: '7158486590867448',
}),
request_id: ctx.wsReqNumber,
type: 3,
};

if (isCallable(callback)) {
ctx.reqCallbacks[ctx.wsReqNumber] = callback;
}

ctx.mqttClient.publish('/ls_req', JSON.stringify(content), { qos: 1, retain: false });
};
};
60 changes: 60 additions & 0 deletions src/forwardMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

const { generateOfflineThreadingID, getCurrentTimestamp } = require('../utils');

function isCallable(func) {
try {
Reflect.apply(func, null, []);
return true;
} catch (error) {
return false;
}
}

module.exports = function (defaultFuncs, api, ctx) {
return function forwardMessage(messageID, threadID, callback) {
if (!ctx.mqttClient) {
throw new Error('Not connected to MQTT');
}

ctx.wsReqNumber += 1;
ctx.wsTaskNumber += 1;

const taskPayload = {
thread_id: threadID,
otid: parseInt(generateOfflineThreadingID()),
source: 65544,
send_type: 5,
sync_group: 1,
forwarded_msg_id: messageID,
strip_forwarded_msg_caption: 0,
initiating_source: 1,
};

const task = {
failure_count: null,
label: '46',
payload: JSON.stringify(taskPayload),
queue_name: `${threadID}`,
task_id: ctx.wsTaskNumber,
};

const content = {
app_id: '2220391788200892',
payload: JSON.stringify({
data_trace_id: null,
epoch_id: parseInt(generateOfflineThreadingID()),
tasks: [task],
version_id: '25095469420099952',
}),
request_id: ctx.wsReqNumber,
type: 3,
};

if (isCallable(callback)) {
ctx.reqCallbacks[ctx.wsReqNumber] = callback;
}

ctx.mqttClient.publish('/ls_req', JSON.stringify(content), { qos: 1, retain: false });
};
};
58 changes: 58 additions & 0 deletions src/pinMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

const { generateOfflineThreadingID, getCurrentTimestamp } = require('../utils');

function isCallable(func) {
try {
Reflect.apply(func, null, []);
return true;
} catch (error) {
return false;
}
}

module.exports = function (defaultFuncs, api, ctx) {
return function pinMessage(pinMode, messageID, threadID, callback) {
if (!ctx.mqttClient) {
throw new Error('Not connected to MQTT');
}

ctx.wsReqNumber += 1;
ctx.wsTaskNumber += 1;

const taskLabel = pinMode ? '430' : '431';
const queueNamePrefix = pinMode ? 'pin_msg_v2_' : 'unpin_msg_v2_';

const taskPayload = {
thread_key: threadID,
message_id: messageID,
timestamp_ms: getCurrentTimestamp(),
};

const task = {
failure_count: null,
label: taskLabel,
payload: JSON.stringify(taskPayload),
queue_name: `${queueNamePrefix}${threadID}`,
task_id: ctx.wsTaskNumber,
};

const content = {
app_id: '2220391788200892',
payload: JSON.stringify({
data_trace_id: null,
epoch_id: parseInt(generateOfflineThreadingID()),
tasks: [task],
version_id: '25095469420099952',
}),
request_id: ctx.wsReqNumber,
type: 3,
};

if (isCallable(callback)) {
ctx.reqCallbacks[ctx.wsReqNumber] = callback;
}

ctx.mqttClient.publish('/ls_req', JSON.stringify(content), { qos: 1, retain: false });
};
};
57 changes: 57 additions & 0 deletions src/sendTypingIndicatorMqtt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

const { generateOfflineThreadingID, getCurrentTimestamp } = require('../utils');

function isCallable(func) {
try {
Reflect.apply(func, null, []);
return true;
} catch (error) {
return false;
}
}

module.exports = function (defaultFuncs, api, ctx) {
return function sendTypingIndicatorMqtt(isTyping, threadID, callback) {
if (!ctx.mqttClient) {
throw new Error('Not connected to MQTT');
}

ctx.wsReqNumber += 1;

api.getThreadInfo(threadID).then(threadData => {
const label = '3';
const isGroupThread = threadData.isGroup ? 1 : 0;
const attribution = 0;

const taskPayload = {
thread_key: threadID,
is_group_thread: isGroupThread,
is_typing: isTyping ? 1 : 0,
attribution: attribution,
};

const payload = JSON.stringify(taskPayload);
const version = '25393437286970779';

const content = {
app_id: '2220391788200892',
payload: JSON.stringify({
label: label,
payload: payload,
version: version,
}),
request_id: ctx.wsReqNumber,
type: 4,
};

if (isCallable(callback)) {
ctx.reqCallbacks[ctx.wsReqNumber] = callback;
}

ctx.mqttClient.publish('/ls_req', JSON.stringify(content), { qos: 1, retain: false });
}).catch(error => {
throw new Error('Failed to get thread info');
});
};
};
Loading

0 comments on commit 0cbe674

Please sign in to comment.