Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember last selected account to post #242

Merged
merged 2 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/db/0000_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 33 additions & 8 deletions src-tauri/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ 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())
.bind(account.client_id.clone())
.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();
Expand Down Expand Up @@ -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| {
Expand All @@ -290,18 +291,42 @@ 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)
.await?;

Ok(accounts)
}

pub(crate) async fn set_usual_account(pool: &SqlitePool, id: i64) -> DBResult<entities::Account> {
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)
}
3 changes: 3 additions & 0 deletions src-tauri/src/entities/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Account {
pub access_token: String,
// Mastodon and Misskey does not provide refresh_token.
pub refresh_token: Option<String>,
pub usual: bool,
}

impl Account {
Expand All @@ -25,6 +26,7 @@ impl Account {
client_secret: String,
access_token: String,
refresh_token: Option<String>,
usual: bool,
) -> Self {
Self {
id,
Expand All @@ -35,6 +37,7 @@ impl Account {
client_secret,
access_token,
refresh_token,
usual,
}
}
}
13 changes: 13 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -466,6 +478,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
add_application,
authorize_code,
get_account,
set_usual_account,
list_accounts,
add_timeline,
list_timelines,
Expand Down
11 changes: 9 additions & 2 deletions src/components/compose/Compose.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ const Compose: React.FC<Props> = props => {
const f = async () => {
const accounts = await invoke<Array<[Account, Server]>>('list_accounts')
setAccounts(accounts)
setFromAccount(accounts[0])

const usual = accounts.find(([a, _]) => a.usual)
h3poteto marked this conversation as resolved.
Show resolved Hide resolved
if (usual) {
setFromAccount(usual)
} else {
setFromAccount(accounts[0])
}
}
f()
}, [props.servers])
Expand All @@ -62,9 +68,10 @@ const Compose: React.FC<Props> = 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 (
Expand Down
19 changes: 10 additions & 9 deletions src/entities/account.ts
Original file line number Diff line number Diff line change
@@ -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
}