Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split set_username into register_user and update_username #7

Merged
merged 2 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ dfx canister call chats send_message '(45_292_552_032_833_753, "whats up?")'
-- Group Chat
dfx identity use matt
dfx canister call user_mgmt get_principal '("Hamish")'
dfx canister call chats create_group_chat '(vec {principal "uhpjy-crxbe-6chrq-vadba-aqrmz-f4d2e-h7nxj-7hrso-ebvqe-gnygw-tae"}, "Xmas 2020")'
dfx canister call chats create_group_chat '(vec { principal "uhpjy-crxbe-6chrq-vadba-aqrmz-f4d2e-h7nxj-7hrso-ebvqe-gnygw-tae" }, "Xmas 2020")'
dfx canister call chats send_message '(12_699_180_841_646_933_661, "Are you coming to mine for xmas?")'

-- Get Users
dfx canister call user_mgmt get_users '(vec { record { id = principal "uhpjy-crxbe-6chrq-vadba-aqrmz-f4d2e-h7nxj-7hrso-ebvqe-gnygw-tae"; cached_version = opt 1 } })'
2 changes: 1 addition & 1 deletion src/chats/src/domain/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use ic_types::Principal;
use enum_dispatch::enum_dispatch;
use highway::{HighwayHasher, HighwayHash};
use serde::Deserialize;
use shared::timestamp::Timestamp;
use crate::domain::direct_chat::{DirectChat, DirectChatSummary};
use crate::domain::group_chat::{GroupChat, GroupChatSummary};
use crate::Timestamp;

#[enum_dispatch(Chat)]
#[derive(CandidType, Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions src/chats/src/domain/chat_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, hash_map::Entry::{Occupied, Vacant}};
use ic_types::Principal;
use shared::StableState;
use crate::Timestamp;
use shared::timestamp::Timestamp;
use shared::upgrade::StableState;
use super::chat::{Chat, ChatEnum, ChatId, ChatSummary};
use super::direct_chat::DirectChat;
use super::group_chat::GroupChat;
Expand Down
2 changes: 1 addition & 1 deletion src/chats/src/domain/direct_chat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ic_cdk::export::candid::CandidType;
use ic_types::Principal;
use serde::Deserialize;
use crate::Timestamp;
use shared::timestamp::Timestamp;
use super::chat::*;

#[derive(CandidType, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/chats/src/domain/group_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::max;
use ic_cdk::export::candid::CandidType;
use ic_types::Principal;
use serde::Deserialize;
use crate::Timestamp;
use shared::timestamp::Timestamp;
use super::chat::*;

#[derive(CandidType, Deserialize)]
Expand Down
8 changes: 1 addition & 7 deletions src/chats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@ mod api;
mod domain;
mod queries;
mod updates;
mod upgrade;

pub(crate) fn get_current_timestamp() -> Timestamp {
ic_cdk::api::time() as Timestamp
}

pub type Timestamp = u64;
mod upgrade;
3 changes: 2 additions & 1 deletion src/chats/src/updates/create_direct_chat.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use ic_cdk::storage;
use ic_types::Principal;
use shared::timestamp;
use crate::domain::chat::ChatId;
use crate::domain::chat_list::ChatList;

pub fn update(recipient: Principal, text: String) -> Option<ChatId> {
let chat_list: &mut ChatList = storage::get_mut();
let me = ic_cdk::caller();
let now = crate::get_current_timestamp();
let now = timestamp::now();

chat_list.create_direct_chat(me, recipient, text, now)
}
3 changes: 2 additions & 1 deletion src/chats/src/updates/create_group_chat.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use ic_cdk::storage;
use ic_types::Principal;
use shared::timestamp;
use crate::domain::chat::ChatId;
use crate::domain::chat_list::ChatList;

pub fn update(participants: Vec<Principal>, subject: String) -> Option<ChatId> {
let chat_list: &mut ChatList = storage::get_mut();
let me = ic_cdk::caller();
let now = crate::get_current_timestamp();
let now = timestamp::now();

chat_list.create_group_chat(me, participants, subject, now)
}
3 changes: 2 additions & 1 deletion src/chats/src/updates/send_message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ic_cdk::storage;
use shared::timestamp;
use crate::domain::chat::{Chat, ChatId};
use crate::domain::chat_list::ChatList;

Expand All @@ -7,7 +8,7 @@ pub fn update(chat_id: ChatId, text: String) -> Option<u32> {
let me = ic_cdk::caller();

if let Some(chat) = chat_list.get_mut(chat_id, &me) {
let now = crate::get_current_timestamp();
let now = timestamp::now();
let message_id = chat.push_message(&me, text, now);
return Some(message_id);
}
Expand Down
5 changes: 3 additions & 2 deletions src/chats/src/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use ic_cdk_macros::*;
use shared::upgrade;
use crate::domain::chat_list::ChatList;

#[pre_upgrade]
fn pre_upgrade() {
shared::pre_upgrade::<ChatList>();
upgrade::pre_upgrade::<ChatList>();
}

#[post_upgrade]
fn post_upgrade() {
shared::post_upgrade::<ChatList>();
upgrade::post_upgrade::<ChatList>();
}
32 changes: 2 additions & 30 deletions src/shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,2 @@
use std::mem;
use ic_cdk::export::candid::CandidType;
use ic_cdk::storage;
use serde::de::DeserializeOwned;

pub fn pre_upgrade<T: StableState>() {
let from_storage: &mut T = storage::get_mut();

let val = mem::take(from_storage);

let to_save = val.drain();

storage::stable_save((to_save,)).unwrap();
}

pub fn post_upgrade<T: StableState>() {
let (saved,) = storage::stable_restore().unwrap();

let val = T::fill(saved);

let from_storage: &mut T = storage::get_mut();

*from_storage = val;
}

pub trait StableState: 'static + Default {
type State: CandidType + DeserializeOwned;
fn drain(self) -> Self::State;
fn fill(source: Self::State) -> Self;
}
pub mod timestamp;
pub mod upgrade;
5 changes: 5 additions & 0 deletions src/shared/src/timestamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub type Timestamp = u64;

pub fn now() -> Timestamp {
ic_cdk::api::time() as Timestamp
}
30 changes: 30 additions & 0 deletions src/shared/src/upgrade.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::mem;
use ic_cdk::export::candid::CandidType;
use ic_cdk::storage;
use serde::de::DeserializeOwned;

pub fn pre_upgrade<T: StableState>() {
let from_storage: &mut T = storage::get_mut();

let val = mem::take(from_storage);

let to_save = val.drain();

storage::stable_save((to_save,)).unwrap();
}

pub fn post_upgrade<T: StableState>() {
let (saved,) = storage::stable_restore().unwrap();

let val = T::fill(saved);

let from_storage: &mut T = storage::get_mut();

*from_storage = val;
}

pub trait StableState: 'static + Default {
type State: CandidType + DeserializeOwned;
fn drain(self) -> Self::State;
fn fill(source: Self::State) -> Self;
}
2 changes: 1 addition & 1 deletion src/user_mgmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ crate-type = ["cdylib"]
ic-cdk = "0.2.0"
ic-cdk-macros = "0.2.0"
ic-types = "0.1.1"
bimap = "0.5.3"
multi-map = "1.3.0"
serde = "1.0.111"
shared = { path = "../shared", version = "0.1.0" }
13 changes: 12 additions & 1 deletion src/user_mgmt/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use ic_cdk_macros::*;
use ic_types::Principal;
use crate::domain::user_store::{RegisterUserResult, UpdateUsernameResult, UserSummary};
use crate::queries::*;
use crate::updates::*;

#[update]
pub fn set_username(username: String) -> bool {
pub fn register_user(username: String) -> RegisterUserResult {
register_user::update(username)
}

#[update]
pub fn update_username(username: String) -> UpdateUsernameResult {
set_username::update(username)
}

Expand All @@ -16,4 +22,9 @@ pub fn get_username() -> Option<String> {
#[query]
pub fn get_principal(username: String) -> Option<Principal> {
get_principal::query(&username)
}

#[query]
pub fn get_users(users: Vec<get_users::GetUserRequest>) -> Vec<UserSummary> {
get_users::query(users)
}
2 changes: 1 addition & 1 deletion src/user_mgmt/src/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod user_data;
pub mod user_store;
56 changes: 0 additions & 56 deletions src/user_mgmt/src/domain/user_data.rs

This file was deleted.

Loading