From 3f327825d7e2ac68e6680bdc29ce491deecfd31b Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 12 Jan 2020 13:59:20 +0100 Subject: [PATCH] Remove tweets which vanished on refresh Signed-off-by: Knut Ahlers --- frontend/app.js | 42 +++++++++++++++++++++++++----------------- http.go | 8 +++++++- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/frontend/app.js b/frontend/app.js index b784385..f19c5a5 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -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] diff --git a/http.go b/http.go index 8a6770e..5500bb4 100644 --- a/http.go +++ b/http.go @@ -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) }