2017-05-05 16:39:15 +02:00
package router
2017-05-06 10:36:37 +02:00
import (
2017-05-22 00:22:42 +02:00
"fmt"
2017-05-06 10:36:37 +02:00
"net/http"
2017-05-08 05:34:12 +02:00
"strconv"
2017-05-10 22:09:17 +02:00
"strings"
2017-05-08 19:26:29 +02:00
"time"
2017-05-06 19:01:15 +02:00
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
2017-05-23 04:05:33 +02:00
"github.com/NyaaPantsu/nyaa/service"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/captcha"
2017-05-21 01:06:40 +02:00
"github.com/NyaaPantsu/nyaa/service/notifier"
2017-05-23 04:05:33 +02:00
"github.com/NyaaPantsu/nyaa/service/report"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/torrent"
"github.com/NyaaPantsu/nyaa/service/user/permission"
2017-05-23 22:09:20 +02:00
"github.com/NyaaPantsu/nyaa/util"
2017-05-22 00:22:42 +02:00
"github.com/NyaaPantsu/nyaa/util/languages"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util/log"
2017-05-24 16:13:56 +02:00
"github.com/NyaaPantsu/nyaa/util/filelist"
2017-05-20 17:01:13 +02:00
msg "github.com/NyaaPantsu/nyaa/util/messages"
2017-05-06 19:01:15 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
func ViewHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-20 17:01:13 +02:00
messages := msg . GetMessages ( r )
2017-05-21 01:06:40 +02:00
user := GetUser ( r )
2017-05-20 17:01:13 +02:00
2017-05-23 05:18:19 +02:00
if r . URL . Query ( ) [ "success" ] != nil {
2017-05-20 17:01:13 +02:00
messages . AddInfo ( "infos" , "Torrent uploaded successfully!" )
}
2017-05-05 16:39:15 +02:00
torrent , err := torrentService . GetTorrentById ( id )
2017-05-23 05:18:19 +02:00
if r . URL . Query ( ) [ "notif" ] != nil {
2017-05-21 01:06:40 +02:00
notifierService . ToggleReadNotification ( torrent . Identifier ( ) , user . ID )
}
2017-05-07 13:51:33 +02:00
if err != nil {
NotFoundHandler ( w , r )
return
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
b := torrent . ToJSON ( )
2017-05-24 16:13:56 +02:00
folder := filelist . FileListToFolder ( torrent . FileList )
2017-05-12 11:58:22 +02:00
captchaID := ""
if userPermission . NeedsCaptcha ( user ) {
captchaID = captcha . GetID ( )
}
2017-05-24 16:13:56 +02:00
htv := ViewTemplateVariables { NewCommonVariables ( r ) , b , folder , captchaID , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-05 16:39:15 +02:00
2017-05-06 19:01:15 +02:00
err = viewTemplate . ExecuteTemplate ( w , "index.html" , htv )
2017-05-05 16:39:15 +02:00
if err != nil {
2017-05-07 13:51:33 +02:00
log . Errorf ( "ViewHandler(): %s" , err )
2017-05-05 16:39:15 +02:00
}
2017-05-06 10:36:37 +02:00
}
2017-05-08 05:34:12 +02:00
2017-05-23 05:18:19 +02:00
func ViewHeadHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id , err := strconv . ParseInt ( vars [ "id" ] , 10 , 32 )
if err != nil {
return
}
_ , err = torrentService . GetRawTorrentById ( uint ( id ) )
if err != nil {
NotFoundHandler ( w , r )
return
}
w . Write ( nil )
}
2017-05-08 05:34:12 +02:00
func PostCommentHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-22 00:22:42 +02:00
torrent , err := torrentService . GetTorrentById ( id )
2017-05-23 05:18:19 +02:00
if err != nil {
2017-05-22 00:22:42 +02:00
NotFoundHandler ( w , r )
return
}
2017-05-08 13:18:52 +02:00
currentUser := GetUser ( r )
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-12 11:58:22 +02:00
if userPermission . NeedsCaptcha ( currentUser ) {
userCaptcha := captcha . Extract ( r )
if ! captcha . Authenticate ( userCaptcha ) {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "bad_captcha" )
2017-05-12 11:58:22 +02:00
}
}
2017-05-24 00:08:02 +02:00
content := util . Sanitize ( r . FormValue ( "comment" ) , "comment" )
2017-05-08 05:34:12 +02:00
2017-05-11 21:46:14 +02:00
if strings . TrimSpace ( content ) == "" {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "comment_empty" )
2017-05-11 21:46:14 +02:00
}
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
userID := currentUser . ID
2017-05-22 00:22:42 +02:00
comment := model . Comment { TorrentID : torrent . ID , UserID : userID , Content : content , CreatedAt : time . Now ( ) }
err := db . ORM . Create ( & comment ) . Error
comment . Torrent = & torrent
url , err := Router . Get ( "view_torrent" ) . URL ( "id" , strconv . FormatUint ( uint64 ( torrent . ID ) , 10 ) )
torrent . Uploader . ParseSettings ( )
if torrent . Uploader . Settings . Get ( "new_comment" ) {
T , _ , _ := languages . TfuncAndLanguageWithFallback ( torrent . Uploader . Language , torrent . Uploader . Language ) // We need to send the notification to every user in their language
notifierService . NotifyUser ( torrent . Uploader , comment . Identifier ( ) , fmt . Sprintf ( T ( "new_comment_on_torrent" ) , torrent . Name ) , url . String ( ) , torrent . Uploader . Settings . Get ( "new_comment_email" ) )
}
2017-05-08 05:34:12 +02:00
2017-05-21 19:38:39 +02:00
if err != nil {
messages . ImportFromError ( "errors" , err )
}
2017-05-08 05:34:12 +02:00
}
2017-05-23 05:18:19 +02:00
ViewHandler ( w , r )
2017-05-08 05:34:12 +02:00
}
2017-05-10 10:17:21 +02:00
func ReportTorrentHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-10 10:17:21 +02:00
currentUser := GetUser ( r )
2017-05-12 11:58:22 +02:00
if userPermission . NeedsCaptcha ( currentUser ) {
userCaptcha := captcha . Extract ( r )
if ! captcha . Authenticate ( userCaptcha ) {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "bad_captcha" )
2017-05-12 11:58:22 +02:00
}
}
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
idNum , err := strconv . Atoi ( id )
userID := currentUser . ID
report := model . TorrentReport {
Description : r . FormValue ( "report_type" ) ,
TorrentID : uint ( idNum ) ,
UserID : userID ,
CreatedAt : time . Now ( ) ,
}
2017-05-10 10:17:21 +02:00
2017-05-21 19:38:39 +02:00
err = db . ORM . Create ( & report ) . Error
if err != nil {
messages . ImportFromError ( "errors" , err )
}
2017-05-10 10:17:21 +02:00
}
2017-05-23 05:18:19 +02:00
ViewHandler ( w , r )
2017-05-10 10:17:21 +02:00
}
2017-05-23 04:05:33 +02:00
func TorrentEditUserPanel ( w http . ResponseWriter , r * http . Request ) {
id := r . URL . Query ( ) . Get ( "id" )
torrent , _ := torrentService . GetTorrentById ( id )
2017-05-24 09:11:13 +02:00
messages := msg . GetMessages ( r )
2017-05-23 04:05:33 +02:00
currentUser := GetUser ( r )
if userPermission . CurrentOrAdmin ( currentUser , torrent . UploaderID ) {
uploadForm := NewUploadForm ( )
2017-05-23 04:12:02 +02:00
uploadForm . Name = torrent . Name
uploadForm . Category = strconv . Itoa ( torrent . Category ) + "_" + strconv . Itoa ( torrent . SubCategory )
uploadForm . Remake = torrent . Status == model . TorrentStatusRemake
uploadForm . WebsiteLink = string ( torrent . WebsiteLink )
uploadForm . Description = string ( torrent . Description )
2017-05-23 04:05:33 +02:00
htv := UserTorrentEdVbs { NewCommonVariables ( r ) , uploadForm , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
err := userTorrentEd . ExecuteTemplate ( w , "index.html" , htv )
log . CheckError ( err )
} else {
NotFoundHandler ( w , r )
}
}
func TorrentPostEditUserPanel ( w http . ResponseWriter , r * http . Request ) {
var uploadForm UploadForm
id := r . URL . Query ( ) . Get ( "id" )
messages := msg . GetMessages ( r )
torrent , _ := torrentService . GetTorrentById ( id )
currentUser := GetUser ( r )
if torrent . ID > 0 && userPermission . CurrentOrAdmin ( currentUser , torrent . UploaderID ) {
errUp := uploadForm . ExtractEditInfo ( r )
if errUp != nil {
messages . AddErrorT ( "errors" , "fail_torrent_update" )
}
if ! messages . HasErrors ( ) {
status := model . TorrentStatusNormal
2017-05-23 04:12:02 +02:00
uploadForm . Remake = r . FormValue ( UploadFormRemake ) == "on"
2017-05-23 04:05:33 +02:00
if uploadForm . Remake { // overrides trusted
status = model . TorrentStatusRemake
} else if currentUser . IsTrusted ( ) {
status = model . TorrentStatusTrusted
}
// update some (but not all!) values
torrent . Name = uploadForm . Name
torrent . Category = uploadForm . CategoryID
torrent . SubCategory = uploadForm . SubCategoryID
torrent . Status = status
torrent . WebsiteLink = uploadForm . WebsiteLink
torrent . Description = uploadForm . Description
torrent . Uploader = nil // GORM will create a new user otherwise (wtf?!)
db . ORM . Save ( & torrent )
messages . AddInfoT ( "infos" , "torrent_updated" )
}
2017-05-24 09:11:13 +02:00
htv := UserTorrentEdVbs { NewCommonVariables ( r ) , uploadForm , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
err_ := userTorrentEd . ExecuteTemplate ( w , "index.html" , htv )
log . CheckError ( err_ )
2017-05-23 04:05:33 +02:00
} else {
NotFoundHandler ( w , r )
}
}
func TorrentDeleteUserPanel ( w http . ResponseWriter , r * http . Request ) {
id := r . URL . Query ( ) . Get ( "id" )
currentUser := GetUser ( r )
torrent , _ := torrentService . GetTorrentById ( id )
if userPermission . CurrentOrAdmin ( currentUser , torrent . UploaderID ) {
_ , err := torrentService . DeleteTorrent ( id )
2017-05-24 09:11:13 +02:00
if err == nil {
2017-05-23 04:05:33 +02:00
//delete reports of torrent
whereParams := serviceBase . CreateWhereParams ( "torrent_id = ?" , id )
reports , _ , _ := reportService . GetTorrentReportsOrderBy ( & whereParams , "" , 0 , 0 )
for _ , report := range reports {
reportService . DeleteTorrentReport ( report . ID )
}
}
url , _ := Router . Get ( "home" ) . URL ( )
http . Redirect ( w , r , url . String ( ) + "?deleted" , http . StatusSeeOther )
} else {
NotFoundHandler ( w , r )
}
2017-05-24 09:11:13 +02:00
}