2022-03-01 04:37:41 +00:00
|
|
|
import 'dart:async';
|
2022-07-03 08:52:06 +00:00
|
|
|
|
2024-06-12 15:08:55 +00:00
|
|
|
import 'package:appflowy/env/cloud_env.dart';
|
|
|
|
|
import 'package:appflowy/startup/startup.dart';
|
2023-01-08 04:10:53 +00:00
|
|
|
import 'package:appflowy_backend/dispatch/dispatch.dart';
|
|
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
2023-12-30 23:29:40 +00:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder/workspace.pb.dart';
|
2023-10-09 15:14:24 +00:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
|
2024-02-24 13:54:10 +00:00
|
|
|
import 'package:appflowy_result/appflowy_result.dart';
|
2023-04-04 00:41:16 +00:00
|
|
|
import 'package:fixnum/fixnum.dart';
|
2022-03-01 04:37:41 +00:00
|
|
|
|
2023-02-26 08:27:17 +00:00
|
|
|
class UserBackendService {
|
2024-06-12 15:08:55 +00:00
|
|
|
UserBackendService({required this.userId});
|
2023-02-16 02:17:08 +00:00
|
|
|
|
2023-04-04 00:41:16 +00:00
|
|
|
final Int64 userId;
|
2023-02-16 02:17:08 +00:00
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
static Future<FlowyResult<UserProfilePB, FlowyError>>
|
2023-05-16 06:58:24 +00:00
|
|
|
getCurrentUserProfile() async {
|
2023-09-21 04:41:52 +00:00
|
|
|
final result = await UserEventGetUserProfile().send();
|
2024-02-24 13:54:10 +00:00
|
|
|
return result;
|
2022-03-01 04:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
Future<FlowyResult<void, FlowyError>> updateUserProfile({
|
2022-07-03 08:52:06 +00:00
|
|
|
String? name,
|
|
|
|
|
String? password,
|
|
|
|
|
String? email,
|
2022-08-08 14:19:05 +00:00
|
|
|
String? iconUrl,
|
2023-02-14 02:04:36 +00:00
|
|
|
String? openAIKey,
|
2023-10-09 15:14:24 +00:00
|
|
|
String? stabilityAiKey,
|
2022-07-03 08:52:06 +00:00
|
|
|
}) {
|
2023-06-07 08:25:37 +00:00
|
|
|
final payload = UpdateUserProfilePayloadPB.create()..id = userId;
|
2022-07-03 08:52:06 +00:00
|
|
|
|
|
|
|
|
if (name != null) {
|
|
|
|
|
payload.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (password != null) {
|
|
|
|
|
payload.password = password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (email != null) {
|
|
|
|
|
payload.email = email;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 14:19:05 +00:00
|
|
|
if (iconUrl != null) {
|
|
|
|
|
payload.iconUrl = iconUrl;
|
2022-08-06 14:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 02:04:36 +00:00
|
|
|
if (openAIKey != null) {
|
|
|
|
|
payload.openaiKey = openAIKey;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 15:14:24 +00:00
|
|
|
if (stabilityAiKey != null) {
|
|
|
|
|
payload.stabilityAiKey = stabilityAiKey;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-03 08:52:06 +00:00
|
|
|
return UserEventUpdateUserProfile(payload).send();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
Future<FlowyResult<void, FlowyError>> deleteWorkspace({
|
2023-04-10 07:10:42 +00:00
|
|
|
required String workspaceId,
|
|
|
|
|
}) {
|
2022-03-01 04:37:41 +00:00
|
|
|
throw UnimplementedError();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 08:33:28 +00:00
|
|
|
static Future<FlowyResult<UserProfilePB, FlowyError>> signInWithMagicLink(
|
|
|
|
|
String email,
|
|
|
|
|
String redirectTo,
|
|
|
|
|
) async {
|
|
|
|
|
final payload = MagicLinkSignInPB(email: email, redirectTo: redirectTo);
|
|
|
|
|
return UserEventMagicLinkSignIn(payload).send();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
static Future<FlowyResult<void, FlowyError>> signOut() {
|
2023-07-14 05:37:13 +00:00
|
|
|
return UserEventSignOut().send();
|
2022-03-01 04:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
Future<FlowyResult<void, FlowyError>> initUser() async {
|
2022-03-01 04:37:41 +00:00
|
|
|
return UserEventInitUser().send();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
static Future<FlowyResult<UserProfilePB, FlowyError>> getAnonUser() async {
|
2023-12-21 06:13:21 +00:00
|
|
|
return UserEventGetAnonUser().send();
|
2023-08-07 14:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
static Future<FlowyResult<void, FlowyError>> openAnonUser() async {
|
2023-12-21 06:13:21 +00:00
|
|
|
return UserEventOpenAnonUser().send();
|
2023-08-07 14:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-04 02:43:00 +00:00
|
|
|
Future<FlowyResult<List<UserWorkspacePB>, FlowyError>> getWorkspaces() {
|
|
|
|
|
return UserEventGetAllWorkspace().send().then((value) {
|
|
|
|
|
return value.fold(
|
|
|
|
|
(workspaces) => FlowyResult.success(workspaces.items),
|
|
|
|
|
(error) => FlowyResult.failure(error),
|
|
|
|
|
);
|
|
|
|
|
});
|
2023-11-01 03:45:35 +00:00
|
|
|
}
|
2022-03-01 04:37:41 +00:00
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
Future<FlowyResult<void, FlowyError>> openWorkspace(String workspaceId) {
|
2023-11-05 06:00:24 +00:00
|
|
|
final payload = UserWorkspaceIdPB.create()..workspaceId = workspaceId;
|
|
|
|
|
return UserEventOpenWorkspace(payload).send();
|
2022-03-01 04:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
Future<FlowyResult<WorkspacePB, FlowyError>> getCurrentWorkspace() {
|
2023-11-01 03:45:35 +00:00
|
|
|
return FolderEventReadCurrentWorkspace().send().then((result) {
|
2022-03-01 04:37:41 +00:00
|
|
|
return result.fold(
|
2024-02-24 13:54:10 +00:00
|
|
|
(workspace) => FlowyResult.success(workspace),
|
|
|
|
|
(error) => FlowyResult.failure(error),
|
2022-03-01 04:37:41 +00:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:54:10 +00:00
|
|
|
Future<FlowyResult<WorkspacePB, FlowyError>> createWorkspace(
|
2023-04-10 07:10:42 +00:00
|
|
|
String name,
|
|
|
|
|
String desc,
|
|
|
|
|
) {
|
2022-07-19 06:11:29 +00:00
|
|
|
final request = CreateWorkspacePayloadPB.create()
|
2022-03-01 04:37:41 +00:00
|
|
|
..name = name
|
|
|
|
|
..desc = desc;
|
2024-03-13 07:07:52 +00:00
|
|
|
return FolderEventCreateFolderWorkspace(request).send().then((result) {
|
2022-03-01 04:37:41 +00:00
|
|
|
return result.fold(
|
2024-02-24 13:54:10 +00:00
|
|
|
(workspace) => FlowyResult.success(workspace),
|
|
|
|
|
(error) => FlowyResult.failure(error),
|
2022-03-01 04:37:41 +00:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-04 02:43:00 +00:00
|
|
|
|
|
|
|
|
Future<FlowyResult<UserWorkspacePB, FlowyError>> createUserWorkspace(
|
|
|
|
|
String name,
|
|
|
|
|
) {
|
|
|
|
|
final request = CreateWorkspacePB.create()..name = name;
|
|
|
|
|
return UserEventCreateWorkspace(request).send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<FlowyResult<void, FlowyError>> deleteWorkspaceById(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
) {
|
|
|
|
|
final request = UserWorkspaceIdPB.create()..workspaceId = workspaceId;
|
|
|
|
|
return UserEventDeleteWorkspace(request).send();
|
|
|
|
|
}
|
2024-03-05 05:51:03 +00:00
|
|
|
|
|
|
|
|
Future<FlowyResult<void, FlowyError>> renameWorkspace(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
String name,
|
|
|
|
|
) {
|
|
|
|
|
final request = RenameWorkspacePB()
|
|
|
|
|
..workspaceId = workspaceId
|
|
|
|
|
..newName = name;
|
|
|
|
|
return UserEventRenameWorkspace(request).send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<FlowyResult<void, FlowyError>> updateWorkspaceIcon(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
String icon,
|
|
|
|
|
) {
|
|
|
|
|
final request = ChangeWorkspaceIconPB()
|
|
|
|
|
..workspaceId = workspaceId
|
|
|
|
|
..newIcon = icon;
|
|
|
|
|
return UserEventChangeWorkspaceIcon(request).send();
|
|
|
|
|
}
|
2024-03-21 04:02:03 +00:00
|
|
|
|
|
|
|
|
Future<FlowyResult<RepeatedWorkspaceMemberPB, FlowyError>>
|
|
|
|
|
getWorkspaceMembers(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
) async {
|
|
|
|
|
final data = QueryWorkspacePB()..workspaceId = workspaceId;
|
|
|
|
|
return UserEventGetWorkspaceMember(data).send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<FlowyResult<void, FlowyError>> addWorkspaceMember(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
String email,
|
|
|
|
|
) async {
|
|
|
|
|
final data = AddWorkspaceMemberPB()
|
|
|
|
|
..workspaceId = workspaceId
|
|
|
|
|
..email = email;
|
|
|
|
|
return UserEventAddWorkspaceMember(data).send();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 13:38:46 +00:00
|
|
|
Future<FlowyResult<void, FlowyError>> inviteWorkspaceMember(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
String email, {
|
|
|
|
|
AFRolePB? role,
|
|
|
|
|
}) async {
|
|
|
|
|
final data = WorkspaceMemberInvitationPB()
|
|
|
|
|
..workspaceId = workspaceId
|
|
|
|
|
..inviteeEmail = email;
|
|
|
|
|
if (role != null) {
|
|
|
|
|
data.role = role;
|
|
|
|
|
}
|
|
|
|
|
return UserEventInviteWorkspaceMember(data).send();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 04:02:03 +00:00
|
|
|
Future<FlowyResult<void, FlowyError>> removeWorkspaceMember(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
String email,
|
|
|
|
|
) async {
|
|
|
|
|
final data = RemoveWorkspaceMemberPB()
|
|
|
|
|
..workspaceId = workspaceId
|
|
|
|
|
..email = email;
|
|
|
|
|
return UserEventRemoveWorkspaceMember(data).send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<FlowyResult<void, FlowyError>> updateWorkspaceMember(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
String email,
|
|
|
|
|
AFRolePB role,
|
|
|
|
|
) async {
|
|
|
|
|
final data = UpdateWorkspaceMemberPB()
|
|
|
|
|
..workspaceId = workspaceId
|
|
|
|
|
..email = email
|
|
|
|
|
..role = role;
|
|
|
|
|
return UserEventUpdateWorkspaceMember(data).send();
|
|
|
|
|
}
|
2024-03-29 11:01:43 +00:00
|
|
|
|
|
|
|
|
Future<FlowyResult<void, FlowyError>> leaveWorkspace(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
) async {
|
|
|
|
|
final data = UserWorkspaceIdPB.create()..workspaceId = workspaceId;
|
|
|
|
|
return UserEventLeaveWorkspace(data).send();
|
|
|
|
|
}
|
2024-06-12 15:08:55 +00:00
|
|
|
|
|
|
|
|
static Future<FlowyResult<RepeatedWorkspaceSubscriptionPB, FlowyError>>
|
|
|
|
|
getWorkspaceSubscriptions() {
|
|
|
|
|
return UserEventGetWorkspaceSubscriptions().send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Future<FlowyResult<PaymentLinkPB, FlowyError>> createSubscription(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
SubscriptionPlanPB plan,
|
|
|
|
|
) {
|
|
|
|
|
final request = SubscribeWorkspacePB()
|
|
|
|
|
..workspaceId = workspaceId
|
|
|
|
|
..recurringInterval = RecurringIntervalPB.Month
|
|
|
|
|
..workspaceSubscriptionPlan = plan
|
|
|
|
|
..successUrl =
|
|
|
|
|
'${getIt<AppFlowyCloudSharedEnv>().appflowyCloudConfig.base_url}/web/payment-success';
|
|
|
|
|
return UserEventSubscribeWorkspace(request).send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Future<FlowyResult<void, FlowyError>> cancelSubscription(
|
|
|
|
|
String workspaceId,
|
|
|
|
|
) {
|
|
|
|
|
final request = UserWorkspaceIdPB()..workspaceId = workspaceId;
|
|
|
|
|
return UserEventCancelWorkspaceSubscription(request).send();
|
|
|
|
|
}
|
2022-03-01 04:37:41 +00:00
|
|
|
}
|