Timeline.vue 2.29 KB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
1
<template>
Bat's avatar
Bat committed
2
  <div class="main pusher" v-title="'Instance Timeline'">
Eliot Berriot's avatar
Eliot Berriot committed
3
    <div class="ui vertical center aligned stripe segment">
4
      <div v-if="isLoading" :class="['ui', {'active': isLoading}, 'inverted', 'dimmer']">
Bat's avatar
Bat committed
5
        <div class="ui text loader">{{ $t('Loading timeline...') }}</div>
6
7
      </div>
      <div v-else class="ui text container">
Bat's avatar
Bat committed
8
        <h1 class="ui header">{{ $t('Recent activity on this instance') }}</h1>
Eliot Berriot's avatar
Eliot Berriot committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        <div class="ui feed">
          <component
            class="event"
            v-for="(event, index) in events"
            :key="event.id + index"
            v-if="components[event.type]"
            :is="components[event.type]"
            :event="event">
          </component>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
26
27
28
import { WebSocketBridge } from 'django-channels'
import axios from 'axios'
import logger from '@/logging'
Eliot Berriot's avatar
Eliot Berriot committed
29
30
31
32
33
34
35

import Like from '@/components/activity/Like'
import Listen from '@/components/activity/Listen'

export default {
  data () {
    return {
36
      isLoading: false,
Eliot Berriot's avatar
Eliot Berriot committed
37
38
39
40
41
42
      components: {
        'Like': Like,
        'Listen': Listen
      }
    }
  },
43
44
45
46
  created () {
    this.openWebsocket()
    this.fetchEvents()
  },
Eliot Berriot's avatar
Eliot Berriot committed
47
48
49
50
  computed: {
    ...mapState({
      events: state => state.instance.events
    })
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  },
  methods: {
    fetchEvents () {
      this.isLoading = true
      let self = this
      axios.get('/activity/').then((response) => {
        self.isLoading = false
        self.$store.commit('instance/events', response.data.results)
      })
    },
    openWebsocket () {
      if (!this.$store.state.auth.authenticated) {
        return
      }
      let self = this
      let token = this.$store.state.auth.token
      // let token = 'test'
      const bridge = new WebSocketBridge()
      bridge.connect(
        `/api/v1/instance/activity?token=${token}`,
        null,
        {reconnectInterval: 5000})
      bridge.listen(function (event) {
        logger.default.info('Received timeline update', event)
        self.$store.commit('instance/event', event)
      })
      bridge.socket.addEventListener('open', function () {
        console.log('Connected to WebSocket')
      })
    }
Eliot Berriot's avatar
Eliot Berriot committed
81
82
83
84
85
86
87
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>