-
-
-
+
-
-
-
-
+
+
+
+
+
+
-
-
+
+
@@ -155,7 +165,7 @@
modalTweet: null,
},
- el: ".container",
+ el: "#app",
methods: {
callModal(tweet) {
@@ -167,6 +177,7 @@
.post('/api/favourite', { id: tweet.id })
.then((res) => {
if (res.data.length === 0) {
+ this.refetch(tweet)
return
}
@@ -188,9 +199,9 @@
.catch((err) => console.log(err))
},
- refresh() {
+ refresh(forceReload = false) {
let apiURL = '/api/page' // By default query page 1
- if (this.tweets.length > 0) {
+ if (this.tweets.length > 0 && !forceReload) {
apiURL = `/api/since?id=${this.tweets[0].id}`
}
@@ -208,6 +219,15 @@
})
},
+ triggerForceFetch() {
+ axios
+ .post('/api/force-reload')
+ .then(() => {
+ window.setTimeout(() => this.refresh(true), 10000)
+ })
+ .catch((err) => console.log(err))
+ },
+
upsertTweets(data) {
let tweets = this.tweets
diff --git a/http.go b/http.go
index abffd5f..914a039 100644
--- a/http.go
+++ b/http.go
@@ -15,6 +15,7 @@ func init() {
http.HandleFunc("/api/since", handleNewest)
http.HandleFunc("/api/favourite", handleFavourite)
http.HandleFunc("/api/refresh", handleTweetRefresh)
+ http.HandleFunc("/api/force-reload", handleForceReload)
}
func handlePage(w http.ResponseWriter, r *http.Request) {
@@ -129,3 +130,14 @@ func handleTweetRefresh(w http.ResponseWriter, r *http.Request) {
tweetResponse(w, tweets)
}
+
+func handleForceReload(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ http.Error(w, "This needs to be POST", http.StatusBadRequest)
+ return
+ }
+
+ go loadAndStoreTweets(true)
+
+ w.WriteHeader(http.StatusNoContent)
+}
diff --git a/main.go b/main.go
index 9ec27c3..a5674ff 100644
--- a/main.go
+++ b/main.go
@@ -63,39 +63,41 @@ func main() {
cfg.UserToken, cfg.UserSecret,
cfg.AppToken, cfg.AppSecret,
)
- go loadAndStoreTweets()
+ go func() {
+ for t := time.NewTicker(time.Minute); true; <-t.C {
+ loadAndStoreTweets(false)
+ }
+ }()
http.Handle("/", http.FileServer(http.Dir(cfg.Frontend)))
http.ListenAndServe(cfg.Listen, nil)
}
-func loadAndStoreTweets() {
- for t := time.NewTicker(time.Minute); true; <-t.C {
- params := url.Values{
- "count": []string{"100"},
- }
+func loadAndStoreTweets(forceRefresh bool) {
+ params := url.Values{
+ "count": []string{"100"},
+ }
- lastTweet := tweetStore.GetLastTweetID()
+ lastTweet := tweetStore.GetLastTweetID()
- if lastTweet > 0 {
- params.Set("since_id", strconv.FormatUint(lastTweet, 10))
- }
+ if lastTweet > 0 && !forceRefresh {
+ params.Set("since_id", strconv.FormatUint(lastTweet, 10))
+ }
- anacondaTweets, err := twitter.GetListTweetsBySlug(cfg.ListSlug, cfg.ListOwner, false, params)
- if err != nil {
- log.WithError(err).Error("Unable to fetch tweets")
- continue
- }
+ anacondaTweets, err := twitter.GetListTweetsBySlug(cfg.ListSlug, cfg.ListOwner, false, params)
+ if err != nil {
+ log.WithError(err).Error("Unable to fetch tweets")
+ return
+ }
- tweets, err := convertTweetList(anacondaTweets, true)
- if err != nil {
- log.WithError(err).Error("Unable to parse tweets")
- continue
- }
+ tweets, err := convertTweetList(anacondaTweets, true)
+ if err != nil {
+ log.WithError(err).Error("Unable to parse tweets")
+ return
+ }
- if err := tweetStore.StoreTweets(tweets); err != nil {
- log.WithError(err).Error("Unable to store tweets")
- continue
- }
+ if err := tweetStore.StoreTweets(tweets); err != nil {
+ log.WithError(err).Error("Unable to store tweets")
+ return
}
}