Skip to content

Commit

Permalink
make sure transfer success is handled (#5075)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianjelfs authored Dec 22, 2023
1 parent 7b379c8 commit da3ad83
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
10 changes: 7 additions & 3 deletions frontend/openchat-agent/src/utils/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
DataContent,
CreatedUser,
DiamondMembershipStatus,
TransferSuccess,
} from "openchat-shared";
import {
chatIdentifiersEqual,
Expand All @@ -41,7 +42,7 @@ import {
} from "openchat-shared";
import type { Principal } from "@dfinity/principal";

const CACHE_VERSION = 93;
const CACHE_VERSION = 94;
const MAX_INDEX = 9999999999;

export type Database = Promise<IDBPDatabase<ChatSchema>>;
Expand Down Expand Up @@ -683,7 +684,7 @@ export function setCachedMessageFromSendResponse(
threadRootMessageIndex?: number,
): ([resp, message]: [SendMessageResponse, Message]) => [SendMessageResponse, Message] {
return ([resp, message]: [SendMessageResponse, Message]) => {
if (resp.kind !== "success") {
if (resp.kind !== "success" && resp.kind !== "transfer_success") {
recordFailedMessage(db, chatId, sentEvent, threadRootMessageIndex);
return [resp, message];
}
Expand Down Expand Up @@ -729,7 +730,10 @@ export async function setCachePrimerTimestamp(
await (await db).put("cachePrimer", timestamp, chatIdentifierString);
}

function messageToEvent(message: Message, resp: SendMessageSuccess): EventWrapper<Message> {
function messageToEvent(
message: Message,
resp: SendMessageSuccess | TransferSuccess,
): EventWrapper<Message> {
return {
event: {
...message,
Expand Down
9 changes: 8 additions & 1 deletion frontend/openchat-client/src/openchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3003,7 +3003,14 @@ export class OpenChat extends OpenChatAgentWorker {
for (const event of newEvents) {
if (event.event.kind === "message") {
const { messageIndex, messageId } = event.event;
failedMessagesStore.delete(context, messageId);
if (failedMessagesStore.delete(context, messageId)) {
this.sendRequest({
kind: "deleteFailedMessage",
chatId,
messageId,
threadRootMessageIndex,
});
}
if (unconfirmed.delete(context, messageId)) {
messagesRead.confirmMessage(context, messageIndex, messageId);
}
Expand Down
49 changes: 31 additions & 18 deletions frontend/openchat-client/src/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,17 @@ export function mergeUnconfirmedIntoSummary(
const senderBlocked = blockedUsers.has(latestMessage.event.sender);

// Don't hide the sender's own messages
const failedMessageFilter = latestMessage.event.sender !== currentUserId
? doesMessageFailFilter(latestMessage.event, messageFilters) !== undefined
: false;
const failedMessageFilter =
latestMessage.event.sender !== currentUserId
? doesMessageFailFilter(latestMessage.event, messageFilters) !== undefined
: false;

if (updates !== undefined || translation !== undefined || senderBlocked || failedMessageFilter) {
if (
updates !== undefined ||
translation !== undefined ||
senderBlocked ||
failedMessageFilter
) {
latestMessage = {
...latestMessage,
event: mergeLocalUpdates(
Expand All @@ -470,7 +476,7 @@ export function mergeUnconfirmedIntoSummary(
undefined,
senderBlocked,
false,
failedMessageFilter
failedMessageFilter,
),
};
}
Expand Down Expand Up @@ -1254,11 +1260,13 @@ export function mergeEventsAndLocalUpdates(
): EventWrapper<ChatEvent>[] {
const eventIndexes = new DRange();
eventIndexes.add(expiredEventRanges);
const confirmedMessageIds = new Set<bigint>();

function processEvent(e: EventWrapper<ChatEvent>): EventWrapper<ChatEvent> {
eventIndexes.add(e.index);

if (e.event.kind === "message") {
confirmedMessageIds.add(e.event.messageId);
const updates = localUpdates.get(e.event.messageId);
const translation = translations.get(e.event.messageId);

Expand Down Expand Up @@ -1288,18 +1296,19 @@ export function mergeEventsAndLocalUpdates(
blockedUsers.has(e.event.repliesTo.senderId);

// Don't hide the sender's own messages
const failedMessageFilter = e.event.sender !== currentUserId
? doesMessageFailFilter(e.event, messageFilters) !== undefined
: false;

const failedMessageFilter =
e.event.sender !== currentUserId
? doesMessageFailFilter(e.event, messageFilters) !== undefined
: false;

if (
updates !== undefined ||
replyContextUpdates !== undefined ||
tallyUpdate !== undefined ||
translation !== undefined ||
replyTranslation !== undefined ||
senderBlocked ||
repliesToSenderBlocked ||
repliesToSenderBlocked ||
failedMessageFilter
) {
return {
Expand All @@ -1312,8 +1321,8 @@ export function mergeEventsAndLocalUpdates(
translation,
replyTranslation,
senderBlocked,
repliesToSenderBlocked,
failedMessageFilter,
repliesToSenderBlocked,
failedMessageFilter,
),
};
}
Expand All @@ -1330,10 +1339,11 @@ export function mergeEventsAndLocalUpdates(
// Only include unconfirmed events that are either contiguous with the loaded confirmed events, or are the
// first events in a new chat
if (
(eventIndexes.length === 0 && message.index <= 1) ||
eventIndexes
.subranges()
.some((s) => s.low - 1 <= message.index && message.index <= s.high + 1)
!confirmedMessageIds.has(message.event.messageId) &&
((eventIndexes.length === 0 && message.index <= 1) ||
eventIndexes
.subranges()
.some((s) => s.low - 1 <= message.index && message.index <= s.high + 1))
) {
merged.push(processEvent(message));
anyAdded = true;
Expand All @@ -1347,7 +1357,10 @@ export function mergeEventsAndLocalUpdates(
return merged;
}

export function doesMessageFailFilter(message: Message, filters: MessageFilter[]): bigint | undefined {
export function doesMessageFailFilter(
message: Message,
filters: MessageFilter[],
): bigint | undefined {
const text = getContentAsText(message.content);

if (text !== undefined) {
Expand All @@ -1368,7 +1381,7 @@ function mergeLocalUpdates(
replyTranslation: string | undefined,
senderBlocked: boolean,
repliesToSenderBlocked: boolean,
failedMessageFilter: boolean
failedMessageFilter: boolean,
): Message {
if (localUpdates?.deleted !== undefined) {
return {
Expand Down

0 comments on commit da3ad83

Please sign in to comment.