Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to avoid mutating input arg #7063

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading