-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Usync: Barebones Usync Protocol support (#960)
* feature(feature/usync-mex): initial commit * chore: fix merge commit * chore:lint
- Loading branch information
Showing
14 changed files
with
521 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Boom } from '@hapi/boom' | ||
import { SocketConfig } from '../Types' | ||
import { BinaryNode, S_WHATSAPP_NET } from '../WABinary' | ||
import { USyncQuery } from '../WAUSync' | ||
import { makeSocket } from './socket' | ||
|
||
export const makeUSyncSocket = (config: SocketConfig) => { | ||
const sock = makeSocket(config) | ||
|
||
const { | ||
generateMessageTag, | ||
query, | ||
} = sock | ||
|
||
const executeUSyncQuery = async(usyncQuery: USyncQuery) => { | ||
if(usyncQuery.protocols.length === 0) { | ||
throw new Boom('USyncQuery must have at least one protocol') | ||
} | ||
|
||
// todo: validate users, throw WARNING on no valid users | ||
// variable below has only validated users | ||
const validUsers = usyncQuery.users | ||
|
||
const userNodes = validUsers.map((user) => { | ||
return { | ||
tag: 'user', | ||
attrs: { | ||
jid: !user.phone ? user.id : undefined, | ||
}, | ||
content: usyncQuery.protocols | ||
.map((a) => a.getUserElement(user)) | ||
.filter(a => a !== null) | ||
} as BinaryNode | ||
}) | ||
|
||
const listNode: BinaryNode = { | ||
tag: 'list', | ||
attrs: {}, | ||
content: userNodes | ||
} | ||
|
||
const queryNode: BinaryNode = { | ||
tag: 'query', | ||
attrs: {}, | ||
content: usyncQuery.protocols.map((a) => a.getQueryElement()) | ||
} | ||
const iq = { | ||
tag: 'iq', | ||
attrs: { | ||
to: S_WHATSAPP_NET, | ||
type: 'get', | ||
xmlns: 'usync', | ||
}, | ||
content: [ | ||
{ | ||
tag: 'usync', | ||
attrs: { | ||
context: usyncQuery.context, | ||
mode: usyncQuery.mode, | ||
sid: generateMessageTag(), | ||
last: 'true', | ||
index: '0', | ||
}, | ||
content: [ | ||
queryNode, | ||
listNode | ||
] | ||
} | ||
], | ||
} | ||
|
||
const result = await query(iq) | ||
|
||
return usyncQuery.parseUSyncQueryResult(result) | ||
} | ||
|
||
return { | ||
...sock, | ||
executeUSyncQuery, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { BinaryNode } from '../WABinary' | ||
import { USyncUser } from '../WAUSync' | ||
|
||
/** | ||
* Defines the interface for a USyncQuery protocol | ||
*/ | ||
export interface USyncQueryProtocol { | ||
/** | ||
* The name of the protocol | ||
*/ | ||
name: string | ||
/** | ||
* Defines what goes inside the query part of a USyncQuery | ||
*/ | ||
getQueryElement: () => BinaryNode | ||
/** | ||
* Defines what goes inside the user part of a USyncQuery | ||
*/ | ||
getUserElement: (user: USyncUser) => BinaryNode | null | ||
|
||
/** | ||
* Parse the result of the query | ||
* @param data Data from the result | ||
* @returns Whatever the protocol is supposed to return | ||
*/ | ||
parser: (data: BinaryNode) => unknown | ||
} |
Oops, something went wrong.