Browse Source

nextcloud,neon: Use proper type for news list type

pull/82/head
jld3103 2 years ago
parent
commit
ff3fcec2fa
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 16
      packages/neon/lib/src/apps/news/blocs/articles.dart
  2. 22
      packages/nextcloud/lib/src/helpers.dart
  3. 32
      packages/nextcloud/test/news_test.dart

16
packages/neon/lib/src/apps/news/blocs/articles.dart

@ -121,7 +121,7 @@ class NewsArticlesBloc extends $NewsArticlesBloc {
// https://github.com/nextcloud/news/blob/master/docs/api/api-v1-2.md#get-items // https://github.com/nextcloud/news/blob/master/docs/api/api-v1-2.md#get-items
// https://github.com/nextcloud/news/blob/48ee5ce4d135da20079961a62ae37958d6a6b628/lib/Db/ListType.php#L21 // https://github.com/nextcloud/news/blob/48ee5ce4d135da20079961a62ae37958d6a6b628/lib/Db/ListType.php#L21
late final int type; late final NewsListType type;
bool? getRead; bool? getRead;
if (listType != null) { if (listType != null) {
switch (_filterTypeSubject.value) { switch (_filterTypeSubject.value) {
@ -136,21 +136,21 @@ class NewsArticlesBloc extends $NewsArticlesBloc {
} }
switch (listType) { switch (listType) {
case ListType.feed: case ListType.feed:
type = 0; type = NewsListType.feed;
break; break;
case ListType.folder: case ListType.folder:
type = 1; type = NewsListType.folder;
break; break;
case null: case null:
switch (_filterTypeSubject.value) { switch (_filterTypeSubject.value) {
case FilterType.starred: case FilterType.starred:
type = 2; type = NewsListType.starred;
break; break;
case FilterType.all: case FilterType.all:
type = 3; type = NewsListType.allItems;
break; break;
case FilterType.unread: case FilterType.unread:
type = 6; type = NewsListType.unread;
break; break;
} }
break; break;
@ -159,9 +159,9 @@ class NewsArticlesBloc extends $NewsArticlesBloc {
newsBloc.requestManager newsBloc.requestManager
.wrapNextcloud<List<NewsArticle>, NewsListArticles>( .wrapNextcloud<List<NewsArticle>, NewsListArticles>(
newsBloc.client.id, newsBloc.client.id,
'news-articles-$type-$id-$getRead', 'news-articles-${type.code}-$id-$getRead',
() async => newsBloc.client.news.listArticles( () async => newsBloc.client.news.listArticles(
type: type, type: type.code,
id: id ?? 0, id: id ?? 0,
getRead: getRead ?? true ? 1 : 0, getRead: getRead ?? true ? 1 : 0,
), ),

22
packages/nextcloud/lib/src/helpers.dart

@ -1,23 +1,20 @@
// ignore_for_file: public_member_api_docs
part of '../nextcloud.dart'; part of '../nextcloud.dart';
// ignore: public_member_api_docs
extension HttpClientResponseBody on HttpClientResponse { extension HttpClientResponseBody on HttpClientResponse {
// ignore: public_member_api_docs
Future<Uint8List> get bodyBytes async => Future<Uint8List> get bodyBytes async =>
Uint8List.fromList((await toList()).reduce((final value, final element) => [...value, ...element])); Uint8List.fromList((await toList()).reduce((final value, final element) => [...value, ...element]));
// ignore: public_member_api_docs
Future<String> get body async => utf8.decode(await bodyBytes); Future<String> get body async => utf8.decode(await bodyBytes);
} }
// ignore: public_member_api_docs
extension UserDetailsDisplayName on ProvisioningApiUserDetails { extension UserDetailsDisplayName on ProvisioningApiUserDetails {
/// This is used to work around an API change that wasn't made for every endpoint /// This is used to work around an API change that wasn't made for every endpoint
/// See https://github.com/nextcloud/server/commit/5086335643b6181284ee50f57b95525002842992 /// See https://github.com/nextcloud/server/commit/5086335643b6181284ee50f57b95525002842992
String? getDisplayName() => displayname ?? displayName; String? getDisplayName() => displayname ?? displayName;
} }
// ignore: public_member_api_docs
extension NextcloudNotificationsPushProxy on NotificationsClient { extension NextcloudNotificationsPushProxy on NotificationsClient {
/// Registers a device at the push proxy server /// Registers a device at the push proxy server
Future registerDeviceAtPushProxy( Future registerDeviceAtPushProxy(
@ -67,3 +64,18 @@ NotificationsPushNotificationDecryptedSubject decryptPushNotificationSubject(
NotificationsPushNotificationDecryptedSubject.fromJson( NotificationsPushNotificationDecryptedSubject.fromJson(
json.decode(privateKey.decrypt(subject)) as Map<String, dynamic>, json.decode(privateKey.decrypt(subject)) as Map<String, dynamic>,
); );
/// See https://github.com/nextcloud/news/blob/4a107b3d53c4fe651ac704251b99e04a53cd587f/lib/Db/ListType.php
enum NewsListType {
feed(0),
folder(1),
starred(2),
allItems(3),
shared(4),
explore(5),
unread(6);
const NewsListType(this.code);
final int code;
}

32
packages/nextcloud/test/news_test.dart

@ -50,7 +50,7 @@ Future main() async {
test('Mark feed as read', () async { test('Mark feed as read', () async {
final feedsResponse = await addWikipediaFeed(); final feedsResponse = await addWikipediaFeed();
var articlesResponse = await client.news.listArticles(type: 6); var articlesResponse = await client.news.listArticles(type: NewsListType.unread.code);
expect(articlesResponse.items!.length, greaterThan(0)); expect(articlesResponse.items!.length, greaterThan(0));
await client.news.markFeedAsRead( await client.news.markFeedAsRead(
@ -58,7 +58,7 @@ Future main() async {
newestItemId: feedsResponse.newestItemId!, newestItemId: feedsResponse.newestItemId!,
); );
articlesResponse = await client.news.listArticles(type: 6); articlesResponse = await client.news.listArticles(type: NewsListType.unread.code);
expect(articlesResponse.items, hasLength(0)); expect(articlesResponse.items, hasLength(0));
}); });
@ -103,36 +103,36 @@ Future main() async {
test('Mark article as read', () async { test('Mark article as read', () async {
await addWikipediaFeed(); await addWikipediaFeed();
var response = await client.news.listArticles(type: 6); var response = await client.news.listArticles(type: NewsListType.unread.code);
final unreadArticles = response.items!.length; final unreadArticles = response.items!.length;
expect(unreadArticles, greaterThan(0)); expect(unreadArticles, greaterThan(0));
await client.news.markArticleAsRead( await client.news.markArticleAsRead(
itemId: response.items![0].id!, itemId: response.items![0].id!,
); );
response = await client.news.listArticles(type: 6); response = await client.news.listArticles(type: NewsListType.unread.code);
expect(response.items, hasLength(unreadArticles - 1)); expect(response.items, hasLength(unreadArticles - 1));
}); });
test('Mark article as unread', () async { test('Mark article as unread', () async {
await addWikipediaFeed(); await addWikipediaFeed();
var response = await client.news.listArticles(type: 6); var response = await client.news.listArticles(type: NewsListType.unread.code);
final readArticle = response.items![0]; final readArticle = response.items![0];
await client.news.markArticleAsRead(itemId: readArticle.id!); await client.news.markArticleAsRead(itemId: readArticle.id!);
response = await client.news.listArticles(type: 6); response = await client.news.listArticles(type: NewsListType.unread.code);
final unreadArticles = response.items!.length; final unreadArticles = response.items!.length;
expect(unreadArticles, greaterThan(0)); expect(unreadArticles, greaterThan(0));
await client.news.markArticleAsUnread(itemId: readArticle.id!); await client.news.markArticleAsUnread(itemId: readArticle.id!);
response = await client.news.listArticles(type: 6); response = await client.news.listArticles(type: NewsListType.unread.code);
expect(response.items, hasLength(unreadArticles + 1)); expect(response.items, hasLength(unreadArticles + 1));
}); });
test('Star article', () async { test('Star article', () async {
await addWikipediaFeed(); await addWikipediaFeed();
var response = await client.news.listArticles(type: 2); var response = await client.news.listArticles(type: NewsListType.starred.code);
final starredArticles = response.items!.length; final starredArticles = response.items!.length;
expect(starredArticles, 0); expect(starredArticles, 0);
@ -140,7 +140,7 @@ Future main() async {
await client.news.starArticle( await client.news.starArticle(
itemId: response.items![0].id!, itemId: response.items![0].id!,
); );
response = await client.news.listArticles(type: 2); response = await client.news.listArticles(type: NewsListType.starred.code);
expect(response.items, hasLength(1)); expect(response.items, hasLength(1));
}); });
@ -153,13 +153,13 @@ Future main() async {
await client.news.starArticle( await client.news.starArticle(
itemId: item.id!, itemId: item.id!,
); );
response = await client.news.listArticles(type: 2); response = await client.news.listArticles(type: NewsListType.starred.code);
expect(response.items, hasLength(1)); expect(response.items, hasLength(1));
await client.news.unstarArticle( await client.news.unstarArticle(
itemId: item.id!, itemId: item.id!,
); );
response = await client.news.listArticles(type: 2); response = await client.news.listArticles(type: NewsListType.starred.code);
expect(response.items, hasLength(0)); expect(response.items, hasLength(0));
}); });
@ -167,17 +167,17 @@ Future main() async {
var response = await client.news.listFolders(); var response = await client.news.listFolders();
expect(response.folders, hasLength(0)); expect(response.folders, hasLength(0));
response = await client.news.createFolder(name: 'test'); response = await client.news.createFolder(name: 'test1');
expect(response.folders, hasLength(1)); expect(response.folders, hasLength(1));
expect(response.folders![0].id, 1); expect(response.folders![0].id, 1);
expect(response.folders![0].name, 'test'); expect(response.folders![0].name, 'test1');
expect(response.folders![0].opened, true); expect(response.folders![0].opened, true);
expect(response.folders![0].feeds, hasLength(0)); expect(response.folders![0].feeds, hasLength(0));
response = await client.news.listFolders(); response = await client.news.listFolders();
expect(response.folders, hasLength(1)); expect(response.folders, hasLength(1));
expect(response.folders![0].id, 1); expect(response.folders![0].id, 1);
expect(response.folders![0].name, 'test'); expect(response.folders![0].name, 'test1');
expect(response.folders![0].opened, true); expect(response.folders![0].opened, true);
expect(response.folders![0].feeds, hasLength(0)); expect(response.folders![0].feeds, hasLength(0));
}); });
@ -215,7 +215,7 @@ Future main() async {
final foldersResponse = await client.news.createFolder(name: 'test1'); final foldersResponse = await client.news.createFolder(name: 'test1');
final feedsResponse = await addWikipediaFeed(1); final feedsResponse = await addWikipediaFeed(1);
var articlesResponse = await client.news.listArticles(type: 6); var articlesResponse = await client.news.listArticles(type: NewsListType.unread.code);
expect(articlesResponse.items!.length, greaterThan(0)); expect(articlesResponse.items!.length, greaterThan(0));
await client.news.markFolderAsRead( await client.news.markFolderAsRead(
@ -223,7 +223,7 @@ Future main() async {
newestItemId: feedsResponse.newestItemId!, newestItemId: feedsResponse.newestItemId!,
); );
articlesResponse = await client.news.listArticles(type: 6); articlesResponse = await client.news.listArticles(type: NewsListType.unread.code);
expect(articlesResponse.items, hasLength(0)); expect(articlesResponse.items, hasLength(0));
}); });
}); });

Loading…
Cancel
Save