Created
August 26, 2024 11:42
-
-
Save marcinwasowicz/d9f3365facba9e226284f470141a531e to your computer and use it in GitHub Desktop.
Test change thread status dm operation
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
diff --git a/lib/tunnelbroker/peer-to-peer-context.js b/lib/tunnelbroker/peer-to-peer-context.js | |
index f117333e51..fb669ed9cd 100644 | |
--- a/lib/tunnelbroker/peer-to-peer-context.js | |
+++ b/lib/tunnelbroker/peer-to-peer-context.js | |
@@ -118,50 +118,61 @@ async function processOutboundP2PMessages( | |
message.deviceID, | |
); | |
}; | |
- | |
+ const promises = []; | |
for (const peerDeviceID in devicesMap) { | |
for (const message of devicesMap[peerDeviceID]) { | |
- if (message.status === outboundP2PMessageStatuses.persisted) { | |
- try { | |
- const result = await olmAPI.encryptAndPersist( | |
- message.plaintext, | |
- message.deviceID, | |
- message.messageID, | |
- ); | |
- | |
- const encryptedMessage: OutboundP2PMessage = { | |
- ...message, | |
- ciphertext: JSON.stringify(result), | |
- }; | |
- await sendMessageToPeer(encryptedMessage); | |
- } catch (e) { | |
- if (!e.message?.includes(olmSessionErrors.sessionDoesNotExists)) { | |
- console.log(`Error sending messages to peer ${peerDeviceID}`, e); | |
- break; | |
- } | |
- try { | |
- await peerOlmSessionsCreator(message.userID, peerDeviceID); | |
- const result = await olmAPI.encryptAndPersist( | |
- message.plaintext, | |
- message.deviceID, | |
- message.messageID, | |
- ); | |
- const encryptedMessage: OutboundP2PMessage = { | |
- ...message, | |
- ciphertext: JSON.stringify(result), | |
- }; | |
+ promises.push( | |
+ (async () => { | |
+ if (message.status === outboundP2PMessageStatuses.persisted) { | |
+ try { | |
+ const result = await olmAPI.encryptAndPersist( | |
+ message.plaintext, | |
+ message.deviceID, | |
+ message.messageID, | |
+ ); | |
- await sendMessageToPeer(encryptedMessage); | |
- } catch (err) { | |
- console.log(`Error sending messages to peer ${peerDeviceID}`, err); | |
- break; | |
+ const encryptedMessage: OutboundP2PMessage = { | |
+ ...message, | |
+ ciphertext: JSON.stringify(result), | |
+ }; | |
+ await sendMessageToPeer(encryptedMessage); | |
+ } catch (e) { | |
+ if (!e.message?.includes(olmSessionErrors.sessionDoesNotExists)) { | |
+ console.log( | |
+ `Error sending messages to peer ${peerDeviceID}`, | |
+ e, | |
+ ); | |
+ return; | |
+ } | |
+ try { | |
+ await peerOlmSessionsCreator(message.userID, peerDeviceID); | |
+ const result = await olmAPI.encryptAndPersist( | |
+ message.plaintext, | |
+ message.deviceID, | |
+ message.messageID, | |
+ ); | |
+ const encryptedMessage: OutboundP2PMessage = { | |
+ ...message, | |
+ ciphertext: JSON.stringify(result), | |
+ }; | |
+ | |
+ await sendMessageToPeer(encryptedMessage); | |
+ } catch (err) { | |
+ console.log( | |
+ `Error sending messages to peer ${peerDeviceID}`, | |
+ err, | |
+ ); | |
+ return; | |
+ } | |
+ } | |
+ } else if (message.status === outboundP2PMessageStatuses.encrypted) { | |
+ await sendMessageToPeer(message); | |
} | |
- } | |
- } else if (message.status === outboundP2PMessageStatuses.encrypted) { | |
- await sendMessageToPeer(message); | |
- } | |
+ })(), | |
+ ); | |
} | |
} | |
+ await Promise.all(promises); | |
} | |
const AUTOMATIC_RETRY_FREQUENCY = 30 * 1000; | |
diff --git a/web/chat/chat-input-bar.react.js b/web/chat/chat-input-bar.react.js | |
index 03ca0c4e44..7eef2166ae 100644 | |
--- a/web/chat/chat-input-bar.react.js | |
+++ b/web/chat/chat-input-bar.react.js | |
@@ -64,6 +64,14 @@ import { | |
webMentionTypeaheadRegex, | |
} from '../utils/typeahead-utils.js'; | |
+import { type DMOperation } from 'lib/types/dm-ops.js'; | |
+import type { MessageSourceMetadata } from 'lib/types/db-ops-types.js'; | |
+import uuid from 'uuid'; | |
+import { useProcessDMOperation } from 'lib/shared/dm-ops/process-dm-ops.js'; | |
+import type { DMOperationSpecification } from 'lib/shared/dm-ops/dm-op-utils.js'; | |
+import { usePeerToPeerCommunication } from 'lib/tunnelbroker/peer-to-peer-context.js'; | |
+import sleep from 'lib/utils/sleep.js'; | |
+ | |
type BaseProps = { | |
+threadInfo: ThreadInfo, | |
+inputState: InputState, | |
@@ -83,6 +91,11 @@ type Props = { | |
+currentUserIsVoiced: boolean, | |
+currentUserCanJoinThread: boolean, | |
+threadFrozen: boolean, | |
+ +processDMOperation: ( | |
+ dmOp: DMOperation, | |
+ metadata: ?MessageSourceMetadata, | |
+ ) => Promise<void>, | |
+ +sendDMOperation: (dmOp: DMOperationSpecification) => Promise<void>, | |
}; | |
class ChatInputBar extends React.PureComponent<Props> { | |
@@ -502,6 +515,56 @@ class ChatInputBar extends React.PureComponent<Props> { | |
this.props.threadInfo, | |
this.props.parentThreadInfo, | |
); | |
+ | |
+ void (async () => { | |
+ const viewerID = this.props.viewerID; | |
+ invariant(viewerID, 'should have viewer ID in order to send a message'); | |
+ const tid = uuid.v4(); | |
+ const createThreadOperation = { | |
+ type: 'create_thread', | |
+ threadID: tid, | |
+ creatorID: viewerID, | |
+ time: Date.now(), | |
+ threadType: 13, | |
+ memberIDs: [viewerID, '0C42D8C8-2605-4A3D-807C-DA96AF56A0F6'], | |
+ roleID: uuid.v4(), | |
+ newMessageID: uuid.v4(), | |
+ }; | |
+ await this.props.processDMOperation(createThreadOperation); | |
+ await this.props.sendDMOperation({ | |
+ op: createThreadOperation, | |
+ supportsAutoRetry: false, | |
+ recipients: { type: 'self_devices' }, | |
+ }); | |
+ await sleep(1000); | |
+ const changeStatusOperation1 = { | |
+ type: 'change_thread_status', | |
+ creatorID: viewerID, | |
+ time: Date.now(), | |
+ threadID: tid, | |
+ unread: false, | |
+ }; | |
+ await this.props.processDMOperation(changeStatusOperation1); | |
+ await this.props.sendDMOperation({ | |
+ op: changeStatusOperation1, | |
+ supportsAutoRetry: false, | |
+ recipients: { type: 'self_devices' }, | |
+ }); | |
+ await sleep(5000); | |
+ const changeStatusOperation2 = { | |
+ type: 'change_thread_status', | |
+ creatorID: viewerID, | |
+ time: Date.now(), | |
+ threadID: tid, | |
+ unread: true, | |
+ }; | |
+ await this.props.processDMOperation(changeStatusOperation2); | |
+ await this.props.sendDMOperation({ | |
+ op: changeStatusOperation2, | |
+ supportsAutoRetry: false, | |
+ recipients: { type: 'self_devices' }, | |
+ }); | |
+ })(); | |
} | |
multimediaInputRef = (multimediaInput: ?HTMLInputElement) => { | |
@@ -667,7 +730,8 @@ const ConnectedChatInputBar: React.ComponentType<BaseProps> = | |
() => [...suggestedUsers, ...suggestedChats], | |
[suggestedUsers, suggestedChats], | |
); | |
- | |
+ const processDMOperation = useProcessDMOperation(); | |
+ const { sendDMOperation } = usePeerToPeerCommunication(); | |
return ( | |
<ChatInputBar | |
{...props} | |
@@ -684,6 +748,8 @@ const ConnectedChatInputBar: React.ComponentType<BaseProps> = | |
currentUserIsVoiced={currentUserIsVoiced} | |
currentUserCanJoinThread={currentUserCanJoinThread} | |
threadFrozen={threadFrozen} | |
+ processDMOperation={processDMOperation} | |
+ sendDMOperation={sendDMOperation} | |
/> | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment