-
Notifications
You must be signed in to change notification settings - Fork 292
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 event builder #17756
base: dev
Are you sure you want to change the base?
feat: Improve event builder #17756
Changes from all commits
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 |
---|---|---|
|
@@ -110,6 +110,7 @@ import { | |
OnConversationVerificationStateChange, | ||
} from './ConversationVerificationStateHandler/shared'; | ||
import {EventMapper} from './EventMapper'; | ||
import {createBaseEvent} from './EventNew'; | ||
import {MessageRepository} from './MessageRepository'; | ||
import {NOTIFICATION_STATE} from './NotificationSetting'; | ||
|
||
|
@@ -122,6 +123,7 @@ import {ConnectionEntity} from '../connection/ConnectionEntity'; | |
import {ConnectionRepository} from '../connection/ConnectionRepository'; | ||
import {ConnectionState} from '../connection/ConnectionState'; | ||
import { | ||
AllVerifiedEvent, | ||
AssetAddEvent, | ||
ButtonActionConfirmationEvent, | ||
ClientConversationEvent, | ||
|
@@ -151,6 +153,7 @@ import {isMemberMessage} from '../guards/Message'; | |
import * as LegalHoldEvaluator from '../legal-hold/LegalHoldEvaluator'; | ||
import {MessageCategory} from '../message/MessageCategory'; | ||
import {SystemMessageType} from '../message/SystemMessageType'; | ||
import {VerificationMessageType} from '../message/VerificationMessageType'; | ||
import {addOtherSelfClientsToMLSConversation} from '../mls'; | ||
import {PropertiesRepository} from '../properties/PropertiesRepository'; | ||
import {SelfRepository} from '../self/SelfRepository'; | ||
|
@@ -926,11 +929,51 @@ export class ConversationRepository { | |
conversationEntity.withAllTeamMembers(allTeamMembersParticipate); | ||
} | ||
|
||
const creationEvent = conversationEntity.isGroup() | ||
? EventBuilder.buildGroupCreation(conversationEntity, isTemporaryGuest, timestamp) | ||
: EventBuilder.build1to1Creation(conversationEntity); | ||
let creationEventNew; | ||
|
||
await this.eventRepository.injectEvent(creationEvent, eventSource); | ||
if (conversationEntity.isGroup()) { | ||
const selfUser = conversationEntity.selfUser(); | ||
|
||
if (!selfUser) { | ||
this.logger.error('Failed to get self user'); | ||
return; | ||
} | ||
|
||
const {creator: creatorId} = conversationEntity; | ||
const selfUserId = selfUser.id; | ||
|
||
const userIds = conversationEntity.participating_user_ids().slice(); | ||
const createdBySelf = creatorId === selfUserId || isTemporaryGuest; | ||
if (!createdBySelf) { | ||
userIds.push(selfUser.qualifiedId); | ||
} | ||
|
||
creationEventNew = createBaseEvent<GroupCreationEvent>({ | ||
conversation: conversationEntity, | ||
eventType: ClientEvent.CONVERSATION.ONE2ONE_CREATION, | ||
additionalData: { | ||
userIds, | ||
name: conversationEntity.name(), | ||
allTeamMembers: conversationEntity.withAllTeamMembers(), | ||
}, | ||
from: isTemporaryGuest ? selfUserId : creatorId, | ||
timestamp: 0, | ||
}); | ||
} else { | ||
creationEventNew = createBaseEvent<OneToOneCreationEvent>({ | ||
conversation: conversationEntity, | ||
eventType: ClientEvent.CONVERSATION.ONE2ONE_CREATION, | ||
additionalData: {userIds: conversationEntity.participating_user_ids()}, | ||
from: conversationEntity.creator, | ||
timestamp: 0, | ||
}); | ||
} | ||
|
||
// const creationEvent = conversationEntity.isGroup() | ||
// ? EventBuilder.buildGroupCreation(conversationEntity, isTemporaryGuest, timestamp) | ||
// : EventBuilder.build1to1Creation(conversationEntity); | ||
|
||
await this.eventRepository.injectEvent(creationEventNew, eventSource); | ||
} | ||
|
||
/** | ||
|
@@ -2311,7 +2354,12 @@ export class ConversationRepository { | |
}) => { | ||
switch (conversationVerificationState) { | ||
case ConversationVerificationState.VERIFIED: | ||
const allVerifiedEvent = EventBuilder.buildAllVerified(conversationEntity); | ||
// const allVerifiedEvent = EventBuilder.buildAllVerified(conversationEntity); | ||
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. also unneeded? |
||
const allVerifiedEvent = createBaseEvent<AllVerifiedEvent>({ | ||
conversation: conversationEntity, | ||
eventType: ClientEvent.CONVERSATION.VERIFICATION, | ||
additionalData: {type: VerificationMessageType.VERIFIED}, | ||
}); | ||
await this.eventRepository.injectEvent(allVerifiedEvent); | ||
break; | ||
case ConversationVerificationState.DEGRADED: | ||
|
@@ -2647,9 +2695,19 @@ export class ConversationRepository { | |
// TODO: Can this even have a response? in the API Client it look like it always returns `void` | ||
const hasResponse = response?.event; | ||
const currentTimestamp = this.serverTimeHandler.toServerTimestamp(); | ||
// const event = hasResponse | ||
// ? response.event | ||
// : EventBuilder.buildMemberLeave(conversationEntity, [user], this.userState.self().id, currentTimestamp); | ||
|
||
const event = hasResponse | ||
? response.event | ||
: EventBuilder.buildMemberLeave(conversationEntity, [user], this.userState.self().id, currentTimestamp); | ||
: createBaseEvent<MemberLeaveEvent>({ | ||
conversation: conversationEntity, | ||
eventType: CONVERSATION_EVENT.MEMBER_LEAVE, | ||
additionalData: {qualified_user_ids: [user], user_ids: [user].map(({id}) => id)}, | ||
from: this.userState.self().id, | ||
timestamp: currentTimestamp, | ||
}); | ||
|
||
this.eventRepository.injectEvent(event, EventRepository.SOURCE.BACKEND_RESPONSE); | ||
return event; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,7 +135,7 @@ | |
expect(conversationB.verification_state()).toBe(ConversationVerificationState.VERIFIED); | ||
expect(conversationAB.is_verified()).toBeDefined(); | ||
expect(conversationAB.is_verified()).toBeTruthy(); | ||
expect(EventBuilder.buildAllVerified).not.toHaveBeenCalled(); | ||
// expect(EventBuilder.buildAllVerified).not.toHaveBeenCalled(); | ||
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. guess this should be replaced with createBaseEvent |
||
expect(testFactory.event_repository.injectEvent).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
@@ -169,8 +169,8 @@ | |
expect(conversationAB.verification_state()).toBe(ConversationVerificationState.VERIFIED); | ||
expect(conversationB.verification_state()).toBe(ConversationVerificationState.VERIFIED); | ||
expect(conversationC.verification_state()).toBe(ConversationVerificationState.VERIFIED); | ||
expect(EventBuilder.buildAllVerified).toHaveBeenCalledTimes(3); | ||
// expect(EventBuilder.buildAllVerified).toHaveBeenCalledTimes(3); | ||
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. same |
||
expect(testFactory.event_repository.injectEvent).toHaveBeenCalledWith(verifiedEvent); | ||
Check failure on line 173 in src/script/conversation/ConversationVerificationStateHandler/Proteus/ProteusStateHandler.test.ts GitHub Actions / testProteusConversationVerificationStateHandler › onClientRemoved › should change state from DEGRADED to VERIFIED if last unverified client was removed
|
||
}); | ||
}); | ||
|
||
|
@@ -199,8 +199,8 @@ | |
expect(conversationAB.verification_state()).toBe(ConversationVerificationState.VERIFIED); | ||
expect(conversationB.verification_state()).toBe(ConversationVerificationState.VERIFIED); | ||
expect(conversationC.verification_state()).toBe(ConversationVerificationState.VERIFIED); | ||
expect(EventBuilder.buildAllVerified).toHaveBeenCalledTimes(3); | ||
// expect(EventBuilder.buildAllVerified).toHaveBeenCalledTimes(3); | ||
expect(testFactory.event_repository.injectEvent).toHaveBeenCalledWith(verifiedEvent); | ||
Check failure on line 203 in src/script/conversation/ConversationVerificationStateHandler/Proteus/ProteusStateHandler.test.ts GitHub Actions / testProteusConversationVerificationStateHandler › onClientsUpdated › should change state from DEGRADED to VERIFIED if last unverified client was removed by other user
|
||
}); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import type {QualifiedId} from '@wireapp/api-client/lib/user'; | ||
|
||
import {createUuid} from 'Util/uuid'; | ||
|
||
import type {Conversation} from '../entity/Conversation'; | ||
|
||
function buildQualifiedId(conversation: QualifiedId | string) { | ||
const qualifiedId = typeof conversation === 'string' ? {domain: '', id: conversation} : conversation; | ||
return { | ||
conversation: qualifiedId.id, | ||
qualified_conversation: {domain: qualifiedId.domain, id: qualifiedId.id}, | ||
}; | ||
} | ||
|
||
type EventInput = { | ||
conversation: Conversation; | ||
eventType: string; | ||
additionalData?: {}; | ||
from?: string; | ||
timestamp?: number; | ||
}; | ||
|
||
export function createBaseEvent<T extends {type: string; data?: unknown}>({ | ||
conversation, | ||
eventType, | ||
additionalData = {}, | ||
from, | ||
timestamp, | ||
}: EventInput): T { | ||
return { | ||
...buildQualifiedId(conversation), | ||
data: additionalData, | ||
from: from ?? conversation.selfUser()?.id, | ||
id: createUuid(), | ||
time: new Date(timestamp || conversation.getNextTimestamp()).toISOString(), | ||
type: eventType, | ||
} as unknown as T; | ||
} |
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.
🤔 what to do with this?