: add bluesky support

This commit is contained in:
VC
2024-09-26 16:32:39 +02:00
parent f7e2aafa7b
commit ac8af5ce95
8 changed files with 1090 additions and 245 deletions

View File

@@ -5,9 +5,14 @@ use std::error::Error;
/// Struct for each query line
#[derive(Debug)]
pub struct TweetToToot {
pub tweet_id: u64,
pub struct TootTweetRecord {
// Mastodon part
pub toot_id: u64,
// Twitter part
pub tweet_id: u64,
// Bluesky part
pub record_uri: String,
pub root_record_uri: String,
pub datetime: Option<DateTime<Utc>>,
}
@@ -15,27 +20,32 @@ pub struct TweetToToot {
pub fn delete_state(conn: &Connection, toot_id: u64) -> Result<(), Box<dyn Error>> {
debug!("Deleting Toot ID {}", toot_id);
conn.execute(
&format!("DELETE FROM tweet_to_toot WHERE toot_id = {}", toot_id),
&format!("DELETE FROM toot_tweet_record WHERE toot_id = {}", toot_id),
[],
)?;
Ok(())
}
/// Retrieves all tweets associated to a toot in the form of a vector
pub fn read_all_tweet_state(conn: &Connection, toot_id: u64) -> Result<Vec<u64>, Box<dyn Error>> {
pub fn read_all_state(
conn: &Connection,
toot_id: u64,
) -> Result<(Vec<u64>, Vec<String>), Box<dyn Error>> {
let query = format!(
"SELECT tweet_id FROM tweet_to_toot WHERE toot_id = {};",
"SELECT tweet_id, record_uri FROM toot_tweet_record WHERE toot_id = {};",
toot_id
);
let mut stmt = conn.prepare(&query)?;
let mut rows = stmt.query([])?;
let mut v = Vec::new();
let mut tweet_v: Vec<u64> = Vec::new();
let mut record_v: Vec<String> = Vec::new();
while let Some(row) = rows.next()? {
v.push(row.get(0)?);
tweet_v.push(row.get(0)?);
record_v.push(row.get(1)?);
}
Ok(v)
Ok((tweet_v, record_v))
}
/// if None is passed, read the last tweet from DB
@@ -43,23 +53,26 @@ pub fn read_all_tweet_state(conn: &Connection, toot_id: u64) -> Result<Vec<u64>,
pub fn read_state(
conn: &Connection,
s: Option<u64>,
) -> Result<Option<TweetToToot>, Box<dyn Error>> {
) -> Result<Option<TootTweetRecord>, Box<dyn Error>> {
debug!("Reading toot_id {:?}", s);
let begin_query = "SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_tweet_record";
let query: String = match s {
Some(i) => format!(
"SELECT tweet_id, toot_id, UNIXEPOCH(datetime) AS datetime FROM tweet_to_toot WHERE toot_id = {i} ORDER BY tweet_id DESC LIMIT 1"
),
None => "SELECT tweet_id, toot_id, UNIXEPOCH(datetime) AS datetime FROM tweet_to_toot ORDER BY toot_id DESC LIMIT 1".to_string(),
Some(i) => format!("{begin_query} WHERE toot_id = {i} ORDER BY tweet_id DESC LIMIT 1"),
None => format!("{begin_query} ORDER BY toot_id DESC LIMIT 1"),
};
let mut stmt = conn.prepare(&query)?;
let t = stmt
.query_row([], |row| {
Ok(TweetToToot {
tweet_id: row.get("tweet_id")?,
Ok(TootTweetRecord {
toot_id: row.get("toot_id")?,
datetime: Some(DateTime::from_timestamp(row.get("datetime").unwrap(), 0).unwrap()),
tweet_id: row.get("tweet_id")?,
record_uri: row.get("record_uri")?,
root_record_uri: row.get("root_record_uri")?,
datetime: Some(
DateTime::from_timestamp(row.get("unix_datetime").unwrap(), 0).unwrap(),
),
})
})
.optional()?;
@@ -68,11 +81,11 @@ pub fn read_state(
}
/// Writes last treated tweet id and toot id to the db
pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box<dyn Error>> {
pub fn write_state(conn: &Connection, t: TootTweetRecord) -> Result<(), Box<dyn Error>> {
debug!("Write struct {:?}", t);
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id) VALUES (?1, ?2)",
params![t.tweet_id, t.toot_id],
"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],
)?;
Ok(())
@@ -87,9 +100,11 @@ pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> {
let conn = Connection::open(d)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS tweet_to_toot (
tweet_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
)",
[],
@@ -98,20 +113,20 @@ pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> {
Ok(())
}
/// Migrate DB from 1.5.x to 1.6.x
/// Migrate DB from 1.6+ to 3+
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 tweet_to_toot;", []);
let res = conn.execute("SELECT datetime FROM toot_tweet_record;", []);
// If the column can be selected then, its 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 column: datetime" => migrate_db_alter_table(&conn), //column does not exist
"Execute returned results - did you mean to call query?" => Ok(()), // return results,
"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,
// column does
// exist
_ => Err(e.into()),
@@ -124,9 +139,11 @@ 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 tweet_to_toot_new (
tweet_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
)",
[],
@@ -134,16 +151,13 @@ fn migrate_db_alter_table(c: &Connection) -> Result<(), Box<dyn Error>> {
// copy data from the old table
c.execute(
"INSERT INTO tweet_to_toot_new (tweet_id, toot_id)
SELECT tweet_id, toot_id FROM tweet_to_toot;",
"INSERT INTO toot_tweet_record (toot_id, tweet_id, datetime)
SELECT toot_id, tweet_id, datetime FROM tweet_to_toot;",
[],
)?;
// drop the old table
c.execute("DROP TABLE tweet_to_toot;", [])?;
// rename the new table
c.execute("ALTER TABLE tweet_to_toot_new RENAME TO tweet_to_toot;", [])?;
c.execute("DROP TABLE IF EXISTS tweet_to_toot;", [])?;
Ok(())
}
@@ -164,7 +178,8 @@ mod tests {
// open said file
let conn = Connection::open(d).unwrap();
conn.execute("SELECT * from tweet_to_toot;", []).unwrap();
conn.execute("SELECT * from toot_tweet_record;", [])
.unwrap();
remove_file(d).unwrap();
}
@@ -179,7 +194,7 @@ mod tests {
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
"INSERT INTO toot_tweet_record (tweet_id, toot_id)
VALUES
(100, 1001);",
[],
@@ -199,34 +214,38 @@ mod tests {
let conn = Connection::open(d).unwrap();
let t_in = TweetToToot {
tweet_id: 123456789,
let t_in = TootTweetRecord {
toot_id: 987654321,
tweet_id: 123456789,
record_uri: "a".to_string(),
root_record_uri: "c".to_string(),
datetime: None,
};
write_state(&conn, t_in).unwrap();
let mut stmt = conn
.prepare(
"SELECT tweet_id, toot_id, UNIXEPOCH(datetime) AS datetime FROM tweet_to_toot;",
)
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_tweet_record;")
.unwrap();
let t_out = stmt
.query_row([], |row| {
Ok(TweetToToot {
tweet_id: row.get("tweet_id").unwrap(),
Ok(TootTweetRecord {
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(
DateTime::from_timestamp(row.get("datetime").unwrap(), 0).unwrap(),
DateTime::from_timestamp(row.get("unix_datetime").unwrap(), 0).unwrap(),
),
})
})
.unwrap();
assert_eq!(t_out.tweet_id, 123456789);
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());
remove_file(d).unwrap();
}
@@ -240,10 +259,10 @@ mod tests {
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri)
VALUES
(101, 1001),
(102, 1002);",
(101, 1001, 'abc'),
(102, 1002, 'def');",
[],
)
.unwrap();
@@ -252,8 +271,9 @@ mod tests {
remove_file(d).unwrap();
assert_eq!(t_out.tweet_id, 102);
assert_eq!(t_out.toot_id, 1002);
assert_eq!(t_out.toot_id, 102);
assert_eq!(t_out.tweet_id, 1002);
assert_eq!(t_out.record_uri, "def".to_string());
}
#[test]
@@ -280,9 +300,9 @@ mod tests {
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri)
VALUES
(100, 1000);",
(100, 1000, 'abc');",
[],
)
.unwrap();
@@ -303,19 +323,20 @@ mod tests {
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri)
VALUES
(100, 1000);",
(100, 1000, 'abc');",
[],
)
.unwrap();
let t_out = read_state(&conn, Some(1000)).unwrap().unwrap();
let t_out = read_state(&conn, Some(100)).unwrap().unwrap();
remove_file(d).unwrap();
assert_eq!(t_out.tweet_id, 100);
assert_eq!(t_out.toot_id, 1000);
assert_eq!(t_out.toot_id, 100);
assert_eq!(t_out.tweet_id, 1000);
assert_eq!(t_out.record_uri, "abc".to_string());
}
#[test]
@@ -327,8 +348,10 @@ mod tests {
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot(tweet_id, toot_id)
VALUES (100, 1000), (101, 1000);",
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri)
VALUES
(1000, 100, 'abc'),
(1000, 101, 'def');",
[],
)
.unwrap();
@@ -337,49 +360,9 @@ mod tests {
remove_file(d).unwrap();
assert_eq!(t_out.tweet_id, 101);
assert_eq!(t_out.toot_id, 1000);
}
#[test]
fn test_migrate_db_alter_table() {
let d = "/tmp/test_migrate_db_alter_table.sqlite";
let conn = Connection::open(d).unwrap();
init_db(d).unwrap();
write_state(
&conn,
TweetToToot {
tweet_id: 0,
toot_id: 0,
datetime: None,
},
)
.unwrap();
write_state(
&conn,
TweetToToot {
tweet_id: 1,
toot_id: 1,
datetime: None,
},
)
.unwrap();
migrate_db_alter_table(&conn).unwrap();
let mut stmt = conn.prepare("PRAGMA table_info(tweet_to_toot);").unwrap();
let mut t = stmt.query([]).unwrap();
while let Some(row) = t.next().unwrap() {
if row.get::<usize, u8>(0).unwrap() == 2 {
assert_eq!(row.get::<usize, String>(1).unwrap(), "datetime".to_string());
}
}
remove_file(d).unwrap();
assert_eq!(t_out.tweet_id, 101);
assert_eq!(t_out.record_uri, "def".to_string());
}
#[test]
@@ -391,14 +374,18 @@ mod tests {
conn.execute(
"CREATE TABLE IF NOT EXISTS tweet_to_toot (
tweet_id INTEGER,
toot_id INTEGER PRIMARY KEY
toot_id INTEGER PRIMARY KEY,
datetime INTEGER DEFAULT CURRENT_TIMESTAMP
)",
[],
)
.unwrap();
conn.execute("INSERT INTO tweet_to_toot VALUES (0, 0), (1, 1);", [])
.unwrap();
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id) VALUES (0, 0), (1, 1);",
[],
)
.unwrap();
migrate_db(d).unwrap();
@@ -421,7 +408,7 @@ mod tests {
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot(tweet_id, toot_id) VALUES (0, 0);",
"INSERT INTO toot_tweet_record(toot_id, tweet_id, record_uri) VALUES (0, 0, 'abc');",
[],
)
.unwrap();
@@ -429,23 +416,25 @@ mod tests {
delete_state(&conn, 0).unwrap();
let mut stmt = conn
.prepare(
"SELECT tweet_id, toot_id, UNIXEPOCH(datetime) AS datetime FROM tweet_to_toot;",
)
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_tweet_record;")
.unwrap();
let t_out = stmt.query_row([], |row| {
Ok(TweetToToot {
tweet_id: row.get("tweet_id").unwrap(),
Ok(TootTweetRecord {
toot_id: row.get("toot_id").unwrap(),
datetime: Some(DateTime::from_timestamp(row.get("datetime").unwrap(), 0).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(
DateTime::from_timestamp(row.get("unix_datetime").unwrap(), 0).unwrap(),
),
})
});
assert!(t_out.is_err_and(|x| x == rusqlite::Error::QueryReturnedNoRows));
conn.execute(
"INSERT INTO tweet_to_toot(tweet_id, toot_id) VALUES(102,42), (103,42);",
"INSERT INTO toot_tweet_record(toot_id, tweet_id, record_uri) VALUES(42, 102, 'abc'), (42, 103, 'def');",
[],
)
.unwrap();
@@ -453,16 +442,18 @@ mod tests {
delete_state(&conn, 42).unwrap();
let mut stmt = conn
.prepare(
"SELECT tweet_id, toot_id, UNIXEPOCH(datetime) AS datetime FROM tweet_to_toot;",
)
.prepare("SELECT *, UNIXEPOCH(datetime) AS unix_datetime FROM toot_tweet_record;")
.unwrap();
let t_out = stmt.query_row([], |row| {
Ok(TweetToToot {
tweet_id: row.get("tweet_id").unwrap(),
Ok(TootTweetRecord {
toot_id: row.get("toot_id").unwrap(),
datetime: Some(DateTime::from_timestamp(row.get("datetime").unwrap(), 0).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(
DateTime::from_timestamp(row.get("unix_datetime").unwrap(), 0).unwrap(),
),
})
});
@@ -472,24 +463,27 @@ mod tests {
}
#[test]
fn test_read_all_tweet_state() {
let d = "/tmp/read_all_tweet_state.sqlite";
fn test_read_all_state() {
let d = "/tmp/read_all_state.sqlite";
init_db(d).unwrap();
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot(tweet_id, toot_id) VALUES (102, 42), (103, 42), (105, 43);",
"INSERT INTO toot_tweet_record (toot_id, tweet_id, record_uri) VALUES (42, 102, 'abc'), (42, 103, 'def'), (43, 105, 'ghi');",
[],
)
.unwrap();
let v1 = read_all_tweet_state(&conn, 43).unwrap();
let v2 = read_all_tweet_state(&conn, 42).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!(v1, vec![105]);
assert_eq!(v2, vec![102, 103]);
assert_eq!(tweet_v1, vec![105]);
assert_eq!(tweet_v2, vec![102, 103]);
assert_eq!(record_v1, vec!["ghi".to_string()]);
assert_eq!(record_v2, vec!["abc".to_string(), "def".to_string()]);
remove_file(d).unwrap();
}