refs #2500 Fix streamings for tag and lists

This commit is contained in:
AkiraFukushima 2023-01-02 00:30:07 +09:00
parent 30fddef463
commit 20a22cc30e
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
3 changed files with 104 additions and 138 deletions

View File

@ -36,7 +36,7 @@ import { insertTag, listTags, removeTag } from './db/hashtags'
import { createOrUpdateSetting, getSetting } from './db/setting'
import { insertServer } from './db/server'
import { DirectStreaming, LocalStreaming, PublicStreaming, StreamingURL, UserStreaming } from './websocket'
import { DirectStreaming, ListStreaming, LocalStreaming, PublicStreaming, StreamingURL, TagStreaming, UserStreaming } from './websocket'
import Preferences from './preferences'
import Fonts from './fonts'
import i18next from '~/src/config/i18n'
@ -569,116 +569,6 @@ ipcMain.on('reset-badge', () => {
}
})
// let listStreaming: ListStreaming | null = null
// type ListStreamingOpts = {
// listID: string
// accountID: string
// }
// ipcMain.on('start-list-streaming', async (event: IpcMainEvent, obj: ListStreamingOpts) => {
// const { listID, accountID } = obj
// try {
// const acct = await accountRepo.getAccount(accountID)
// // Stop old list streaming
// if (listStreaming !== null) {
// listStreaming.stop()
// listStreaming = null
// }
// const proxy = await proxyConfiguration.forMastodon()
// const sns = await detector(acct.baseURL, proxy)
// const url = await StreamingURL(sns, acct, proxy)
// listStreaming = new ListStreaming(sns, acct, url, proxy)
// listStreaming.start(
// listID,
// (update: Entity.Status) => {
// if (!event.sender.isDestroyed()) {
// event.sender.send('update-start-list-streaming', update)
// }
// },
// (id: string) => {
// if (!event.sender.isDestroyed()) {
// event.sender.send('delete-start-list-streaming', id)
// }
// },
// (err: Error) => {
// log.error(err)
// if (!event.sender.isDestroyed()) {
// event.sender.send('error-start-list-streaming', err)
// }
// }
// )
// } catch (err) {
// log.error(err)
// if (!event.sender.isDestroyed()) {
// event.sender.send('error-start-list-streaming', err)
// }
// }
// })
// ipcMain.on('stop-list-streaming', () => {
// if (listStreaming !== null) {
// listStreaming.stop()
// listStreaming = null
// }
// })
// let tagStreaming: TagStreaming | null = null
// type TagStreamingOpts = {
// tag: string
// accountID: string
// }
// ipcMain.on('start-tag-streaming', async (event: IpcMainEvent, obj: TagStreamingOpts) => {
// const { tag, accountID } = obj
// try {
// const acct = await accountRepo.getAccount(accountID)
// // Stop old tag streaming
// if (tagStreaming !== null) {
// tagStreaming.stop()
// tagStreaming = null
// }
// const proxy = await proxyConfiguration.forMastodon()
// const sns = await detector(acct.baseURL, proxy)
// const url = await StreamingURL(sns, acct, proxy)
// tagStreaming = new TagStreaming(sns, acct, url, proxy)
// tagStreaming.start(
// tag,
// (update: Entity.Status) => {
// if (!event.sender.isDestroyed()) {
// event.sender.send('update-start-tag-streaming', update)
// }
// },
// (id: string) => {
// if (!event.sender.isDestroyed()) {
// event.sender.send('delete-start-tag-streaming', id)
// }
// },
// (err: Error) => {
// log.error(err)
// if (!event.sender.isDestroyed()) {
// event.sender.send('error-start-tag-streaming', err)
// }
// }
// )
// } catch (err) {
// log.error(err)
// if (!event.sender.isDestroyed()) {
// event.sender.send('error-start-tag-streaming', err)
// }
// }
// })
// ipcMain.on('stop-tag-streaming', () => {
// if (tagStreaming !== null) {
// tagStreaming.stop()
// tagStreaming = null
// }
// })
// sounds
ipcMain.on('fav-rt-action-sound', () => {
const preferences = new Preferences(preferencesDBPath)
@ -1214,10 +1104,10 @@ const decodeLanguage = (lang: string): LanguageType => {
//----------------------------------------------
// Streamings
//----------------------------------------------
let userStreamings: { [key: number]: UserStreaming } = []
let directStreamings: { [key: number]: DirectStreaming } = []
let localStreamings: { [key: number]: DirectStreaming } = []
let publicStreamings: { [key: number]: DirectStreaming } = []
const userStreamings: { [key: number]: UserStreaming } = {}
const directStreamings: { [key: number]: DirectStreaming } = {}
const localStreamings: { [key: number]: DirectStreaming } = {}
const publicStreamings: { [key: number]: DirectStreaming } = {}
const stopAllStreamings = () => {
Object.keys(userStreamings).forEach((key: string) => {
@ -1460,3 +1350,89 @@ const username = (account: Entity.Account): string => {
return account.username
}
}
//----------------------------------------
// List streamings
//----------------------------------------
const listStreamings: { [key: number]: ListStreaming } = {}
type ListStreamingOpts = {
listId: string
accountId: number
}
ipcMain.on('start-list-streaming', async (event: IpcMainEvent, obj: ListStreamingOpts) => {
const { listId, accountId } = obj
try {
const [account, server] = await getAccount(db, accountId)
// Stop old list streaming
if (listStreamings[accountId] !== undefined) {
listStreamings[accountId].stop()
}
const proxy = await proxyConfiguration.forMastodon()
const url = await StreamingURL(server.sns, account, server, proxy)
listStreamings[accountId] = new ListStreaming(server.sns, account, url, proxy)
listStreamings[accountId].start(
listId,
(update: Entity.Status) => {
if (!event.sender.isDestroyed()) {
event.sender.send(`update-list-streamings-${accountId}`, update)
}
},
(id: string) => {
if (!event.sender.isDestroyed()) {
event.sender.send(`delete-list-streamings-${accountId}`, id)
}
},
(err: Error) => {
log.error(err)
}
)
} catch (err) {
log.error(err)
}
})
//----------------------------------------
// Tag streamings
//----------------------------------------
const tagStreamings: { [key: number]: TagStreaming } = {}
type TagStreamingOpts = {
tag: string
accountId: number
}
ipcMain.on('start-tag-streaming', async (event: IpcMainEvent, obj: TagStreamingOpts) => {
const { tag, accountId } = obj
try {
const [account, server] = await getAccount(db, accountId)
// Stop old tag streaming
if (tagStreamings[accountId] !== undefined) {
tagStreamings[accountId].stop()
}
const proxy = await proxyConfiguration.forMastodon()
const url = await StreamingURL(server.sns, account, server, proxy)
tagStreamings[accountId] = new TagStreaming(server.sns, account, url, proxy)
tagStreamings[accountId].start(
tag,
(update: Entity.Status) => {
if (!event.sender.isDestroyed()) {
event.sender.send(`update-tag-streamings-${accountId}`, update)
}
},
(id: string) => {
if (!event.sender.isDestroyed()) {
event.sender.send(`delete-tag-streamings-${accountId}`, id)
}
},
(err: Error) => {
log.error(err)
}
)
} catch (err) {
log.error(err)
}
})

View File

@ -114,13 +114,13 @@ const actions: ActionTree<TagState, RootState> = {
return res.data
},
[ACTION_TYPES.START_STREAMING]: ({ state, commit, rootState }, tag: string) => {
win.ipcRenderer.on('update-start-tag-streaming', (_, update: Entity.Status) => {
win.ipcRenderer.on(`update-tag-streamings-${rootState.TimelineSpace.account!.id}`, (_, update: Entity.Status) => {
commit(MUTATION_TYPES.APPEND_TIMELINE, update)
if (state.heading && Math.random() > 0.8) {
commit(MUTATION_TYPES.ARCHIVE_TIMELINE)
}
})
win.ipcRenderer.on('delete-start-tag-streaming', (_, id: string) => {
win.ipcRenderer.on(`delete-tag-streamings-${rootState.TimelineSpace.account!.id}`, (_, id: string) => {
commit(MUTATION_TYPES.DELETE_TOOT, id)
})
// @ts-ignore
@ -128,19 +128,14 @@ const actions: ActionTree<TagState, RootState> = {
// eslint-disable-line no-unused-vars
win.ipcRenderer.send('start-tag-streaming', {
tag: encodeURIComponent(tag),
accountID: rootState.TimelineSpace.account!.id
})
win.ipcRenderer.once('error-start-tag-streaming', (_, err: Error) => {
reject(err)
accountId: rootState.TimelineSpace.account!.id
})
})
},
[ACTION_TYPES.STOP_STREAMING]: () => {
[ACTION_TYPES.STOP_STREAMING]: ({ rootState }) => {
return new Promise(resolve => {
win.ipcRenderer.removeAllListeners('error-start-tag-streaming')
win.ipcRenderer.removeAllListeners('update-start-tag-streaming')
win.ipcRenderer.removeAllListeners('delete-start-tag-streaming')
win.ipcRenderer.send('stop-tag-streaming')
win.ipcRenderer.removeAllListeners(`update-tag-streamings-${rootState.TimelineSpace.account!.id}`)
win.ipcRenderer.removeAllListeners(`update-tag-streamings-${rootState.TimelineSpace.account!.id}`)
resolve(null)
})
},

View File

@ -114,33 +114,28 @@ const actions: ActionTree<ShowState, RootState> = {
return res.data
},
[ACTION_TYPES.START_STREAMING]: ({ state, commit, rootState }, listID: string) => {
win.ipcRenderer.on('update-start-list-streaming', (_, update: Entity.Status) => {
win.ipcRenderer.on(`update-list-streamings-${rootState.TimelineSpace.account!.id}`, (_, update: Entity.Status) => {
commit(MUTATION_TYPES.APPEND_TIMELINE, update)
if (state.heading && Math.random() > 0.8) {
commit(MUTATION_TYPES.ARCHIVE_TIMELINE)
}
})
win.ipcRenderer.on('delete-start-list-streaming', (_, id: string) => {
win.ipcRenderer.on(`delete-list-streamings-${rootState.TimelineSpace.account!.id}`, (_, id: string) => {
commit(MUTATION_TYPES.DELETE_TOOT, id)
})
// @ts-ignore
return new Promise((resolve, reject) => {
// eslint-disable-line no-unused-vars
win.ipcRenderer.send('start-list-streaming', {
listID: listID,
accountID: rootState.TimelineSpace.account!.id
})
win.ipcRenderer.once('error-start-list-streaming', (_, err: Error) => {
reject(err)
listId: listID,
accountId: rootState.TimelineSpace.account!.id
})
})
},
[ACTION_TYPES.STOP_STREAMING]: () => {
[ACTION_TYPES.STOP_STREAMING]: ({ rootState }) => {
return new Promise(resolve => {
win.ipcRenderer.removeAllListeners('error-start-list-streaming')
win.ipcRenderer.removeAllListeners('update-start-list-streaming')
win.ipcRenderer.removeAllListeners('delete-start-list-streaming')
win.ipcRenderer.send('stop-list-streaming')
win.ipcRenderer.removeAllListeners(`update-list-streamings-${rootState.TimelineSpace.account!.id}`)
win.ipcRenderer.removeAllListeners(`delete-list-streamings-${rootState.TimelineSpace.account!.id}`)
resolve(null)
})
},