-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
180 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
dfx identity use matt | ||
dfx canister call user_mgmt set_username '("Matt")' | ||
dfx canister call user_mgmt get_principal '("Matt")' | ||
dfx identity use hamish | ||
dfx canister call user_mgmt set_username '("Hamish")' | ||
dfx canister call user_mgmt get_principal '("Hamish")' | ||
dfx canister call chats create_chat '(principal "lpfg6-5goq7-vn6yz-wegiu-r6goc-sxoor-wj3b2-dpyup-k6i5m-qhv5m-xae", "Hi Matt, how is it going")' | ||
dfx canister call chats send_message '(45_292_552_032_833_753, "hello!!??")' | ||
dfx identity use matt | ||
dfx canister call chats list_chats | ||
dfx canister call chats get_messages '(45_292_552_032_833_753, 0)' | ||
dfx canister call chats send_message '(45_292_552_032_833_753, "whats up?")' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
type Message = | ||
type Message = | ||
record { | ||
id: nat64; | ||
timestamp: nat64; | ||
sent_by_me: bool; | ||
text: text; | ||
} | ||
}; | ||
|
||
type ChatSummary = | ||
record { | ||
id: nat64; | ||
them: principal; | ||
most_recent: Message; | ||
}; | ||
|
||
service : { | ||
create_chat: (principal) -> (opt nat64); | ||
send_message: (nat64, text) -> (bool); | ||
create_chat: (principal, text) -> (opt nat64); | ||
send_message: (nat64, text) -> (opt nat64); | ||
get_messages: (nat64, nat64) -> (opt vec Message) query; | ||
list_chats: () -> (vec ChatSummary) query; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
use ic_cdk_macros::*; | ||
use ic_types::Principal; | ||
use crate::domain::chat::Message; | ||
use crate::domain::chat::{ChatId, ChatSummary, Message}; | ||
use crate::queries::*; | ||
use crate::updates::*; | ||
use crate::domain::chat_list::ChatId; | ||
|
||
// #[query] | ||
// fn list_chats() -> Vec<ChatSummary> { | ||
// } | ||
#[query] | ||
fn list_chats() -> Vec<ChatSummary> { | ||
list_chats::query() | ||
} | ||
|
||
#[query] | ||
fn get_messages(chat_id: ChatId, from_index: usize) -> Option<Vec<Message>> { | ||
get_messages::query(chat_id, from_index) | ||
} | ||
|
||
#[update] | ||
fn create_chat(recipient: Principal) -> Option<ChatId> { | ||
create_chat::update(recipient) | ||
fn create_chat(recipient: Principal, text: String) -> Option<ChatId> { | ||
create_chat::update(recipient, text) | ||
} | ||
|
||
#[update] | ||
fn send_message(chat_id: ChatId, text: String) -> bool { | ||
fn send_message(chat_id: ChatId, text: String) -> Option<u64> { | ||
send_message::update(chat_id, text) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,78 @@ | ||
use std::collections::{HashMap, hash_map::Entry::{Occupied, Vacant}}; | ||
use ic_cdk::export::candid::CandidType; | ||
use ic_types::Principal; | ||
use highway::{HighwayHasher, HighwayHash}; | ||
use serde::Deserialize; | ||
use shared::StableState; | ||
use super::chat::Chat; | ||
use super::chat::{Chat, ChatId, ChatSummary}; | ||
|
||
#[derive(Default)] | ||
pub struct ChatList { | ||
chats: HashMap<ChatId, Chat> | ||
} | ||
|
||
impl ChatList { | ||
|
||
pub fn create(&mut self, sender: Principal, recipient: Principal) -> Option<ChatId> { | ||
|
||
pub fn create(&mut self, sender: Principal, recipient: Principal, text: String, timestamp: u64) -> Option<ChatId> { | ||
let chat_id = ChatId::new(&sender, &recipient); | ||
|
||
match self.chats.entry(chat_id) { | ||
Occupied(_) => None, | ||
Vacant(e) => { | ||
e.insert(Chat::new(sender, recipient)); | ||
e.insert(Chat::new(chat_id, sender, recipient, text, timestamp)); | ||
Some(chat_id) | ||
} | ||
} | ||
} | ||
|
||
pub fn get(&self, chat_id: ChatId, on_behalf_of: &Principal) -> Option<&Chat> { | ||
|
||
pub fn get(&self, chat_id: ChatId, me: &Principal) -> Option<&Chat> { | ||
let chat = self.chats.get(&chat_id)?; | ||
|
||
if !chat.involves_user(on_behalf_of) { | ||
if !chat.involves_user(me) { | ||
return None; | ||
} | ||
|
||
Some(chat) | ||
} | ||
|
||
pub fn get_mut(&mut self, chat_id: ChatId, on_behalf_of: &Principal) -> Option<&mut Chat> { | ||
|
||
pub fn get_mut(&mut self, chat_id: ChatId, me: &Principal) -> Option<&mut Chat> { | ||
let chat = self.chats.get_mut(&chat_id)?; | ||
|
||
if !chat.involves_user(on_behalf_of) { | ||
if !chat.involves_user(me) { | ||
return None; | ||
} | ||
|
||
Some(chat) | ||
} | ||
|
||
pub fn list_chats(&self, user: &Principal) -> Vec<ChatSummary> { | ||
// For now this will iterate through every chat... | ||
let mut list: Vec<_> = self | ||
.chats | ||
.values() | ||
.filter(|chat| chat.involves_user(user)) | ||
.map(|chat| chat.to_summary(user)) | ||
.collect(); | ||
|
||
list.sort_unstable_by(|c1, c2| { | ||
let t1 = c1.get_most_recent().get_timestamp(); | ||
let t2 = c2.get_most_recent().get_timestamp(); | ||
t2.cmp(&t1) | ||
}); | ||
|
||
list | ||
} | ||
} | ||
|
||
impl StableState for ChatList { | ||
type State = Vec<(ChatId, Chat)>; | ||
type State = Vec<Chat>; | ||
|
||
fn drain(self) -> Vec<(ChatId, Chat)> { | ||
fn drain(self) -> Vec<Chat> { | ||
self.chats | ||
.into_iter() | ||
.map(|(_, c)| c) | ||
.collect() | ||
} | ||
|
||
fn fill(chats: Vec<(ChatId, Chat)>) -> ChatList { | ||
fn fill(chats: Vec<Chat>) -> ChatList { | ||
let map: HashMap<ChatId, Chat> = chats | ||
.into_iter() | ||
.map(|c| (c.get_id(), c)) | ||
.collect(); | ||
|
||
ChatList { | ||
chats: map | ||
} | ||
} | ||
} | ||
|
||
/// TODO: We would preferably use a Uuid or u128 but these haven't yet got a CandidType implementation | ||
#[derive(CandidType, Deserialize, PartialEq, Eq, Hash, Copy, Clone)] | ||
pub struct ChatId(u64); | ||
|
||
impl ChatId { | ||
fn new(user1: &Principal, user2: &Principal) -> ChatId { | ||
let mut hasher = HighwayHasher::default(); | ||
|
||
if user1 < user2 { | ||
hasher.append(user1.as_slice()); | ||
hasher.append(user2.as_slice()); | ||
} else { | ||
hasher.append(user2.as_slice()); | ||
hasher.append(user1.as_slice()); | ||
} | ||
|
||
ChatId(hasher.finalize64()) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use ic_cdk::storage; | ||
use crate::domain::chat_list::ChatList; | ||
use crate::domain::chat::ChatSummary; | ||
|
||
pub fn query() -> Vec<ChatSummary> { | ||
let chat_list: &ChatList = storage::get(); | ||
let me = ic_cdk::caller(); | ||
|
||
chat_list.list_chats(&me) | ||
} |
Oops, something went wrong.