-
Notifications
You must be signed in to change notification settings - Fork 289
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
feat: improve quote event loading [SQSERVICES-1834] #14632
Changes from all commits
cb39275
57632f0
ead0453
540cd05
d46d6b4
26de135
4ee3397
a47f44b
d4130bb
b41c83a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -921,7 +921,7 @@ export class MessageRepository { | |
if (!message.user().isMe && !message.ephemeral_expires()) { | ||
throw new ConversationError(ConversationError.TYPE.WRONG_USER, ConversationError.MESSAGE.WRONG_USER); | ||
} | ||
const userIds = options.targetedUsers || conversation.allUserEntities.map(user => user.qualifiedId); | ||
const userIds = options.targetedUsers || conversation.allUserEntities.map(user => user!.qualifiedId); | ||
const payload = MessageBuilder.buildDeleteMessage({ | ||
messageId: message.id, | ||
}); | ||
|
@@ -1173,17 +1173,10 @@ export class MessageRepository { | |
* | ||
* @param conversation Conversation message belongs to | ||
* @param messageId ID of message | ||
* @param skipConversationMessages Don't use message entity from conversation | ||
* @param ensureUser Make sure message entity has a valid user | ||
* @returns Resolves with the message | ||
*/ | ||
async getMessageInConversationById( | ||
conversation: Conversation, | ||
messageId: string, | ||
skipConversationMessages = false, | ||
ensureUser = false, | ||
): Promise<StoredContentMessage> { | ||
const messageEntity = !skipConversationMessages && conversation.getMessage(messageId); | ||
async getMessageInConversationById(conversation: Conversation, messageId: string): Promise<StoredContentMessage> { | ||
const messageEntity = conversation.getMessage(messageId); | ||
const message = | ||
messageEntity || | ||
(await this.eventService.loadEvent(conversation.id, messageId).then(event => { | ||
|
@@ -1197,7 +1190,33 @@ export class MessageRepository { | |
); | ||
} | ||
|
||
if (ensureUser && message.from && !message.user().id) { | ||
return message as StoredContentMessage; | ||
} | ||
|
||
/** | ||
* Get Message with given ID or a replacementId from the database. | ||
* | ||
* @param conversation Conversation message belongs to | ||
* @param messageId ID of message | ||
* @returns Resolves with the message | ||
*/ | ||
async getMessageInConversationByReplacementId( | ||
conversation: Conversation, | ||
messageId: string, | ||
): Promise<StoredContentMessage> { | ||
const message = conversation.getMessageByReplacementId(messageId); | ||
if (!message) { | ||
throw new ConversationError( | ||
ConversationError.TYPE.MESSAGE_NOT_FOUND, | ||
ConversationError.MESSAGE.MESSAGE_NOT_FOUND, | ||
); | ||
} | ||
|
||
return message as StoredContentMessage; | ||
} | ||
|
||
async ensureMessageSender(message: Message) { | ||
if (message.from && !message.user().id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's extract this logic to a different method and have a single responsibility for the const event = getMessageInConverstation(..) || getMessageInConversationByReplacementId()
const event = loadUsers(event); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i did something but i think based on your comment you would like something different |
||
const user = await this.userRepository.getUserById({domain: message.user().domain, id: message.from}); | ||
message.user(user); | ||
return message as StoredContentMessage; | ||
|
@@ -1210,7 +1229,7 @@ export class MessageRepository { | |
// Since this is a bare API client user we use `.deleted` | ||
const isDeleted = user.deleted === true; | ||
if (isDeleted) { | ||
await this.conversationRepositoryProvider().teamMemberLeave(this.teamState.team().id, { | ||
await this.conversationRepositoryProvider().teamMemberLeave(this.teamState.team().id ?? '', { | ||
domain: this.userState.self().domain, | ||
id: user.id, | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,18 +127,52 @@ export class EventService { | |
|
||
try { | ||
if (this.storageService.db) { | ||
const entry = await this.storageService.db | ||
return await this.storageService.db | ||
.table(StorageSchemata.OBJECT_STORE.EVENTS) | ||
.where('conversation') | ||
.equals(conversationId) | ||
.filter(item => item.data?.replacing_message_id === eventId || item.id === eventId) | ||
.first(); | ||
} | ||
|
||
const records = (await this.storageService.getAll(StorageSchemata.OBJECT_STORE.EVENTS)) as EventRecord[]; | ||
return records | ||
.filter(record => record.id === eventId && record.conversation === conversationId) | ||
.sort(compareEventsById) | ||
.shift(); | ||
} catch (error) { | ||
const logMessage = `Failed to get event '${eventId}' for conversation '${conversationId}': ${error.message}`; | ||
this.logger.error(logMessage, error); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Load a replacing event from database. | ||
* To be used in certain cases (during some cases with quoting) when the original message cannot be found. | ||
* | ||
* @param conversationId ID of conversation | ||
* @param eventId ID of event to retrieve | ||
*/ | ||
async loadReplacingEvent(conversationId: string, eventId: string): Promise<EventRecord> { | ||
if (!conversationId || !eventId) { | ||
this.logger.error(`Cannot get event '${eventId}' in conversation '${conversationId}' without IDs`); | ||
throw new ConversationError(BASE_ERROR_TYPE.MISSING_PARAMETER, BaseError.MESSAGE.MISSING_PARAMETER); | ||
} | ||
|
||
try { | ||
if (this.storageService.db) { | ||
return await this.storageService.db | ||
.table(StorageSchemata.OBJECT_STORE.EVENTS) | ||
.where('id') | ||
.equals(eventId) | ||
.filter(record => record.conversation === conversationId) | ||
.first(); | ||
return entry; | ||
} | ||
|
||
const records = (await this.storageService.getAll(StorageSchemata.OBJECT_STORE.EVENTS)) as EventRecord[]; | ||
return records | ||
.filter(record => record.id === eventId && record.conversation === conversationId) | ||
.filter(record => record.conversation === conversationId && record.data?.replacing_message_id === eventId) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's only lookup by replacing id at this point. The consumer is responsible for falling back to something else if it's not found There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the other function on L140, we are also checking by conversation && then id. (This was also the previous implementation) Should we change that as well or leave both the same? I assume there was some logic to checking both, although the chances of an overlap here seem relatively astronomical to me. Still sort of unclear when this section of the function would be called anyway. |
||
.sort(compareEventsById) | ||
.shift(); | ||
} catch (error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the default value of this method is not to look up the local state. This might be a regression here (since now it always lookup to local state)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the default value is false and this is negated. so the default case is always to look up.