diff --git a/src/renderer/components/TimelineSpace/Local.vue b/src/renderer/components/TimelineSpace/Local.vue index 83e19f14..c5effa98 100644 --- a/src/renderer/components/TimelineSpace/Local.vue +++ b/src/renderer/components/TimelineSpace/Local.vue @@ -15,17 +15,39 @@ export default { components: { Toot }, computed: { ...mapState({ - account: state => state.TimelineSpace.account, timeline: state => state.TimelineSpace.Local.timeline }) }, created () { - this.$store.dispatch('TimelineSpace/Local/startLocalStreaming', this.account) + const loading = this.$loading({ + lock: true, + text: 'Loading', + spinner: 'el-icon-loading', + background: 'rgba(0, 0, 0, 0.7)' + }) + this.initialize() + .then(() => { + loading.close() + }) + .catch(() => { + loading.close() + }) }, beforeDestroy () { this.$store.dispatch('TimelineSpace/Local/stopLocalStreaming') }, methods: { + async initialize () { + try { + await this.$store.dispatch('TimelineSpace/Local/fetchLocalTimeline') + } catch (err) { + this.$message({ + message: 'Could not fetch timeline', + type: 'error' + }) + } + this.$store.dispatch('TimelineSpace/Local/startLocalStreaming') + }, updateToot (message) { this.$store.commit('TimelineSpace/Local/updateToot', message) } diff --git a/src/renderer/components/TimelineSpace/Public.vue b/src/renderer/components/TimelineSpace/Public.vue index 981a5581..99d8f765 100644 --- a/src/renderer/components/TimelineSpace/Public.vue +++ b/src/renderer/components/TimelineSpace/Public.vue @@ -15,17 +15,39 @@ export default { components: { Toot }, computed: { ...mapState({ - account: state => state.TimelineSpace.account, timeline: state => state.TimelineSpace.Public.timeline }) }, created () { - this.$store.dispatch('TimelineSpace/Public/startPublicStreaming', this.account) + const loading = this.$loading({ + lock: true, + text: 'Loading', + spinner: 'el-icon-loading', + background: 'rgba(0, 0, 0, 0.7)' + }) + this.initialize() + .then(() => { + loading.close() + }) + .catch(() => { + loading.close() + }) }, beforeDestroy () { this.$store.dispatch('TimelineSpace/Public/stopPublicStreaming') }, methods: { + async initialize () { + try { + await this.$store.dispatch('TimelineSpace/Public/fetchPublicTimeline') + } catch (err) { + this.$message({ + message: 'Could not fetch timeline', + type: 'error' + }) + } + this.$store.dispatch('TimelineSpace/Public/startPublicStreaming') + }, updateToot (message) { this.$store.commit('TimelineSpace/Public/updateToot', message) } diff --git a/src/renderer/store/TimelineSpace.js b/src/renderer/store/TimelineSpace.js index 8af31664..a1e37ade 100644 --- a/src/renderer/store/TimelineSpace.js +++ b/src/renderer/store/TimelineSpace.js @@ -42,11 +42,11 @@ const TimelineSpace = { appendNotifications (state, notifications) { state.notifications = [notifications].concat(state.notifications) }, - insertHomeTimeline (state, messages) { - state.homeTimeline = state.homeTimeline.concat(messages) + updateHomeTimeline (state, messages) { + state.homeTimeline = messages }, - insertNotifications (state, notifications) { - state.notifications = state.notifications.concat(notifications) + updateNotifications (state, notifications) { + state.notifications = notifications }, updateToot (state, message) { // Replace target message in homeTimeline and notifications @@ -163,7 +163,7 @@ const TimelineSpace = { ) client.get('/timelines/home', { limit: 40 }, (err, data, res) => { if (err) return reject(err) - commit('insertHomeTimeline', data) + commit('updateHomeTimeline', data) resolve(res) }) }) @@ -178,7 +178,7 @@ const TimelineSpace = { ) client.get('/notifications', { limit: 30 }, (err, data, res) => { if (err) return reject(err) - commit('insertNotifications', data) + commit('updateNotifications', data) resolve(res) }) }) diff --git a/src/renderer/store/TimelineSpace/Favourites.js b/src/renderer/store/TimelineSpace/Favourites.js index f0463f78..efce75da 100644 --- a/src/renderer/store/TimelineSpace/Favourites.js +++ b/src/renderer/store/TimelineSpace/Favourites.js @@ -6,7 +6,7 @@ const Favourites = { favourites: [] }, mutations: { - insertFavourites (state, favourites) { + updateFavourites (state, favourites) { state.favourites = favourites }, updateToot (state, message) { @@ -37,7 +37,7 @@ const Favourites = { ) client.get('/favourites', { limit: 40 }, (err, data, res) => { if (err) return reject(err) - commit('insertFavourites', data) + commit('updateFavourites', data) resolve(res) }) }) diff --git a/src/renderer/store/TimelineSpace/Local.js b/src/renderer/store/TimelineSpace/Local.js index 693fa55f..7bb50d48 100644 --- a/src/renderer/store/TimelineSpace/Local.js +++ b/src/renderer/store/TimelineSpace/Local.js @@ -1,4 +1,5 @@ import { ipcRenderer } from 'electron' +import Mastodon from 'mastodon-api' const Local = { namespaced: true, @@ -9,6 +10,9 @@ const Local = { appendTimeline (state, update) { state.timeline = [update].concat(state.timeline) }, + updateTimeline (state, messages) { + state.timeline = messages + }, updateToot (state, message) { state.timeline = state.timeline.map((toot) => { if (toot.id === message.id) { @@ -27,12 +31,27 @@ const Local = { } }, actions: { - startLocalStreaming ({ commit }, account) { + fetchLocalTimeline ({ state, commit, rootState }) { + return new Promise((resolve, reject) => { + const client = new Mastodon( + { + access_token: rootState.TimelineSpace.account.accessToken, + api_url: rootState.TimelineSpace.account.baseURL + '/api/v1' + } + ) + client.get('/timelines/public', { limit: 40, local: true }, (err, data, res) => { + if (err) return reject(err) + commit('updateTimeline', data) + resolve(res) + }) + }) + }, + startLocalStreaming ({ state, commit, rootState }) { ipcRenderer.on('update-start-local-streaming', (event, update) => { commit('appendTimeline', update) }) return new Promise((resolve, reject) => { - ipcRenderer.send('start-local-streaming', account) + ipcRenderer.send('start-local-streaming', rootState.TimelineSpace.account) ipcRenderer.once('error-start-local-streaming', (event, err) => { reject(err) }) diff --git a/src/renderer/store/TimelineSpace/Public.js b/src/renderer/store/TimelineSpace/Public.js index 6174f61c..cdfa2aae 100644 --- a/src/renderer/store/TimelineSpace/Public.js +++ b/src/renderer/store/TimelineSpace/Public.js @@ -1,4 +1,5 @@ import { ipcRenderer } from 'electron' +import Mastodon from 'mastodon-api' const Public = { namespaced: true, @@ -9,6 +10,9 @@ const Public = { appendTimeline (state, update) { state.timeline = [update].concat(state.timeline) }, + updateTimeline (state, messages) { + state.timeline = messages + }, updateToot (state, message) { state.timeline = state.timeline.map((toot) => { if (toot.id === message.id) { @@ -27,12 +31,27 @@ const Public = { } }, actions: { - startPublicStreaming ({ commit }, account) { + fetchPublicTimeline ({ state, commit, rootState }) { + return new Promise((resolve, reject) => { + const client = new Mastodon( + { + access_token: rootState.TimelineSpace.account.accessToken, + api_url: rootState.TimelineSpace.account.baseURL + '/api/v1' + } + ) + client.get('/timelines/public', { limit: 40 }, (err, data, res) => { + if (err) return reject(err) + commit('updateTimeline', data) + resolve(res) + }) + }) + }, + startPublicStreaming ({ state, commit, rootState }) { ipcRenderer.on('update-start-public-streaming', (event, update) => { commit('appendTimeline', update) }) return new Promise((resolve, reject) => { - ipcRenderer.send('start-public-streaming', account) + ipcRenderer.send('start-public-streaming', rootState.TimelineSpace.account) ipcRenderer.once('error-start-public-streaming', (event, err) => { reject(err) })