2019-04-23 13:54:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2019-04-23 22:13:42 +00:00
|
|
|
"strings"
|
2019-04-23 13:54:42 +00:00
|
|
|
|
|
|
|
"github.com/ChimeraCoder/anaconda"
|
2020-01-12 12:37:20 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/pkg/errors"
|
2019-04-23 13:54:42 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-01-12 12:37:20 +00:00
|
|
|
router.HandleFunc("/api/{tweetID}/favorite", handleFavorite).Methods(http.MethodPut)
|
|
|
|
router.HandleFunc("/api/{tweetID}", handleDelete).Methods(http.MethodDelete)
|
|
|
|
router.HandleFunc("/api/force-reload", handleForceReload).Methods(http.MethodPut)
|
|
|
|
router.HandleFunc("/api/page", handlePage).Methods(http.MethodGet)
|
|
|
|
router.HandleFunc("/api/{tweetID}/refresh", handleTweetRefresh).Methods(http.MethodPut)
|
|
|
|
router.HandleFunc("/api/since", handleNewest).Methods(http.MethodGet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDelete(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var vars = mux.Vars(r)
|
|
|
|
|
|
|
|
tweetID, err := strconv.ParseInt(vars["tweetID"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "Unable to parse TweetID").Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2019-04-23 13:54:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:53:34 +00:00
|
|
|
// handleFavorite takes the ID of a tweet and submits a favorite to Twitter
|
|
|
|
func handleFavorite(w http.ResponseWriter, r *http.Request) {
|
2020-01-12 12:37:20 +00:00
|
|
|
var vars = mux.Vars(r)
|
2019-04-23 13:54:42 +00:00
|
|
|
|
2020-01-12 12:37:20 +00:00
|
|
|
tweetID, err := strconv.ParseInt(vars["tweetID"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "Unable to parse TweetID").Error(), http.StatusBadRequest)
|
2019-04-23 13:54:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:37:20 +00:00
|
|
|
tweet, err := twitter.Favorite(tweetID)
|
2019-04-23 13:54:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to favourite tweet")
|
|
|
|
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tweets, err := convertTweetList([]anaconda.Tweet{tweet}, true)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to convert tweet for storing")
|
|
|
|
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tweetStore.StoreTweets(tweets); err != nil {
|
|
|
|
log.WithError(err).Error("Unable to update tweet")
|
|
|
|
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tweetResponse(w, tweets)
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:53:34 +00:00
|
|
|
// handleForceReload issues a full load of the latest tweets to update their state
|
|
|
|
func handleForceReload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
go loadAndStoreTweets(true)
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleNewest returns all tweets newly stored since the given tweet ID
|
|
|
|
func handleNewest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var since uint64
|
|
|
|
|
|
|
|
if s, err := strconv.ParseUint(r.URL.Query().Get("id"), 10, 64); err == nil && s > 0 {
|
|
|
|
since = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if since == 0 {
|
|
|
|
http.Error(w, "Must specify last id", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tweetResponse(w, tweetStore.GetTweetsSince(since))
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlePage loads older tweets with pagination
|
|
|
|
func handlePage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var page = 1
|
|
|
|
|
|
|
|
if p, err := strconv.Atoi(r.URL.Query().Get("n")); err == nil && p > 0 {
|
|
|
|
page = p
|
|
|
|
}
|
|
|
|
|
|
|
|
tweetResponse(w, tweetStore.GetTweetPage(page))
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleTweetRefresh refreshes the state of the tweet with the given ID against the Twitter API
|
2019-04-23 13:54:42 +00:00
|
|
|
func handleTweetRefresh(w http.ResponseWriter, r *http.Request) {
|
2020-01-12 12:37:20 +00:00
|
|
|
var vars = mux.Vars(r)
|
2019-04-23 13:54:42 +00:00
|
|
|
|
2020-01-12 12:37:20 +00:00
|
|
|
tweetID, err := strconv.ParseInt(vars["tweetID"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "Unable to parse TweetID").Error(), http.StatusBadRequest)
|
2019-04-23 13:54:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:37:20 +00:00
|
|
|
tweet, err := twitter.GetTweet(tweetID, url.Values{})
|
2019-04-23 13:54:42 +00:00
|
|
|
if err != nil {
|
2019-04-23 22:13:42 +00:00
|
|
|
if strings.Contains(err.Error(), "No status found with that ID.") {
|
2020-01-12 12:37:20 +00:00
|
|
|
log.WithField("id", tweetID).Info("Removing no longer existing tweet")
|
|
|
|
if err = tweetStore.DeleteTweetByID(uint64(tweetID)); err != nil {
|
2019-04-23 22:13:42 +00:00
|
|
|
log.WithError(err).Error("Unable to delete tweet")
|
|
|
|
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
2020-01-12 12:59:20 +00:00
|
|
|
return
|
2019-04-23 22:13:42 +00:00
|
|
|
}
|
2020-01-12 12:59:20 +00:00
|
|
|
jsonResponse(w, map[string]bool{"gone": true})
|
2019-04-23 22:13:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-23 13:54:42 +00:00
|
|
|
log.WithError(err).Error("Unable to fetch tweet")
|
|
|
|
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tweets, err := convertTweetList([]anaconda.Tweet{tweet}, true)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to convert tweet for storing")
|
|
|
|
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tweetStore.StoreTweets(tweets); err != nil {
|
|
|
|
log.WithError(err).Error("Unable to update tweet")
|
|
|
|
http.Error(w, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tweetResponse(w, tweets)
|
|
|
|
}
|
2019-04-23 22:00:28 +00:00
|
|
|
|
2019-04-24 18:53:34 +00:00
|
|
|
// tweetResponse is a generic wrapper to return a list of tweets through JSON
|
|
|
|
func tweetResponse(w http.ResponseWriter, tweets []tweet) {
|
2020-01-12 12:59:20 +00:00
|
|
|
jsonResponse(w, tweets)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonResponse(w http.ResponseWriter, data interface{}) {
|
2019-04-24 18:53:34 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-04-23 22:00:28 +00:00
|
|
|
|
2020-01-12 12:59:20 +00:00
|
|
|
json.NewEncoder(w).Encode(data)
|
2019-04-23 22:00:28 +00:00
|
|
|
}
|