Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #105 Get recent timeline in local and public #109

Merged
merged 4 commits into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/renderer/components/TimelineSpace/Local.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
26 changes: 24 additions & 2 deletions src/renderer/components/TimelineSpace/Public.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions src/renderer/store/TimelineSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
})
Expand All @@ -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)
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/store/TimelineSpace/Favourites.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Favourites = {
favourites: []
},
mutations: {
insertFavourites (state, favourites) {
updateFavourites (state, favourites) {
state.favourites = favourites
},
updateToot (state, message) {
Expand Down Expand Up @@ -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)
})
})
Expand Down
23 changes: 21 additions & 2 deletions src/renderer/store/TimelineSpace/Local.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ipcRenderer } from 'electron'
import Mastodon from 'mastodon-api'

const Local = {
namespaced: true,
Expand All @@ -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) {
Expand All @@ -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)
})
Expand Down
23 changes: 21 additions & 2 deletions src/renderer/store/TimelineSpace/Public.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ipcRenderer } from 'electron'
import Mastodon from 'mastodon-api'

const Public = {
namespaced: true,
Expand All @@ -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) {
Expand All @@ -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)
})
Expand Down