Timeline.vue 2.51 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
5
6
7
      <div v-if="isLoading" :class="['ui', {'active': isLoading}, 'inverted', 'dimmer']">
        <div class="ui text loader">Loading timeline...</div>
      </div>
      <div v-else class="ui text container">
Eliot Berriot's avatar
Eliot Berriot committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        <h1 class="ui header">Recent activity on this instance</h1>
        <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">
            <username
              class="user"
              :username="event.actor.local_id"
              slot="user"></username>
              {{ event.published }}
            <human-date class="date" :date="event.published" slot="date"></human-date>
          </component>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
32
33
34
import { WebSocketBridge } from 'django-channels'
import axios from 'axios'
import logger from '@/logging'
Eliot Berriot's avatar
Eliot Berriot committed
35
36
37
38
39
40
41

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

export default {
  data () {
    return {
42
      isLoading: false,
Eliot Berriot's avatar
Eliot Berriot committed
43
44
45
46
47
48
      components: {
        'Like': Like,
        'Listen': Listen
      }
    }
  },
49
50
51
52
  created () {
    this.openWebsocket()
    this.fetchEvents()
  },
Eliot Berriot's avatar
Eliot Berriot committed
53
54
55
56
  computed: {
    ...mapState({
      events: state => state.instance.events
    })
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  },
  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
87
88
89
90
91
92
93
  }
}
</script>

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