Skip to content

Commit

Permalink
refactor: Improve incoming message typings (#16010)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc authored Oct 16, 2023
1 parent f781bf8 commit 5738385
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 247 deletions.
7 changes: 4 additions & 3 deletions src/script/backup/BackupRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,12 @@ export class BackupRepository {
}

private prepareEvents(entity: EventRecord) {
if (entity.data) {
const data = entity.data as any;
if (data) {
UINT8ARRAY_FIELDS.forEach(field => {
const dataField = entity.data[field];
const dataField = data[field];
if (dataField) {
entity.data[field] = new Uint8Array(Object.values(dataField));
data[field] = new Uint8Array(Object.values(dataField));
}
});
}
Expand Down
28 changes: 13 additions & 15 deletions src/script/conversation/ConversationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2112,9 +2112,10 @@ export class ConversationRepository {
}

const {conversation, qualified_conversation, data: eventData, type} = eventJson;
const dataConversationId: string = (eventData as any).conversationId;
// data.conversationId is always the conversationId that should be read first. If not found we can fallback to qualified_conversation or conversation
const conversationId: QualifiedId = eventData?.conversationId
? {domain: '', id: eventData.conversationId}
const conversationId: QualifiedId = dataConversationId
? {domain: '', id: dataConversationId}
: qualified_conversation || {domain: '', id: conversation};

const inSelfConversation = this.conversationState.isSelfConversation(conversationId);
Expand Down Expand Up @@ -2224,18 +2225,15 @@ export class ConversationRepository {
const isFromUnknownUser = allParticipants.every(participant => participant.id !== senderId);

if (isFromUnknownUser) {
const membersUpdateMessages = [
CONVERSATION_EVENT.MEMBER_LEAVE,
CONVERSATION_EVENT.MEMBER_JOIN,
ClientEvent.CONVERSATION.TEAM_MEMBER_LEAVE,
];
const isMembersUpdateEvent = membersUpdateMessages.includes(eventJson.type);
if (isMembersUpdateEvent) {
const isFromUpdatedMember = eventJson.data.user_ids?.includes(senderId);
if (isFromUpdatedMember) {
// we ignore leave/join events that are sent by the user actually leaving or joining
return conversationEntity;
}
switch (eventJson.type) {
case CONVERSATION_EVENT.MEMBER_LEAVE:
case CONVERSATION_EVENT.MEMBER_JOIN:
case ClientEvent.CONVERSATION.TEAM_MEMBER_LEAVE:
const isFromUpdatedMember = eventJson.data.user_ids?.includes(senderId);
if (isFromUpdatedMember) {
// we ignore leave/join events that are sent by the user actually leaving or joining
return conversationEntity;
}
}

const message = `Received '${type}' event from user '${senderId}' unknown in '${conversationEntity.id}'`;
Expand Down Expand Up @@ -2272,7 +2270,7 @@ export class ConversationRepository {
data: {legal_hold_status: messageLegalHoldStatus},
from: userId,
time: isoTimestamp,
} = eventJson;
} = eventJson as any;
const timestamp = new Date(isoTimestamp).getTime();
const qualifiedConversation = qualified_conversation || {domain: '', id: conversationId};
const qualifiedUser = qualified_from || {domain: '', id: userId};
Expand Down
4 changes: 2 additions & 2 deletions src/script/conversation/ConversationStateHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class ConversationStateHandler extends AbstractConversationEventHandler {

private _mapConversationAccessState(
conversationEntity: Conversation,
eventJson: ConversationEvent<ConversationAccessUpdateData>,
eventJson: ConversationEvent<CONVERSATION_EVENT.ACCESS_UPDATE, ConversationAccessUpdateData>,
): void {
const {access: accessModes, ...roles} = eventJson.data;
ConversationMapper.mapAccessState(conversationEntity, accessModes, roles?.access_role, roles?.access_role_v2);
Expand All @@ -140,7 +140,7 @@ export class ConversationStateHandler extends AbstractConversationEventHandler {

private _updateConversationAccessCode(
conversationEntity: Conversation,
eventJson: ConversationEvent<ConversationCode>,
eventJson: ConversationEvent<CONVERSATION_EVENT.CODE_UPDATE, ConversationCode>,
): void {
ConversationMapper.mapAccessCode(conversationEntity, eventJson.data);
}
Expand Down
Loading

0 comments on commit 5738385

Please sign in to comment.