2019-04-24 17:44:50 +00:00
|
|
|
function sortOrder(i, j) {
|
|
|
|
switch (true) {
|
|
|
|
case i < j:
|
|
|
|
return -1
|
|
|
|
case j < i:
|
|
|
|
return 1
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new Vue({
|
|
|
|
computed: {
|
|
|
|
},
|
|
|
|
|
|
|
|
data: {
|
|
|
|
tweets: [],
|
|
|
|
modalTweet: null,
|
|
|
|
},
|
|
|
|
|
|
|
|
el: '#app',
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
callModal(tweet) {
|
|
|
|
this.modalTweet = tweet
|
|
|
|
},
|
|
|
|
|
2020-01-12 12:37:20 +00:00
|
|
|
deleteTweet(tweet) {
|
|
|
|
axios
|
|
|
|
.delete(`/api/${tweet.id}`)
|
2020-01-12 12:59:20 +00:00
|
|
|
.then(() => this.removeTweet(tweet.id))
|
2020-01-12 12:37:20 +00:00
|
|
|
.catch(err => console.log(err))
|
|
|
|
},
|
|
|
|
|
2019-04-24 17:44:50 +00:00
|
|
|
favourite(tweet) {
|
|
|
|
axios
|
2020-01-12 12:37:20 +00:00
|
|
|
.put(`/api/${tweet.id}/favorite`)
|
2019-04-24 17:44:50 +00:00
|
|
|
.then(res => {
|
|
|
|
if (res.data.length === 0) {
|
|
|
|
this.refetch(tweet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.upsertTweets(res.data)
|
|
|
|
})
|
|
|
|
.catch(err => console.log(err))
|
|
|
|
},
|
|
|
|
|
|
|
|
notify(text, title = 'MediaTimeline Viewer') {
|
|
|
|
this.$bvToast.toast(text, {
|
|
|
|
title,
|
|
|
|
autoHideDelay: 3000,
|
|
|
|
appendToast: true,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
refetch(tweet) {
|
|
|
|
axios
|
2020-01-12 12:37:20 +00:00
|
|
|
.put(`/api/${tweet.id}/refresh`)
|
2019-04-24 17:44:50 +00:00
|
|
|
.then(res => {
|
2020-01-12 12:59:20 +00:00
|
|
|
if (res.data.gone) {
|
|
|
|
return this.removeTweet(tweet.id)
|
|
|
|
}
|
|
|
|
|
2019-04-24 17:44:50 +00:00
|
|
|
if (res.data.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.upsertTweets(res.data)
|
|
|
|
})
|
|
|
|
.catch(err => console.log(err))
|
|
|
|
},
|
|
|
|
|
|
|
|
refresh(forceReload = false) {
|
|
|
|
let apiURL = '/api/page' // By default query page 1
|
2020-01-12 12:59:20 +00:00
|
|
|
let append = false
|
2019-04-24 17:44:50 +00:00
|
|
|
if (this.tweets.length > 0 && !forceReload) {
|
|
|
|
apiURL = `/api/since?id=${this.tweets[0].id}`
|
2020-01-12 12:59:20 +00:00
|
|
|
append = true
|
2019-04-24 17:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
axios
|
|
|
|
.get(apiURL)
|
|
|
|
.then(resp => {
|
|
|
|
if (resp.data.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:59:20 +00:00
|
|
|
this.upsertTweets(resp.data, append)
|
2019-04-24 17:44:50 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-01-12 12:59:20 +00:00
|
|
|
removeTweet(id) {
|
|
|
|
const tweets = []
|
|
|
|
|
|
|
|
for (const i in this.tweets) {
|
|
|
|
const t = this.tweets[i]
|
|
|
|
if (t.id === id) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tweets.push(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tweets = tweets
|
|
|
|
},
|
|
|
|
|
2019-04-24 17:44:50 +00:00
|
|
|
triggerForceFetch() {
|
|
|
|
axios
|
2020-01-12 12:37:20 +00:00
|
|
|
.put('/api/force-reload')
|
2019-04-24 17:44:50 +00:00
|
|
|
.then(() => {
|
|
|
|
this.notify('Force refresh triggered, reloading tweets in 10s')
|
|
|
|
window.setTimeout(() => this.refresh(true), 10000)
|
|
|
|
})
|
|
|
|
.catch(err => console.log(err))
|
|
|
|
},
|
|
|
|
|
2020-01-12 12:59:20 +00:00
|
|
|
upsertTweets(data, append = true) {
|
|
|
|
let tweets = append ? this.tweets : []
|
2019-04-24 17:44:50 +00:00
|
|
|
|
|
|
|
for (const idx in data) {
|
|
|
|
const tweet = data[idx]
|
|
|
|
let inserted = false
|
|
|
|
|
|
|
|
for (let i = 0; i < tweets.length; i++) {
|
|
|
|
if (tweets[i].id === tweet.id) {
|
|
|
|
tweets[i] = tweet
|
|
|
|
inserted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inserted) {
|
|
|
|
tweets = [...tweets, tweet]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tweets.sort((j, i) => sortOrder(i.id, j.id))
|
|
|
|
this.tweets = tweets
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
|
|
|
this.refresh()
|
|
|
|
window.setInterval(this.refresh, 30000)
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|