From 0728f214476e1d71d5e5a91404e77c550356edb8 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 1 Jan 2023 16:04:06 +0900 Subject: [PATCH 1/2] Remember last selected account to post --- src-tauri/db/0000_init.sql | 3 ++- src-tauri/src/database.rs | 41 ++++++++++++++++++++++++------ src-tauri/src/entities/account.rs | 3 +++ src-tauri/src/main.rs | 13 ++++++++++ src/components/compose/Compose.tsx | 11 ++++++-- src/entities/account.ts | 19 +++++++------- 6 files changed, 70 insertions(+), 20 deletions(-) diff --git a/src-tauri/db/0000_init.sql b/src-tauri/db/0000_init.sql index 71cc6643..2ea91e2b 100644 --- a/src-tauri/db/0000_init.sql +++ b/src-tauri/db/0000_init.sql @@ -16,7 +16,8 @@ CREATE TABLE IF NOT EXISTS accounts( client_id TEXT DEFAULT NULL, client_secret TEXT NOT NULL, access_token TEXT NOT NULL, - refresh_token TEXT DEFAULT NULL + refresh_token TEXT DEFAULT NULL, + usual BOOL NOT NULL DEFAULT FALSE ); CREATE TABLE IF NOT EXISTS timelines( diff --git a/src-tauri/src/database.rs b/src-tauri/src/database.rs index cfd6fb8b..f35c40d8 100644 --- a/src-tauri/src/database.rs +++ b/src-tauri/src/database.rs @@ -78,7 +78,7 @@ pub(crate) async fn add_account( let mut tx = pool.begin().await?; let mut created = account.clone(); - let res = sqlx::query("INSERT INTO accounts (username, account_id, avatar, client_id, client_secret, access_token, refresh_token) VALUES (?, ?, ?, ?, ?, ?, ?)") + let res = sqlx::query("INSERT INTO accounts (username, account_id, avatar, client_id, client_secret, access_token, refresh_token, usual) VALUES (?, ?, ?, ?, ?, ?, ?, ?)") .bind(account.username.clone()) .bind(account.account_id.clone()) .bind(account.avatar.clone()) @@ -86,6 +86,7 @@ pub(crate) async fn add_account( .bind(account.client_secret.clone()) .bind(account.access_token.clone()) .bind(account.refresh_token.clone()) + .bind(account.usual.clone()) .execute(&mut tx) .await?; let id = res.last_insert_rowid(); @@ -276,7 +277,7 @@ pub(crate) async fn list_account( let accounts = sqlx::query( r#" SELECT accounts.id, accounts.username, accounts.account_id, accounts.avatar, accounts.client_id, accounts.client_secret, - accounts.access_token, accounts.refresh_token, servers.id, servers.domain, servers.base_url, servers.sns, + accounts.access_token, accounts.refresh_token, accounts.usual, servers.id, servers.domain, servers.base_url, servers.sns, servers.favicon, servers.account_id FROM accounts INNER JOIN servers ON accounts.id = servers.account_id"# ).map(|row: SqliteRow| { @@ -290,14 +291,15 @@ FROM accounts INNER JOIN servers ON accounts.id = servers.account_id"# client_secret: row.get(5), access_token: row.get(6), refresh_token: row.get(7), + usual: row.get(8), }, entities::Server { - id: row.get(8), - domain: row.get(9), - base_url: row.get(10), - sns: row.get(11), - favicon: row.get(12), - account_id: row.get(13), + id: row.get(9), + domain: row.get(10), + base_url: row.get(11), + sns: row.get(12), + favicon: row.get(13), + account_id: row.get(14), }, ) }).fetch_all(pool) @@ -305,3 +307,26 @@ FROM accounts INNER JOIN servers ON accounts.id = servers.account_id"# Ok(accounts) } + +pub(crate) async fn set_usual_account(pool: &SqlitePool, id: i64) -> DBResult { + let mut tx = pool.begin().await?; + + sqlx::query("UPDATE accounts SET usual = ? WHERE id = ?") + .bind(true) + .bind(id) + .execute(&mut tx) + .await?; + sqlx::query("UPDATE accounts SET usual = ? WHERE id != ?") + .bind(false) + .bind(id) + .execute(&mut tx) + .await?; + tx.commit().await?; + + let account = query_as::<_, entities::Account>("SELECT * FROM accounts WHERE id = ?") + .bind(id) + .fetch_one(pool) + .await?; + + Ok(account) +} diff --git a/src-tauri/src/entities/account.rs b/src-tauri/src/entities/account.rs index 041de2b3..a64d70f8 100644 --- a/src-tauri/src/entities/account.rs +++ b/src-tauri/src/entities/account.rs @@ -13,6 +13,7 @@ pub struct Account { pub access_token: String, // Mastodon and Misskey does not provide refresh_token. pub refresh_token: Option, + pub usual: bool, } impl Account { @@ -25,6 +26,7 @@ impl Account { client_secret: String, access_token: String, refresh_token: Option, + usual: bool, ) -> Self { Self { id, @@ -35,6 +37,7 @@ impl Account { client_secret, access_token, refresh_token, + usual, } } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 60fdc66a..f0fa161c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -155,6 +155,7 @@ async fn authorize_code( client_secret, token_data.access_token, token_data.refresh_token, + false, ); database::add_account(&sqlite_pool, &server, &account) @@ -255,6 +256,17 @@ async fn get_account( Ok(account) } +#[tauri::command] +async fn set_usual_account( + sqlite_pool: State<'_, sqlx::SqlitePool>, + id: i64, +) -> Result<(), String> { + let _ = database::set_usual_account(&sqlite_pool, id) + .await + .map_err(|e| e.to_string())?; + Ok(()) +} + #[tauri::command] async fn switch_left_timeline( app_handle: AppHandle, @@ -466,6 +478,7 @@ fn main() -> Result<(), Box> { add_application, authorize_code, get_account, + set_usual_account, list_accounts, add_timeline, list_timelines, diff --git a/src/components/compose/Compose.tsx b/src/components/compose/Compose.tsx index 93d96c34..cd486b53 100644 --- a/src/components/compose/Compose.tsx +++ b/src/components/compose/Compose.tsx @@ -49,7 +49,13 @@ const Compose: React.FC = props => { const f = async () => { const accounts = await invoke>('list_accounts') setAccounts(accounts) - setFromAccount(accounts[0]) + + const usual = accounts.find(([a, _]) => a.usual) + if (usual) { + setFromAccount(usual) + } else { + setFromAccount(accounts[0]) + } } f() }, [props.servers]) @@ -62,9 +68,10 @@ const Compose: React.FC = props => { setClient(client) }, [fromAccount]) - const selectAccount = (eventKey: string) => { + const selectAccount = async (eventKey: string) => { const account = accounts[parseInt(eventKey)] setFromAccount(account) + await invoke('set_usual_account', { id: account[0].id }) } return ( diff --git a/src/entities/account.ts b/src/entities/account.ts index 3d3a2ae1..2c7f1c5a 100644 --- a/src/entities/account.ts +++ b/src/entities/account.ts @@ -1,10 +1,11 @@ export type Account = { - id: number; - username: string; - account_id: string; - avatar: string | null; - client_id: string | null; - client_secret: string; - access_token: string; - refresh_token: string; -}; + id: number + username: string + account_id: string + avatar: string | null + client_id: string | null + client_secret: string + access_token: string + refresh_token: string + usual: boolean +} From 2f264c58e93cdc5fccc597e6ff5e2caac3420792 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 1 Jan 2023 16:30:39 +0900 Subject: [PATCH 2/2] Ingore _ vars for eslint --- .eslintrc.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 964f0473..ea9e3582 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -26,7 +26,14 @@ "react/prop-types": "off", "react/display-name": "off", "@typescript-eslint/no-explicit-any": "off", - "@typescript-eslint/ban-ts-comment": "off" + "@typescript-eslint/ban-ts-comment": "off", + "no-unused-vars": "off", + "@typescript-eslint/no-unused-vars": [ + "error", + { + "argsIgnorePattern": "^_" + } + ] }, "settings": { "react": {