Skip to content

Commit

Permalink
Merge branch 'master' into sebin/task/#1571-create-vuex-module-for-chat
Browse files Browse the repository at this point in the history
  • Loading branch information
taoeffect committed Feb 23, 2024
2 parents 6995d07 + ccbe124 commit de40165
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
18 changes: 18 additions & 0 deletions backend/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ route.GET('/time', {}, function (request, h) {
const MEGABYTE = 1048576 // TODO: add settings for these
const SECOND = 1000

// API endpoint to check for streams support
route.POST('/streams-test', {
payload: {
parse: 'false'
}
},
function (request, h) {
if (
request.payload.byteLength === 2 &&
Buffer.from(request.payload).toString() === 'ok'
) {
return h.response().code(204)
} else {
return Boom.badRequest()
}
}
)

// File upload route.
// If accepted, the file will be stored in Chelonia DB.
route.POST('/file', {
Expand Down
6 changes: 2 additions & 4 deletions frontend/controller/actions/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,8 @@ export default (sbp('sbp/selectors/register', {
'gi.actions/identity/shareNewPEK': async (contractID: string, newKeys) => {
const rootState = sbp('state/vuex/state')
const state = rootState[contractID]
const identityContractID = state.attributes.identityContractID

// TODO: Also share PEK with DMs
await Promise.all((state.loginState?.groupIds || []).filter(groupID => !!rootState.contracts[groupID]).map(groupID => {
await Promise.all(Object.keys(state.groups || {}).filter(groupID => !!rootState.contracts[groupID]).map(groupID => {
const CEKid = findKeyIdByName(rootState[groupID], 'cek')
const CSKid = findKeyIdByName(rootState[groupID], 'csk')

Expand Down Expand Up @@ -567,7 +565,7 @@ export default (sbp('sbp/selectors/register', {
hooks: {
preSendCheck: (_, state) => {
// Don't send this message if we're no longer a group member
return state?.profiles?.[identityContractID]?.status === PROFILE_STATUS.ACTIVE
return state?.profiles?.[contractID]?.status === PROFILE_STATUS.ACTIVE
}
}
}).catch(e => {
Expand Down
5 changes: 2 additions & 3 deletions frontend/model/contracts/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -1915,9 +1915,8 @@ sbp('chelonia/defineContract', {

Promise.resolve()
.then(() => sbp('gi.contracts/group/rotateKeys', contractID))
.then(() => {
return sbp('gi.contracts/group/revokeGroupKeyAndRotateOurPEK', contractID, false)
}).catch((e) => {
.then(() => sbp('gi.contracts/group/revokeGroupKeyAndRotateOurPEK', contractID, false))
.catch((e) => {
console.error(`[gi.contracts/group/leaveGroup] for ${contractID}: Error rotating group keys or our PEK`, e)
})

Expand Down
27 changes: 23 additions & 4 deletions shared/domains/chelonia/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { coerce } from '~/shared/multiformats/bytes.js'

// Snippet from <https://github.com/WebKit/standards-positions/issues/24#issuecomment-1181821440>
// Node.js supports request streams, but also this check isn't meant for Node.js
const supportsRequestStreams = typeof window !== 'object' || (() => {
// This part only checks for client-side support. Later, when we try uploading
// a file for the first time, we'll check if requests work, as streams are not
// supported in HTTP/1.1 and lower versions.
let supportsRequestStreams = typeof window !== 'object' || (() => {
let duplexAccessed = false

const hasContentType = new Request('', {
Expand Down Expand Up @@ -44,7 +47,22 @@ const streamToUint8Array = async (s: ReadableStream) => {

// Check for streaming support, as of today (Feb 2024) only Blink-
// based browsers support this (i.e., Firefox and Safari don't).
const ArrayBufferToUint8ArrayStream = async (s: ReadableStream) => {
const ArrayBufferToUint8ArrayStream = async (connectionURL: string, s: ReadableStream) => {
// Even if the browser supports streams, the server must support HTTP/2
if (supportsRequestStreams === true) {
await fetch(`${connectionURL}/streams-test`, {
method: 'POST',
body: new ReadableStream({ start (c) { c.enqueue(Buffer.from('ok')) } }),
duplex: 'half'
}).then((r) => {
if (!r.ok) throw new Error('Unexpected response')
// supportsRequestStreams is tri-state
supportsRequestStreams = 2
}).catch(() => {
console.info('files: Disabling streams support because the streams test failed')
supportsRequestStreams = false
})
}
if (!supportsRequestStreams) {
return await streamToUint8Array(s)
}
Expand Down Expand Up @@ -299,8 +317,9 @@ export default (sbp('sbp/selectors/register', {
const uploadResponse = await fetch(`${this.config.connectionURL}/file`, {
method: 'POST',
signal: this.abortController.signal,
body: await ArrayBufferToUint8ArrayStream(stream),
headers: new Headers([['content-type', `multipart/form-data; boundary=${boundary}`]])
body: await ArrayBufferToUint8ArrayStream(this.config.connectionURL, stream),
headers: new Headers([['content-type', `multipart/form-data; boundary=${boundary}`]]),
duplex: 'half'
})

if (!uploadResponse.ok) throw new Error('Error uploading file')
Expand Down

0 comments on commit de40165

Please sign in to comment.