Skip to content

Commit

Permalink
fixup! fixup! Integration federation request token module
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev committed Feb 12, 2025
1 parent b82f035 commit 5ad98e4
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,21 @@ class FederationContact with EquatableMixin {
this.emails,
});

Set<String> calculateHashUsingAllPeppers({
required String lookupPepper,
required Set<String> algorithms,
}) {
final Set<String> hashes = {};

if (algorithms.isEmpty) {
return hashes;
}

if (phoneNumbers != null) {
for (final phoneNumber in phoneNumbers!) {
final hash = phoneNumber.calculateHashWithAlgorithmSha256(
pepper: lookupPepper,
);

hashes.add(hash);
}
}

if (emails != null) {
for (final email in emails!) {
final hash = email.calculateHashWithAlgorithmSha256(
pepper: lookupPepper,
);

hashes.add(hash);
}
}

return hashes;
}

@override
List<Object?> get props => [
id,
phoneNumbers,
emails,
];

FederationContact copyWith({
Set<FederationPhone>? phoneNumbers,
Set<FederationEmail>? emails,
}) {
return FederationContact(
id: id,
phoneNumbers: phoneNumbers ?? this.phoneNumbers,
emails: emails ?? this.emails,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ abstract class FederationThirdPartyContact with EquatableMixin {

final String thirdPartyId;

final Map<String, String>? thirdPartyIdToHashMap;

FederationThirdPartyContact({
this.matrixId,
required this.thirdPartyIdType,
required this.thirdPartyId,
this.thirdPartyIdToHashMap,
});

@override
List<Object?> get props => [
matrixId,
thirdPartyIdType,
thirdPartyId,
thirdPartyIdToHashMap,
];

String calculateHashWithAlgorithmSha256({
Expand Down Expand Up @@ -80,6 +84,7 @@ class FederationPhone extends FederationThirdPartyContact {
FederationPhone({
required this.number,
super.matrixId,
super.thirdPartyIdToHashMap,
}) : super(
thirdPartyIdType: 'msisdn',
thirdPartyId: number.msisdnSanitizer(),
Expand All @@ -90,14 +95,18 @@ class FederationPhone extends FederationThirdPartyContact {
matrixId,
number,
thirdPartyIdType,
thirdPartyIdToHashMap,
];

FederationPhone copyWith({
String? matrixId,
Map<String, String>? thirdPartyIdToHashMap,
}) {
return FederationPhone(
matrixId: matrixId ?? this.matrixId,
number: number,
thirdPartyIdToHashMap:
thirdPartyIdToHashMap ?? this.thirdPartyIdToHashMap,
);
}
}
Expand All @@ -108,6 +117,7 @@ class FederationEmail extends FederationThirdPartyContact {
FederationEmail({
super.matrixId,
required this.address,
super.thirdPartyIdToHashMap,
}) : super(
thirdPartyIdType: 'email',
thirdPartyId: address,
Expand All @@ -118,14 +128,18 @@ class FederationEmail extends FederationThirdPartyContact {
address,
matrixId,
thirdPartyIdType,
thirdPartyIdToHashMap,
];

FederationEmail copyWith({
String? matrixId,
Map<String, String>? thirdPartyIdToHashMap,
}) {
return FederationEmail(
matrixId: matrixId ?? this.matrixId,
address: address,
thirdPartyIdToHashMap:
thirdPartyIdToHashMap ?? this.thirdPartyIdToHashMap,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/modules/federation_identity_lookup/domain/models/federation_arguments.dart';
import 'package:fluffychat/modules/federation_identity_lookup/domain/models/federation_contact.dart';
import 'package:fluffychat/modules/federation_identity_lookup/domain/models/federation_hash_details_response.dart';
import 'package:fluffychat/modules/federation_identity_lookup/domain/models/federation_lookup_mxid_request.dart';
import 'package:fluffychat/modules/federation_identity_lookup/domain/models/federation_third_party_contact.dart';
import 'package:fluffychat/modules/federation_identity_lookup/domain/repository/federation_identity_lookup_repository.dart';
import 'package:fluffychat/modules/federation_identity_lookup/domain/state/federation_identity_lookup_state.dart';

Expand Down Expand Up @@ -38,26 +38,66 @@ class FederationIdentityLookupInteractor {
token: registerResponse.token!,
);

if (hashDetails.lookupPepper == null ||
hashDetails.lookupPepper == null) {
if (hashDetails.lookupPepper == null || hashDetails.algorithms == null) {
yield const Left(FederationIdentityGetHashDetailsFailure());
}

final contactIdToHashMap = {
for (final contact in arguments.contactMaps.values) contact.id: contact,
};

final Map<String, FederationContact> newContacts = {};

final addresses = _calculateHashes(
hashDetails: hashDetails,
arguments: arguments,
);
final phoneToHashMap = <String, String>{};

final emailToHashMap = <String, String>{};

for (final contact in arguments.contactMaps.values) {
if (hashDetails.algorithms!.isEmpty) {
yield const Left(FederationIdentityGetHashDetailsFailure());
}

if (contact.phoneNumbers != null) {
for (final phone in contact.phoneNumbers!) {
final hash = phone.calculateHashWithAlgorithmSha256(
pepper: hashDetails.lookupPepper!,
);
phoneToHashMap.putIfAbsent(phone.number, () => hash);
}
}

if (addresses.isEmpty) {
if (contact.emails != null) {
for (final email in contact.emails!) {
final hash = email.calculateHashWithAlgorithmSha256(
pepper: hashDetails.lookupPepper!,
);

emailToHashMap.putIfAbsent(email.address, () => hash);
}
}

final updatedContact = updateContactWithHashes(
contact,
phoneToHashMap,
emailToHashMap,
);

contactIdToHashMap.putIfAbsent(contact.id, () => updatedContact);
}

final contactToHashMap = {
...phoneToHashMap,
...emailToHashMap,
};

if (contactToHashMap.values.isEmpty) {
yield const Left(FederationIdentityCalculationHashesEmpty());
}

final lookupMxidResponse =
await federationIdentityLookupRepository.lookupMxid(
request: FederationLookupMxidRequest(
addresses: addresses,
addresses: contactToHashMap.values.toSet(),
algorithm: hashDetails.algorithms?.firstOrNull,
pepper: hashDetails.lookupPepper,
),
Expand All @@ -70,6 +110,31 @@ class FederationIdentityLookupInteractor {
);
}

for (final mapping in lookupMxidResponse.mappings!.entries) {
final contact = contactIdToHashMap[mapping.key];
if (contact != null) {
final updatedPhoneNumbers = _updatePhoneNumbers(
contact,
{
mapping.key: mapping.value,
},
);
final updatedEmails = _updateEmails(
contact,
{
mapping.key: mapping.value,
},
);

final updatedContact = contact.copyWith(
phoneNumbers: updatedPhoneNumbers,
emails: updatedEmails,
);

newContacts.putIfAbsent(contact.id, () => updatedContact);
}
}

yield Right(
FederationIdentityLookupSuccess(
newContacts: newContacts,
Expand All @@ -80,18 +145,73 @@ class FederationIdentityLookupInteractor {
}
}

Set<String> _calculateHashes({
required FederationHashDetailsResponse hashDetails,
required FederationArguments arguments,
}) {
final addresses = <String>{};
for (final contact in arguments.contactMaps.values) {
final hashes = contact.calculateHashUsingAllPeppers(
lookupPepper: hashDetails.lookupPepper ?? '',
algorithms: hashDetails.algorithms ?? {},
);
addresses.addAll(hashes);
FederationContact updateContactWithHashes(
FederationContact contact,
Map<String, String> phoneToHashMap,
Map<String, String> emailToHashMap,
) {
final updatedPhoneNumbers = <FederationPhone>{};
final updatedEmails = <FederationEmail>{};

for (final phoneNumber in contact.phoneNumbers!) {
final hashes = phoneToHashMap[phoneNumber.number];
if (hashes != null) {
final updatedPhoneNumber = phoneNumber.copyWith(
thirdPartyIdToHashMap: phoneToHashMap,
);
updatedPhoneNumbers.add(updatedPhoneNumber);
}
}

for (final email in contact.emails!) {
final hashes = emailToHashMap[email.address];

if (hashes != null) {
final emailUpdated = email.copyWith(
thirdPartyIdToHashMap: emailToHashMap,
);
updatedEmails.add(emailUpdated);
}
}

return contact.copyWith(
phoneNumbers: updatedPhoneNumbers,
emails: updatedEmails,
);
}

Set<FederationPhone> _updatePhoneNumbers(
FederationContact contact,
Map<String, String> mappings,
) {
final updatedPhoneNumbers = <FederationPhone>{};
for (final phoneNumber in contact.phoneNumbers!) {
final thirdPartyIdToHashMap = phoneNumber.thirdPartyIdToHashMap ?? {};

if (thirdPartyIdToHashMap.values.contains(mappings.values.toString())) {
final updatePhoneNumber = phoneNumber.copyWith(
matrixId: mappings.keys.toString(),
);
updatedPhoneNumbers.add(updatePhoneNumber);
}
}
return updatedPhoneNumbers;
}

Set<FederationEmail> _updateEmails(
FederationContact contact,
Map<String, String> mappings,
) {
final updatedEmails = <FederationEmail>{};
for (final email in contact.emails!) {
final thirdPartyIdToHashMap = email.thirdPartyIdToHashMap ?? {};
if (thirdPartyIdToHashMap.values.contains(mappings.values.toString())) {
final updatedEmail = email.copyWith(
matrixId: mappings.keys.toString(),
);
updatedEmails.add(updatedEmail);
}
}
return addresses;
return updatedEmails;
}
}

0 comments on commit 5ad98e4

Please sign in to comment.