Use client class instead

Add getter/setter to authorized clients
Save new account client to Client.authorizedHTTP
This commit is contained in:
kPherox 2019-04-27 19:04:08 +09:00
parent c22484db7c
commit f06327c1e4
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D
2 changed files with 21 additions and 6 deletions

View File

@ -3,6 +3,8 @@ import Mastodon, { Response, Account, OAuth } from "megalodon"
import { join } from "path"
import Datastore from "nedb"
import Client from "./Client"
interface AccountDoc {
_id?: string
domain: string
@ -59,9 +61,8 @@ export default class Auth {
ipcMain.on(
"new-account-auth",
async (event: Event, code: string, instance: string) => {
let url: string = "https://" + instance
try {
let tokenData: Partial<{ accessToken: string }> = await Mastodon.fetchAccessToken(clientId, clientSecret, code, url)
let tokenData: Partial<{ accessToken: string }> = await Mastodon.fetchAccessToken(clientId, clientSecret, code, "https://" + instance)
if (tokenData.accessToken === undefined) {
event.sender.send(`error`, {
id: "ERROR_GET_TOKEN",
@ -70,10 +71,7 @@ export default class Auth {
return
}
const client = new Mastodon(
tokenData.accessToken,
url + "/api/v1"
)
const client = Client.createAuthClient('http', instance, tokenData.accessToken)
let resp: Response<Account> = await client.get<Account>("/accounts/verify_credentials")
let you = resp.data
@ -98,6 +96,7 @@ export default class Auth {
message: "You cannot login already logined account."
})
} else {
Client.setAuthClient('http', `@${newDocs.acct}@${newDocs.domain}`, client)
event.sender.send(`login-complete`, newDocs)
}
})

View File

@ -12,6 +12,22 @@ export default class Clients {
private static nonAuthorizedHTTP: Map<string, Mastodon> = new Map()
private static nonAuthorizedWebsocket: Map<string, Mastodon> = new Map()
public static getAuthClient(username: string, protocol: Protocol = 'http'): Mastodon {
let clients = protocol === 'http' ? this.authorizedHTTP : this.authorizedWebSocket
if (!clients.has(username)) {
// usernameからドメインをとトークンをデータベースから取得してクライアントを作る
//this.setAuthClient(protocol, username, this.createAuthClient(protocol, domain, accessToken))
}
return clients.get(username)!
}
public static setAuthClient(protocol: Protocol, username: string, client: Mastodon) {
let clients = protocol === 'http' ? this.nonAuthorizedHTTP : this.nonAuthorizedWebsocket
clients.set(username, client)
}
public static createAuthClient(protocol: Protocol, domain: string, accessToken: string): Mastodon {
let scheme = protocol === 'http' ? 'https://' : 'wss://'
return new Mastodon(