7eee47b0d3
* Use ModeratorDir variable * Rename cookieHelper to cookie_helper for consistency * Use named constant instead of literals * Fix ability to upload when uploads are disabled The old code let people upload under the right conditions when uploads were disabled. (ie: User is banned and config.AdminAreStillAllowedTo is false) * Increase timeout (fixes #517) * Fix inconsistent indentation *.{js, css} (fix #583) * Fix negative page Temporary fix. The issue was that going to a negative page caused the sql query to have a negative offset. This caused an error in the database query. We need to cleanup this code, but this will work for now. * Fix wrong PG_DATA directory due to upgrade to 9.6 * Add server status link to FAQ * Fix failing tests * Clarify group_vars/all and hosts doc * Add a wrapper to protect /mod route * Fix login page not showing form errors
94 lignes
2,2 Kio
Go
94 lignes
2,2 Kio
Go
package router
|
|
|
|
import (
|
|
"github.com/NyaaPantsu/nyaa/config"
|
|
userService "github.com/NyaaPantsu/nyaa/service/user"
|
|
"github.com/NyaaPantsu/nyaa/util"
|
|
"github.com/NyaaPantsu/nyaa/util/search"
|
|
"github.com/gorilla/feeds"
|
|
"github.com/gorilla/mux"
|
|
"html"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func RSSHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
page := vars["page"]
|
|
userID := vars["id"]
|
|
|
|
var err error
|
|
pagenum := 1
|
|
if page != "" {
|
|
pagenum, err = strconv.Atoi(html.EscapeString(page))
|
|
if err != nil {
|
|
util.SendError(w, err, 400)
|
|
return
|
|
}
|
|
if pagenum <= 0 {
|
|
NotFoundHandler(w, r)
|
|
return
|
|
}
|
|
}
|
|
|
|
if userID != "" {
|
|
userIDnum, err := strconv.Atoi(html.EscapeString(userID))
|
|
// Should we have a feed for anonymous uploads?
|
|
if err != nil || userIDnum == 0 {
|
|
util.SendError(w, err, 400)
|
|
return
|
|
}
|
|
|
|
_, _, err = userService.RetrieveUserForAdmin(userID)
|
|
if err != nil {
|
|
util.SendError(w, err, 404)
|
|
return
|
|
}
|
|
|
|
// Set the user ID on the request, so that SearchByQuery finds it.
|
|
query := r.URL.Query()
|
|
query.Set("userID", userID)
|
|
r.URL.RawQuery = query.Encode()
|
|
}
|
|
|
|
_, torrents, err := search.SearchByQueryNoCount(r, pagenum)
|
|
if err != nil {
|
|
util.SendError(w, err, 400)
|
|
return
|
|
}
|
|
createdAsTime := time.Now()
|
|
|
|
if len(torrents) > 0 {
|
|
createdAsTime = torrents[0].Date
|
|
}
|
|
feed := &feeds.Feed{
|
|
Title: "Nyaa Pantsu",
|
|
Link: &feeds.Link{Href: "https://" + config.WebAddress + "/"},
|
|
Created: createdAsTime,
|
|
}
|
|
feed.Items = make([]*feeds.Item, len(torrents))
|
|
|
|
for i, torrent := range torrents {
|
|
torrentJSON := torrent.ToJSON()
|
|
feed.Items[i] = &feeds.Item{
|
|
Id: "https://" + config.WebAddress + "/view/" + torrentJSON.ID,
|
|
Title: torrent.Name,
|
|
Link: &feeds.Link{Href: string(torrentJSON.Magnet)},
|
|
Description: string(torrentJSON.Description),
|
|
Created: torrent.Date,
|
|
Updated: torrent.Date,
|
|
}
|
|
}
|
|
// allow cross domain AJAX requests
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
rss, rssErr := feed.ToRss()
|
|
if rssErr != nil {
|
|
http.Error(w, rssErr.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
_, writeErr := w.Write([]byte(rss))
|
|
if writeErr != nil {
|
|
http.Error(w, writeErr.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|