1
0
Fork 0
mirror of https://github.com/Luzifer/mediatimeline.git synced 2024-11-08 06:40:06 +00:00

Remove tweets which vanished on refresh

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-01-12 13:59:20 +01:00
parent 7b4546727b
commit 3f327825d7
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 32 additions and 18 deletions

View file

@ -28,20 +28,7 @@ new Vue({
deleteTweet(tweet) {
axios
.delete(`/api/${tweet.id}`)
.then(() => {
const tweets = []
for (const i in this.tweets) {
const t = this.tweets[i]
if (t.id === tweet.id) {
continue
}
tweets.push(t)
}
this.tweets = tweets
})
.then(() => this.removeTweet(tweet.id))
.catch(err => console.log(err))
},
@ -71,6 +58,10 @@ new Vue({
axios
.put(`/api/${tweet.id}/refresh`)
.then(res => {
if (res.data.gone) {
return this.removeTweet(tweet.id)
}
if (res.data.length === 0) {
return
}
@ -82,8 +73,10 @@ new Vue({
refresh(forceReload = false) {
let apiURL = '/api/page' // By default query page 1
let append = false
if (this.tweets.length > 0 && !forceReload) {
apiURL = `/api/since?id=${this.tweets[0].id}`
append = true
}
axios
@ -93,13 +86,28 @@ new Vue({
return
}
this.upsertTweets(resp.data)
this.upsertTweets(resp.data, append)
})
.catch(err => {
console.log(err)
})
},
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
},
triggerForceFetch() {
axios
.put('/api/force-reload')
@ -110,8 +118,8 @@ new Vue({
.catch(err => console.log(err))
},
upsertTweets(data) {
let tweets = this.tweets
upsertTweets(data, append = true) {
let tweets = append ? this.tweets : []
for (const idx in data) {
const tweet = data[idx]

View file

@ -124,7 +124,9 @@ func handleTweetRefresh(w http.ResponseWriter, r *http.Request) {
if err = tweetStore.DeleteTweetByID(uint64(tweetID)); err != nil {
log.WithError(err).Error("Unable to delete tweet")
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
jsonResponse(w, map[string]bool{"gone": true})
return
}
@ -151,9 +153,13 @@ func handleTweetRefresh(w http.ResponseWriter, r *http.Request) {
// tweetResponse is a generic wrapper to return a list of tweets through JSON
func tweetResponse(w http.ResponseWriter, tweets []tweet) {
jsonResponse(w, tweets)
}
func jsonResponse(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets)
json.NewEncoder(w).Encode(data)
}