Page MenuHomePhorge

D14541.1768464867.diff
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

D14541.1768464867.diff

diff --git a/lib/utils/delete-message-utils.js b/lib/utils/delete-message-utils.js
new file mode 100644
--- /dev/null
+++ b/lib/utils/delete-message-utils.js
@@ -0,0 +1,78 @@
+// @flow
+
+import invariant from 'invariant';
+import * as React from 'react';
+import uuid from 'uuid';
+
+import { useDispatchActionPromise } from './redux-promise-utils.js';
+import { useSelector } from './redux-utils.js';
+import {
+ sendDeleteMessageActionTypes,
+ useSendDeleteMessage,
+} from '../actions/message-actions.js';
+import {
+ dmOperationSpecificationTypes,
+ type OutboundDMOperationSpecification,
+} from '../shared/dm-ops/dm-op-utils.js';
+import { useProcessAndSendDMOperation } from '../shared/dm-ops/process-dm-ops.js';
+import type { DMSendDeleteMessageOperation } from '../types/dm-ops.js';
+import type { MessageInfo } from '../types/message-types.js';
+import { thickThreadTypes } from '../types/thread-types-enum.js';
+
+function useDeleteMessage(): (message: MessageInfo) => Promise<void> {
+ const callDeleteMessage = useSendDeleteMessage();
+ const dispatchActionPromise = useDispatchActionPromise();
+ const processAndSendDMOperation = useProcessAndSendDMOperation();
+ const viewerID = useSelector(
+ state => state.currentUserInfo && state.currentUserInfo.id,
+ );
+ const threadInfos = useSelector(state => state.threadStore.threadInfos);
+
+ return React.useCallback(
+ async (message: MessageInfo) => {
+ const messageID = message.id;
+ invariant(messageID, 'Message ID should be set');
+ const threadInfo = threadInfos[message.threadID];
+ if (threadInfo.thick) {
+ invariant(viewerID, 'viewerID should be set');
+ const op: DMSendDeleteMessageOperation = {
+ type: 'send_delete_message',
+ threadID: threadInfo.id,
+ creatorID: viewerID,
+ time: Date.now(),
+ messageID: uuid.v4(),
+ targetMessageID: messageID,
+ };
+ const opSpecification: OutboundDMOperationSpecification = {
+ type: dmOperationSpecificationTypes.OUTBOUND,
+ op,
+ recipients: {
+ type: 'all_thread_members',
+ threadID:
+ threadInfo.type === thickThreadTypes.THICK_SIDEBAR &&
+ threadInfo.parentThreadID
+ ? threadInfo.parentThreadID
+ : threadInfo.id,
+ },
+ };
+ await processAndSendDMOperation(opSpecification);
+ } else {
+ void dispatchActionPromise(
+ sendDeleteMessageActionTypes,
+ callDeleteMessage({
+ targetMessageID: messageID,
+ }),
+ );
+ }
+ },
+ [
+ callDeleteMessage,
+ dispatchActionPromise,
+ processAndSendDMOperation,
+ threadInfos,
+ viewerID,
+ ],
+ );
+}
+
+export { useDeleteMessage };

File Metadata

Mime Type
text/plain
Expires
Thu, Jan 15, 8:14 AM (6 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5936883
Default Alt Text
D14541.1768464867.diff (2 KB)

Event Timeline