1
0
Fork 0
mirror of https://github.com/Luzifer/mediatimeline.git synced 2024-11-09 15:20:05 +00:00

Allow triggering force-reload without cached ID

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-04-24 00:00:28 +02:00
parent 38ab270812
commit 5c38527dc0
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 130 additions and 96 deletions

View file

@ -35,11 +35,20 @@
</head>
<body>
<div id="app">
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-5">
<a class="navbar-brand" href="#">MediaTimeline Viewer</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#" @click="triggerForceFetch"><i class="fas fa-sync"></i> Force reload</a>
</li>
</ul>
</div>
</nav>
<div class="container">
@ -116,6 +125,7 @@
</div>
</div> <!-- /.container -->
</div> <!-- /#app -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
@ -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

12
http.go
View file

@ -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)
}

18
main.go
View file

@ -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 {
func loadAndStoreTweets(forceRefresh bool) {
params := url.Values{
"count": []string{"100"},
}
lastTweet := tweetStore.GetLastTweetID()
if lastTweet > 0 {
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
return
}
tweets, err := convertTweetList(anacondaTweets, true)
if err != nil {
log.WithError(err).Error("Unable to parse tweets")
continue
return
}
if err := tweetStore.StoreTweets(tweets); err != nil {
log.WithError(err).Error("Unable to store tweets")
continue
}
return
}
}