-
Notifications
You must be signed in to change notification settings - Fork 54
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
9 changed files
with
109 additions
and
50 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
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
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,56 +1,91 @@ | ||
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; | ||
|
||
#[derive(Default)] | ||
pub struct ChatList { | ||
chats: HashMap<(Principal, Principal), Chat> | ||
chats: HashMap<ChatId, Chat> | ||
} | ||
|
||
impl ChatList { | ||
pub fn get(&self, sender: Principal, recipient: Principal) -> Option<&Chat> { | ||
let (user1, user2) = ChatList::order_users(sender, recipient); | ||
|
||
self.chats.get(&(user1, user2)) | ||
pub fn create(&mut self, sender: Principal, recipient: Principal) -> 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)); | ||
Some(chat_id) | ||
} | ||
} | ||
} | ||
|
||
pub fn get_or_add_chat(&mut self, sender: Principal, recipient: Principal) -> &mut Chat { | ||
let (user1, user2) = ChatList::order_users(sender, recipient); | ||
pub fn get(&self, chat_id: ChatId, on_behalf_of: &Principal) -> Option<&Chat> { | ||
|
||
match self.chats.entry((user1.clone(), user2.clone())) { | ||
Occupied(e) => e.into_mut(), | ||
Vacant(e) => e.insert(Chat::new(user1, user2)) | ||
let chat = self.chats.get(&chat_id)?; | ||
|
||
if !chat.involves_user(on_behalf_of) { | ||
return None; | ||
} | ||
|
||
Some(chat) | ||
} | ||
|
||
fn order_users(user1: Principal, user2: Principal) -> (Principal, Principal) { | ||
if user1 < user2 { | ||
(user1, user2) | ||
} else { | ||
(user2, user1) | ||
pub fn get_mut(&mut self, chat_id: ChatId, on_behalf_of: &Principal) -> Option<&mut Chat> { | ||
|
||
let chat = self.chats.get_mut(&chat_id)?; | ||
|
||
if !chat.involves_user(on_behalf_of) { | ||
return None; | ||
} | ||
|
||
Some(chat) | ||
} | ||
} | ||
|
||
impl StableState for ChatList { | ||
type State = Vec<Chat>; | ||
type State = Vec<(ChatId, Chat)>; | ||
|
||
fn drain(self) -> Vec<Chat> { | ||
fn drain(self) -> Vec<(ChatId, Chat)> { | ||
self.chats | ||
.into_iter() | ||
.map(|(_, v)| v) | ||
.collect() | ||
} | ||
|
||
fn fill(chats: Vec<Chat>) -> ChatList { | ||
let map: HashMap<(Principal, Principal), Chat> = chats | ||
fn fill(chats: Vec<(ChatId, Chat)>) -> ChatList { | ||
let map: HashMap<ChatId, Chat> = chats | ||
.into_iter() | ||
.map(|c| ((c.get_key(), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
use ic_cdk::storage; | ||
use ic_types::Principal; | ||
use crate::domain::chat_list::ChatList; | ||
use crate::domain::chat_list::{ChatId, ChatList}; | ||
use crate::domain::chat::Message; | ||
|
||
pub fn query(from_user: Principal, from_index: usize) -> Vec<Message> { | ||
let me = ic_cdk::caller(); | ||
pub fn query(chat_id: ChatId, from_index: usize) -> Option<Vec<Message>> { | ||
|
||
let chat_list: &mut ChatList = storage::get_mut(); | ||
let me = ic_cdk::caller(); | ||
|
||
let chat = chat_list.get(chat_id, &me)?; | ||
|
||
match chat_list.get(from_user, me.clone()) { | ||
Some(c) => c.get_messages(&me, from_index), | ||
None => Vec::new() | ||
} | ||
Some(chat.get_messages(&me, from_index)) | ||
} |
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,11 @@ | ||
use ic_types::Principal; | ||
use crate::domain::chat_list::{ChatId, ChatList}; | ||
use ic_cdk::{storage}; | ||
|
||
pub fn update(recipient: Principal) -> Option<ChatId> { | ||
|
||
let chat_list: &mut ChatList = storage::get_mut(); | ||
let me = ic_cdk::caller(); | ||
|
||
chat_list.create(me, recipient) | ||
} |
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 +1,2 @@ | ||
pub mod send_message; | ||
pub mod send_message; | ||
pub mod create_chat; |
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,15 +1,16 @@ | ||
use crate::domain::chat_list::{ChatId, ChatList}; | ||
use ic_cdk::{api, storage}; | ||
use ic_types::Principal; | ||
use crate::domain::chat_list::ChatList; | ||
|
||
pub fn update(recipient: Principal, text: String) { | ||
let me = ic_cdk::caller(); | ||
pub fn update(chat_id: ChatId, text: String) -> bool { | ||
|
||
let chat_list: &mut ChatList = storage::get_mut(); | ||
let me = ic_cdk::caller(); | ||
|
||
let chat = chat_list.get_or_add_chat(me.clone(), recipient); | ||
|
||
let timestamp = api::time() as u64; | ||
if let Some(chat) = chat_list.get_mut(chat_id, &me) { | ||
let timestamp = api::time() as u64; | ||
chat.push_message(&me, text, timestamp); | ||
return true; | ||
} | ||
|
||
chat.push_message(&me, text, timestamp); | ||
false | ||
} |