1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2025-01-27 07:46:15 +01:00

Merge pull request #54 from h3poteto/iss-24

refs #24 Stream local/public timeline
This commit is contained in:
AkiraFukushima 2018-03-14 14:58:55 +09:00 committed by GitHub
commit bd1cd482eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 241 additions and 28 deletions

View File

@ -258,6 +258,72 @@ ipcMain.on('stop-user-streaming', (event, _) => {
userStreaming = null
})
let localStreaming = null
ipcMain.on('start-local-streaming', (event, ac) => {
const account = new Account(db)
account.getAccount(ac._id)
.catch((err) => {
event.sender.send('error-start-local-streaming', err)
})
.then((account) => {
// Stop old local streaming
if (localStreaming !== null) {
localStreaming.stop()
localStreaming = null
}
localStreaming = new Streaming(account)
localStreaming.start(
'/streaming/public/local',
(update) => {
event.sender.send('update-start-local-streaming', update)
},
(err) => {
event.sender.send('error-start-local-streaming', err)
}
)
})
})
ipcMain.on('stop-local-streaming', (event, _) => {
localStreaming.stop()
localStreaming = null
})
let publicStreaming = null
ipcMain.on('start-public-streaming', (event, ac) => {
const account = new Account(db)
account.getAccount(ac._id)
.catch((err) => {
event.sender.send('error-start-public-streaming', err)
})
.then((account) => {
// Stop old public streaming
if (publicStreaming !== null) {
publicStreaming.stop()
publicStreaming = null
}
publicStreaming = new Streaming(account)
publicStreaming.start(
'/streaming/public',
(update) => {
event.sender.send('update-start-public-streaming', update)
},
(err) => {
event.sender.send('error-start-public-streaming', err)
}
)
})
})
ipcMain.on('stop-public-streaming', (event, _) => {
publicStreaming.stop()
publicStreaming = null
})
/**
* Auto Updater
*

View File

@ -14,6 +14,7 @@ export default class Streaming {
startUserStreaming (updateCallback, notificationCallback, errCallback) {
this.listener = this.client.stream('/streaming/user')
console.log('/streaming/user started')
this.listener.on('message', (msg) => {
switch (msg.event) {
@ -34,7 +35,27 @@ export default class Streaming {
})
}
start (path, updateCallback, errCallback) {
this.listener = this.client.stream(path)
console.log(`${path} started`)
this.listener.on('message', (msg) => {
switch (msg.event) {
case 'update':
updateCallback(msg.data)
break
default:
break
}
})
this.listener.on('error', (err) => {
errCallback(err)
})
}
stop () {
this.listener.stop()
console.log('streaming stopped')
}
}

View File

@ -22,6 +22,7 @@ export default {
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
})
this.$store.dispatch('TimelineSpace/watchShortcutEvents')
this.$store.dispatch('TimelineSpace/fetchAccount', this.$route.params.id)
.then((account) => {
this.$store.dispatch('TimelineSpace/fetchHomeTimeline', account)
@ -35,13 +36,6 @@ export default {
type: 'error'
})
})
this.$store.dispatch('TimelineSpace/startUserStreaming', account)
.catch(() => {
this.$message({
message: 'Could not start user streaming',
type: 'error'
})
})
this.$store.dispatch('TimelineSpace/username', account)
.catch(() => {
this.$message({
@ -56,7 +50,13 @@ export default {
type: 'error'
})
})
this.$store.dispatch('TimelineSpace/watchShortcutEvents', account)
this.$store.dispatch('TimelineSpace/startUserStreaming', account)
.catch(() => {
this.$message({
message: 'Could not start user streaming',
type: 'error'
})
})
})
.catch(() => {
loading.close()
@ -68,6 +68,7 @@ export default {
},
beforeDestroy () {
this.$store.dispatch('TimelineSpace/stopUserStreaming')
this.$store.dispatch('TimelineSpace/removeShortcutEvents')
}
}
</script>

View File

@ -1,11 +0,0 @@
<template>
<div id="global">
global
</div>
</template>
<script>
export default {
name: 'global'
}
</script>

View File

@ -1,11 +1,35 @@
<template>
<div id="local">
local
<div class="local-timeline" v-for="message in timeline" v-bind:key="message.id">
<toot :message="message"></toot>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
import Toot from './Cards/Toot'
export default {
name: 'local'
name: 'local',
components: { Toot },
computed: {
...mapState({
account: state => state.TimelineSpace.account,
timeline: state => state.TimelineSpace.Local.timeline
})
},
created () {
this.$store.dispatch('TimelineSpace/Local/startLocalStreaming', this.account)
},
beforeDestroy () {
this.$store.dispatch('TimelineSpace/Local/stopLocalStreaming')
}
}
</script>
<style lang="scss" scoped>
#local {
margin-left: 16px;
}
</style>

View File

@ -0,0 +1,35 @@
<template>
<div id="public">
<div class="public-timeline" v-for="message in timeline" v-bind:key="message.id">
<toot :message="message"></toot>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
import Toot from './Cards/Toot'
export default {
name: 'public',
components: { Toot },
computed: {
...mapState({
account: state => state.TimelineSpace.account,
timeline: state => state.TimelineSpace.Public.timeline
})
},
created () {
this.$store.dispatch('TimelineSpace/Public/startPublicStreaming', this.account)
},
beforeDestroy () {
this.$store.dispatch('TimelineSpace/Public/stopPublicStreaming')
}
}
</script>
<style lang="scss" scoped>
#public {
margin-left: 16px;
}
</style>

View File

@ -29,7 +29,7 @@
<icon name="users"></icon>
<span>LocalTimeline</span>
</el-menu-item>
<el-menu-item :index="`/${id()}/global`">
<el-menu-item :index="`/${id()}/public`">
<icon name="globe"></icon>
<span>PublicTimeline</span>
</el-menu-item>

View File

@ -46,9 +46,9 @@ export default new Router({
component: require('@/components/TimelineSpace/Local').default
},
{
path: 'global',
name: 'global',
component: require('@/components/TimelineSpace/Global').default
path: 'public',
name: 'public',
component: require('@/components/TimelineSpace/Public').default
}
]
}

View File

@ -2,13 +2,17 @@ import { ipcRenderer } from 'electron'
import Mastodon from 'mastodon-api'
import SideMenu from './TimelineSpace/SideMenu'
import Favourites from './TimelineSpace/Favourites'
import Local from './TimelineSpace/Local'
import Public from './TimelineSpace/Public'
import router from '../router'
const TimelineSpace = {
namespaced: true,
modules: {
SideMenu,
Favourites
Favourites,
Local,
Public
},
state: {
account: {
@ -84,15 +88,18 @@ const TimelineSpace = {
return new Promise((resolve, reject) => {
ipcRenderer.send('start-user-streaming', account)
ipcRenderer.once('error-start-userstreaming', (event, err) => {
ipcRenderer.once('error-start-user-streaming', (event, err) => {
reject(err)
})
})
},
stopUserStreaming ({ commit }) {
ipcRenderer.removeAll('update-start-user-streaming')
ipcRenderer.removeAll('notification-start-user-streaming')
ipcRenderer.removeAll('error-start-user-streaming')
ipcRenderer.send('stop-user-streaming')
},
watchShortcutEvents ({ commit }, account) {
watchShortcutEvents ({ commit }) {
ipcRenderer.on('CmdOrCtrl+N', () => {
commit('changeNewTootModal', true)
})
@ -101,6 +108,10 @@ const TimelineSpace = {
console.log('reply')
})
},
removeShortcutEvents () {
ipcRenderer.removeAll('CmdOrCtrl+N')
ipcRenderer.removeAll('CmdOrCtrl+R')
},
fetchHomeTimeline ({ commit }, account) {
return new Promise((resolve, reject) => {
const client = new Mastodon(

View File

@ -0,0 +1,33 @@
import { ipcRenderer } from 'electron'
const Local = {
namespaced: true,
state: {
timeline: []
},
mutations: {
appendTimeline (state, update) {
state.timeline = [update].concat(state.timeline)
}
},
actions: {
startLocalStreaming ({ commit }, account) {
ipcRenderer.on('update-start-local-streaming', (event, update) => {
commit('appendTimeline', update)
})
return new Promise((resolve, reject) => {
ipcRenderer.send('start-local-streaming', account)
ipcRenderer.once('error-start-local-streaming', (event, err) => {
reject(err)
})
})
},
stopLocalStreaming ({ commit }) {
ipcRenderer.removeAllListeners('error-start-local-streaming')
ipcRenderer.removeAllListeners('update-start-local-streaming')
ipcRenderer.send('stop-local-streaming')
}
}
}
export default Local

View File

@ -0,0 +1,33 @@
import { ipcRenderer } from 'electron'
const Public = {
namespaced: true,
state: {
timeline: []
},
mutations: {
appendTimeline (state, update) {
state.timeline = [update].concat(state.timeline)
}
},
actions: {
startPublicStreaming ({ commit }, account) {
ipcRenderer.on('update-start-public-streaming', (event, update) => {
commit('appendTimeline', update)
})
return new Promise((resolve, reject) => {
ipcRenderer.send('start-public-streaming', account)
ipcRenderer.once('error-start-public-streaming', (event, err) => {
reject(err)
})
})
},
stopPublicStreaming ({ commit }) {
ipcRenderer.removeAllListeners('error-start-public-streaming')
ipcRenderer.removeAllListeners('update-start-public-streaming')
ipcRenderer.send('stop-public-streaming')
}
}
}
export default Public