mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-10 21:56:09 -06:00
[client] use bootstrap request instead of bookmarks and users
This commit is contained in:
@@ -14,6 +14,7 @@ import 'package:phylum/util/logging.dart';
|
||||
|
||||
const _persistKeyAccessToken = 'accessToken';
|
||||
const _persistKeyUser = 'user';
|
||||
const _persistKeyBootstrapUntil = 'bootstrapUntil';
|
||||
|
||||
AccountManager<PhylumAccount> createAccountManager(bool autoInitialize) => AccountManager((id) async {
|
||||
final account = PhylumAccount.restore(id: id);
|
||||
@@ -59,7 +60,7 @@ class PhylumAccount extends Account<PhylumAccount> {
|
||||
set loggedInUser(LoggedInUser value) {
|
||||
if (_loggedInUser != value) {
|
||||
_loggedInUser = value;
|
||||
persist(_persistKeyUser, value.toJson());
|
||||
persist(_persistKeyUser, jsonEncode(value.toJson()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +70,11 @@ class PhylumAccount extends Account<PhylumAccount> {
|
||||
String get userHome => _loggedInUser.home;
|
||||
int get userPermissions => _loggedInUser.permissions;
|
||||
|
||||
int get bootstrapUntil => getPersisted<num>(_persistKeyBootstrapUntil)?.toInt() ?? 0;
|
||||
set bootstrapUntil(int value) {
|
||||
persist(_persistKeyBootstrapUntil, value);
|
||||
}
|
||||
|
||||
PhylumAccount.create({required super.serverUri, required String accessToken, required LoggedInUser user})
|
||||
: _initialAccessToken = accessToken,
|
||||
_initialLoggedInUser = user,
|
||||
@@ -100,7 +106,12 @@ class PhylumAccount extends Account<PhylumAccount> {
|
||||
}
|
||||
}
|
||||
});
|
||||
_loggedInUser = _initialLoggedInUser ?? LoggedInUser.fromJson(jsonDecode(getPersisted(_persistKeyUser)));
|
||||
if (_initialLoggedInUser != null) {
|
||||
_loggedInUser = _initialLoggedInUser!;
|
||||
persist(_persistKeyUser, jsonEncode(_initialLoggedInUser!.toJson()));
|
||||
} else {
|
||||
_loggedInUser = LoggedInUser.fromJson(jsonDecode(getPersisted(_persistKeyUser)));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -7,15 +7,12 @@ import 'package:phylum/libphylum/actions/action_resource_bookmark_add.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_bookmark_remove.dart';
|
||||
import 'package:phylum/libphylum/db/db.dart';
|
||||
import 'package:phylum/libphylum/phylum_account.dart';
|
||||
import 'package:phylum/libphylum/requests/bookmarks_request.dart';
|
||||
import 'package:phylum/libphylum/responses/responses.dart';
|
||||
|
||||
const _remoteBookmarksBoxName = 'remote_bookmarks';
|
||||
|
||||
class BookmarkRepository extends Repository<PhylumAccount, Bookmark> {
|
||||
late final PhylumAccount _account;
|
||||
late final LazyBox<Bookmark?> _remoteDataBox;
|
||||
int? _lastBookmarkFetch;
|
||||
|
||||
BookmarkRepository();
|
||||
|
||||
@@ -51,17 +48,6 @@ class BookmarkRepository extends Repository<PhylumAccount, Bookmark> {
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
return _account.apiClient
|
||||
.sendRequest(BookmarksRequest(since: _lastBookmarkFetch),
|
||||
(request, response) => parseJsonMapResponse(response, BookmarkListResponse.fromResponse))
|
||||
.then((response) {
|
||||
if (response is BookmarkListResponse) {
|
||||
_lastBookmarkFetch = response.until;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> persistRemoteData(Map<String, Bookmark?> remoteData) {
|
||||
return _remoteDataBox.putAll(remoteData);
|
||||
|
||||
@@ -6,8 +6,6 @@ import 'package:offtheline/offtheline.dart';
|
||||
import 'package:phylum/libphylum/actions/action_user_invite.dart';
|
||||
import 'package:phylum/libphylum/db/db.dart';
|
||||
import 'package:phylum/libphylum/phylum_account.dart';
|
||||
import 'package:phylum/libphylum/requests/users_list_request.dart';
|
||||
import 'package:phylum/libphylum/responses/responses.dart';
|
||||
import 'package:phylum/util/user_display_name.dart';
|
||||
|
||||
const _remoteUsersBoxName = 'remote_users';
|
||||
@@ -42,13 +40,6 @@ class UserRepository extends Repository<PhylumAccount, User> {
|
||||
return remoteData;
|
||||
}
|
||||
|
||||
Future<ApiResult> refresh() async {
|
||||
return _account.apiClient.sendRequest(
|
||||
UsersListRequest(),
|
||||
(request, response) => parseJsonMapResponse(response, UserListResponse.fromResponse),
|
||||
);
|
||||
}
|
||||
|
||||
String getUserDisplayName(int userId) {
|
||||
return _users[userId]?.displayName ?? 'Unknown User($userId)';
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ import 'dart:typed_data';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:offtheline/offtheline.dart';
|
||||
|
||||
class BookmarksRequest extends ApiRequest {
|
||||
class BootstrapRequest extends ApiRequest {
|
||||
final int? since;
|
||||
|
||||
BookmarksRequest({this.since});
|
||||
BootstrapRequest({this.since});
|
||||
|
||||
@override
|
||||
BaseRequest createRequest(ApiClient api, {Uint8List? data}) {
|
||||
final uri = api.createUriBuilder('/api/v1/my/bookmarks/list');
|
||||
final uri = api.createUriBuilder('/api/v1/my/bootstrap');
|
||||
if (since != null) {
|
||||
uri.queryParameters['since'] = since.toString();
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:http/http.dart';
|
||||
import 'package:offtheline/offtheline.dart';
|
||||
|
||||
class UsersListRequest extends ApiRequest {
|
||||
const UsersListRequest();
|
||||
|
||||
@override
|
||||
BaseRequest createRequest(ApiClient api, {Uint8List? data}) {
|
||||
final uri = api.createUriBuilder('/api/v1/users/list');
|
||||
return Request('get', uri.build());
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
part of 'responses.dart';
|
||||
|
||||
const keyLastBookmarkFetch = "lastBookmarksFetch";
|
||||
|
||||
class BookmarkListResponse extends PhylumApiSuccessResponse {
|
||||
final Iterable<Bookmark> bookmarks;
|
||||
final int until;
|
||||
|
||||
BookmarkListResponse({required this.bookmarks, required this.until});
|
||||
|
||||
factory BookmarkListResponse.fromResponse(Map<String, dynamic> data) {
|
||||
final bookmarks = (data["bookmarks"] as List).cast<Map>().map((u) => parseBookmark(u.cast<String, dynamic>()));
|
||||
return BookmarkListResponse(bookmarks: bookmarks, until: data['until'] as int);
|
||||
}
|
||||
|
||||
@override
|
||||
Future process(PhylumAccount account) async {
|
||||
await account.db.batch((batch) {
|
||||
batch.insertAll(account.db.bookmarks, bookmarks, mode: InsertMode.replace);
|
||||
});
|
||||
for (final b in bookmarks) {
|
||||
await account.datastore.get<Bookmark>().replaceRemoteData(b.resourceId, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@ class BootstrapLoginResponse extends PhylumApiSuccessResponse {
|
||||
});
|
||||
|
||||
account.loggedInUser = user;
|
||||
account.bootstrapUntil = until;
|
||||
|
||||
for (final b in bookmarks) {
|
||||
await account.datastore.get<Bookmark>().replaceRemoteData(b.resourceId, b);
|
||||
|
||||
@@ -9,7 +9,6 @@ import 'package:phylum/libphylum/parsers/parsers.dart';
|
||||
import 'package:phylum/libphylum/phylum_account.dart';
|
||||
import 'package:phylum/util/permissions.dart';
|
||||
|
||||
part 'bookmark_list_response.dart';
|
||||
part 'bookmark_response.dart';
|
||||
part 'bootstrap_login_response.dart';
|
||||
part 'empty_response.dart';
|
||||
@@ -18,7 +17,6 @@ part 'resource_response.dart';
|
||||
part 'search_response.dart';
|
||||
part 'shared_resources_response.dart';
|
||||
part 'trash_list_response.dart';
|
||||
part 'user_list_response.dart';
|
||||
part 'user_response.dart';
|
||||
|
||||
typedef JsonMapResponseParser = PhylumApiSuccessResponse Function(Map<String, dynamic> data);
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
part of 'responses.dart';
|
||||
|
||||
class UserListResponse extends PhylumApiSuccessResponse {
|
||||
final Iterable<User> users;
|
||||
|
||||
@override
|
||||
Future<void> process(PhylumAccount account) async {
|
||||
await account.db.batch((batch) {
|
||||
batch.insertAllOnConflictUpdate(account.db.users, users);
|
||||
});
|
||||
}
|
||||
|
||||
UserListResponse({required this.users});
|
||||
|
||||
factory UserListResponse.fromResponse(Map<String, dynamic> data) {
|
||||
final users = (data["users"] as List).cast<Map>().map((u) => parseUser(u));
|
||||
return UserListResponse(users: users);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:phylum/libphylum/db/resource_helpers.dart';
|
||||
import 'package:phylum/libphylum/local_upload_errors.dart';
|
||||
import 'package:phylum/libphylum/phylum_account.dart';
|
||||
import 'package:phylum/libphylum/requests/bootstrap_request.dart';
|
||||
import 'package:phylum/libphylum/responses/responses.dart';
|
||||
import 'package:phylum/ui/destination_picker/destination_picker.dart';
|
||||
import 'package:phylum/ui/explorer/explorer_actions.dart';
|
||||
import 'package:phylum/ui/explorer/explorer_gesture_handler.dart';
|
||||
@@ -42,8 +44,8 @@ class _ExplorerViewState extends State<ExplorerView> {
|
||||
|
||||
final account = context.read<PhylumAccount>();
|
||||
_listener = AppLifecycleListener(onResume: () {
|
||||
account.userRepository.refresh();
|
||||
account.bookmarkRepository.refresh();
|
||||
account.apiClient.sendRequest(BootstrapRequest(since: account.bootstrapUntil),
|
||||
(request, response) => parseJsonMapResponse(response, BootstrapLoginResponse.fromResponse));
|
||||
});
|
||||
|
||||
if (!kIsWeb && Platform.isAndroid) {
|
||||
|
||||
Reference in New Issue
Block a user