99 lines
2.5 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/workspace.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pb.dart';
class UserBackendService {
UserBackendService({
required this.userId,
});
final String userId;
static Future<Either<UserProfilePB, FlowyError>> getCurrentUserProfile() {
return UserEventGetUserProfile().send();
}
Future<Either<Unit, FlowyError>> updateUserProfile({
String? name,
String? password,
String? email,
2022-08-08 22:19:05 +08:00
String? iconUrl,
String? openAIKey,
}) {
2022-07-19 14:40:56 +08:00
var payload = UpdateUserProfilePayloadPB.create()..id = userId;
if (name != null) {
payload.name = name;
}
if (password != null) {
payload.password = password;
}
if (email != null) {
payload.email = email;
}
2022-08-08 22:19:05 +08:00
if (iconUrl != null) {
payload.iconUrl = iconUrl;
2022-08-06 22:31:55 +08:00
}
if (openAIKey != null) {
payload.openaiKey = openAIKey;
}
return UserEventUpdateUserProfile(payload).send();
}
2022-08-08 22:19:05 +08:00
Future<Either<Unit, FlowyError>> deleteWorkspace(
{required String workspaceId}) {
throw UnimplementedError();
}
Future<Either<Unit, FlowyError>> signOut() {
return UserEventSignOut().send();
}
Future<Either<Unit, FlowyError>> initUser() async {
return UserEventInitUser().send();
}
2022-07-19 14:11:29 +08:00
Future<Either<List<WorkspacePB>, FlowyError>> getWorkspaces() {
final request = WorkspaceIdPB.create();
return FolderEventReadWorkspaces(request).send().then((result) {
return result.fold(
(workspaces) => left(workspaces.items),
(error) => right(error),
);
});
}
2022-07-19 14:11:29 +08:00
Future<Either<WorkspacePB, FlowyError>> openWorkspace(String workspaceId) {
final request = WorkspaceIdPB.create()..value = workspaceId;
return FolderEventOpenWorkspace(request).send().then((result) {
return result.fold(
(workspace) => left(workspace),
(error) => right(error),
);
});
}
2022-08-08 22:19:05 +08:00
Future<Either<WorkspacePB, FlowyError>> createWorkspace(
String name, String desc) {
2022-07-19 14:11:29 +08:00
final request = CreateWorkspacePayloadPB.create()
..name = name
..desc = desc;
return FolderEventCreateWorkspace(request).send().then((result) {
return result.fold(
(workspace) => left(workspace),
(error) => right(error),
);
});
}
}