From f6509c18543c52685401c31487ce571cd6d059db Mon Sep 17 00:00:00 2001 From: xiaoyuanxun <2060363684@qq.com> Date: Tue, 13 Aug 2024 11:21:06 +0800 Subject: [PATCH] user created_time --- user/src/lib.rs | 34 ++++++++++++++++++++++++++++++++-- user/user.did | 1 + 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/user/src/lib.rs b/user/src/lib.rs index d771631..006d280 100644 --- a/user/src/lib.rs +++ b/user/src/lib.rs @@ -18,7 +18,8 @@ struct Profile { location: String, back_img_url: String, avatar_url: String, - feed_canister: Option + feed_canister: Option, + created_at: Option } impl Storable for Profile { @@ -56,6 +57,34 @@ thread_local! { ); } +#[ic_cdk::post_upgrade] +fn post_upgrade() { + let entries = PROFILE_MAP.with(|map| { + let entries: Vec<(Principal, Profile)> = map.borrow().iter().collect(); + entries + }); + + let now = ic_cdk::api::time(); + + for (_, profile) in entries { + let new_profile = Profile { + id: profile.id, + handle: profile.handle, + name: profile.name, + biography: profile.biography, + website: profile.website, + location: profile.location, + back_img_url: profile.back_img_url, + avatar_url: profile.avatar_url, + feed_canister: profile.feed_canister, + created_at: Some(now) + }; + PROFILE_MAP.with(|map| { + map.borrow_mut().insert(profile.id, new_profile) + }); + } +} + #[ic_cdk::update] fn follow(to: Principal) { let from = ic_cdk::caller(); @@ -220,7 +249,8 @@ fn update_handle(new_handle: String) -> bool { location: old_profile.location, back_img_url: old_profile.back_img_url, avatar_url: old_profile.avatar_url, - feed_canister: old_profile.feed_canister + feed_canister: old_profile.feed_canister, + created_at: old_profile.created_at }; HANDLE_MAP.with(|map| map.borrow_mut().remove(&old_profile.handle)); diff --git a/user/user.did b/user/user.did index 319fa2d..12e283e 100644 --- a/user/user.did +++ b/user/user.did @@ -22,6 +22,7 @@ type Profile = record { avatar_url : text; name : text; biography : text; + created_at : opt nat64; website : text; feed_canister : opt principal; handle : text;