1
0
mirror of https://github.com/Luzifer/mediatimeline.git synced 2024-09-18 23:42:56 +00:00

Delete non-existent tweets on refresh

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-04-24 00:13:42 +02:00
parent 5c38527dc0
commit 00d1bacbfb
Signed by: luzifer
GPG Key ID: DC2729FDD34BE99E
2 changed files with 52 additions and 16 deletions

10
http.go
View File

@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"github.com/ChimeraCoder/anaconda"
log "github.com/sirupsen/logrus"
@ -110,6 +111,15 @@ func handleTweetRefresh(w http.ResponseWriter, r *http.Request) {
tweet, err := twitter.GetTweet(req.ID, url.Values{})
if err != nil {
if strings.Contains(err.Error(), "No status found with that ID.") {
log.WithField("id", req.ID).Info("Removing no longer existing tweet")
if err = tweetStore.DeleteTweetByID(uint64(req.ID)); err != nil {
log.WithError(err).Error("Unable to delete tweet")
http.Error(w, "Something went wrong", http.StatusInternalServerError)
}
return
}
log.WithError(err).Error("Unable to fetch tweet")
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return

View File

@ -31,32 +31,29 @@ func newStore(location string) (*store, error) {
return s, s.load()
}
func (s *store) StoreTweets(tweets []tweet) error {
func (s *store) DeleteTweetByID(id uint64) error {
s.lock.Lock()
defer s.lock.Unlock()
tmp := s.s
var (
tmp = []tweet{}
needsSave bool
)
for _, t := range tweets {
var stored bool
for i := 0; i < len(tmp); i++ {
if tmp[i].ID == t.ID {
tmp[i] = t
stored = true
break
}
for _, t := range s.s {
if t.ID == id {
needsSave = true
continue
}
if !stored {
tmp = append(tmp, t)
}
tmp = append(tmp, t)
}
sort.Slice(tmp, func(j, i int) bool { return tmp[i].ID < tmp[j].ID })
if !needsSave {
return nil
}
s.s = tmp
return s.save()
}
@ -106,6 +103,35 @@ func (s *store) GetTweetsSince(since uint64) ([]tweet, error) {
return s.s[:i], nil
}
func (s *store) StoreTweets(tweets []tweet) error {
s.lock.Lock()
defer s.lock.Unlock()
tmp := s.s
for _, t := range tweets {
var stored bool
for i := 0; i < len(tmp); i++ {
if tmp[i].ID == t.ID {
tmp[i] = t
stored = true
break
}
}
if !stored {
tmp = append(tmp, t)
}
}
sort.Slice(tmp, func(j, i int) bool { return tmp[i].ID < tmp[j].ID })
s.s = tmp
return s.save()
}
func (s *store) load() error {
s.lock.Lock()
defer s.lock.Unlock()