aboutsummaryrefslogtreecommitdiffstats
path: root/filamento/src/chat.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@bunny.garden>2025-04-03 03:41:38 +0100
committerLibravatar cel 🌸 <cel@bunny.garden>2025-04-03 03:41:38 +0100
commit91f1994af940085d5d475a97820900ebbf0eb553 (patch)
tree6aab872f71d17a785d3d9286742fef38983d274c /filamento/src/chat.rs
parent9ce3827a7d25714d17f266f0f50bb29f41090175 (diff)
downloadluz-91f1994af940085d5d475a97820900ebbf0eb553.tar.gz
luz-91f1994af940085d5d475a97820900ebbf0eb553.tar.bz2
luz-91f1994af940085d5d475a97820900ebbf0eb553.zip
feat: better message handling, pep publish, xep_0172: nick
Diffstat (limited to 'filamento/src/chat.rs')
-rw-r--r--filamento/src/chat.rs64
1 files changed, 62 insertions, 2 deletions
diff --git a/filamento/src/chat.rs b/filamento/src/chat.rs
index c1194ea..147c7f7 100644
--- a/filamento/src/chat.rs
+++ b/filamento/src/chat.rs
@@ -1,5 +1,6 @@
use chrono::{DateTime, Utc};
use jid::JID;
+use sqlx::Sqlite;
use uuid::Uuid;
#[derive(Debug, sqlx::FromRow, Clone)]
@@ -7,7 +8,9 @@ pub struct Message {
pub id: Uuid,
// does not contain full user information
#[sqlx(rename = "from_jid")]
+ // bare jid (for now)
pub from: JID,
+ pub delivery: Option<Delivery>,
pub timestamp: DateTime<Utc>,
// TODO: originally_from
// TODO: message edits
@@ -16,6 +19,59 @@ pub struct Message {
pub body: Body,
}
+#[derive(Debug, Clone, Copy)]
+pub enum Delivery {
+ Sending,
+ Written,
+ Sent,
+ Delivered,
+ Read,
+ Failed,
+ Queued,
+}
+
+impl sqlx::Type<Sqlite> for Delivery {
+ fn type_info() -> <Sqlite as sqlx::Database>::TypeInfo {
+ <&str as sqlx::Type<Sqlite>>::type_info()
+ }
+}
+
+impl sqlx::Decode<'_, Sqlite> for Delivery {
+ fn decode(
+ value: <Sqlite as sqlx::Database>::ValueRef<'_>,
+ ) -> Result<Self, sqlx::error::BoxDynError> {
+ let value = <&str as sqlx::Decode<Sqlite>>::decode(value)?;
+ match value {
+ "sending" => Ok(Self::Sending),
+ "written" => Ok(Self::Written),
+ "sent" => Ok(Self::Sent),
+ "delivered" => Ok(Self::Delivered),
+ "read" => Ok(Self::Read),
+ "failed" => Ok(Self::Failed),
+ "queued" => Ok(Self::Queued),
+ _ => unreachable!(),
+ }
+ }
+}
+
+impl sqlx::Encode<'_, Sqlite> for Delivery {
+ fn encode_by_ref(
+ &self,
+ buf: &mut <Sqlite as sqlx::Database>::ArgumentBuffer<'_>,
+ ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
+ let value = match self {
+ Delivery::Sending => "sending",
+ Delivery::Written => "written",
+ Delivery::Sent => "sent",
+ Delivery::Delivered => "delivered",
+ Delivery::Read => "read",
+ Delivery::Failed => "failed",
+ Delivery::Queued => "queued",
+ };
+ <&str as sqlx::Encode<Sqlite>>::encode(value, buf)
+ }
+}
+
// TODO: user migrations
// pub enum Migrated {
// Jabber(User),
@@ -31,6 +87,7 @@ pub struct Body {
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct Chat {
pub correspondent: JID,
+ pub have_chatted: bool,
// pub unread_messages: i32,
// pub latest_message: Message,
// when a new message is received, the chat should be updated, and the new message should be delivered too.
@@ -41,8 +98,11 @@ pub struct Chat {
pub enum ChatUpdate {}
impl Chat {
- pub fn new(correspondent: JID) -> Self {
- Self { correspondent }
+ pub fn new(correspondent: JID, have_chatted: bool) -> Self {
+ Self {
+ correspondent,
+ have_chatted,
+ }
}
pub fn correspondent(&self) -> &JID {
&self.correspondent