diff --git a/phoebe/migrations/20220416061935_create_message_links_table.sql b/phoebe/migrations/20220416061935_create_message_links_table.sql index 037ff3e..64b4ad5 100644 --- a/phoebe/migrations/20220416061935_create_message_links_table.sql +++ b/phoebe/migrations/20220416061935_create_message_links_table.sql @@ -3,8 +3,10 @@ CREATE TABLE message_links ( ) STRICT; CREATE TABLE messages ( - link_id INTEGER NOT NULL REFERENCES message_links(id), + message_id INTEGER PRIMARY KEY, + link_id INTEGER NOT NULL REFERENCES message_links(id), service TEXT NOT NULL, channel TEXT NOT NULL, - message TEXT NOT NULL -) + message TEXT NOT NULL, + original INTEGER +) STRICT; diff --git a/phoebe/src/lib.rs b/phoebe/src/lib.rs index cbfc4a4..77f0c5c 100644 --- a/phoebe/src/lib.rs +++ b/phoebe/src/lib.rs @@ -55,17 +55,34 @@ pub async fn link_messages( .await? .last_insert_rowid(); - for resultant in std::iter::once(origin).chain(messages) { + let original_id = { + let service = &origin.channel.service; + let channel = &origin.channel.id; + let message = &origin.message_id; + + let query = sqlx::query!( + "INSERT INTO messages (link_id, service, channel, message, original) VALUES (?, ?, ?, ?, NULL)", + message_link, + service, + channel, + message + ); + + query.execute(&mut *conn).await?.last_insert_rowid() + }; + + for resultant in messages { let service = &resultant.channel.service; let channel = &resultant.channel.id; let message = &resultant.message_id; let query = sqlx::query!( - "INSERT INTO messages VALUES (?, ?, ?, ?)", + "INSERT INTO messages (link_id, service, channel, message, original) VALUES (?, ?, ?, ?, ?)", message_link, service, channel, - message + message, + original_id ); let _ = query.execute(&mut *conn).await?;