Skip to content

Commit

Permalink
Minor refactor to move "type" definitions to top of files
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored and hpeebles committed Dec 31, 2020
1 parent 01c2145 commit 4c6d6fa
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 96 deletions.
56 changes: 28 additions & 28 deletions src/chats/src/domain/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ pub trait Chat {
#[derive(CandidType, Deserialize, PartialEq, Eq, Hash, Copy, Clone)]
pub struct ChatId(u64);

#[derive(CandidType, Deserialize, Clone)]
pub struct Message {
id: u32,
timestamp: Timestamp,
sender: UserId,
text: String
}

#[derive(CandidType)]
pub enum ChatSummary {
Direct(DirectChatSummary),
Group(GroupChatSummary)
}

impl ChatId {

pub fn for_group_chat(creator: &UserId, timestamp: Timestamp) -> ChatId {
Expand All @@ -55,33 +69,6 @@ impl ChatId {
}
}

#[derive(CandidType)]
pub enum ChatSummary {
Direct(DirectChatSummary),
Group(GroupChatSummary)
}

impl ChatSummary {
// Date bumped by:
// 1 - New message from any user
// 2 - Group created with 'me' in it
// 3 - 'me' added to existing group
pub fn get_updated_date(&self) -> Timestamp {
match self {
ChatSummary::Direct(summary) => summary.get_updated_date(),
ChatSummary::Group(summary) => summary.get_updated_date()
}
}
}

#[derive(CandidType, Deserialize, Clone)]
pub struct Message {
id: u32,
timestamp: Timestamp,
sender: UserId,
text: String
}

impl Message {
pub fn new(id: u32, now: Timestamp, sender: UserId, text: String) -> Message {
Message {
Expand All @@ -99,4 +86,17 @@ impl Message {
pub fn get_id(&self) -> u32 {
self.id
}
}
}

impl ChatSummary {
// Date bumped by:
// 1 - New message from any user
// 2 - Group created with 'me' in it
// 3 - 'me' added to existing group
pub fn get_updated_date(&self) -> Timestamp {
match self {
ChatSummary::Direct(summary) => summary.get_updated_date(),
ChatSummary::Group(summary) => summary.get_updated_date()
}
}
}
16 changes: 8 additions & 8 deletions src/chats/src/domain/direct_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ pub struct DirectChat {
messages: Vec<Message>
}

#[derive(CandidType)]
pub struct DirectChatSummary {
id: ChatId,
them: UserId,
unread: u32,
latest_message: Message
}

impl DirectChat {
pub fn new(id: ChatId, sender: UserId, recipient: UserId, text: String, now: Timestamp) -> DirectChat {

Expand Down Expand Up @@ -121,14 +129,6 @@ impl Chat for DirectChat {
}
}

#[derive(CandidType)]
pub struct DirectChatSummary {
id: ChatId,
them: UserId,
unread: u32,
latest_message: Message
}

impl DirectChatSummary {
fn new(chat: &DirectChat, me: &UserId) -> DirectChatSummary {
let is_user1 = *me == chat.user1;
Expand Down
53 changes: 27 additions & 26 deletions src/chats/src/domain/group_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ pub struct GroupChat {
messages: Vec<Message>
}

#[derive(CandidType, Deserialize)]
struct Participant {
user_id: UserId,
admin: bool,
latest_read: u32,
date_added: Timestamp
}

#[derive(CandidType)]
pub struct GroupChatSummary {
id: ChatId,
subject: String,
updated_date: Timestamp,
participants: Vec<UserId>,
unread: u32,
latest_message: Option<Message>
}

impl GroupChat {
pub fn new(
id: ChatId,
Expand Down Expand Up @@ -172,14 +190,15 @@ impl Chat for GroupChat {
}
}

#[derive(CandidType)]
pub struct GroupChatSummary {
id: ChatId,
subject: String,
updated_date: Timestamp,
participants: Vec<UserId>,
unread: u32,
latest_message: Option<Message>
impl Participant {
fn new(user_id: UserId, admin: bool, now: Timestamp) -> Participant {
Participant {
user_id,
admin,
latest_read: 0,
date_added: now
}
}
}

impl GroupChatSummary {
Expand Down Expand Up @@ -215,21 +234,3 @@ impl GroupChatSummary {
}
}

#[derive(CandidType, Deserialize)]
struct Participant {
user_id: UserId,
admin: bool,
latest_read: u32,
date_added: Timestamp
}

impl Participant {
fn new(user_id: UserId, admin: bool, now: Timestamp) -> Participant {
Participant {
user_id,
admin,
latest_read: 0,
date_added: now
}
}
}
2 changes: 1 addition & 1 deletion src/chats/src/queries/get_direct_messages.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ic_cdk::storage;
use shared::user_id::UserId;
use crate::domain::chat_list::ChatList;
use crate::domain::chat::{Chat, ChatId, Message};
use shared::user_id::UserId;

pub fn query(user_id: UserId, from_id: u32, page_size: u32) -> Option<Vec<Message>> {
let chat_list: &ChatList = storage::get();
Expand Down
2 changes: 1 addition & 1 deletion src/chats/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod get_direct_messages;
pub mod get_messages;
pub mod list_chats;
pub mod get_direct_messages;
2 changes: 1 addition & 1 deletion src/chats/src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pub mod add_participants;
pub mod create_group_chat;
pub mod mark_read;
pub mod remove_participant;
pub mod send_message;
pub mod send_direct_message;
pub mod send_message;
62 changes: 31 additions & 31 deletions src/user_mgmt/src/domain/user_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ pub struct UserStore {
data: MultiMap<UserId, String, User>
}

#[derive(CandidType, Deserialize, Debug)]
pub struct User {
id: UserId,
username: String,
joined: Timestamp,
last_updated: Timestamp,
version: u32
}

#[derive(CandidType)]
pub struct UserSummary {
id: UserId,
username: String,
version: u32
}

#[derive(CandidType)]
pub enum RegisterUserResult {
Success(UserSummary),
UserExists,
UsernameTaken
}

#[derive(CandidType)]
pub enum UpdateUsernameResult {
Success,
SuccessNoChange,
UsernameTaken,
UserNotFound
}

impl UserStore {
pub fn register_user(&mut self, user_id: UserId, username: String, now: Timestamp) -> RegisterUserResult {
if self.data.contains_key(&user_id) { return RegisterUserResult::UserExists; }
Expand Down Expand Up @@ -94,22 +125,6 @@ impl StableState for UserStore {
}
}

#[derive(CandidType, Deserialize, Debug)]
pub struct User {
id: UserId,
username: String,
joined: Timestamp,
last_updated: Timestamp,
version: u32
}

#[derive(CandidType)]
pub struct UserSummary {
id: UserId,
username: String,
version: u32
}

impl UserSummary {
fn new(user: &User) -> UserSummary {
UserSummary {
Expand All @@ -119,18 +134,3 @@ impl UserSummary {
}
}
}

#[derive(CandidType)]
pub enum RegisterUserResult {
Success(UserSummary),
UserExists,
UsernameTaken
}

#[derive(CandidType)]
pub enum UpdateUsernameResult {
Success,
SuccessNoChange,
UsernameTaken,
UserNotFound
}

0 comments on commit 4c6d6fa

Please sign in to comment.