Skip to content

Commit

Permalink
refs #2500 Fix streamings for tag and lists
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Jan 2, 2023
1 parent 1484f9e commit 61abf81
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 138 deletions.
206 changes: 91 additions & 115 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -546,116 +546,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)
Expand Down Expand Up @@ -1191,10 +1081,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) => {
Expand Down Expand Up @@ -1437,3 +1327,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)
}
})
17 changes: 6 additions & 11 deletions src/renderer/store/TimelineSpace/Contents/Hashtag/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,28 @@ 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
return new Promise((resolve, reject) => {
// 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)
})
},
Expand Down
19 changes: 7 additions & 12 deletions src/renderer/store/TimelineSpace/Contents/Lists/Show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
},
Expand Down

0 comments on commit 61abf81

Please sign in to comment.