1
0
Fork 0
mirror of https://github.com/Luzifer/mediatimeline.git synced 2024-11-09 15:20:05 +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) { deleteTweet(tweet) {
axios axios
.delete(`/api/${tweet.id}`) .delete(`/api/${tweet.id}`)
.then(() => { .then(() => this.removeTweet(tweet.id))
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
})
.catch(err => console.log(err)) .catch(err => console.log(err))
}, },
@ -71,6 +58,10 @@ new Vue({
axios axios
.put(`/api/${tweet.id}/refresh`) .put(`/api/${tweet.id}/refresh`)
.then(res => { .then(res => {
if (res.data.gone) {
return this.removeTweet(tweet.id)
}
if (res.data.length === 0) { if (res.data.length === 0) {
return return
} }
@ -82,8 +73,10 @@ new Vue({
refresh(forceReload = false) { refresh(forceReload = false) {
let apiURL = '/api/page' // By default query page 1 let apiURL = '/api/page' // By default query page 1
let append = false
if (this.tweets.length > 0 && !forceReload) { if (this.tweets.length > 0 && !forceReload) {
apiURL = `/api/since?id=${this.tweets[0].id}` apiURL = `/api/since?id=${this.tweets[0].id}`
append = true
} }
axios axios
@ -93,13 +86,28 @@ new Vue({
return return
} }
this.upsertTweets(resp.data) this.upsertTweets(resp.data, append)
}) })
.catch(err => { .catch(err => {
console.log(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() { triggerForceFetch() {
axios axios
.put('/api/force-reload') .put('/api/force-reload')
@ -110,8 +118,8 @@ new Vue({
.catch(err => console.log(err)) .catch(err => console.log(err))
}, },
upsertTweets(data) { upsertTweets(data, append = true) {
let tweets = this.tweets let tweets = append ? this.tweets : []
for (const idx in data) { for (const idx in data) {
const tweet = data[idx] 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 { if err = tweetStore.DeleteTweetByID(uint64(tweetID)); err != nil {
log.WithError(err).Error("Unable to delete tweet") log.WithError(err).Error("Unable to delete tweet")
http.Error(w, "Something went wrong", http.StatusInternalServerError) http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
} }
jsonResponse(w, map[string]bool{"gone": true})
return 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 // tweetResponse is a generic wrapper to return a list of tweets through JSON
func tweetResponse(w http.ResponseWriter, tweets []tweet) { 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("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Cache-Control", "no-cache")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets) json.NewEncoder(w).Encode(data)
} }