Skip to content

Commit

Permalink
TW-1690: Add public room interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hieutbui authored and hoangdat committed May 14, 2024
1 parent 1643de7 commit 1789021
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 16 deletions.
15 changes: 0 additions & 15 deletions lib/data/model/search/public_room_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,18 @@ class PublicRoomRequest with EquatableMixin {
@JsonKey(name: 'filter')
final PublicRoomQueryFilter? filter;

@JsonKey(name: 'include_all_networks')
final bool? includeAllNetworks;

@JsonKey(name: 'limit')
final int? limit;

@JsonKey(name: 'since')
final String? since;

@JsonKey(name: 'third_party_instance_id')
final String? thirdPartyInstanceId;

PublicRoomRequest({
this.filter,
this.includeAllNetworks = false,
this.limit,
this.since,
this.thirdPartyInstanceId,
});

@override
List<Object?> get props => [
filter,
includeAllNetworks,
limit,
since,
thirdPartyInstanceId,
];

factory PublicRoomRequest.fromJson(Map<String, dynamic> json) =>
Expand Down
2 changes: 1 addition & 1 deletion lib/data/model/search/public_room_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ part 'public_room_response.g.dart';
@JsonSerializable()
class PublicRoomResponse with EquatableMixin {
@JsonKey(name: 'chunk')
final PublicRoomsChunk chunk;
final List<PublicRoomsChunk> chunk;

@JsonKey(name: 'next_batch')
final String? nextBatch;
Expand Down
21 changes: 21 additions & 0 deletions lib/di/global/get_it_initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:fluffychat/data/datasource/lookup_datasource.dart';
import 'package:fluffychat/data/datasource/media/media_data_source.dart';
import 'package:fluffychat/data/datasource/multiple_account/multiple_account_datasource.dart';
import 'package:fluffychat/data/datasource/phonebook_datasouce.dart';
import 'package:fluffychat/data/datasource/pubic_room_datasource.dart';
import 'package:fluffychat/data/datasource/recovery_words_data_source.dart';
import 'package:fluffychat/data/datasource/server_config_datasource.dart';
import 'package:fluffychat/data/datasource/server_search_datasource.dart';
Expand All @@ -17,6 +18,7 @@ import 'package:fluffychat/data/datasource_impl/contact/tom_contacts_datasource_
import 'package:fluffychat/data/datasource_impl/localizations/localizations_datasource_impl.dart';
import 'package:fluffychat/data/datasource_impl/media/media_data_source_impl.dart';
import 'package:fluffychat/data/datasource_impl/multiple_account/multiple_account_datasource_impl.dart';
import 'package:fluffychat/data/datasource_impl/pubilc_room_datasource_impl.dart';
import 'package:fluffychat/data/datasource_impl/recovery_words_data_source_impl.dart';
import 'package:fluffychat/data/datasource_impl/server_config_datasource_impl.dart';
import 'package:fluffychat/data/datasource_impl/server_search_datasource_impl.dart';
Expand All @@ -28,6 +30,7 @@ import 'package:fluffychat/data/network/contact/tom_contact_api.dart';
import 'package:fluffychat/data/network/dio_cache_option.dart';
import 'package:fluffychat/data/network/media/media_api.dart';
import 'package:fluffychat/data/network/recovery_words/recovery_words_api.dart';
import 'package:fluffychat/data/network/search/public_room_api.dart';
import 'package:fluffychat/data/network/search/server_search_api.dart';
import 'package:fluffychat/data/network/server_config_api.dart';
import 'package:fluffychat/data/repository/contact/lookup_repository_impl.dart';
Expand All @@ -36,6 +39,7 @@ import 'package:fluffychat/data/repository/contact/tom_contact_repository_impl.d
import 'package:fluffychat/data/repository/localizations/localizations_repository_impl.dart';
import 'package:fluffychat/data/repository/media/media_repository_impl.dart';
import 'package:fluffychat/data/repository/multiple_account/multiple_account_repository_impl.dart';
import 'package:fluffychat/data/repository/public_room_repository_impl.dart';
import 'package:fluffychat/data/repository/recovery_words_repository_impl.dart';
import 'package:fluffychat/data/repository/server_config_repository_impl.dart';
import 'package:fluffychat/data/repository/server_search_repository_impl.dart';
Expand All @@ -49,6 +53,7 @@ import 'package:fluffychat/domain/repository/localizations/localizations_reposit
import 'package:fluffychat/domain/repository/lookup_repository.dart';
import 'package:fluffychat/domain/repository/multiple_account/multiple_account_repository.dart';
import 'package:fluffychat/domain/repository/phonebook_contact_repository.dart';
import 'package:fluffychat/domain/repository/public_room_reposity.dart';
import 'package:fluffychat/domain/repository/recovery_words_repository.dart';
import 'package:fluffychat/domain/repository/server_config_repository.dart';
import 'package:fluffychat/domain/repository/server_search_repository.dart';
Expand All @@ -75,6 +80,7 @@ import 'package:fluffychat/domain/usecase/room/update_pinned_messages_interactor
import 'package:fluffychat/domain/usecase/room/upload_content_interactor.dart';
import 'package:fluffychat/domain/usecase/room/upload_content_for_web_interactor.dart';
import 'package:fluffychat/domain/usecase/search/pre_search_recent_contacts_interactor.dart';
import 'package:fluffychat/domain/usecase/search/public_room_interactor.dart';
import 'package:fluffychat/domain/usecase/search/search_recent_chat_interactor.dart';
import 'package:fluffychat/domain/usecase/search/server_search_interactor.dart';
import 'package:fluffychat/domain/usecase/send_file_interactor.dart';
Expand Down Expand Up @@ -155,6 +161,7 @@ class GetItInitializer {
getIt.registerSingleton<MediaAPI>(MediaAPI());
getIt.registerSingleton<ServerSearchAPI>(ServerSearchAPI());
getIt.registerSingleton<ServerConfigAPI>(ServerConfigAPI());
getIt.registerSingleton<PublicRoomAPI>(PublicRoomAPI());
}

void bindingManager() {
Expand Down Expand Up @@ -187,6 +194,9 @@ class GetItInitializer {
getIt.registerFactory<MultipleAccountDatasource>(
() => MultipleAccountDatasourceImpl(),
);
getIt.registerFactory<PublicRoomDatasource>(
() => PublicRoomDatasourceImpl(),
);
}

void bindingDatasourceImpl() {
Expand Down Expand Up @@ -216,6 +226,10 @@ class GetItInitializer {
getIt.registerFactory<ServerSearchDatasourceImpl>(
() => ServerSearchDatasourceImpl(),
);

getIt.registerFactory<PublicRoomDatasourceImpl>(
() => PublicRoomDatasourceImpl(),
);
}

void bindingRepositories() {
Expand Down Expand Up @@ -249,6 +263,9 @@ class GetItInitializer {
getIt.registerFactory<ServerConfigRepository>(
() => ServerConfigRepositoryImpl(),
);
getIt.registerFactory<PublicRoomRepository>(
() => PublicRoomRepositoryImpl(),
);
}

void bindingInteractor() {
Expand Down Expand Up @@ -350,6 +367,10 @@ class GetItInitializer {
getIt.registerSingleton<DownloadMediaFileInteractor>(
DownloadMediaFileInteractor(),
);

getIt.registerFactory<PublicRoomInteractor>(
() => PublicRoomInteractor(),
);
}

void _bindingControllers() {
Expand Down
21 changes: 21 additions & 0 deletions lib/domain/app_state/search/public_room_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:matrix/matrix.dart';

class PublicRoomSuccess extends Success {
final List<PublicRoomsChunk>? publicRoomsChunk;

const PublicRoomSuccess({this.publicRoomsChunk});

@override
List<Object?> get props => [publicRoomsChunk];
}

class PublicRoomFailed extends Failure {
final dynamic exception;

const PublicRoomFailed({required this.exception});

@override
List<Object?> get props => [exception];
}
33 changes: 33 additions & 0 deletions lib/domain/usecase/search/public_room_interactor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/search/public_room_state.dart';
import 'package:fluffychat/domain/repository/public_room_reposity.dart';
import 'package:matrix/matrix.dart';

class PublicRoomInteractor {
final PublicRoomRepository _publicRoomRepository =
getIt.get<PublicRoomRepository>();

Stream<Either<Failure, Success>> execute({
PublicRoomQueryFilter? filter,
String? server,
int? limit,
}) async* {
try {
final response = await _publicRoomRepository.search(
filter: filter,
server: server,
limit: limit,
);

yield Right(PublicRoomSuccess(publicRoomsChunk: response.chunk));
} catch (e) {
Logs().e(
'PublicRoomInteractor::execute(): Exception - $e}.',
);
yield Left(PublicRoomFailed(exception: e));
}
}
}

0 comments on commit 1789021

Please sign in to comment.