Skip to content

Commit

Permalink
Refactor to avoid mutating input arg
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Dec 13, 2024
1 parent a26e1b9 commit 8291c67
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 61 deletions.
21 changes: 10 additions & 11 deletions frontend/openchat-agent/src/services/community/community.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1179,25 +1179,24 @@ export class CommunityClient extends CandidService {
: dataClient.uploadData(event.event.content, [chatId.communityId]);

return uploadContentPromise.then((content) => {
if (content !== undefined) {
event.event.content = content;
}
const newEvent =
content !== undefined ? { ...event, event: { ...event.event, content } } : event;
const args = {
channel_id: BigInt(chatId.channelId),
content: apiMessageContent(event.event.content),
message_id: event.event.messageId,
content: apiMessageContent(newEvent.event.content),
message_id: newEvent.event.messageId,
sender_name: senderName,
sender_display_name: senderDisplayName,
community_rules_accepted: communityRulesAccepted,
channel_rules_accepted: channelRulesAccepted,
replies_to: mapOptional(event.event.repliesTo, (replyContext) => ({
replies_to: mapOptional(newEvent.event.repliesTo, (replyContext) => ({
event_index: replyContext.eventIndex,
})),
mentioned: mentioned.map(apiUserV2),
forwarding: event.event.forwarded,
forwarding: newEvent.event.forwarded,
thread_root_message_index: threadRootMessageIndex,
message_filter_failed: messageFilterFailed,
block_level_markdown: event.event.blockLevelMarkdown,
block_level_markdown: newEvent.event.blockLevelMarkdown,
new_achievement: newAchievement,
};
return this.executeMsgpackUpdate(
Expand All @@ -1209,17 +1208,17 @@ export class CommunityClient extends CandidService {
onRequestAccepted,
)
.then((resp) => {
const retVal: [SendMessageResponse, Message] = [resp, event.event];
const retVal: [SendMessageResponse, Message] = [resp, newEvent.event];
setCachedMessageFromSendResponse(
this.db,
chatId,
event,
newEvent,
threadRootMessageIndex,
)(retVal);
return retVal;
})
.catch((err) => {
recordFailedMessage(this.db, chatId, event, threadRootMessageIndex);
recordFailedMessage(this.db, chatId, newEvent, threadRootMessageIndex);
throw err;
});
});
Expand Down
21 changes: 10 additions & 11 deletions frontend/openchat-agent/src/services/group/group.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,24 +596,23 @@ export class GroupClient extends CandidService {
: dataClient.uploadData(event.event.content, [this.chatId.groupId]);

return uploadContentPromise.then((content) => {
if (content !== undefined) {
event.event.content = content;
}
const newEvent =
content !== undefined ? { ...event, event: { ...event.event, content } } : event;
const args = {
content: apiMessageContent(event.event.content),
message_id: event.event.messageId,
content: apiMessageContent(newEvent.event.content),
message_id: newEvent.event.messageId,
sender_name: senderName,
sender_display_name: senderDisplayName,
rules_accepted: rulesAccepted,
replies_to: mapOptional(event.event.repliesTo, (replyContext) => ({
replies_to: mapOptional(newEvent.event.repliesTo, (replyContext) => ({
event_index: replyContext.eventIndex,
})),
mentioned: mentioned.map(apiUserV2),
forwarding: event.event.forwarded,
forwarding: newEvent.event.forwarded,
thread_root_message_index: threadRootMessageIndex,
message_filter_failed: messageFilterFailed,
correlation_id: generateUint64(),
block_level_markdown: event.event.blockLevelMarkdown,
block_level_markdown: newEvent.event.blockLevelMarkdown,
new_achievement: newAchievement,
};

Expand All @@ -626,17 +625,17 @@ export class GroupClient extends CandidService {
onRequestAccepted,
)
.then((resp) => {
const retVal: [SendMessageResponse, Message] = [resp, event.event];
const retVal: [SendMessageResponse, Message] = [resp, newEvent.event];
setCachedMessageFromSendResponse(
this.db,
this.chatId,
event,
newEvent,
threadRootMessageIndex,
)(retVal);
return retVal;
})
.catch((err) => {
recordFailedMessage(this.db, this.chatId, event, threadRootMessageIndex);
recordFailedMessage(this.db, this.chatId, newEvent, threadRootMessageIndex);
throw err;
});
});
Expand Down
62 changes: 23 additions & 39 deletions frontend/openchat-agent/src/services/user/user.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,67 +731,51 @@ export class UserClient extends CandidService {
onRequestAccepted: () => void,
): Promise<[SendMessageResponse, Message]> {
removeFailedMessage(this.db, this.chatId, event.event.messageId, threadRootMessageIndex);
return this.sendMessageToBackend(
chatId,
event,
messageFilterFailed,
threadRootMessageIndex,
pin,
onRequestAccepted,
)
.then(
setCachedMessageFromSendResponse(
this.db,
this.chatId,
event,
threadRootMessageIndex,
),
)
.catch((err) => {
recordFailedMessage(this.db, this.chatId, event, threadRootMessageIndex);
throw err;
});
}

sendMessageToBackend(
chatId: DirectChatIdentifier,
event: EventWrapper<Message>,
messageFilterFailed: bigint | undefined,
threadRootMessageIndex: number | undefined,
pin: string | undefined,
onRequestAccepted: () => void,
): Promise<[SendMessageResponse, Message]> {
const dataClient = new DataClient(this.identity, this.agent, this.config);
const uploadContentPromise = event.event.forwarded
? dataClient.forwardData(event.event.content, [this.userId, chatId.userId])
: dataClient.uploadData(event.event.content, [this.userId, chatId.userId]);

return uploadContentPromise.then((content) => {
if (content !== undefined) {
event.event.content = content;
}
const newEvent =
content !== undefined ? { ...event, event: { ...event.event, content } } : event;
const req = {
content: apiMessageContent(event.event.content),
content: apiMessageContent(newEvent.event.content),
recipient: principalStringToBytes(chatId.userId),
message_id: event.event.messageId,
replies_to: mapOptional(event.event.repliesTo, (replyContext) =>
message_id: newEvent.event.messageId,
replies_to: mapOptional(newEvent.event.repliesTo, (replyContext) =>
apiReplyContextArgs(chatId, replyContext),
),
forwarding: event.event.forwarded,
forwarding: newEvent.event.forwarded,
thread_root_message_index: threadRootMessageIndex,
message_filter_failed: messageFilterFailed,
pin,
correlation_id: generateUint64(),
block_level_markdown: event.event.blockLevelMarkdown,
block_level_markdown: newEvent.event.blockLevelMarkdown,
};
return this.executeMsgpackUpdate(
"send_message_v2",
req,
(resp) => sendMessageResponse(resp, event.event.sender, chatId.userId),
(resp) => sendMessageResponse(resp, newEvent.event.sender, chatId.userId),
UserSendMessageArgs,
UserSendMessageResponse,
onRequestAccepted,
).then((resp) => [resp, event.event]);
)
.then((resp) => {
const retVal: [SendMessageResponse, Message] = [resp, newEvent.event];
setCachedMessageFromSendResponse(
this.db,
chatId,
newEvent,
threadRootMessageIndex,
)(retVal);
return retVal;
})
.catch((err) => {
recordFailedMessage(this.db, chatId, newEvent, threadRootMessageIndex);
throw err;
});
});
}

Expand Down

0 comments on commit 8291c67

Please sign in to comment.