thedesk/src/main/Client.ts

37 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-04-24 03:58:06 +09:00
import Mastodon from 'megalodon'
2019-04-24 06:07:15 +09:00
type Protocol = 'http' | 'websocket'
2019-04-24 03:58:06 +09:00
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 getNoAuthClient(domain: string, protocol: Protocol = 'http'): Mastodon {
let clients = protocol === 'http' ? this.nonAuthorizedHTTP : this.nonAuthorizedWebsocket
if (!clients.has(domain)) {
2019-04-27 18:21:18 +09:00
this.setNoAuthClient(protocol, domain, this.createNoAuthClient(protocol, domain))
2019-04-24 03:58:06 +09:00
}
return clients.get(domain)!
}
2019-04-27 18:21:18 +09:00
public static setNoAuthClient(protocol: Protocol, domain: string, client: Mastodon) {
let clients = protocol === 'http' ? this.nonAuthorizedHTTP : this.nonAuthorizedWebsocket
clients.set(domain, client)
}
2019-04-24 03:58:06 +09:00
2019-04-27 18:21:18 +09:00
private static createNoAuthClient(protocol: Protocol, domain: string): Mastodon {
let scheme = protocol === 'http' ? 'https://' : 'wss://'
return new Mastodon(
2019-04-24 03:58:06 +09:00
'',
2019-04-27 18:21:18 +09:00
scheme + domain + '/api/v1'
)
2019-04-24 03:58:06 +09:00
}
}