(feat): add MessageDeleteLive
This commit is contained in:
parent
790a1db788
commit
3ce708b86a
6 changed files with 86 additions and 45 deletions
10
packages/cache/src/sync.tsx
vendored
10
packages/cache/src/sync.tsx
vendored
|
|
@ -242,6 +242,14 @@ export default function CacheSync() {
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (message.type === "MessageDeleteLive") {
|
||||||
|
const deleteData = message.data as {
|
||||||
|
ChatPartnerId: number;
|
||||||
|
SendTime: number;
|
||||||
|
};
|
||||||
|
await removeMessage(deleteData.ChatPartnerId, deleteData.SendTime);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (message.type === "MessageState") {
|
if (message.type === "MessageState") {
|
||||||
await replaceMessage(
|
await replaceMessage(
|
||||||
Number(data.ChatPartnerId),
|
Number(data.ChatPartnerId),
|
||||||
|
|
@ -257,7 +265,7 @@ export default function CacheSync() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[accountId, insertMessage, replaceMessage],
|
[accountId, insertMessage, removeMessage, replaceMessage],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
|
||||||
|
|
@ -470,6 +470,52 @@ export default function Provider({ children }: { children: ReactNode }) {
|
||||||
[currentChatSecret, userIdValue],
|
[currentChatSecret, userIdValue],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const removeMessage = useCallback(
|
||||||
|
(sendTime: number) => {
|
||||||
|
setLiveMessagesState((prev) =>
|
||||||
|
prev.filter((message) => message.SendTime !== sendTime),
|
||||||
|
);
|
||||||
|
const queryKey = [
|
||||||
|
"chat-messages",
|
||||||
|
String(userIdValue),
|
||||||
|
currentChatSecret !== null,
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
queryClient.setQueryData<InfiniteData<RawMessages>>(
|
||||||
|
queryKey,
|
||||||
|
(current) => {
|
||||||
|
if (!current) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
let updated = false;
|
||||||
|
|
||||||
|
const pages = current.pages.map((page) => {
|
||||||
|
const nextPage = page.filter((message) => {
|
||||||
|
const keep = message.SendTime !== sendTime;
|
||||||
|
if (!keep) {
|
||||||
|
updated = true;
|
||||||
|
}
|
||||||
|
return keep;
|
||||||
|
});
|
||||||
|
|
||||||
|
return nextPage;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!updated) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...current,
|
||||||
|
pages,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[currentChatSecret, userIdValue],
|
||||||
|
);
|
||||||
|
|
||||||
const deleteMessage = useCallback(
|
const deleteMessage = useCallback(
|
||||||
async (sendTime: number) => {
|
async (sendTime: number) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -479,53 +525,13 @@ export default function Provider({ children }: { children: ReactNode }) {
|
||||||
});
|
});
|
||||||
|
|
||||||
assertProtocolSuccess("MessageDelete", response);
|
assertProtocolSuccess("MessageDelete", response);
|
||||||
|
removeMessage(sendTime);
|
||||||
setLiveMessagesState((prev) =>
|
|
||||||
prev.filter((message) => message.SendTime !== sendTime),
|
|
||||||
);
|
|
||||||
const queryKey = [
|
|
||||||
"chat-messages",
|
|
||||||
String(userIdValue),
|
|
||||||
currentChatSecret !== null,
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
queryClient.setQueryData<InfiniteData<RawMessages>>(
|
|
||||||
queryKey,
|
|
||||||
(current) => {
|
|
||||||
if (!current) {
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
let updated = false;
|
|
||||||
|
|
||||||
const pages = current.pages.map((page) => {
|
|
||||||
const nextPage = page.filter((message) => {
|
|
||||||
const keep = message.SendTime !== sendTime;
|
|
||||||
if (!keep) {
|
|
||||||
updated = true;
|
|
||||||
}
|
|
||||||
return keep;
|
|
||||||
});
|
|
||||||
|
|
||||||
return nextPage;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!updated) {
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...current,
|
|
||||||
pages,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log(1, "chat", "red", "Failed to delete message", err);
|
log(1, "chat", "red", "Failed to delete message", err);
|
||||||
toast("error", "Failed to delete message", String(err));
|
toast("error", "Failed to delete message", String(err));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[currentChatSecret, send, userIdValue],
|
[removeMessage, send, userIdValue],
|
||||||
);
|
);
|
||||||
|
|
||||||
const setReaction = useCallback(
|
const setReaction = useCallback(
|
||||||
|
|
@ -717,6 +723,18 @@ export default function Provider({ children }: { children: ReactNode }) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message.type === "MessageDeleteLive") {
|
||||||
|
const data = message.data as {
|
||||||
|
ChatPartnerId: number;
|
||||||
|
SendTime: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (data.ChatPartnerId !== userIdValue) return;
|
||||||
|
|
||||||
|
removeMessage(data.SendTime);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (message.type !== "MessageState") return;
|
if (message.type !== "MessageState") return;
|
||||||
|
|
||||||
const rawData = message.data as {
|
const rawData = message.data as {
|
||||||
|
|
@ -762,7 +780,14 @@ export default function Provider({ children }: { children: ReactNode }) {
|
||||||
MessageState: nextState.MessageState,
|
MessageState: nextState.MessageState,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}, [currentChatSecret, editMessage, send, subscribePush, userIdValue]);
|
}, [
|
||||||
|
currentChatSecret,
|
||||||
|
editMessage,
|
||||||
|
removeMessage,
|
||||||
|
send,
|
||||||
|
subscribePush,
|
||||||
|
userIdValue,
|
||||||
|
]);
|
||||||
|
|
||||||
// Replys
|
// Replys
|
||||||
const [replyTo, setReplyTo] = useState<number | undefined>(undefined);
|
const [replyTo, setReplyTo] = useState<number | undefined>(undefined);
|
||||||
|
|
|
||||||
|
|
@ -212,6 +212,7 @@ export function Provider(props: {
|
||||||
"MessageLive",
|
"MessageLive",
|
||||||
"MessageEditLive",
|
"MessageEditLive",
|
||||||
"MessageReactionLive",
|
"MessageReactionLive",
|
||||||
|
"MessageDeleteLive",
|
||||||
"MessageState",
|
"MessageState",
|
||||||
"CallInvite",
|
"CallInvite",
|
||||||
"ErrorNoIota",
|
"ErrorNoIota",
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
- Get omikron url & public key from omega
|
|
||||||
|
|
@ -209,6 +209,13 @@ export const mtp = {
|
||||||
}),
|
}),
|
||||||
response: z.object({}),
|
response: z.object({}),
|
||||||
},
|
},
|
||||||
|
MessageDeleteLive: {
|
||||||
|
request: z.object({}),
|
||||||
|
response: z.object({
|
||||||
|
ChatPartnerId: z.number(),
|
||||||
|
SendTime: z.number(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
MessageEditLive: {
|
MessageEditLive: {
|
||||||
request: z.object({}),
|
request: z.object({}),
|
||||||
response: z.object({
|
response: z.object({
|
||||||
|
|
|
||||||
|
|
@ -149,6 +149,7 @@ type_maps:
|
||||||
MessageReactionAdd: 146
|
MessageReactionAdd: 146
|
||||||
MessageReactionRemove: 147
|
MessageReactionRemove: 147
|
||||||
MessageReactionLive: 148
|
MessageReactionLive: 148
|
||||||
|
MessageDeleteLive: 150
|
||||||
DataTypes:
|
DataTypes:
|
||||||
ErrorType: 32
|
ErrorType: 32
|
||||||
ErrorProtocol: 33
|
ErrorProtocol: 33
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue