Create room table for easier room deletion
This commit is contained in:
parent
54e5a7c97c
commit
6c26f62008
3 changed files with 61 additions and 5 deletions
|
|
@ -167,6 +167,7 @@ impl State {
|
||||||
}
|
}
|
||||||
Data::SnapshotEvent(d) => {
|
Data::SnapshotEvent(d) => {
|
||||||
info!("e&{}: successfully joined", self.name);
|
info!("e&{}: successfully joined", self.name);
|
||||||
|
self.vault.join();
|
||||||
self.last_msg_id = Some(d.log.last().map(|m| m.id));
|
self.last_msg_id = Some(d.log.last().map(|m| m.id));
|
||||||
self.vault.add_messages(d.log, None);
|
self.vault.add_messages(d.log, None);
|
||||||
let _ = self.ui_event_tx.send(UiEvent::Redraw);
|
let _ = self.ui_event_tx.send(UiEvent::Redraw);
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use tokio::sync::{mpsc, oneshot};
|
||||||
use crate::euph::api::{Message, Snowflake, Time};
|
use crate::euph::api::{Message, Snowflake, Time};
|
||||||
use crate::store::{Msg, MsgStore, Path, Tree};
|
use crate::store::{Msg, MsgStore, Path, Tree};
|
||||||
|
|
||||||
use super::Request;
|
use super::{Request, Vault};
|
||||||
|
|
||||||
impl ToSql for Snowflake {
|
impl ToSql for Snowflake {
|
||||||
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
|
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
|
||||||
|
|
@ -76,6 +76,16 @@ impl From<EuphRequest> for Request {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Vault {
|
||||||
|
pub async fn euph_rooms(&self) -> Vec<String> {
|
||||||
|
// TODO vault::Error
|
||||||
|
let (tx, rx) = oneshot::channel();
|
||||||
|
let request = EuphRequest::Rooms { result: tx };
|
||||||
|
let _ = self.tx.send(request.into());
|
||||||
|
rx.await.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct EuphVault {
|
pub struct EuphVault {
|
||||||
pub(super) tx: mpsc::UnboundedSender<Request>,
|
pub(super) tx: mpsc::UnboundedSender<Request>,
|
||||||
|
|
@ -83,6 +93,13 @@ pub struct EuphVault {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EuphVault {
|
impl EuphVault {
|
||||||
|
pub fn join(&self) {
|
||||||
|
let request = EuphRequest::Join {
|
||||||
|
room: self.room.clone(),
|
||||||
|
};
|
||||||
|
let _ = self.tx.send(request.into());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_message(&self, msg: Message, prev_msg: Option<Snowflake>) {
|
pub fn add_message(&self, msg: Message, prev_msg: Option<Snowflake>) {
|
||||||
let request = EuphRequest::AddMsg {
|
let request = EuphRequest::AddMsg {
|
||||||
room: self.room.clone(),
|
room: self.room.clone(),
|
||||||
|
|
@ -187,6 +204,12 @@ impl MsgStore<EuphMsg> for EuphVault {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) enum EuphRequest {
|
pub(super) enum EuphRequest {
|
||||||
|
Rooms {
|
||||||
|
result: oneshot::Sender<Vec<String>>,
|
||||||
|
},
|
||||||
|
Join {
|
||||||
|
room: String,
|
||||||
|
},
|
||||||
AddMsg {
|
AddMsg {
|
||||||
room: String,
|
room: String,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
|
|
@ -234,6 +257,8 @@ pub(super) enum EuphRequest {
|
||||||
impl EuphRequest {
|
impl EuphRequest {
|
||||||
pub(super) fn perform(self, conn: &mut Connection) {
|
pub(super) fn perform(self, conn: &mut Connection) {
|
||||||
let result = match self {
|
let result = match self {
|
||||||
|
EuphRequest::Rooms { result } => Self::rooms(conn, result),
|
||||||
|
EuphRequest::Join { room } => Self::join(conn, room),
|
||||||
EuphRequest::AddMsg {
|
EuphRequest::AddMsg {
|
||||||
room,
|
room,
|
||||||
msg,
|
msg,
|
||||||
|
|
@ -265,6 +290,31 @@ impl EuphRequest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn rooms(conn: &mut Connection, result: oneshot::Sender<Vec<String>>) -> rusqlite::Result<()> {
|
||||||
|
let rooms = conn
|
||||||
|
.prepare(
|
||||||
|
"
|
||||||
|
SELECT room
|
||||||
|
FROM euph_rooms
|
||||||
|
",
|
||||||
|
)?
|
||||||
|
.query_map([], |row| row.get(0))?
|
||||||
|
.collect::<rusqlite::Result<_>>()?;
|
||||||
|
let _ = result.send(rooms);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn join(conn: &mut Connection, room: String) -> rusqlite::Result<()> {
|
||||||
|
conn.execute(
|
||||||
|
"
|
||||||
|
INSERT INTO euph_rooms (room)
|
||||||
|
VALUES (?)
|
||||||
|
",
|
||||||
|
[room],
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn insert_msgs(tx: &Transaction, room: &str, msgs: Vec<Message>) -> rusqlite::Result<()> {
|
fn insert_msgs(tx: &Transaction, room: &str, msgs: Vec<Message>) -> rusqlite::Result<()> {
|
||||||
let mut insert_msg = tx.prepare(
|
let mut insert_msg = tx.prepare(
|
||||||
"
|
"
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,10 @@ const MIGRATIONS: [fn(&mut Transaction) -> rusqlite::Result<()>; 1] = [m1];
|
||||||
fn m1(tx: &mut Transaction) -> rusqlite::Result<()> {
|
fn m1(tx: &mut Transaction) -> rusqlite::Result<()> {
|
||||||
tx.execute_batch(
|
tx.execute_batch(
|
||||||
"
|
"
|
||||||
|
CREATE TABLE euph_rooms (
|
||||||
|
room TEXT NOT NULL PRIMARY KEY
|
||||||
|
) STRICT;
|
||||||
|
|
||||||
CREATE TABLE euph_msgs (
|
CREATE TABLE euph_msgs (
|
||||||
-- Message
|
-- Message
|
||||||
room TEXT NOT NULL,
|
room TEXT NOT NULL,
|
||||||
|
|
@ -45,7 +49,9 @@ fn m1(tx: &mut Transaction) -> rusqlite::Result<()> {
|
||||||
client_address TEXT,
|
client_address TEXT,
|
||||||
real_client_address TEXT,
|
real_client_address TEXT,
|
||||||
|
|
||||||
PRIMARY KEY (room, id)
|
PRIMARY KEY (room, id),
|
||||||
|
FOREIGN KEY (room) REFERENCES euph_rooms (room)
|
||||||
|
ON DELETE CASCADE
|
||||||
) STRICT;
|
) STRICT;
|
||||||
|
|
||||||
CREATE TABLE euph_spans (
|
CREATE TABLE euph_spans (
|
||||||
|
|
@ -54,9 +60,8 @@ fn m1(tx: &mut Transaction) -> rusqlite::Result<()> {
|
||||||
end INT,
|
end INT,
|
||||||
|
|
||||||
UNIQUE (room, start, end),
|
UNIQUE (room, start, end),
|
||||||
FOREIGN KEY (room, start) REFERENCES euph_msgs (room, id),
|
FOREIGN KEY (room) REFERENCES euph_rooms (room)
|
||||||
FOREIGN KEY (room, end) REFERENCES euph_msgs (room, id),
|
ON DELETE CASCADE,
|
||||||
|
|
||||||
CHECK (start IS NULL OR end IS NOT NULL)
|
CHECK (start IS NULL OR end IS NOT NULL)
|
||||||
) STRICT;
|
) STRICT;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue