refs #983 Get streaming url for instance API before start streaming

This commit is contained in:
AkiraFukushima 2019-07-29 23:32:02 +09:00
parent 869defd927
commit 1c836dfb95
2 changed files with 247 additions and 240 deletions

View File

@ -28,7 +28,7 @@ import sanitizeHtml from 'sanitize-html'
import pkg from '~/package.json'
import Authentication from './auth'
import Account from './account'
import WebSocket from './websocket'
import WebSocket, { StreamingURL } from './websocket'
import Preferences from './preferences'
import Fonts from './fonts'
import Hashtags from './hashtags'
@ -441,17 +441,17 @@ ipcMain.on('reset-badge', () => {
let userStreamings: { [key: string]: WebSocket | null } = {}
ipcMain.on('start-all-user-streamings', (event: Event, accounts: Array<LocalAccount>) => {
accounts.map(account => {
accounts.map(async account => {
const id: string = account._id!
accountManager
.getAccount(id)
.then(acct => {
try {
const acct = await accountManager.getAccount(id)
// Stop old user streaming
if (userStreamings[id]) {
userStreamings[id]!.stop()
userStreamings[id] = null
}
userStreamings[id] = new WebSocket(acct)
const url = await StreamingURL(acct)
userStreamings[id] = new WebSocket(acct, url)
userStreamings[id]!.startUserStreaming(
(update: Status) => {
if (!event.sender.isDestroyed()) {
@ -506,14 +506,13 @@ ipcMain.on('start-all-user-streamings', (event: Event, accounts: Array<LocalAcco
}
}
)
})
.catch((err: Error) => {
} catch (err) {
log.error(err)
const streamingError = new StreamingError(err.message, account.domain)
if (!event.sender.isDestroyed()) {
event.sender.send('error-start-all-user-streamings', streamingError)
}
})
}
})
})
@ -545,18 +544,19 @@ type StreamingSetting = {
let directMessagesStreaming: WebSocket | null = null
ipcMain.on('start-directmessages-streaming', (event: Event, obj: StreamingSetting) => {
ipcMain.on('start-directmessages-streaming', async (event: Event, obj: StreamingSetting) => {
const { account } = obj
accountManager
.getAccount(account._id!)
.then(acct => {
try {
const acct = await accountManager.getAccount(account._id!)
// Stop old directmessages streaming
if (directMessagesStreaming !== null) {
directMessagesStreaming.stop()
directMessagesStreaming = null
}
directMessagesStreaming = new WebSocket(acct)
const url = await StreamingURL(acct)
directMessagesStreaming = new WebSocket(acct, url)
directMessagesStreaming.start(
'direct',
(update: Status) => {
@ -576,13 +576,12 @@ ipcMain.on('start-directmessages-streaming', (event: Event, obj: StreamingSettin
}
}
)
})
.catch(err => {
} catch (err) {
log.error(err)
if (!event.sender.isDestroyed()) {
event.sender.send('error-start-directmessages-streaming', err)
}
})
}
})
ipcMain.on('stop-directmessages-streaming', () => {
@ -594,18 +593,19 @@ ipcMain.on('stop-directmessages-streaming', () => {
let localStreaming: WebSocket | null = null
ipcMain.on('start-local-streaming', (event: Event, obj: StreamingSetting) => {
ipcMain.on('start-local-streaming', async (event: Event, obj: StreamingSetting) => {
const { account } = obj
accountManager
.getAccount(account._id!)
.then(acct => {
try {
const acct = await accountManager.getAccount(account._id!)
// Stop old local streaming
if (localStreaming !== null) {
localStreaming.stop()
localStreaming = null
}
localStreaming = new WebSocket(acct)
const url = await StreamingURL(acct)
localStreaming = new WebSocket(acct, url)
localStreaming.start(
'public:local',
(update: Status) => {
@ -625,13 +625,12 @@ ipcMain.on('start-local-streaming', (event: Event, obj: StreamingSetting) => {
}
}
)
})
.catch(err => {
} catch (err) {
log.error(err)
if (!event.sender.isDestroyed()) {
event.sender.send('error-start-local-streaming', err)
}
})
}
})
ipcMain.on('stop-local-streaming', () => {
@ -643,18 +642,19 @@ ipcMain.on('stop-local-streaming', () => {
let publicStreaming: WebSocket | null = null
ipcMain.on('start-public-streaming', (event: Event, obj: StreamingSetting) => {
ipcMain.on('start-public-streaming', async (event: Event, obj: StreamingSetting) => {
const { account } = obj
accountManager
.getAccount(account._id!)
.then(acct => {
try {
const acct = await accountManager.getAccount(account._id!)
// Stop old public streaming
if (publicStreaming !== null) {
publicStreaming.stop()
publicStreaming = null
}
publicStreaming = new WebSocket(acct)
const url = await StreamingURL(acct)
publicStreaming = new WebSocket(acct, url)
publicStreaming.start(
'public',
(update: Status) => {
@ -674,13 +674,12 @@ ipcMain.on('start-public-streaming', (event: Event, obj: StreamingSetting) => {
}
}
)
})
.catch(err => {
} catch (err) {
log.error(err)
if (!event.sender.isDestroyed()) {
event.sender.send('error-start-public-streaming', err)
}
})
}
})
ipcMain.on('stop-public-streaming', () => {
@ -696,18 +695,19 @@ type ListID = {
listID: string
}
ipcMain.on('start-list-streaming', (event: Event, obj: ListID & StreamingSetting) => {
ipcMain.on('start-list-streaming', async (event: Event, obj: ListID & StreamingSetting) => {
const { listID, account } = obj
accountManager
.getAccount(account._id!)
.then(acct => {
try {
const acct = await accountManager.getAccount(account._id!)
// Stop old list streaming
if (listStreaming !== null) {
listStreaming.stop()
listStreaming = null
}
listStreaming = new WebSocket(acct)
const url = await StreamingURL(acct)
listStreaming = new WebSocket(acct, url)
listStreaming.start(
`list&list=${listID}`,
(update: Status) => {
@ -727,13 +727,12 @@ ipcMain.on('start-list-streaming', (event: Event, obj: ListID & StreamingSetting
}
}
)
})
.catch(err => {
} catch (err) {
log.error(err)
if (!event.sender.isDestroyed()) {
event.sender.send('error-start-list-streaming', err)
}
})
}
})
ipcMain.on('stop-list-streaming', () => {
@ -749,18 +748,19 @@ type Tag = {
tag: string
}
ipcMain.on('start-tag-streaming', (event: Event, obj: Tag & StreamingSetting) => {
ipcMain.on('start-tag-streaming', async (event: Event, obj: Tag & StreamingSetting) => {
const { tag, account } = obj
accountManager
.getAccount(account._id!)
.then(acct => {
try {
const acct = await accountManager.getAccount(account._id!)
// Stop old tag streaming
if (tagStreaming !== null) {
tagStreaming.stop()
tagStreaming = null
}
tagStreaming = new WebSocket(acct)
const url = await StreamingURL(acct)
tagStreaming = new WebSocket(acct, url)
tagStreaming.start(
`hashtag&tag=${tag}`,
(update: Status) => {
@ -780,13 +780,12 @@ ipcMain.on('start-tag-streaming', (event: Event, obj: Tag & StreamingSetting) =>
}
}
)
})
.catch(err => {
} catch (err) {
log.error(err)
if (!event.sender.isDestroyed()) {
event.sender.send('error-start-tag-streaming', err)
}
})
}
})
ipcMain.on('stop-tag-streaming', () => {

View File

@ -1,13 +1,21 @@
import Mastodon, { WebSocket as SocketListener, Status, Notification } from 'megalodon'
import Mastodon, { WebSocket as SocketListener, Status, Notification, Instance } from 'megalodon'
import log from 'electron-log'
import { LocalAccount } from '~/src/types/localAccount'
const StreamingURL = async (account: LocalAccount): Promise<string> => {
const client = new Mastodon(account.accessToken!, account.baseURL + '/api/v1')
const res = await client.get<Instance>('/instance')
return res.data.urls.streaming_api
}
export { StreamingURL }
export default class WebSocket {
private client: Mastodon
private listener: SocketListener | null
constructor(account: LocalAccount) {
const url = account.baseURL.replace(/^https:\/\//, 'wss://')
constructor(account: LocalAccount, streamingURL: string) {
const url = streamingURL.replace(/^https:\/\//, 'wss://')
this.client = new Mastodon(account.accessToken!, url + '/api/v1')
this.listener = null
}