Solian/lib/providers/content/posts.dart

135 lines
3.9 KiB
Dart
Raw Normal View History

2024-05-18 10:17:16 +00:00
import 'package:get/get.dart';
import 'package:solian/exceptions/request.dart';
import 'package:solian/exceptions/unauthorized.dart';
2024-09-16 15:35:44 +00:00
import 'package:solian/models/post.dart';
2024-07-09 14:39:44 +00:00
import 'package:solian/providers/auth.dart';
2024-05-18 10:17:16 +00:00
2024-10-10 16:28:09 +00:00
class PostProvider extends GetxController {
2024-09-03 15:07:20 +00:00
Future<Response> seeWhatsNew(int pivot) async {
final AuthProvider auth = Get.find();
2024-10-10 16:28:09 +00:00
final client = await auth.configureClient('co');
2024-09-03 15:07:20 +00:00
final resp = await client.get('/whats-new?pivot=$pivot');
if (resp.statusCode != 200) {
throw RequestException(resp);
}
return resp;
}
Future<Response> listRecommendations(int page,
{String? realm, String? channel, int take = 10}) async {
2024-05-29 14:42:11 +00:00
final queries = [
'take=$take',
2024-05-29 14:42:11 +00:00
'offset=$page',
if (realm != null) 'realm=$realm',
2024-05-29 14:42:11 +00:00
];
2024-10-10 16:28:09 +00:00
final AuthProvider auth = Get.find();
final client = await auth.configureClient('interactive');
final resp = await client.get(
2024-07-24 18:00:29 +00:00
channel == null
? '/recommendations?${queries.join('&')}'
: '/recommendations/$channel?${queries.join('&')}',
);
2024-05-25 05:19:16 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-25 05:19:16 +00:00
}
return resp;
}
2024-07-09 14:39:44 +00:00
Future<Response> listDraft(int page) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-07-09 14:39:44 +00:00
final queries = [
'take=${10}',
'offset=$page',
2024-10-17 14:44:00 +00:00
'truncate=false',
2024-07-09 14:39:44 +00:00
];
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('interactive');
2024-10-17 14:44:00 +00:00
final resp = await client.get(
'/posts/drafts?${queries.join('&')}',
);
2024-07-09 14:39:44 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-07-09 14:39:44 +00:00
}
return resp;
}
2024-10-13 13:48:53 +00:00
Future<Response> searchPost(String probe, int page,
{String? realm, String? author, tag, category, int take = 10}) async {
final queries = [
'probe=$probe',
'take=$take',
'offset=$page',
if (tag != null) 'tag=$tag',
if (category != null) 'category=$category',
if (author != null) 'author=$author',
if (realm != null) 'realm=$realm',
];
final AuthProvider auth = Get.find();
final client = await auth.configureClient('co');
final resp = await client.get('/posts/search?${queries.join('&')}');
if (resp.statusCode != 200) {
throw RequestException(resp);
}
return resp;
}
2024-07-25 17:31:45 +00:00
Future<Response> listPost(int page,
{String? realm, String? author, tag, category, int take = 10}) async {
final queries = [
'take=$take',
'offset=$page',
2024-07-25 17:31:45 +00:00
if (tag != null) 'tag=$tag',
if (category != null) 'category=$category',
2024-07-26 08:53:05 +00:00
if (author != null) 'author=$author',
if (realm != null) 'realm=$realm',
];
2024-10-10 16:28:09 +00:00
final AuthProvider auth = Get.find();
final client = await auth.configureClient('co');
final resp = await client.get('/posts?${queries.join('&')}');
if (resp.statusCode != 200) {
throw RequestException(resp);
}
return resp;
}
2024-05-25 09:21:27 +00:00
Future<Response> listPostReplies(String alias, int page) async {
2024-10-10 16:28:09 +00:00
final AuthProvider auth = Get.find();
final client = await auth.configureClient('co');
final resp =
await client.get('/posts/$alias/replies?take=${10}&offset=$page');
2024-05-25 09:21:27 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-05-25 09:21:27 +00:00
}
return resp;
}
2024-09-16 18:14:23 +00:00
Future<List<Post>> listPostFeaturedReply(String alias, {int take = 1}) async {
2024-10-10 16:28:09 +00:00
final AuthProvider auth = Get.find();
final client = await auth.configureClient('co');
final resp = await client.get('/posts/$alias/replies/featured?take=$take');
2024-09-16 15:35:44 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
}
return List<Post>.from(resp.body.map((x) => Post.fromJson(x)));
}
2024-05-25 05:19:16 +00:00
Future<Response> getPost(String alias) async {
2024-10-10 16:28:09 +00:00
final AuthProvider auth = Get.find();
final client = await auth.configureClient('co');
final resp = await client.get('/posts/$alias');
2024-07-10 02:50:10 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-07-10 02:50:10 +00:00
}
return resp;
}
2024-05-18 10:17:16 +00:00
}