Skip to content

Commit

Permalink
user created_time
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyuanxun committed Aug 13, 2024
1 parent 8cf8a82 commit f6509c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
34 changes: 32 additions & 2 deletions user/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ struct Profile {
location: String,
back_img_url: String,
avatar_url: String,
feed_canister: Option<Principal>
feed_canister: Option<Principal>,
created_at: Option<u64>
}

impl Storable for Profile {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions user/user.did
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f6509c1

Please sign in to comment.