Usync: Barebones Usync Protocol support (#960)

* feature(feature/usync-mex): initial commit

* chore: fix merge commit

* chore:lint
This commit is contained in:
Rajeh Taher
2024-12-22 23:38:41 +02:00
committed by GitHub
parent 8333a25fca
commit f1f49ad2c8
14 changed files with 525 additions and 147 deletions
@@ -0,0 +1,32 @@
import { USyncQueryProtocol } from '../../Types/USync'
import { assertNodeErrorFree, BinaryNode } from '../../WABinary'
import { USyncUser } from '../USyncUser'
export class USyncContactProtocol implements USyncQueryProtocol {
name = 'contact'
getQueryElement(): BinaryNode {
return {
tag: 'contact',
attrs: {},
}
}
getUserElement(user: USyncUser): BinaryNode {
//TODO: Implement type / username fields (not yet supported)
return {
tag: 'contact',
attrs: {},
content: user.phone,
}
}
parser(node: BinaryNode): boolean {
if(node.tag === 'contact') {
assertNodeErrorFree(node)
return node?.attrs?.type === 'in'
}
return false
}
}
@@ -0,0 +1,78 @@
import { USyncQueryProtocol } from '../../Types/USync'
import { assertNodeErrorFree, BinaryNode, getBinaryNodeChild } from '../../WABinary'
//import { USyncUser } from '../USyncUser'
export type KeyIndexData = {
timestamp: number
signedKeyIndex?: Uint8Array
expectedTimestamp?: number
}
export type DeviceListData = {
id: number
keyIndex?: number
isHosted?: boolean
}
export type ParsedDeviceInfo = {
deviceList?: DeviceListData[]
keyIndex?: KeyIndexData
}
export class USyncDeviceProtocol implements USyncQueryProtocol {
name = 'devices'
getQueryElement(): BinaryNode {
return {
tag: 'devices',
attrs: {
version: '2',
},
}
}
getUserElement(/* user: USyncUser */): BinaryNode | null {
//TODO: Implement device phashing, ts and expectedTs
//TODO: if all are not present, return null <- current behavior
//TODO: otherwise return a node w tag 'devices' w those as attrs
return null
}
parser(node: BinaryNode): ParsedDeviceInfo {
const deviceList: DeviceListData[] = []
let keyIndex: KeyIndexData | undefined = undefined
if(node.tag === 'devices') {
assertNodeErrorFree(node)
const deviceListNode = getBinaryNodeChild(node, 'device-list')
const keyIndexNode = getBinaryNodeChild(node, 'key-index-list')
if(Array.isArray(deviceListNode?.content)) {
for(const { tag, attrs } of deviceListNode!.content) {
const id = +attrs.id
const keyIndex = +attrs['key-index']
if(tag === 'device') {
deviceList.push({
id,
keyIndex,
isHosted: !!(attrs['is_hosted'] && attrs['is_hosted'] === 'true')
})
}
}
}
if(keyIndexNode?.tag === 'key-index-list') {
keyIndex = {
timestamp: +keyIndexNode.attrs['ts'],
signedKeyIndex: keyIndexNode?.content as Uint8Array,
expectedTimestamp: keyIndexNode.attrs['expected_ts'] ? +keyIndexNode.attrs['expected_ts'] : undefined
}
}
}
return {
deviceList,
keyIndex
}
}
}
@@ -0,0 +1,35 @@
import { USyncQueryProtocol } from '../../Types/USync'
import { assertNodeErrorFree, BinaryNode } from '../../WABinary'
export type DisappearingModeData = {
duration: number
setAt?: Date
}
export class USyncDisappearingModeProtocol implements USyncQueryProtocol {
name = 'disappearing_mode'
getQueryElement(): BinaryNode {
return {
tag: 'disappearing_mode',
attrs: {},
}
}
getUserElement(): null {
return null
}
parser(node: BinaryNode): DisappearingModeData | undefined {
if(node.tag === 'status') {
assertNodeErrorFree(node)
const duration: number = +node?.attrs.duration
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
return {
duration,
setAt,
}
}
}
}
@@ -0,0 +1,44 @@
import { USyncQueryProtocol } from '../../Types/USync'
import { assertNodeErrorFree, BinaryNode } from '../../WABinary'
export type StatusData = {
status?: string | null
setAt?: Date
}
export class USyncStatusProtocol implements USyncQueryProtocol {
name = 'status'
getQueryElement(): BinaryNode {
return {
tag: 'status',
attrs: {},
}
}
getUserElement(): null {
return null
}
parser(node: BinaryNode): StatusData | undefined {
if(node.tag === 'status') {
assertNodeErrorFree(node)
let status: string | null = node?.content!.toString()
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
if(!status) {
if(+node.attrs?.code === 401) {
status = ''
} else {
status = null
}
} else if(typeof status === 'string' && status.length === 0) {
status = null
}
return {
status,
setAt,
}
}
}
}
+4
View File
@@ -0,0 +1,4 @@
export * from './USyncDeviceProtocol'
export * from './USyncContactProtocol'
export * from './USyncStatusProtocol'
export * from './USyncDisappearingModeProtocol'