Skip to content

Commit

Permalink
JS - Private groups - Fix tests for new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Aug 28, 2024
1 parent 5ae5229 commit ed6e5e4
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 98 deletions.
35 changes: 25 additions & 10 deletions golos-lib-js/src/auth/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ function msgFromHex(hex, lengthPrefixed = false) {

const parseMsg = (message, rawMsg, raw_messages = false) => {
message.raw_message = rawMsg
message.decrypt_date = message.receive_date
if (!raw_messages) {
let msg = JSON.parse(message.raw_message)
msg.type = msg.type || 'text'
Expand All @@ -260,6 +261,12 @@ const parseMsg = (message, rawMsg, raw_messages = false) => {
}
}

const parseQuery = (query) => {
assert(query && typeof(query) === 'object' && !Array.isArray(query),
'argument should be an object with argument-field. See golos-lib-js documentation.')
return query
}

const warnDeprecation = (oldMethod, newMethod) => {
console.warn(`golos.messages.${oldMethod} deprecated. ` +
`It is not async, not supports groups, etc. ` +
Expand Down Expand Up @@ -342,12 +349,16 @@ export function decode(private_memo_key, second_user_public_memo_key, message_ob
return results;
}

export async function decodeMsgs({ msgs, before_decode, for_each, on_error,
raw_messages,
private_memo, // chats
api, login, // groups
begin_idx, end_idx,
}) {
export async function decodeMsgs(query) {
const { msgs, before_decode, for_each, on_error,
raw_messages,
private_memo, // chats
api, login, // groups
begin_idx, end_idx,
} = parseQuery(query)

assert(msgs, 'msgs is required')

let private_key = private_memo && toPrivateObj(private_memo)
const myPublic = private_key.toPublic().toString()
let shareds = {}
Expand All @@ -364,11 +375,16 @@ export async function decodeMsgs({ msgs, before_decode, for_each, on_error,
}
return true
}
console.error('golos.messages.decodeMsgs', i, exception)
console.warn('golos.messages.decodeMsgs', i, exception)
return false
}

try {
if (!message.group || message.decrypt_date !== message.receive_date) {
message.raw_message = null // Will be set if message will be successfully decoded
message.message = null // Will be set if message will be also successfully parsed and validated
}

if (before_decode && before_decode(message, i, results)) {
return true
}
Expand All @@ -378,9 +394,6 @@ export async function decodeMsgs({ msgs, before_decode, for_each, on_error,
message.to_memo_key : message.from_memo_key
// Most "heavy" line (in private chats)
if (!shareds[pubKey]) {
console.error(pubKey)
console.error(message.from_memo_key)
console.error(message.to_memo_key)
shareds[pubKey] = private_key.get_shared_secret(toPublicObj(pubKey))
}

Expand All @@ -393,6 +406,7 @@ export async function decodeMsgs({ msgs, before_decode, for_each, on_error,
decrypted = msgFromBuf(mbuf, true)

parseMsg(message, decrypted, raw_messages)
message.decryptor = 'golos-lib'
} else {
let rawMsg = msgFromHex(message.encrypted_message)
const isEncrypted = rawMsg.startsWith('{"t":"em"')
Expand Down Expand Up @@ -448,6 +462,7 @@ export async function decodeMsgs({ msgs, before_decode, for_each, on_error,
const msg = results[idxs[i]]
dec.decrypted = dec.body // TODO: but `decrypted` should be hex
parseMsg(msg, dec.decrypted, raw_messages)
message.decryptor = 'node/decrypt_messages'
// for_each
} else {
// on_error, for_each if not handled
Expand Down
Loading

0 comments on commit ed6e5e4

Please sign in to comment.