kordophone-db: adds support for the Messages table
This commit is contained in:
40
kordophone-db/src/models/db/message.rs
Normal file
40
kordophone-db/src/models/db/message.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use diesel::prelude::*;
|
||||
use chrono::NaiveDateTime;
|
||||
use crate::models::{Message, Participant};
|
||||
|
||||
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)]
|
||||
#[diesel(table_name = crate::schema::messages)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Record {
|
||||
pub id: String,
|
||||
pub sender_participant_id: Option<i32>,
|
||||
pub text: String,
|
||||
pub date: NaiveDateTime,
|
||||
}
|
||||
|
||||
impl From<Message> for Record {
|
||||
fn from(message: Message) -> Self {
|
||||
Self {
|
||||
id: message.id,
|
||||
sender_participant_id: match message.sender {
|
||||
Participant::Me => None,
|
||||
Participant::Remote { id, .. } => id,
|
||||
},
|
||||
text: message.text,
|
||||
date: message.date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Record> for Message {
|
||||
fn from(record: Record) -> Self {
|
||||
Self {
|
||||
id: record.id,
|
||||
// We'll set the proper sender later when loading participant info
|
||||
sender: Participant::Me,
|
||||
text: record.text,
|
||||
date: record.date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod conversation;
|
||||
pub mod participant;
|
||||
pub mod message;
|
||||
@@ -6,19 +6,28 @@ use crate::schema::conversation_participants;
|
||||
#[diesel(table_name = crate::schema::participants)]
|
||||
pub struct Record {
|
||||
pub id: i32,
|
||||
pub display_name: String
|
||||
pub display_name: Option<String>,
|
||||
pub is_me: bool,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = crate::schema::participants)]
|
||||
pub struct InsertableRecord {
|
||||
pub display_name: String
|
||||
pub display_name: Option<String>,
|
||||
pub is_me: bool,
|
||||
}
|
||||
|
||||
impl From<Participant> for InsertableRecord {
|
||||
fn from(participant: Participant) -> Self {
|
||||
InsertableRecord {
|
||||
display_name: participant.display_name
|
||||
match participant {
|
||||
Participant::Me => InsertableRecord {
|
||||
display_name: None,
|
||||
is_me: true,
|
||||
},
|
||||
Participant::Remote { display_name, .. } => InsertableRecord {
|
||||
display_name: Some(display_name),
|
||||
is_me: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,17 +44,30 @@ pub struct ConversationParticipant {
|
||||
|
||||
impl From<Record> for Participant {
|
||||
fn from(record: Record) -> Self {
|
||||
Participant {
|
||||
display_name: record.display_name
|
||||
if record.is_me {
|
||||
Participant::Me
|
||||
} else {
|
||||
Participant::Remote {
|
||||
id: Some(record.id),
|
||||
display_name: record.display_name.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Participant> for Record {
|
||||
fn from(participant: Participant) -> Self {
|
||||
Record {
|
||||
id: 0, // This will be set by the database
|
||||
display_name: participant.display_name,
|
||||
match participant {
|
||||
Participant::Me => Record {
|
||||
id: 0, // This will be set by the database
|
||||
display_name: None,
|
||||
is_me: true,
|
||||
},
|
||||
Participant::Remote { display_name, .. } => Record {
|
||||
id: 0, // This will be set by the database
|
||||
display_name: Some(display_name),
|
||||
is_me: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
84
kordophone-db/src/models/message.rs
Normal file
84
kordophone-db/src/models/message.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
use chrono::{DateTime, NaiveDateTime};
|
||||
use uuid::Uuid;
|
||||
use crate::models::participant::Participant;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Message {
|
||||
pub id: String,
|
||||
pub sender: Participant,
|
||||
pub text: String,
|
||||
pub date: NaiveDateTime,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn builder() -> MessageBuilder {
|
||||
MessageBuilder::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<kordophone::model::Message> for Message {
|
||||
fn from(value: kordophone::model::Message) -> Self {
|
||||
Self {
|
||||
id: value.guid,
|
||||
sender: match value.sender {
|
||||
Some(sender) => Participant::Remote {
|
||||
id: None,
|
||||
display_name: sender,
|
||||
},
|
||||
None => Participant::Me,
|
||||
},
|
||||
text: value.text,
|
||||
date: DateTime::from_timestamp(
|
||||
value.date.unix_timestamp(),
|
||||
value.date.unix_timestamp_nanos()
|
||||
.try_into()
|
||||
.unwrap_or(0),
|
||||
)
|
||||
.unwrap()
|
||||
.naive_local()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MessageBuilder {
|
||||
id: Option<String>,
|
||||
sender: Option<Participant>,
|
||||
text: Option<String>,
|
||||
date: Option<NaiveDateTime>,
|
||||
}
|
||||
|
||||
impl MessageBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
id: None,
|
||||
sender: None,
|
||||
text: None,
|
||||
date: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sender(mut self, sender: Participant) -> Self {
|
||||
self.sender = Some(sender);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn text(mut self, text: String) -> Self {
|
||||
self.text = Some(text);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn date(mut self, date: NaiveDateTime) -> Self {
|
||||
self.date = Some(date);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Message {
|
||||
Message {
|
||||
id: self.id.unwrap_or_else(|| Uuid::new_v4().to_string()),
|
||||
sender: self.sender.unwrap_or(Participant::Me),
|
||||
text: self.text.unwrap_or_default(),
|
||||
date: self.date.unwrap_or_else(|| chrono::Utc::now().naive_utc()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
pub mod conversation;
|
||||
pub mod participant;
|
||||
pub mod message;
|
||||
pub mod db;
|
||||
|
||||
pub use conversation::Conversation;
|
||||
pub use participant::Participant;
|
||||
pub use participant::Participant;
|
||||
pub use message::Message;
|
||||
@@ -1,16 +1,35 @@
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Participant {
|
||||
pub display_name: String,
|
||||
pub enum Participant {
|
||||
Me,
|
||||
Remote {
|
||||
id: Option<i32>,
|
||||
display_name: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<String> for Participant {
|
||||
fn from(display_name: String) -> Self {
|
||||
Participant { display_name }
|
||||
Participant::Remote {
|
||||
id: None,
|
||||
display_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Participant {
|
||||
fn from(display_name: &str) -> Self {
|
||||
Participant { display_name: display_name.to_string() }
|
||||
Participant::Remote {
|
||||
id: None,
|
||||
display_name: display_name.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Participant {
|
||||
pub fn display_name(&self) -> String {
|
||||
match self {
|
||||
Participant::Me => "(Me)".to_string(),
|
||||
Participant::Remote { display_name, .. } => display_name.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user