thedesk/src/main/Client.ts
kPherox f06327c1e4
Use client class instead
Add getter/setter to authorized clients
Save new account client to Client.authorizedHTTP
2019-04-27 19:04:08 +09:00

61 lines
2.4 KiB
TypeScript

import Mastodon from 'megalodon'
type Protocol = 'http' | 'websocket'
export default class Clients {
// Authorized Accounts. keyには`@username@domain`を設定します
private static authorizedHTTP: Map<string, Mastodon> = new Map()
private static authorizedWebSocket: Map<string, Mastodon> = new Map()
// Non-authorized Accounts. keyには`domain`を設定します
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(
accessToken,
scheme + domain + '/api/v1'
)
}
public static getNoAuthClient(domain: string, protocol: Protocol = 'http'): Mastodon {
let clients = protocol === 'http' ? this.nonAuthorizedHTTP : this.nonAuthorizedWebsocket
if (!clients.has(domain)) {
this.setNoAuthClient(protocol, domain, this.createNoAuthClient(protocol, domain))
}
return clients.get(domain)!
}
public static setNoAuthClient(protocol: Protocol, domain: string, client: Mastodon) {
let clients = protocol === 'http' ? this.nonAuthorizedHTTP : this.nonAuthorizedWebsocket
clients.set(domain, client)
}
private static createNoAuthClient(protocol: Protocol, domain: string): Mastodon {
let scheme = protocol === 'http' ? 'https://' : 'wss://'
return new Mastodon(
'',
scheme + domain + '/api/v1'
)
}
}