loris/lib/business_logic/timeline/timeline.dart

141 lines
4.2 KiB
Dart
Raw Normal View History

2022-07-09 10:48:21 +00:00
import 'dart:convert';
2022-07-04 17:37:53 +00:00
import 'package:http/http.dart' as http;
import 'package:loris/business_logic/account/account.dart';
import 'package:loris/business_logic/timeline/media.dart';
2022-07-04 20:39:25 +00:00
import '../../global.dart' as global;
2022-07-03 13:47:24 +00:00
2022-07-09 13:24:19 +00:00
class TimelinePartModel {
late List<ThreadModel> threads;
late int minId;
late int maxId;
}
enum Visibility { public, unlisted, private, direct }
2022-08-01 10:01:19 +00:00
class PostModel implements Comparable {
2022-07-09 13:24:19 +00:00
late String id;
2022-08-10 21:48:09 +00:00
late String? reblogId;
2022-08-07 17:01:59 +00:00
late String createdAt;
2022-07-09 13:24:19 +00:00
late String uri;
late String content;
late Visibility visibility;
late bool sensitive;
late String spoilerText;
late bool favourited;
late bool reblogged;
2022-08-06 11:47:18 +00:00
late AccountModel account;
2022-08-07 17:01:59 +00:00
late AccountModel? rebloggedBy;
2022-08-06 14:05:14 +00:00
late List<MediaAttachmentModel> attachments;
2022-07-09 13:24:19 +00:00
PostModel.fromJson(Map<String, dynamic> json) {
id = json["id"] as String;
2022-08-10 21:48:09 +00:00
reblogId = null;
2022-08-07 17:01:59 +00:00
createdAt = json["created_at"];
if (json["reblog"] != null) {
rebloggedBy = AccountModel.fromJson(json["account"]);
json = json["reblog"];
2022-08-10 21:48:09 +00:00
reblogId = json["id"];
2022-08-07 17:01:59 +00:00
} else {
rebloggedBy = null;
}
2022-07-09 13:24:19 +00:00
uri = json["uri"] as String;
content = json["content"] as String;
visibility = Visibility.values.firstWhere(
// ignore: prefer_interpolation_to_compose_strings
(element) => element.toString() == "Visibility." + json["visibility"]);
sensitive = json["sensitive"] as bool;
spoilerText = json["spoiler_text"] as String;
favourited = json["favourited"] as bool;
reblogged = json["reblogged"] as bool;
2022-08-06 11:47:18 +00:00
account = AccountModel.fromJson(json["account"]);
2022-08-06 14:05:14 +00:00
attachments = [];
List<dynamic> jsonAttachmentList = json["media_attachments"];
for (int i = 0; i < jsonAttachmentList.length; i++) {
attachments.add(MediaAttachmentModel.fromJson(jsonAttachmentList[i]));
}
2022-07-09 13:24:19 +00:00
}
2022-08-01 10:01:19 +00:00
@override
int compareTo(dynamic b) {
return id.compareTo(b.id);
}
2022-07-09 13:24:19 +00:00
Future<ThreadModel> getThread() async {
2022-08-13 16:14:12 +00:00
final activeId = global.settings!.activeIdentity;
2022-08-14 14:34:28 +00:00
if (global.settings!.identities[activeId] == null) {
return ThreadModel([this]);
}
2022-08-13 16:14:12 +00:00
final token = global.settings!.identities[activeId]!.token;
final baseUrl = global.settings!.identities[activeId]!.instanceUrl;
2022-07-09 13:24:19 +00:00
Map<String, String> headers = {"Authorization": "Bearer $token"};
headers.addAll(global.defaultHeaders);
2022-08-10 21:48:09 +00:00
String currentId = reblogId ?? id;
2022-07-09 13:24:19 +00:00
final url = Uri(
scheme: "https",
host: baseUrl,
2022-08-10 21:48:09 +00:00
path: "/api/v1/statuses/$currentId/context",
2022-07-09 13:24:19 +00:00
);
2022-08-10 21:48:09 +00:00
http.Response response = await http.get(url, headers: headers);
2022-08-13 16:14:12 +00:00
if (response.statusCode != 200 || response.body.isEmpty) {
2022-07-09 13:24:19 +00:00
return ThreadModel([this]);
}
final json = jsonDecode(response.body);
final List<dynamic> ancestorsJson = json["ancestors"];
List<PostModel> posts = [this];
int i = 0;
while (i < ancestorsJson.length) {
posts.add(PostModel.fromJson(ancestorsJson[i]));
i++;
}
return ThreadModel(posts);
}
}
class ThreadModel {
late List<PostModel> posts;
ThreadModel(List<PostModel> allPosts) {
2022-08-01 10:01:19 +00:00
allPosts.sort();
2022-07-09 13:24:19 +00:00
posts = allPosts;
}
}
2022-07-09 10:48:21 +00:00
2022-08-10 16:47:34 +00:00
Future<List<ThreadModel>> getTimelineFromServer(String? index) async {
2022-08-13 16:14:12 +00:00
final activeId = global.settings!.activeIdentity;
2022-08-14 14:34:28 +00:00
if (global.settings!.identities[activeId] == null) {
return [];
}
final limit = global.settings?.batchSize;
final token = global.settings?.identities[activeId]?.token;
2022-08-01 10:01:19 +00:00
2022-08-10 16:47:34 +00:00
Map<String, String> query = {"limit": limit.toString()};
if (index != null) {
query.addAll({"max_id": index});
2022-08-01 10:01:19 +00:00
}
2022-08-13 16:14:12 +00:00
final baseUrl = global.settings!.identities[activeId]!.instanceUrl;
2022-07-04 20:39:25 +00:00
final url = Uri(
scheme: "https",
host: baseUrl,
path: "/api/v1/timelines/home",
2022-08-01 10:01:19 +00:00
queryParameters: query,
2022-07-04 20:39:25 +00:00
);
2022-08-01 10:01:19 +00:00
2022-07-04 20:39:25 +00:00
Map<String, String> headers = {"Authorization": "Bearer $token"};
headers.addAll(global.defaultHeaders);
2022-07-04 17:37:53 +00:00
2022-08-09 18:58:11 +00:00
http.Response response = await http.get(url, headers: headers);
2022-08-10 16:47:34 +00:00
2022-08-09 18:58:11 +00:00
while (response.statusCode != 200) {
await Future.delayed(const Duration(seconds: 5));
response = await http.get(url, headers: headers);
2022-08-07 17:01:59 +00:00
}
2022-08-09 18:58:11 +00:00
final List<dynamic> json = await jsonDecode(response.body);
2022-08-10 16:47:34 +00:00
List<ThreadModel> threads = [];
for (int i = 0; i < json.length; i++) {
threads.add(await PostModel.fromJson(json[i]).getThread());
}
return threads;
2022-07-04 20:39:25 +00:00
}