Add clients manager class

This commit is contained in:
kPherox 2019-04-24 03:58:06 +09:00
parent 49acb2c7b7
commit ab37afb4e6
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D
3 changed files with 44 additions and 9 deletions

View File

@ -73,12 +73,12 @@ input {
border-color: #26d69a; border-color: #26d69a;
} }
} }
#timelines{ #timelines {
display:flex; display: flex;
width:100%; width: 100%;
} }
.tl{ .tl {
height:100%; height: 100%;
flex-grow: 4; flex-grow: 4;
} }
</style> </style>

36
src/main/Client.ts Normal file
View File

@ -0,0 +1,36 @@
import Mastodon from 'megalodon'
type Protocol = 'http' | 'ws'
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)) {
this.createNoAuthClient(domain)
}
return clients.get(domain)!
}
private static createNoAuthClient(domain: string) {
this.nonAuthorizedHTTP.set(domain, new Mastodon(
'',
'https://' + domain + '/api/v1'
))
this.nonAuthorizedWebsocket.set(domain, new Mastodon(
'',
'wss://' + domain + '/api/v1'
))
}
}

View File

@ -3,13 +3,12 @@ import {
} from 'electron' } from 'electron'
import Mastodon, { Status, Response } from 'megalodon' import Mastodon, { Status, Response } from 'megalodon'
import Client from './Client'
export default class Timeline { export default class Timeline {
public static ready() { public static ready() {
ipcMain.on('no-auth-streaming', (event: Event, instance: string) => { ipcMain.on('no-auth-streaming', (event: Event, instance: string) => {
const client = new Mastodon( const client = Client.getNoAuthClient(instance)
'',
'https://' + instance + '/api/v1'
)
client.get<[Status]>('/timelines/public?local=true') client.get<[Status]>('/timelines/public?local=true')
.then((resp: Response<[Status]>) => { .then((resp: Response<[Status]>) => {