1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2025-02-04 11:17:32 +01:00

Merge pull request #109 from h3poteto/iss-105

closes #105 Get recent timeline in local and public
This commit is contained in:
AkiraFukushima 2018-03-24 09:34:30 +09:00 committed by GitHub
commit b5b97d554e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 16 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
})
})

View File

@ -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)
})
})

View File

@ -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)
})

View File

@ -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)
})