mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-12-06 14:53:15 +01:00
💥: now incompatible with Twitter
This commit is contained in:
135
src/state.rs
135
src/state.rs
@@ -5,11 +5,9 @@ use std::error::Error;
|
||||
|
||||
/// Struct for each query line
|
||||
#[derive(Debug)]
|
||||
pub struct TootTweetRecord {
|
||||
pub struct TootRecord {
|
||||
// Mastodon part
|
||||
pub toot_id: u64,
|
||||
// Twitter part
|
||||
pub tweet_id: u64,
|
||||
// Bluesky part
|
||||
pub record_uri: String,
|
||||
pub root_record_uri: String,
|
||||
@@ -20,44 +18,36 @@ pub struct TootTweetRecord {
|
||||
pub fn delete_state(conn: &Connection, toot_id: u64) -> Result<(), Box<dyn Error>> {
|
||||
debug!("Deleting Toot ID {}", toot_id);
|
||||
conn.execute(
|
||||
&format!("DELETE FROM toot_tweet_record WHERE toot_id = {}", toot_id),
|
||||
&format!("DELETE FROM toot_record WHERE toot_id = {}", toot_id),
|
||||
[],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Retrieves all tweets associated to a toot in the form of a vector
|
||||
pub fn read_all_state(
|
||||
conn: &Connection,
|
||||
toot_id: u64,
|
||||
) -> Result<(Vec<u64>, Vec<String>), Box<dyn Error>> {
|
||||
pub fn read_all_state(conn: &Connection, toot_id: u64) -> Result<Vec<String>, Box<dyn Error>> {
|
||||
let query = format!(
|
||||
"SELECT tweet_id, record_uri FROM toot_tweet_record WHERE toot_id = {};",
|
||||
"SELECT record_uri FROM toot_record WHERE toot_id = {};",
|
||||
toot_id
|
||||
);
|
||||
let mut stmt = conn.prepare(&query)?;
|
||||
let mut rows = stmt.query([])?;
|
||||
|
||||
let mut tweet_v: Vec<u64> = Vec::new();
|
||||
let mut record_v: Vec<String> = Vec::new();
|
||||
while let Some(row) = rows.next()? {
|
||||
tweet_v.push(row.get(0)?);
|
||||
record_v.push(row.get(1)?);
|
||||
record_v.push(row.get(0)?);
|
||||
}
|
||||
|
||||
Ok((tweet_v, record_v))
|
||||
Ok(record_v)
|
||||
}
|
||||
|
||||
/// if None is passed, read the last tweet from DB
|
||||
/// if a tweet_id is passed, read this particular tweet from DB
|
||||
pub fn read_state(
|
||||
conn: &Connection,
|
||||
s: Option<u64>,
|
||||
) -> Result<Option<TootTweetRecord>, Box<dyn Error>> {
|
||||
pub fn read_state(conn: &Connection, s: Option<u64>) -> Result<Option<TootRecord>, Box<dyn Error>> {
|
||||
debug!("Reading toot_id {:?}", s);
|
||||
let begin_query = "SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_tweet_record";
|
||||
let begin_query = "SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_record";
|
||||
let query: String = match s {
|
||||
Some(i) => format!("{begin_query} WHERE toot_id = {i} ORDER BY tweet_id DESC LIMIT 1"),
|
||||
Some(i) => format!("{begin_query} WHERE toot_id = {i} ORDER BY record_uri DESC LIMIT 1"),
|
||||
None => format!("{begin_query} ORDER BY toot_id DESC LIMIT 1"),
|
||||
};
|
||||
|
||||
@@ -65,9 +55,8 @@ pub fn read_state(
|
||||
|
||||
let t = stmt
|
||||
.query_row([], |row| {
|
||||
Ok(TootTweetRecord {
|
||||
Ok(TootRecord {
|
||||
toot_id: row.get("toot_id")?,
|
||||
tweet_id: row.get("tweet_id")?,
|
||||
record_uri: row.get("record_uri")?,
|
||||
root_record_uri: row.get("root_record_uri")?,
|
||||
datetime: Some(
|
||||
@@ -81,11 +70,11 @@ pub fn read_state(
|
||||
}
|
||||
|
||||
/// Writes last treated tweet id and toot id to the db
|
||||
pub fn write_state(conn: &Connection, t: TootTweetRecord) -> Result<(), Box<dyn Error>> {
|
||||
pub fn write_state(conn: &Connection, t: TootRecord) -> Result<(), Box<dyn Error>> {
|
||||
debug!("Write struct {:?}", t);
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri, root_record_uri) VALUES (?1, ?2, ?3, ?4)",
|
||||
params![t.toot_id, t.tweet_id, t.record_uri, t.root_record_uri],
|
||||
"INSERT INTO toot_record (toot_id, record_uri, root_record_uri) VALUES (?1, ?2, ?3)",
|
||||
params![t.toot_id, t.record_uri, t.root_record_uri],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
@@ -100,10 +89,9 @@ pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> {
|
||||
let conn = Connection::open(d)?;
|
||||
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS toot_tweet_record (
|
||||
"CREATE TABLE IF NOT EXISTS toot_record (
|
||||
toot_id INTEGER,
|
||||
tweet_id INTEGER PRIMARY KEY,
|
||||
record_uri VARCHAR(128) DEFAULT '',
|
||||
record_uri VARCHAR(128) PRIMARY KEY,
|
||||
root_record_uri VARCHAR(128) DEFAULT '',
|
||||
datetime INTEGER DEFAULT CURRENT_TIMESTAMP
|
||||
)",
|
||||
@@ -113,20 +101,20 @@ pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Migrate DB from 1.6+ to 3+
|
||||
/// Migrate DB from 3+ to 4+
|
||||
pub fn migrate_db(d: &str) -> Result<(), Box<dyn Error>> {
|
||||
debug!("Migration DB for Oolatoocs");
|
||||
|
||||
let conn = Connection::open(d)?;
|
||||
|
||||
let res = conn.execute("SELECT datetime FROM toot_tweet_record;", []);
|
||||
let res = conn.execute("SELECT datetime FROM toot_record;", []);
|
||||
|
||||
// If the column can be selected then, it’s OK
|
||||
// if not, see if the error is a missing column and add it
|
||||
match res {
|
||||
Err(e) => match e.to_string().as_str() {
|
||||
"no such table: toot_tweet_record" => migrate_db_alter_table(&conn), // table does not exist
|
||||
"Execute returned results - did you mean to call query?" => Ok(()), // return results,
|
||||
"no such table: toot_record" => migrate_db_alter_table(&conn), // table does not exist
|
||||
"Execute returned results - did you mean to call query?" => Ok(()), // return results,
|
||||
// column does
|
||||
// exist
|
||||
_ => Err(e.into()),
|
||||
@@ -139,10 +127,9 @@ pub fn migrate_db(d: &str) -> Result<(), Box<dyn Error>> {
|
||||
fn migrate_db_alter_table(c: &Connection) -> Result<(), Box<dyn Error>> {
|
||||
// create the new table
|
||||
c.execute(
|
||||
"CREATE TABLE IF NOT EXISTS toot_tweet_record (
|
||||
"CREATE TABLE IF NOT EXISTS toot_record (
|
||||
toot_id INTEGER,
|
||||
tweet_id INTEGER PRIMARY KEY,
|
||||
record_uri VARCHAR(128) DEFAULT '',
|
||||
record_uri VARCHAR(128) PRIMARY KEY,
|
||||
root_record_uri VARCHAR(128) DEFAULT '',
|
||||
datetime INTEGER DEFAULT CURRENT_TIMESTAMP
|
||||
)",
|
||||
@@ -151,13 +138,14 @@ fn migrate_db_alter_table(c: &Connection) -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// copy data from the old table
|
||||
c.execute(
|
||||
"INSERT INTO toot_tweet_record (toot_id, tweet_id, datetime)
|
||||
SELECT toot_id, tweet_id, datetime FROM tweet_to_toot;",
|
||||
"INSERT INTO toot_record (toot_id, record_uri, root_record_uri, datetime)
|
||||
SELECT toot_id, record_uri, root_record_uri, datetime FROM toot_tweet_record
|
||||
WHERE record_uri != '';",
|
||||
[],
|
||||
)?;
|
||||
|
||||
// drop the old table
|
||||
c.execute("DROP TABLE IF EXISTS tweet_to_toot;", [])?;
|
||||
c.execute("DROP TABLE IF EXISTS toot_tweet_record;", [])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -178,8 +166,7 @@ mod tests {
|
||||
|
||||
// open said file
|
||||
let conn = Connection::open(d).unwrap();
|
||||
conn.execute("SELECT * from toot_tweet_record;", [])
|
||||
.unwrap();
|
||||
conn.execute("SELECT * from toot_record;", []).unwrap();
|
||||
|
||||
remove_file(d).unwrap();
|
||||
}
|
||||
@@ -194,9 +181,9 @@ mod tests {
|
||||
let conn = Connection::open(d).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record (tweet_id, toot_id)
|
||||
"INSERT INTO toot_record (record_uri, toot_id)
|
||||
VALUES
|
||||
(100, 1001);",
|
||||
('a', 1001);",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
@@ -214,9 +201,8 @@ mod tests {
|
||||
|
||||
let conn = Connection::open(d).unwrap();
|
||||
|
||||
let t_in = TootTweetRecord {
|
||||
let t_in = TootRecord {
|
||||
toot_id: 987654321,
|
||||
tweet_id: 123456789,
|
||||
record_uri: "a".to_string(),
|
||||
root_record_uri: "c".to_string(),
|
||||
datetime: None,
|
||||
@@ -225,14 +211,13 @@ mod tests {
|
||||
write_state(&conn, t_in).unwrap();
|
||||
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_tweet_record;")
|
||||
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_record;")
|
||||
.unwrap();
|
||||
|
||||
let t_out = stmt
|
||||
.query_row([], |row| {
|
||||
Ok(TootTweetRecord {
|
||||
Ok(TootRecord {
|
||||
toot_id: row.get("toot_id").unwrap(),
|
||||
tweet_id: row.get("tweet_id").unwrap(),
|
||||
record_uri: row.get("record_uri").unwrap(),
|
||||
root_record_uri: row.get("root_record_uri").unwrap(),
|
||||
datetime: Some(
|
||||
@@ -243,7 +228,6 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(t_out.toot_id, 987654321);
|
||||
assert_eq!(t_out.tweet_id, 123456789);
|
||||
assert_eq!(t_out.record_uri, "a".to_string());
|
||||
assert_eq!(t_out.root_record_uri, "c".to_string());
|
||||
|
||||
@@ -259,10 +243,10 @@ mod tests {
|
||||
let conn = Connection::open(d).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri)
|
||||
"INSERT INTO toot_record (toot_id, record_uri)
|
||||
VALUES
|
||||
(101, 1001, 'abc'),
|
||||
(102, 1002, 'def');",
|
||||
(101, 'abc'),
|
||||
(102, 'def');",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
@@ -272,7 +256,6 @@ mod tests {
|
||||
remove_file(d).unwrap();
|
||||
|
||||
assert_eq!(t_out.toot_id, 102);
|
||||
assert_eq!(t_out.tweet_id, 1002);
|
||||
assert_eq!(t_out.record_uri, "def".to_string());
|
||||
}
|
||||
|
||||
@@ -300,9 +283,9 @@ mod tests {
|
||||
let conn = Connection::open(d).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri)
|
||||
"INSERT INTO toot_record (toot_id, record_uri)
|
||||
VALUES
|
||||
(100, 1000, 'abc');",
|
||||
(100, 'abc');",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
@@ -323,9 +306,9 @@ mod tests {
|
||||
let conn = Connection::open(d).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri)
|
||||
"INSERT INTO toot_record (toot_id, record_uri)
|
||||
VALUES
|
||||
(100, 1000, 'abc');",
|
||||
(100, 'abc');",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
@@ -335,7 +318,6 @@ mod tests {
|
||||
remove_file(d).unwrap();
|
||||
|
||||
assert_eq!(t_out.toot_id, 100);
|
||||
assert_eq!(t_out.tweet_id, 1000);
|
||||
assert_eq!(t_out.record_uri, "abc".to_string());
|
||||
}
|
||||
|
||||
@@ -348,10 +330,10 @@ mod tests {
|
||||
let conn = Connection::open(d).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri)
|
||||
"INSERT INTO toot_record (toot_id, record_uri)
|
||||
VALUES
|
||||
(1000, 100, 'abc'),
|
||||
(1000, 101, 'def');",
|
||||
(1000, 'abc'),
|
||||
(1000, 'def');",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
@@ -361,7 +343,6 @@ mod tests {
|
||||
remove_file(d).unwrap();
|
||||
|
||||
assert_eq!(t_out.toot_id, 1000);
|
||||
assert_eq!(t_out.tweet_id, 101);
|
||||
assert_eq!(t_out.record_uri, "def".to_string());
|
||||
}
|
||||
|
||||
@@ -372,9 +353,11 @@ mod tests {
|
||||
|
||||
let conn = Connection::open(d).unwrap();
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS tweet_to_toot (
|
||||
tweet_id INTEGER,
|
||||
toot_id INTEGER PRIMARY KEY,
|
||||
"CREATE TABLE IF NOT EXISTS toot_tweet_record (
|
||||
toot_id INTEGER,
|
||||
tweet_id INTEGER PRIMARY KEY,
|
||||
record_uri VARCHAR(128) DEFAULT '',
|
||||
root_record_uri VARCHAR(128) DEFAULT '',
|
||||
datetime INTEGER DEFAULT CURRENT_TIMESTAMP
|
||||
)",
|
||||
[],
|
||||
@@ -382,7 +365,7 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO tweet_to_toot (tweet_id, toot_id) VALUES (0, 0), (1, 1);",
|
||||
"INSERT INTO toot_tweet_record (tweet_id, toot_id, record_uri) VALUES (0, 0, ''), (1, 1, 'abc');",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
@@ -391,7 +374,6 @@ mod tests {
|
||||
|
||||
let last_state = read_state(&conn, None).unwrap().unwrap();
|
||||
|
||||
assert_eq!(last_state.tweet_id, 1);
|
||||
assert_eq!(last_state.toot_id, 1);
|
||||
|
||||
migrate_db(d).unwrap(); // shouldn’t do anything
|
||||
@@ -408,7 +390,7 @@ mod tests {
|
||||
let conn = Connection::open(d).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record(toot_id, tweet_id, record_uri) VALUES (0, 0, 'abc');",
|
||||
"INSERT INTO toot_record(toot_id, record_uri) VALUES (0, 'abc');",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
@@ -416,13 +398,12 @@ mod tests {
|
||||
delete_state(&conn, 0).unwrap();
|
||||
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_tweet_record;")
|
||||
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_record;")
|
||||
.unwrap();
|
||||
|
||||
let t_out = stmt.query_row([], |row| {
|
||||
Ok(TootTweetRecord {
|
||||
Ok(TootRecord {
|
||||
toot_id: row.get("toot_id").unwrap(),
|
||||
tweet_id: row.get("tweet_id").unwrap(),
|
||||
record_uri: row.get("record_uri").unwrap(),
|
||||
root_record_uri: row.get("root_record_uri").unwrap(),
|
||||
datetime: Some(
|
||||
@@ -434,7 +415,7 @@ mod tests {
|
||||
assert!(t_out.is_err_and(|x| x == rusqlite::Error::QueryReturnedNoRows));
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record(toot_id, tweet_id, record_uri) VALUES(42, 102, 'abc'), (42, 103, 'def');",
|
||||
"INSERT INTO toot_record(toot_id, record_uri) VALUES(42, 'abc'), (42, 'def');",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
@@ -442,13 +423,12 @@ mod tests {
|
||||
delete_state(&conn, 42).unwrap();
|
||||
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_tweet_record;")
|
||||
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_record;")
|
||||
.unwrap();
|
||||
|
||||
let t_out = stmt.query_row([], |row| {
|
||||
Ok(TootTweetRecord {
|
||||
Ok(TootRecord {
|
||||
toot_id: row.get("toot_id").unwrap(),
|
||||
tweet_id: row.get("tweet_id").unwrap(),
|
||||
record_uri: row.get("record_uri").unwrap(),
|
||||
root_record_uri: row.get("root_record_uri").unwrap(),
|
||||
datetime: Some(
|
||||
@@ -471,16 +451,13 @@ mod tests {
|
||||
let conn = Connection::open(d).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri) VALUES (42, 102, 'abc'), (42, 103, 'def'), (43, 105, 'ghi');",
|
||||
"INSERT INTO toot_record (toot_id, record_uri) VALUES (42, 'abc'), (42, 'def'), (43, 'ghi');",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let (tweet_v1, record_v1) = read_all_state(&conn, 43).unwrap();
|
||||
let (tweet_v2, record_v2) = read_all_state(&conn, 42).unwrap();
|
||||
|
||||
assert_eq!(tweet_v1, vec![105]);
|
||||
assert_eq!(tweet_v2, vec![102, 103]);
|
||||
let record_v1 = read_all_state(&conn, 43).unwrap();
|
||||
let record_v2 = read_all_state(&conn, 42).unwrap();
|
||||
|
||||
assert_eq!(record_v1, vec!["ghi".to_string()]);
|
||||
assert_eq!(record_v2, vec!["abc".to_string(), "def".to_string()]);
|
||||
|
||||
Reference in New Issue
Block a user