Skip to content

Commit

Permalink
feat(sendAttachment): compute CIDs on the client side
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Sep 17, 2024
1 parent 77cd1a6 commit 84660ac
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/store/modules/chat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Cryptos, TransactionStatus as TS, MessageType } from '@/lib/constants'
import { isStringEqualCI } from '@/lib/textHelpers'
import { replyMessageAsset, attachmentAsset } from '@/lib/adamant-api/asset'
import { encodeFile } from '../../../lib/adamant-api'
import { readFileAsBuffer, uploadFiles } from '../../../lib/file'
import { computeCID, readFileAsBuffer, uploadFiles } from '../../../lib/file'

import { generateAdamantChats } from './utils/generateAdamantChats'
import { isAllNodesDisabledError, isAllNodesOfflineError } from '@/lib/nodes/utils/errors'
Expand Down Expand Up @@ -791,11 +791,14 @@ const actions = {
)
)

// Updating nonces
// Updating CIDs and Nonces
const nonces = encodedFiles.map((file) => [file.nonce, file.nonce]) // @todo preview nonce
const cidsList = await Promise.all(encodedFiles.map((file) => computeCID(file.binary))) // @todo preview cid
const cids = cidsList.map((cid) => [cid, cid])

let newAsset = replyToId
? { replyto_id: replyToId, reply_message: attachmentAsset(files, nonces, undefined, message) }
: attachmentAsset(files, nonces, undefined, message)
? { replyto_id: replyToId, reply_message: attachmentAsset(files, nonces, cids, message) }
: attachmentAsset(files, nonces, cids, message)
commit('updateMessage', {
id: messageObject.id,
partnerId: recipientId,
Expand All @@ -806,18 +809,6 @@ const actions = {
const uploadData = await uploadFiles(files, encodedFiles)
console.log('Files uploaded', uploadData)

// Update CIDs
const cids = uploadData.cids.map((cid) => [cid, cid]) // @todo preview cid
newAsset = replyToId
? { replyto_id: replyToId, reply_message: attachmentAsset(files, nonces, cids, message) }
: attachmentAsset(files, nonces, cids, message)
commit('updateMessage', {
id: messageObject.id,
partnerId: recipientId,
asset: newAsset
})
console.log('Updated CIDs', newAsset)

return queueMessage(newAsset, recipientId, MessageType.RICH_CONTENT_MESSAGE)
.then((res) => {
if (!res.success) {
Expand Down

0 comments on commit 84660ac

Please sign in to comment.