77 lines
1.7 KiB
Dart
77 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:loris/business_logic/account/account.dart';
|
|
import 'package:loris/business_logic/timeline/timeline.dart';
|
|
import 'package:loris/global.dart' as global;
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class SearchResult {
|
|
final String identitiy;
|
|
final List<AccountModel> accountModels;
|
|
final List<PostModel> postModels;
|
|
final String query;
|
|
|
|
SearchResult(
|
|
this.identitiy,
|
|
this.query, {
|
|
this.postModels = const [],
|
|
this.accountModels = const [],
|
|
});
|
|
}
|
|
|
|
Future<MapEntry<int, SearchResult?>> searchForEntry(
|
|
String q, String identityName) async {
|
|
final identity = global.settings!.identities[identityName]!;
|
|
final headers = {
|
|
...identity.getAuthHeaders(),
|
|
...global.defaultHeaders,
|
|
};
|
|
|
|
final uri = Uri(
|
|
scheme: "https",
|
|
host: identity.instanceUrl,
|
|
path: "/api/v2/search",
|
|
queryParameters: {"q": q},
|
|
);
|
|
|
|
var response = await http.get(uri, headers: headers);
|
|
if (response.statusCode != 200) {
|
|
response = await http.get(
|
|
Uri(
|
|
scheme: uri.scheme,
|
|
host: uri.host,
|
|
path: "/api/v1/search",
|
|
queryParameters: uri.queryParameters,
|
|
),
|
|
headers: headers);
|
|
}
|
|
if (response.statusCode != 200) {
|
|
return MapEntry(response.statusCode, null);
|
|
}
|
|
|
|
final json = jsonDecode(response.body);
|
|
|
|
List<AccountModel> accounts = [];
|
|
for (var account in json["accounts"]) {
|
|
accounts.add(AccountModel.fromJson(
|
|
account,
|
|
identityName,
|
|
));
|
|
}
|
|
|
|
List<PostModel> posts = [];
|
|
for (var post in json["statuses"]) {
|
|
posts.add(PostModel.fromJson(post, identityName));
|
|
}
|
|
|
|
return MapEntry(
|
|
200,
|
|
SearchResult(
|
|
identityName,
|
|
q,
|
|
postModels: posts,
|
|
accountModels: accounts,
|
|
),
|
|
);
|
|
}
|