From 8fcb46dc7f5982b0104dd34f43240099a3c16e52 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sat, 17 Aug 2019 19:16:08 +0200 Subject: [PATCH] Add globalTimeline - Add EventSource without Authorization header --- src/components/ShortTimeline.vue | 45 ++++++++++++++++++++++++++++++++ src/store.js | 29 ++++++++++++++++---- src/views/Home.vue | 9 +++++-- 3 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 src/components/ShortTimeline.vue diff --git a/src/components/ShortTimeline.vue b/src/components/ShortTimeline.vue new file mode 100644 index 0000000..0fc5bdd --- /dev/null +++ b/src/components/ShortTimeline.vue @@ -0,0 +1,45 @@ + + + + + \ No newline at end of file diff --git a/src/store.js b/src/store.js index 808ea8e..2b969cd 100644 --- a/src/store.js +++ b/src/store.js @@ -17,7 +17,8 @@ export default new Vuex.Store({ micropubEndpoint: '', microsubEndpoint: '/microsub', channelCreatorIsOpen: false, - eventSource: null + eventSource: null, + globalTimeline: {items: [{name:'Testing the global timeline'}]} }; let loginData = JSON.parse(window.localStorage.getItem('login_data')) if (loginData) { @@ -62,10 +63,10 @@ export default new Vuex.Store({ state.microsubEndpoint = endpoints.microsubEndpoint }, createEventSource(state, url) { - state.eventSource = new EventSource(state.microsubEndpoint + url, { - headers: { - 'Authorization': 'Bearer ' + this.state.access_token - } + state.eventSource = new EventSource(state.microsubEndpoint + url + "&access_token=" + state.access_token, { + // headers: { + // 'Authorization': 'Bearer ' + this.state.access_token + // } }) state.eventSource.addEventListener('open', evt => { // eslint-disable-next-line @@ -83,6 +84,24 @@ export default new Vuex.Store({ // eslint-disable-next-line console.log(evt) }) + state.eventSource.addEventListener('new item', evt => { + let item = JSON.parse(evt.data) + // eslint-disable-next-line + state.timeline.items = [...state.timeline.items, item] + state.globalTimeline.items = _.take([item, ...state.globalTimeline.items], 10) + }) + state.eventSource.addEventListener('new item in channel', evt => { + // eslint-disable-next-line + console.log(evt) + + let d = JSON.parse(evt.data) + // eslint-disable-next-line + console.log(d, state.channels) + let channel = _.find(state.channels, item => item.uid === d.uid) + if (channel) { + channel.unread = d.unread + } + }) } }, diff --git a/src/views/Home.vue b/src/views/Home.vue index 231c7c9..6af272a 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -25,7 +25,9 @@ @getPage="getPage" @followFeed="openFeedFollower(arguments[0])"> -
+
+ +
@@ -39,6 +41,7 @@ import ChannelCreator from '../components/ChannelCreator.vue' import FeedFollower from "../components/FeedFollower" import NewPost from "../components/NewPost" + import ShortTimeline from "../components/ShortTimeline"; export default { name: 'home', @@ -48,7 +51,8 @@ Channels, Channel, ChannelCreator, - NewPost + NewPost, + ShortTimeline }, data() { @@ -107,6 +111,7 @@ mounted() { if (this.$store.state.logged_in) { + this.$store.dispatch('startEventListening', '?action=events') this.$store.dispatch('fetchChannels') this.$store.dispatch('fetchTimeline', {uid: this.uid}) }