aboutsummaryrefslogtreecommitdiff
path: root/src/components/timeline/timeline.js
blob: 660a87527cecf32e859d984da0787733c8dd6ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import Status from '../status/status.vue'
import timelineFetcher from '../../services/timeline_fetcher/timeline_fetcher.service.js'
import StatusOrConversation from '../status_or_conversation/status_or_conversation.vue'
import UserCard from '../user_card/user_card.vue'

const Timeline = {
  props: [
    'timeline',
    'timelineName',
    'title',
    'userId',
    'tag'
  ],
  data () {
    return {
      paused: false
    }
  },
  computed: {
    timelineError () { return this.$store.state.statuses.error },
    followers () {
      return this.timeline.followers
    },
    friends () {
      return this.timeline.friends
    },
    viewing () {
      return this.timeline.viewing
    },
    newStatusCount () {
      return this.timeline.newStatusCount
    },
    newStatusCountStr () {
      if (this.timeline.flushMarker !== 0) {
        return ''
      } else {
        return ` (${this.newStatusCount})`
      }
    }
  },
  components: {
    Status,
    StatusOrConversation,
    UserCard
  },
  created () {
    const store = this.$store
    const credentials = store.state.users.currentUser.credentials
    const showImmediately = this.timeline.visibleStatuses.length === 0

    window.addEventListener('scroll', this.scrollLoad)

    timelineFetcher.fetchAndUpdate({
      store,
      credentials,
      timeline: this.timelineName,
      showImmediately,
      userId: this.userId,
      tag: this.tag
    })

    // don't fetch followers for public, friend, twkn
    if (this.timelineName === 'user') {
      this.fetchFriends()
      this.fetchFollowers()
    }
  },
  destroyed () {
    window.removeEventListener('scroll', this.scrollLoad)
    this.$store.commit('setLoading', { timeline: this.timelineName, value: false })
  },
  methods: {
    showNewStatuses () {
      if (this.timeline.flushMarker !== 0) {
        this.$store.commit('clearTimeline', { timeline: this.timelineName })
        this.$store.commit('queueFlush', { timeline: this.timelineName, id: 0 })
        this.fetchOlderStatuses()
      } else {
        this.$store.commit('showNewStatuses', { timeline: this.timelineName })
        this.paused = false
      }
    },
    fetchOlderStatuses () {
      const store = this.$store
      const credentials = store.state.users.currentUser.credentials
      store.commit('setLoading', { timeline: this.timelineName, value: true })
      timelineFetcher.fetchAndUpdate({
        store,
        credentials,
        timeline: this.timelineName,
        older: true,
        showImmediately: true,
        userId: this.userId,
        tag: this.tag
      }).then(() => store.commit('setLoading', { timeline: this.timelineName, value: false }))
    },
    fetchFollowers () {
      const id = this.userId
      this.$store.state.api.backendInteractor.fetchFollowers({ id })
        .then((followers) => this.$store.dispatch('addFollowers', { followers }))
    },
    fetchFriends () {
      const id = this.userId
      this.$store.state.api.backendInteractor.fetchFriends({ id })
        .then((friends) => this.$store.dispatch('addFriends', { friends }))
    },
    scrollLoad (e) {
      let height = Math.max(document.body.offsetHeight, document.body.scrollHeight)
      if (this.timeline.loading === false &&
          this.$store.state.config.autoLoad &&
          this.$el.offsetHeight > 0 &&
          (window.innerHeight + window.pageYOffset) >= (height - 750)) {
        this.fetchOlderStatuses()
      }
    }
  },
  watch: {
    newStatusCount (count) {
      if (!this.$store.state.config.streaming) {
        return
      }
      if (count > 0) {
        // only 'stream' them when you're scrolled to the top
        if (window.pageYOffset < 15 && !this.paused) {
          this.showNewStatuses()
        } else {
          this.paused = true
        }
      }
    }
  }
}

export default Timeline