1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00
comunicmobile/lib/helpers/search_helper.dart

48 lines
1.5 KiB
Dart
Raw Normal View History

2019-04-27 12:40:27 +00:00
import 'package:comunic/helpers/users_helper.dart';
2020-04-17 08:30:29 +00:00
import 'package:comunic/lists/search_results_list.dart';
2019-04-27 12:40:27 +00:00
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/api_request.dart';
2020-04-17 08:30:29 +00:00
import 'package:comunic/models/search_result.dart';
2020-04-16 13:14:27 +00:00
import 'package:comunic/utils/api_utils.dart';
2019-04-27 12:40:27 +00:00
/// Search helper
///
/// @author Pierre HUBERT
class SearchHelper {
/// Search for user. This method returns information about the target users
///
/// Returns information about the target users or null if an error occurred
Future<UsersList?> searchUser(String query) async {
2019-04-27 12:40:27 +00:00
// Execute the query on the server
final response = await APIRequest(
2020-04-16 13:14:27 +00:00
uri: "user/search", needLogin: true, args: {"query": query}).exec();
2019-04-27 12:40:27 +00:00
if (response.code != 200) return null;
2020-04-16 13:14:27 +00:00
return await UsersHelper()
.getUsersInfo(response.getArray()!.map((f) => cast<int>(f)).toList());
2019-04-27 12:40:27 +00:00
}
2020-04-17 08:30:29 +00:00
/// Perform a global search
Future<SearchResultsList> globalSearch(String query) async {
final result = await APIRequest(
uri: "search/global", needLogin: true, args: {"query": query}).exec();
result.assertOk();
return SearchResultsList()..addAll(result.getArray()!.map((f) {
2020-04-17 08:30:29 +00:00
switch (f["kind"]) {
case "user":
return SearchResult(id: f["id"], kind: SearchResultKind.USER);
case "group":
return SearchResult(id: f["id"], kind: SearchResultKind.GROUP);
default:
throw Exception("Unkown search kind: ${f["kind"]}");
}
}).toList());
}
2020-04-16 13:14:27 +00:00
}