From 52312662fb90ee6d5d316f16409916c7568ad019 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 11 Oct 2024 00:28:09 +0800 Subject: [PATCH] :bug: Fix post visibility issue --- lib/providers/auth.dart | 2 +- lib/providers/content/attachment.dart | 34 +++++++++--------- lib/providers/content/posts.dart | 50 +++++++++------------------ 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index 21ee211..1cb22a5 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -125,7 +125,7 @@ class AuthProvider extends GetConnect { userAgent: await ServiceFinder.getUserAgent(), sendUserAgent: true, ); - client.httpClient.addAuthenticator(requestAuthenticator); + client.httpClient.addRequestModifier(requestAuthenticator); client.httpClient.baseUrl = ServiceFinder.buildUrl(service, null); return client; diff --git a/lib/providers/content/attachment.dart b/lib/providers/content/attachment.dart index 4f4a302..0112063 100644 --- a/lib/providers/content/attachment.dart +++ b/lib/providers/content/attachment.dart @@ -41,25 +41,27 @@ class AttachmentProvider extends GetConnect { } } - final resp = await get( - '/attachments?take=${pendingQuery.length}&id=${pendingQuery.join(',')}', - ); - if (resp.statusCode != 200) return result; + if (pendingQuery.isNotEmpty) { + final resp = await get( + '/attachments?take=${pendingQuery.length}&id=${pendingQuery.join(',')}', + ); + if (resp.statusCode != 200) return result; - final rawOut = PaginationResult.fromJson(resp.body); - if (rawOut.data == null) return result; + final rawOut = PaginationResult.fromJson(resp.body); + if (rawOut.data == null) return result; - final List out = - rawOut.data!.map((x) => Attachment.fromJson(x)).toList(); - for (final item in out) { - if (item.destination != 0 && item.isAnalyzed) { - _cachedResponses[item.rid] = item; + final List out = + rawOut.data!.map((x) => Attachment.fromJson(x)).toList(); + for (final item in out) { + if (item.destination != 0 && item.isAnalyzed) { + _cachedResponses[item.rid] = item; + } } - } - for (var i = 0; i < out.length; i++) { - for (var j = 0; j < rid.length; j++) { - if (out[i].rid == rid[j]) { - result[j] = out[i]; + for (var i = 0; i < out.length; i++) { + for (var j = 0; j < rid.length; j++) { + if (out[i].rid == rid[j]) { + result[j] = out[i]; + } } } } diff --git a/lib/providers/content/posts.dart b/lib/providers/content/posts.dart index c22e481..e78361f 100644 --- a/lib/providers/content/posts.dart +++ b/lib/providers/content/posts.dart @@ -3,22 +3,11 @@ import 'package:solian/exceptions/request.dart'; import 'package:solian/exceptions/unauthorized.dart'; import 'package:solian/models/post.dart'; import 'package:solian/providers/auth.dart'; -import 'package:solian/services.dart'; - -class PostProvider extends GetConnect { - @override - void onInit() { - httpClient.baseUrl = ServiceFinder.buildUrl('interactive', null); - } +class PostProvider extends GetxController { Future seeWhatsNew(int pivot) async { - GetConnect client; final AuthProvider auth = Get.find(); - if (auth.isAuthorized.value) { - client = await auth.configureClient('co'); - } else { - client = await ServiceFinder.configureClient('co'); - } + final client = await auth.configureClient('co'); final resp = await client.get('/whats-new?pivot=$pivot'); if (resp.statusCode != 200) { throw RequestException(resp); @@ -29,18 +18,13 @@ class PostProvider extends GetConnect { Future listRecommendations(int page, {String? realm, String? channel, int take = 10}) async { - GetConnect client; - final AuthProvider auth = Get.find(); final queries = [ 'take=$take', 'offset=$page', if (realm != null) 'realm=$realm', ]; - if (auth.isAuthorized.value) { - client = await auth.configureClient('co'); - } else { - client = await ServiceFinder.configureClient('co'); - } + final AuthProvider auth = Get.find(); + final client = await auth.configureClient('interactive'); final resp = await client.get( channel == null ? '/recommendations?${queries.join('&')}' @@ -80,7 +64,9 @@ class PostProvider extends GetConnect { if (author != null) 'author=$author', if (realm != null) 'realm=$realm', ]; - final resp = await get('/posts?${queries.join('&')}'); + 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); } @@ -89,7 +75,10 @@ class PostProvider extends GetConnect { } Future listPostReplies(String alias, int page) async { - final resp = await get('/posts/$alias/replies?take=${10}&offset=$page'); + final AuthProvider auth = Get.find(); + final client = await auth.configureClient('co'); + final resp = + await client.get('/posts/$alias/replies?take=${10}&offset=$page'); if (resp.statusCode != 200) { throw RequestException(resp); } @@ -98,7 +87,9 @@ class PostProvider extends GetConnect { } Future> listPostFeaturedReply(String alias, {int take = 1}) async { - final resp = await get('/posts/$alias/replies/featured?take=$take'); + final AuthProvider auth = Get.find(); + final client = await auth.configureClient('co'); + final resp = await client.get('/posts/$alias/replies/featured?take=$take'); if (resp.statusCode != 200) { throw RequestException(resp); } @@ -107,16 +98,9 @@ class PostProvider extends GetConnect { } Future getPost(String alias) async { - final resp = await get('/posts/$alias'); - if (resp.statusCode != 200) { - throw RequestException(resp); - } - - return resp; - } - - Future getArticle(String alias) async { - final resp = await get('/articles/$alias'); + final AuthProvider auth = Get.find(); + final client = await auth.configureClient('co'); + final resp = await client.get('/posts/$alias'); if (resp.statusCode != 200) { throw RequestException(resp); }