refs #983 Get streaming url for instance API before start streaming
This commit is contained in:
parent
869defd927
commit
1c836dfb95
|
@ -28,7 +28,7 @@ import sanitizeHtml from 'sanitize-html'
|
||||||
import pkg from '~/package.json'
|
import pkg from '~/package.json'
|
||||||
import Authentication from './auth'
|
import Authentication from './auth'
|
||||||
import Account from './account'
|
import Account from './account'
|
||||||
import WebSocket from './websocket'
|
import WebSocket, { StreamingURL } from './websocket'
|
||||||
import Preferences from './preferences'
|
import Preferences from './preferences'
|
||||||
import Fonts from './fonts'
|
import Fonts from './fonts'
|
||||||
import Hashtags from './hashtags'
|
import Hashtags from './hashtags'
|
||||||
|
@ -441,17 +441,17 @@ ipcMain.on('reset-badge', () => {
|
||||||
let userStreamings: { [key: string]: WebSocket | null } = {}
|
let userStreamings: { [key: string]: WebSocket | null } = {}
|
||||||
|
|
||||||
ipcMain.on('start-all-user-streamings', (event: Event, accounts: Array<LocalAccount>) => {
|
ipcMain.on('start-all-user-streamings', (event: Event, accounts: Array<LocalAccount>) => {
|
||||||
accounts.map(account => {
|
accounts.map(async account => {
|
||||||
const id: string = account._id!
|
const id: string = account._id!
|
||||||
accountManager
|
try {
|
||||||
.getAccount(id)
|
const acct = await accountManager.getAccount(id)
|
||||||
.then(acct => {
|
|
||||||
// Stop old user streaming
|
// Stop old user streaming
|
||||||
if (userStreamings[id]) {
|
if (userStreamings[id]) {
|
||||||
userStreamings[id]!.stop()
|
userStreamings[id]!.stop()
|
||||||
userStreamings[id] = null
|
userStreamings[id] = null
|
||||||
}
|
}
|
||||||
userStreamings[id] = new WebSocket(acct)
|
const url = await StreamingURL(acct)
|
||||||
|
userStreamings[id] = new WebSocket(acct, url)
|
||||||
userStreamings[id]!.startUserStreaming(
|
userStreamings[id]!.startUserStreaming(
|
||||||
(update: Status) => {
|
(update: Status) => {
|
||||||
if (!event.sender.isDestroyed()) {
|
if (!event.sender.isDestroyed()) {
|
||||||
|
@ -506,14 +506,13 @@ ipcMain.on('start-all-user-streamings', (event: Event, accounts: Array<LocalAcco
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
} catch (err) {
|
||||||
.catch((err: Error) => {
|
|
||||||
log.error(err)
|
log.error(err)
|
||||||
const streamingError = new StreamingError(err.message, account.domain)
|
const streamingError = new StreamingError(err.message, account.domain)
|
||||||
if (!event.sender.isDestroyed()) {
|
if (!event.sender.isDestroyed()) {
|
||||||
event.sender.send('error-start-all-user-streamings', streamingError)
|
event.sender.send('error-start-all-user-streamings', streamingError)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -545,18 +544,19 @@ type StreamingSetting = {
|
||||||
|
|
||||||
let directMessagesStreaming: WebSocket | null = null
|
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
|
const { account } = obj
|
||||||
accountManager
|
try {
|
||||||
.getAccount(account._id!)
|
const acct = await accountManager.getAccount(account._id!)
|
||||||
.then(acct => {
|
|
||||||
// Stop old directmessages streaming
|
// Stop old directmessages streaming
|
||||||
if (directMessagesStreaming !== null) {
|
if (directMessagesStreaming !== null) {
|
||||||
directMessagesStreaming.stop()
|
directMessagesStreaming.stop()
|
||||||
directMessagesStreaming = null
|
directMessagesStreaming = null
|
||||||
}
|
}
|
||||||
|
|
||||||
directMessagesStreaming = new WebSocket(acct)
|
const url = await StreamingURL(acct)
|
||||||
|
directMessagesStreaming = new WebSocket(acct, url)
|
||||||
directMessagesStreaming.start(
|
directMessagesStreaming.start(
|
||||||
'direct',
|
'direct',
|
||||||
(update: Status) => {
|
(update: Status) => {
|
||||||
|
@ -576,13 +576,12 @@ ipcMain.on('start-directmessages-streaming', (event: Event, obj: StreamingSettin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
} catch (err) {
|
||||||
.catch(err => {
|
|
||||||
log.error(err)
|
log.error(err)
|
||||||
if (!event.sender.isDestroyed()) {
|
if (!event.sender.isDestroyed()) {
|
||||||
event.sender.send('error-start-directmessages-streaming', err)
|
event.sender.send('error-start-directmessages-streaming', err)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('stop-directmessages-streaming', () => {
|
ipcMain.on('stop-directmessages-streaming', () => {
|
||||||
|
@ -594,18 +593,19 @@ ipcMain.on('stop-directmessages-streaming', () => {
|
||||||
|
|
||||||
let localStreaming: WebSocket | null = null
|
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
|
const { account } = obj
|
||||||
accountManager
|
try {
|
||||||
.getAccount(account._id!)
|
const acct = await accountManager.getAccount(account._id!)
|
||||||
.then(acct => {
|
|
||||||
// Stop old local streaming
|
// Stop old local streaming
|
||||||
if (localStreaming !== null) {
|
if (localStreaming !== null) {
|
||||||
localStreaming.stop()
|
localStreaming.stop()
|
||||||
localStreaming = null
|
localStreaming = null
|
||||||
}
|
}
|
||||||
|
|
||||||
localStreaming = new WebSocket(acct)
|
const url = await StreamingURL(acct)
|
||||||
|
localStreaming = new WebSocket(acct, url)
|
||||||
localStreaming.start(
|
localStreaming.start(
|
||||||
'public:local',
|
'public:local',
|
||||||
(update: Status) => {
|
(update: Status) => {
|
||||||
|
@ -625,13 +625,12 @@ ipcMain.on('start-local-streaming', (event: Event, obj: StreamingSetting) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
} catch (err) {
|
||||||
.catch(err => {
|
|
||||||
log.error(err)
|
log.error(err)
|
||||||
if (!event.sender.isDestroyed()) {
|
if (!event.sender.isDestroyed()) {
|
||||||
event.sender.send('error-start-local-streaming', err)
|
event.sender.send('error-start-local-streaming', err)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('stop-local-streaming', () => {
|
ipcMain.on('stop-local-streaming', () => {
|
||||||
|
@ -643,18 +642,19 @@ ipcMain.on('stop-local-streaming', () => {
|
||||||
|
|
||||||
let publicStreaming: WebSocket | null = null
|
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
|
const { account } = obj
|
||||||
accountManager
|
try {
|
||||||
.getAccount(account._id!)
|
const acct = await accountManager.getAccount(account._id!)
|
||||||
.then(acct => {
|
|
||||||
// Stop old public streaming
|
// Stop old public streaming
|
||||||
if (publicStreaming !== null) {
|
if (publicStreaming !== null) {
|
||||||
publicStreaming.stop()
|
publicStreaming.stop()
|
||||||
publicStreaming = null
|
publicStreaming = null
|
||||||
}
|
}
|
||||||
|
|
||||||
publicStreaming = new WebSocket(acct)
|
const url = await StreamingURL(acct)
|
||||||
|
publicStreaming = new WebSocket(acct, url)
|
||||||
publicStreaming.start(
|
publicStreaming.start(
|
||||||
'public',
|
'public',
|
||||||
(update: Status) => {
|
(update: Status) => {
|
||||||
|
@ -674,13 +674,12 @@ ipcMain.on('start-public-streaming', (event: Event, obj: StreamingSetting) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
} catch (err) {
|
||||||
.catch(err => {
|
|
||||||
log.error(err)
|
log.error(err)
|
||||||
if (!event.sender.isDestroyed()) {
|
if (!event.sender.isDestroyed()) {
|
||||||
event.sender.send('error-start-public-streaming', err)
|
event.sender.send('error-start-public-streaming', err)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('stop-public-streaming', () => {
|
ipcMain.on('stop-public-streaming', () => {
|
||||||
|
@ -696,18 +695,19 @@ type ListID = {
|
||||||
listID: string
|
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
|
const { listID, account } = obj
|
||||||
accountManager
|
try {
|
||||||
.getAccount(account._id!)
|
const acct = await accountManager.getAccount(account._id!)
|
||||||
.then(acct => {
|
|
||||||
// Stop old list streaming
|
// Stop old list streaming
|
||||||
if (listStreaming !== null) {
|
if (listStreaming !== null) {
|
||||||
listStreaming.stop()
|
listStreaming.stop()
|
||||||
listStreaming = null
|
listStreaming = null
|
||||||
}
|
}
|
||||||
|
|
||||||
listStreaming = new WebSocket(acct)
|
const url = await StreamingURL(acct)
|
||||||
|
listStreaming = new WebSocket(acct, url)
|
||||||
listStreaming.start(
|
listStreaming.start(
|
||||||
`list&list=${listID}`,
|
`list&list=${listID}`,
|
||||||
(update: Status) => {
|
(update: Status) => {
|
||||||
|
@ -727,13 +727,12 @@ ipcMain.on('start-list-streaming', (event: Event, obj: ListID & StreamingSetting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
} catch (err) {
|
||||||
.catch(err => {
|
|
||||||
log.error(err)
|
log.error(err)
|
||||||
if (!event.sender.isDestroyed()) {
|
if (!event.sender.isDestroyed()) {
|
||||||
event.sender.send('error-start-list-streaming', err)
|
event.sender.send('error-start-list-streaming', err)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('stop-list-streaming', () => {
|
ipcMain.on('stop-list-streaming', () => {
|
||||||
|
@ -749,18 +748,19 @@ type Tag = {
|
||||||
tag: string
|
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
|
const { tag, account } = obj
|
||||||
accountManager
|
try {
|
||||||
.getAccount(account._id!)
|
const acct = await accountManager.getAccount(account._id!)
|
||||||
.then(acct => {
|
|
||||||
// Stop old tag streaming
|
// Stop old tag streaming
|
||||||
if (tagStreaming !== null) {
|
if (tagStreaming !== null) {
|
||||||
tagStreaming.stop()
|
tagStreaming.stop()
|
||||||
tagStreaming = null
|
tagStreaming = null
|
||||||
}
|
}
|
||||||
|
|
||||||
tagStreaming = new WebSocket(acct)
|
const url = await StreamingURL(acct)
|
||||||
|
tagStreaming = new WebSocket(acct, url)
|
||||||
tagStreaming.start(
|
tagStreaming.start(
|
||||||
`hashtag&tag=${tag}`,
|
`hashtag&tag=${tag}`,
|
||||||
(update: Status) => {
|
(update: Status) => {
|
||||||
|
@ -780,13 +780,12 @@ ipcMain.on('start-tag-streaming', (event: Event, obj: Tag & StreamingSetting) =>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
} catch (err) {
|
||||||
.catch(err => {
|
|
||||||
log.error(err)
|
log.error(err)
|
||||||
if (!event.sender.isDestroyed()) {
|
if (!event.sender.isDestroyed()) {
|
||||||
event.sender.send('error-start-tag-streaming', err)
|
event.sender.send('error-start-tag-streaming', err)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('stop-tag-streaming', () => {
|
ipcMain.on('stop-tag-streaming', () => {
|
||||||
|
|
|
@ -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 log from 'electron-log'
|
||||||
import { LocalAccount } from '~/src/types/localAccount'
|
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 {
|
export default class WebSocket {
|
||||||
private client: Mastodon
|
private client: Mastodon
|
||||||
private listener: SocketListener | null
|
private listener: SocketListener | null
|
||||||
|
|
||||||
constructor(account: LocalAccount) {
|
constructor(account: LocalAccount, streamingURL: string) {
|
||||||
const url = account.baseURL.replace(/^https:\/\//, 'wss://')
|
const url = streamingURL.replace(/^https:\/\//, 'wss://')
|
||||||
this.client = new Mastodon(account.accessToken!, url + '/api/v1')
|
this.client = new Mastodon(account.accessToken!, url + '/api/v1')
|
||||||
this.listener = null
|
this.listener = null
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue