chore: fix historical user open issue (#3106)

This commit is contained in:
Nathan.fooo
2023-08-03 12:07:42 +08:00
committed by GitHub
parent 03518c951c
commit 135d8a811e
2 changed files with 28 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'; import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart'; import 'package:appflowy_backend/dispatch/dispatch.dart';
@ -16,7 +17,10 @@ class UserBackendService {
static Future<Either<FlowyError, UserProfilePB>> static Future<Either<FlowyError, UserProfilePB>>
getCurrentUserProfile() async { getCurrentUserProfile() async {
final result = await UserEventGetUserProfile().send(); final result = await UserEventGetUserProfile().send().then((value) {
value.fold((l) => null, (r) => Log.error(r));
return value;
});
return result.swap(); return result.swap();
} }

View File

@ -6,6 +6,7 @@ use chrono::prelude::*;
use serde::de::{Deserializer, MapAccess, Visitor}; use serde::de::{Deserializer, MapAccess, Visitor};
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use serde_json::Value;
use flowy_user_deps::entities::{SignInResponse, UserWorkspace}; use flowy_user_deps::entities::{SignInResponse, UserWorkspace};
@ -44,7 +45,9 @@ impl<'de> Visitor<'de> for SessionVisitor {
"user_workspace" => { "user_workspace" => {
user_workspace = Some(map.next_value()?); user_workspace = Some(map.next_value()?);
}, },
_ => {}, _ => {
let _ = map.next_value::<Value>();
},
} }
} }
let user_id = user_id.ok_or(serde::de::Error::missing_field("user_id"))?; let user_id = user_id.ok_or(serde::de::Error::missing_field("user_id"))?;
@ -102,23 +105,40 @@ impl std::convert::From<Session> for String {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use serde_json::json;
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
struct OldSession { struct OldSession {
user_id: i64, user_id: i64,
workspace_id: String, workspace_id: String,
name: String,
} }
#[test] #[test]
fn deserialize_user_workspace_from_workspace_id() { fn deserialize_user_workspace_from_workspace_id() {
// For historical reasons, the session used to contain a workspace_id field. // For historical reasons, the session used to contain a workspace_id field.
let old = OldSession { let old = OldSession {
user_id: 1, user_id: 223238635422486528,
workspace_id: uuid::Uuid::new_v4().to_string(), workspace_id: "f58f5492-ee0a-4a9f-8cf1-dacb459a55f6".to_string(),
name: "Me".to_string(),
}; };
let s = serde_json::to_string(&old).unwrap(); let s = serde_json::to_string(&old).unwrap();
let new = serde_json::from_str::<Session>(&s).unwrap(); let new = serde_json::from_str::<Session>(&s).unwrap();
assert_eq!(old.user_id, new.user_id); assert_eq!(old.user_id, new.user_id);
assert_eq!(old.workspace_id, new.user_workspace.id); assert_eq!(old.workspace_id, new.user_workspace.id);
let json = json!({
"user_id": 2232386,
"workspace_id": "f58f5492-ee0a-4a9f-8cf1-dacb459a55f6",
"name": "Me",
"token": null,
"email": "0085bfda-85fa-4611-bfbe-25d5a1229f44@appflowy.io"
});
let new = serde_json::from_value::<Session>(json).unwrap();
assert_eq!(new.user_id, 2232386);
assert_eq!(
new.user_workspace.id,
"f58f5492-ee0a-4a9f-8cf1-dacb459a55f6"
);
} }
} }