2020-05-17 19:35:57 +00:00
|
|
|
import 'dart:developer';
|
|
|
|
|
2020-05-17 12:39:38 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2020-05-22 18:22:55 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2020-05-17 12:39:38 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2020-05-22 18:22:55 +00:00
|
|
|
import 'package:photos/db/folder_db.dart';
|
|
|
|
import 'package:photos/db/photo_db.dart';
|
2020-05-25 14:36:24 +00:00
|
|
|
import 'package:photos/events/user_authenticated_event.dart';
|
2020-05-17 19:35:57 +00:00
|
|
|
import 'package:photos/models/folder.dart';
|
2020-05-22 18:22:55 +00:00
|
|
|
import 'package:photos/models/photo.dart';
|
2020-05-17 12:39:38 +00:00
|
|
|
|
2020-05-25 14:36:24 +00:00
|
|
|
import 'core/event_bus.dart';
|
|
|
|
|
2020-05-17 14:35:31 +00:00
|
|
|
class FolderSharingService {
|
2020-05-22 18:22:55 +00:00
|
|
|
final _logger = Logger("FolderSharingService");
|
2020-05-17 12:39:38 +00:00
|
|
|
final _dio = Dio();
|
2020-05-22 18:22:55 +00:00
|
|
|
static final _diffLimit = 100;
|
2020-05-17 12:39:38 +00:00
|
|
|
|
2020-05-25 14:36:24 +00:00
|
|
|
FolderSharingService._privateConstructor() {
|
|
|
|
Bus.instance.on<UserAuthenticatedEvent>().listen((event) {
|
|
|
|
sync();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:35:31 +00:00
|
|
|
static final FolderSharingService instance =
|
|
|
|
FolderSharingService._privateConstructor();
|
2020-05-17 12:39:38 +00:00
|
|
|
|
2020-05-18 15:35:38 +00:00
|
|
|
void sync() {
|
2020-05-22 18:22:55 +00:00
|
|
|
getFolders().then((f) async {
|
|
|
|
var folders = f.toSet();
|
|
|
|
var currentFolders = await FolderDB.instance.getFolders();
|
|
|
|
for (final currentFolder in currentFolders) {
|
|
|
|
if (!folders.contains(currentFolder)) {
|
|
|
|
await PhotoDB.instance.deletePhotosInRemoteFolder(currentFolder.id);
|
2020-05-25 14:33:31 +00:00
|
|
|
await FolderDB.instance.deleteFolder(currentFolder);
|
2020-05-22 18:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (final folder in folders) {
|
|
|
|
await syncDiff(folder);
|
|
|
|
await FolderDB.instance.putFolder(folder);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> syncDiff(Folder folder) async {
|
|
|
|
int lastSyncTimestamp = 0;
|
|
|
|
try {
|
|
|
|
Photo photo =
|
|
|
|
await PhotoDB.instance.getLatestPhotoInRemoteFolder(folder.id);
|
|
|
|
lastSyncTimestamp = photo.syncTimestamp;
|
|
|
|
} catch (e) {
|
|
|
|
// Folder has never been synced
|
|
|
|
}
|
|
|
|
var photos = await getDiff(folder.id, lastSyncTimestamp, _diffLimit);
|
|
|
|
await PhotoDB.instance.insertPhotos(photos);
|
|
|
|
if (photos.length == _diffLimit) {
|
|
|
|
await syncDiff(folder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Photo>> getDiff(
|
|
|
|
int folderId, int sinceTimestamp, int limit) async {
|
|
|
|
Response response = await _dio.get(
|
|
|
|
Configuration.instance.getHttpEndpoint() +
|
2020-05-25 14:33:31 +00:00
|
|
|
"/folders/diff/" +
|
2020-05-22 18:22:55 +00:00
|
|
|
folderId.toString(),
|
|
|
|
options:
|
|
|
|
Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
|
|
|
|
queryParameters: {
|
|
|
|
"sinceTimestamp": sinceTimestamp,
|
|
|
|
"limit": limit,
|
|
|
|
},
|
|
|
|
).catchError((e) => _logger.severe(e));
|
|
|
|
if (response != null) {
|
|
|
|
return (response.data["diff"] as List).map((p) {
|
|
|
|
Photo photo = new Photo.fromJson(p);
|
2020-05-25 14:33:31 +00:00
|
|
|
photo.localId = null;
|
2020-05-22 18:22:55 +00:00
|
|
|
photo.remoteFolderId = folderId;
|
|
|
|
return photo;
|
|
|
|
}).toList();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-18 15:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Folder>> getFolders() async {
|
|
|
|
return _dio
|
|
|
|
.get(
|
|
|
|
Configuration.instance.getHttpEndpoint() + "/folders/",
|
|
|
|
options:
|
|
|
|
Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
|
|
|
|
)
|
|
|
|
.then((foldersResponse) {
|
|
|
|
return (foldersResponse.data as List)
|
|
|
|
.map((f) => Folder.fromMap(f))
|
|
|
|
.toList();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-22 18:22:55 +00:00
|
|
|
Future<Folder> getFolder(String deviceFolder) async {
|
|
|
|
return _dio
|
|
|
|
.get(
|
2020-05-25 17:15:47 +00:00
|
|
|
Configuration.instance.getHttpEndpoint() + "/folders/folder/",
|
|
|
|
queryParameters: {
|
|
|
|
"deviceFolder": deviceFolder,
|
|
|
|
},
|
|
|
|
options:
|
|
|
|
Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
|
|
|
|
)
|
|
|
|
.then((response) {
|
|
|
|
return Folder.fromMap(response.data);
|
|
|
|
}).catchError((e) {
|
|
|
|
return Folder(
|
|
|
|
null,
|
|
|
|
Configuration.instance.getUsername() + "'s " + deviceFolder,
|
|
|
|
Configuration.instance.getUsername(),
|
|
|
|
deviceFolder,
|
|
|
|
Set<String>(),
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
});
|
2020-05-22 18:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, bool>> getSharingStatus(Folder folder) async {
|
2020-05-17 12:39:38 +00:00
|
|
|
return _dio
|
|
|
|
.get(
|
|
|
|
Configuration.instance.getHttpEndpoint() + "/users",
|
|
|
|
options:
|
|
|
|
Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
|
|
|
|
)
|
2020-05-22 18:22:55 +00:00
|
|
|
.then((response) {
|
|
|
|
final users = (response.data["users"] as List).toList();
|
|
|
|
final result = Map<String, bool>();
|
|
|
|
for (final user in users) {
|
|
|
|
if (user != Configuration.instance.getUsername()) {
|
|
|
|
result[user] = folder.sharedWith.contains(user);
|
2020-05-17 19:35:57 +00:00
|
|
|
}
|
2020-05-22 18:22:55 +00:00
|
|
|
}
|
|
|
|
return result;
|
2020-05-17 12:39:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-22 18:22:55 +00:00
|
|
|
Future<void> updateFolder(Folder folder) {
|
|
|
|
log("Updating folder: " + folder.toString());
|
2020-05-17 20:39:21 +00:00
|
|
|
return _dio
|
|
|
|
.put(Configuration.instance.getHttpEndpoint() + "/folders/",
|
|
|
|
options: Options(
|
|
|
|
headers: {"X-Auth-Token": Configuration.instance.getToken()}),
|
|
|
|
data: folder.toMap())
|
2020-05-22 18:22:55 +00:00
|
|
|
.then((response) => log(response.toString()))
|
|
|
|
.catchError((error) => log(error.toString()));
|
2020-05-17 20:39:21 +00:00
|
|
|
}
|
2020-05-17 12:39:38 +00:00
|
|
|
}
|