plub through attachment guids via messages
This commit is contained in:
@@ -13,6 +13,7 @@ diesel_migrations = { version = "2.2.0", features = ["sqlite"] }
|
||||
kordophone = { path = "../kordophone" }
|
||||
log = "0.4.27"
|
||||
serde = { version = "1.0.215", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
time = "0.3.37"
|
||||
tokio = "1.44.2"
|
||||
uuid = { version = "1.11.0", features = ["v4"] }
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Remove attachment_metadata column from messages table
|
||||
ALTER TABLE messages DROP COLUMN attachment_metadata;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Add attachment_metadata column to messages table
|
||||
ALTER TABLE messages ADD COLUMN attachment_metadata TEXT;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Remove file_transfer_guids column from messages table
|
||||
ALTER TABLE messages DROP COLUMN file_transfer_guids;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Add file_transfer_guids column to messages table
|
||||
ALTER TABLE messages ADD COLUMN file_transfer_guids TEXT;
|
||||
@@ -10,10 +10,21 @@ pub struct Record {
|
||||
pub sender_participant_id: Option<i32>,
|
||||
pub text: String,
|
||||
pub date: NaiveDateTime,
|
||||
pub file_transfer_guids: Option<String>, // JSON array
|
||||
pub attachment_metadata: Option<String>, // JSON string
|
||||
}
|
||||
|
||||
impl From<Message> for Record {
|
||||
fn from(message: Message) -> Self {
|
||||
let file_transfer_guids = if message.file_transfer_guids.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(serde_json::to_string(&message.file_transfer_guids).unwrap_or_default())
|
||||
};
|
||||
|
||||
let attachment_metadata = message.attachment_metadata
|
||||
.map(|metadata| serde_json::to_string(&metadata).unwrap_or_default());
|
||||
|
||||
Self {
|
||||
id: message.id,
|
||||
sender_participant_id: match message.sender {
|
||||
@@ -22,18 +33,29 @@ impl From<Message> for Record {
|
||||
},
|
||||
text: message.text,
|
||||
date: message.date,
|
||||
file_transfer_guids,
|
||||
attachment_metadata,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Record> for Message {
|
||||
fn from(record: Record) -> Self {
|
||||
let file_transfer_guids = record.file_transfer_guids
|
||||
.and_then(|json| serde_json::from_str(&json).ok())
|
||||
.unwrap_or_default();
|
||||
|
||||
let attachment_metadata = record.attachment_metadata
|
||||
.and_then(|json| serde_json::from_str(&json).ok());
|
||||
|
||||
Self {
|
||||
id: record.id,
|
||||
// We'll set the proper sender later when loading participant info
|
||||
sender: Participant::Me,
|
||||
text: record.text,
|
||||
date: record.date,
|
||||
file_transfer_guids,
|
||||
attachment_metadata,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use chrono::{DateTime, NaiveDateTime};
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
use crate::models::participant::Participant;
|
||||
use kordophone::model::outgoing_message::OutgoingMessage;
|
||||
use kordophone::model::message::AttachmentMetadata;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Message {
|
||||
@@ -9,6 +11,8 @@ pub struct Message {
|
||||
pub sender: Participant,
|
||||
pub text: String,
|
||||
pub date: NaiveDateTime,
|
||||
pub file_transfer_guids: Vec<String>,
|
||||
pub attachment_metadata: Option<HashMap<String, AttachmentMetadata>>,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
@@ -36,7 +40,9 @@ impl From<kordophone::model::Message> for Message {
|
||||
.unwrap_or(0),
|
||||
)
|
||||
.unwrap()
|
||||
.naive_local()
|
||||
.naive_local(),
|
||||
file_transfer_guids: value.file_transfer_guids,
|
||||
attachment_metadata: value.attachment_metadata,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +54,8 @@ impl From<&OutgoingMessage> for Message {
|
||||
sender: Participant::Me,
|
||||
text: value.text.clone(),
|
||||
date: value.date,
|
||||
file_transfer_guids: Vec::new(), // Outgoing messages don't have file transfer GUIDs initially
|
||||
attachment_metadata: None, // Outgoing messages don't have attachment metadata initially
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,6 +65,8 @@ pub struct MessageBuilder {
|
||||
sender: Option<Participant>,
|
||||
text: Option<String>,
|
||||
date: Option<NaiveDateTime>,
|
||||
file_transfer_guids: Option<Vec<String>>,
|
||||
attachment_metadata: Option<HashMap<String, AttachmentMetadata>>,
|
||||
}
|
||||
|
||||
impl Default for MessageBuilder {
|
||||
@@ -72,6 +82,8 @@ impl MessageBuilder {
|
||||
sender: None,
|
||||
text: None,
|
||||
date: None,
|
||||
file_transfer_guids: None,
|
||||
attachment_metadata: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,12 +102,24 @@ impl MessageBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn file_transfer_guids(mut self, file_transfer_guids: Vec<String>) -> Self {
|
||||
self.file_transfer_guids = Some(file_transfer_guids);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn attachment_metadata(mut self, attachment_metadata: HashMap<String, AttachmentMetadata>) -> Self {
|
||||
self.attachment_metadata = Some(attachment_metadata);
|
||||
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()),
|
||||
file_transfer_guids: self.file_transfer_guids.unwrap_or_default(),
|
||||
attachment_metadata: self.attachment_metadata,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,9 @@ diesel::table! {
|
||||
id -> Text, // guid
|
||||
text -> Text,
|
||||
sender_participant_id -> Nullable<Integer>,
|
||||
date -> Timestamp,
|
||||
date -> Timestamp,
|
||||
file_transfer_guids -> Nullable<Text>, // JSON array of file transfer GUIDs
|
||||
attachment_metadata -> Nullable<Text>, // JSON string of attachment metadata
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user