1
0
Fork 0
mirror of https://github.com/Luzifer/mediatimeline.git synced 2024-11-09 15:20:05 +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/http"
"net/url" "net/url"
"strconv" "strconv"
"strings"
"github.com/ChimeraCoder/anaconda" "github.com/ChimeraCoder/anaconda"
log "github.com/sirupsen/logrus" 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{}) tweet, err := twitter.GetTweet(req.ID, url.Values{})
if err != nil { 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") log.WithError(err).Error("Unable to fetch tweet")
http.Error(w, "Something went wrong", http.StatusInternalServerError) http.Error(w, "Something went wrong", http.StatusInternalServerError)
return return

View file

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