Query and set seen status via store

This commit is contained in:
Joscha 2022-08-08 15:00:20 +02:00
parent 20ec6ef3b3
commit ff4118e21d
4 changed files with 48 additions and 1 deletions

View file

@ -94,6 +94,7 @@ pub struct SmallMessage {
pub time: Time,
pub nick: String,
pub content: String,
pub seen: bool,
}
fn as_me(content: &str) -> Option<&str> {
@ -144,6 +145,10 @@ impl Msg for SmallMessage {
self.parent
}
fn seen(&self) -> bool {
self.seen
}
fn last_possible_id() -> Self::Id {
Snowflake::MAX
}

View file

@ -31,6 +31,10 @@ impl Msg for LogMsg {
None
}
fn seen(&self) -> bool {
true
}
fn last_possible_id() -> Self::Id {
Self::Id::MAX
}
@ -118,6 +122,8 @@ impl MsgStore<LogMsg> for Logger {
async fn newer_msg_id(&self, id: &usize) -> Option<usize> {
self.next_tree_id(id).await
}
async fn set_seen(&self, _id: &usize, _seen: bool) {}
}
impl Log for Logger {

View file

@ -9,6 +9,7 @@ pub trait Msg {
type Id: Clone + Debug + Hash + Eq + Ord;
fn id(&self) -> Self::Id;
fn parent(&self) -> Option<Self::Id>;
fn seen(&self) -> bool;
fn last_possible_id() -> Self::Id;
}
@ -128,4 +129,5 @@ pub trait MsgStore<M: Msg> {
async fn newest_msg_id(&self) -> Option<M::Id>;
async fn older_msg_id(&self, id: &M::Id) -> Option<M::Id>;
async fn newer_msg_id(&self, id: &M::Id) -> Option<M::Id>;
async fn set_seen(&self, id: &M::Id, seen: bool);
}

View file

@ -258,6 +258,15 @@ impl MsgStore<SmallMessage> for EuphVault {
let _ = self.vault.tx.send(request.into());
rx.await.unwrap()
}
async fn set_seen(&self, id: &Snowflake, seen: bool) {
let request = EuphRequest::SetSeen {
room: self.room.clone(),
id: *id,
seen,
};
let _ = self.vault.tx.send(request.into());
}
}
pub(super) enum EuphRequest {
@ -350,6 +359,11 @@ pub(super) enum EuphRequest {
id: Snowflake,
result: oneshot::Sender<Option<Snowflake>>,
},
SetSeen {
room: String,
id: Snowflake,
seen: bool,
},
}
impl EuphRequest {
@ -399,6 +413,7 @@ impl EuphRequest {
EuphRequest::GetNewerMsgId { room, id, result } => {
Self::get_newer_msg_id(conn, room, id, result)
}
EuphRequest::SetSeen { room, id, seen } => Self::set_seen(conn, room, id, seen),
};
if let Err(e) = result {
// If an error occurs here, the rest of the UI will likely panic and
@ -778,7 +793,7 @@ impl EuphRequest {
ON tree.room = euph_msgs.room
AND tree.id = euph_msgs.parent
)
SELECT id, parent, time, name, content
SELECT id, parent, time, name, content, seen
FROM euph_msgs
JOIN tree USING (room, id)
ORDER BY id ASC
@ -791,6 +806,7 @@ impl EuphRequest {
time: row.get(2)?,
nick: row.get(3)?,
content: row.get(4)?,
seen: row.get(5)?,
})
})?
.collect::<rusqlite::Result<_>>()?;
@ -974,4 +990,22 @@ impl EuphRequest {
let _ = result.send(tree);
Ok(())
}
fn set_seen(
conn: &Connection,
room: String,
id: Snowflake,
seen: bool,
) -> rusqlite::Result<()> {
conn.execute(
"
UPDATE euph_msgs
SET seen = :seen
WHERE room = :room
AND id = :id
",
named_params! {":room": room, ":id": id, ":seen": seen},
)?;
Ok(())
}
}