Skip to content
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

[PART-1] TW-1690: Implement search external channel #1736

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/data/datasource/pubic_room_datasource.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:fluffychat/data/model/search/public_room_response.dart';
import 'package:matrix/matrix.dart';

abstract class PublicRoomDatasource {
Future<PublicRoomResponse> search({
PublicRoomQueryFilter? filter,
String? server,
int? limit,
});
}
22 changes: 22 additions & 0 deletions lib/data/datasource_impl/pubilc_room_datasource_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:fluffychat/data/datasource/pubic_room_datasource.dart';
import 'package:fluffychat/data/model/search/public_room_response.dart';
import 'package:fluffychat/data/network/search/public_room_api.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:matrix/matrix.dart';

class PublicRoomDatasourceImpl extends PublicRoomDatasource {
final PublicRoomAPI _publicRoomAPI = getIt.get<PublicRoomAPI>();

@override
Future<PublicRoomResponse> search({
PublicRoomQueryFilter? filter,
String? server,
int? limit,
}) {
return _publicRoomAPI.search(
filter: filter,
server: server,
limit: limit,
);
}
}
30 changes: 30 additions & 0 deletions lib/data/model/search/public_room_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:matrix/matrix.dart';

part 'public_room_request.g.dart';

@JsonSerializable()
class PublicRoomRequest with EquatableMixin {
@JsonKey(name: 'filter')
final PublicRoomQueryFilter? filter;

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

PublicRoomRequest({
this.filter,
this.limit,
});

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

factory PublicRoomRequest.fromJson(Map<String, dynamic> json) =>
_$PublicRoomRequestFromJson(json);

Map<String, dynamic> toJson() => _$PublicRoomRequestToJson(this);
}
40 changes: 40 additions & 0 deletions lib/data/model/search/public_room_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:matrix/matrix.dart';

part 'public_room_response.g.dart';

@JsonSerializable()
class PublicRoomResponse with EquatableMixin {
@JsonKey(name: 'chunk')
final List<PublicRoomsChunk> chunk;

@JsonKey(name: 'next_batch')
final String? nextBatch;

@JsonKey(name: 'prev_batch')
final String? prevBatch;

@JsonKey(name: 'total_room_count_estimate')
final int? totalRoomCountEstimate;

PublicRoomResponse({
required this.chunk,
this.nextBatch,
this.prevBatch,
this.totalRoomCountEstimate,
});

@override
List<Object?> get props => [
chunk,
nextBatch,
prevBatch,
totalRoomCountEstimate,
];

factory PublicRoomResponse.fromJson(Map<String, dynamic> json) =>
_$PublicRoomResponseFromJson(json);

Map<String, dynamic> toJson() => _$PublicRoomResponseToJson(this);
}
4 changes: 4 additions & 0 deletions lib/data/network/homeserver_endpoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class HomeserverEndpoint {
static final ServicePath configPath = ServicePath(
'/config',
);

static final ServicePath publicRoomsPath = ServicePath(
'/publicRooms',
);
}

extension ServicePathHomeserver on ServicePath {
Expand Down
38 changes: 38 additions & 0 deletions lib/data/network/search/public_room_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:fluffychat/data/model/search/public_room_request.dart';
import 'package:fluffychat/data/model/search/public_room_response.dart';
import 'package:fluffychat/data/network/dio_client.dart';
import 'package:fluffychat/data/network/homeserver_endpoint.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/di/global/network_di.dart';
import 'package:matrix/matrix.dart';

class PublicRoomAPI {
final DioClient _client =
getIt.get<DioClient>(instanceName: NetworkDI.homeDioClientName);

PublicRoomAPI();

Future<PublicRoomResponse> search({
PublicRoomQueryFilter? filter,
String? server,
int? limit,
}) async {
final Map<String, dynamic> queryParameters = {};
if (server?.isNotEmpty == true) {
queryParameters['server'] = server;
}
final response = await _client
.post(
HomeserverEndpoint.publicRoomsPath
.generateHomeserverServerSearchPath(),
queryParameters: queryParameters,
data: PublicRoomRequest(
filter: filter,
limit: limit,
),
)
.onError((error, stackTrace) => throw Exception(error));

return PublicRoomResponse.fromJson(response.data);
}
}
23 changes: 23 additions & 0 deletions lib/data/repository/public_room_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:fluffychat/data/datasource/pubic_room_datasource.dart';
import 'package:fluffychat/data/model/search/public_room_response.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/repository/public_room_reposity.dart';
import 'package:matrix/matrix.dart';

class PublicRoomRepositoryImpl extends PublicRoomRepository {
final PublicRoomDatasource _publicRoomDatasource =
getIt.get<PublicRoomDatasource>();

@override
Future<PublicRoomResponse> search({
PublicRoomQueryFilter? filter,
String? server,
int? limit,
}) {
return _publicRoomDatasource.search(
filter: filter,
server: server,
limit: limit,
);
}
}
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];
}
10 changes: 10 additions & 0 deletions lib/domain/repository/public_room_reposity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:fluffychat/data/model/search/public_room_response.dart';
import 'package:matrix/matrix.dart';

abstract class PublicRoomRepository {
Future<PublicRoomResponse> search({
PublicRoomQueryFilter? filter,
String? server,
int? limit,
});
}
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));
}
}
}
Loading