2017-05-05 14:20:51 +02:00
package torrentService
2017-05-07 03:10:35 +02:00
2017-05-05 14:20:51 +02:00
import (
2017-05-07 03:10:35 +02:00
"errors"
2017-06-05 15:19:25 +02:00
"fmt"
2017-06-15 04:44:46 +02:00
"html/template"
2017-05-10 20:42:20 +02:00
"net/http"
2017-05-09 01:56:57 +02:00
"strconv"
"strings"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service"
2017-06-15 04:44:46 +02:00
"github.com/NyaaPantsu/nyaa/service/activity"
2017-06-05 15:19:25 +02:00
"github.com/NyaaPantsu/nyaa/service/notifier"
"github.com/NyaaPantsu/nyaa/service/user"
"github.com/NyaaPantsu/nyaa/service/user/permission"
2017-05-26 01:48:14 +02:00
"github.com/NyaaPantsu/nyaa/util/log"
2017-06-05 15:19:25 +02:00
"github.com/NyaaPantsu/nyaa/util/publicSettings"
"github.com/gorilla/mux"
2017-05-05 14:20:51 +02:00
)
/ * Function to interact with Models
*
* Get the torrents with where clause
*
* /
2017-05-26 12:12:52 +02:00
// GetTorrentByID : get a torrent with its id
func GetTorrentByID ( id string ) ( torrent model . Torrent , err error ) {
2017-05-10 02:30:41 +02:00
// Postgres DB integer size is 32-bit
2017-05-26 12:12:52 +02:00
idInt , err := strconv . ParseInt ( id , 10 , 32 )
2017-05-08 23:40:54 +02:00
if err != nil {
2017-05-09 19:23:21 +02:00
return
2017-05-08 23:40:54 +02:00
}
2017-05-05 14:20:51 +02:00
2017-05-20 16:26:22 +02:00
tmp := db . ORM . Where ( "torrent_id = ?" , id ) . Preload ( "Comments" )
2017-05-31 04:21:57 +02:00
if idInt > int64 ( config . Conf . Models . LastOldTorrentID ) {
2017-05-20 16:26:22 +02:00
tmp = tmp . Preload ( "FileList" )
2017-05-09 19:23:21 +02:00
}
2017-05-31 04:21:57 +02:00
if idInt <= int64 ( config . Conf . Models . LastOldTorrentID ) && ! config . IsSukebei ( ) {
2017-05-08 23:40:54 +02:00
// only preload old comments if they could actually exist
tmp = tmp . Preload ( "OldComments" )
}
2017-05-20 16:26:22 +02:00
err = tmp . Error
if err != nil {
return
}
2017-05-08 23:40:54 +02:00
if tmp . Find ( & torrent ) . RecordNotFound ( ) {
2017-05-26 12:12:52 +02:00
err = errors . New ( "Article is not found" )
2017-05-09 19:23:21 +02:00
return
2017-05-05 14:20:51 +02:00
}
2017-05-09 12:35:46 +02:00
// GORM relly likes not doing its job correctly
// (or maybe I'm just retarded)
torrent . Uploader = new ( model . User )
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
db . ORM . Where ( "user_id = ?" , torrent . UploaderID ) . Find ( torrent . Uploader )
2017-05-10 13:32:45 +02:00
torrent . OldUploader = ""
2017-05-31 04:21:57 +02:00
if torrent . ID <= config . Conf . Models . LastOldTorrentID && torrent . UploaderID == 0 {
2017-05-10 13:32:45 +02:00
var tmp model . UserUploadsOld
2017-05-20 16:26:22 +02:00
if ! db . ORM . Where ( "torrent_id = ?" , torrent . ID ) . Find ( & tmp ) . RecordNotFound ( ) {
2017-05-10 13:32:45 +02:00
torrent . OldUploader = tmp . Username
}
}
2017-05-08 21:18:48 +02:00
for i := range torrent . Comments {
torrent . Comments [ i ] . User = new ( model . User )
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
err = db . ORM . Where ( "user_id = ?" , torrent . Comments [ i ] . UserID ) . Find ( torrent . Comments [ i ] . User ) . Error
2017-05-09 19:23:21 +02:00
if err != nil {
return
}
2017-05-08 21:18:48 +02:00
}
2017-05-05 14:20:51 +02:00
2017-05-09 19:23:21 +02:00
return
2017-05-05 14:20:51 +02:00
}
2017-05-26 12:12:52 +02:00
// GetRawTorrentByID : Get torrent with id without user or comments
2017-05-13 17:29:21 +02:00
// won't fetch user or comments
2017-05-26 12:12:52 +02:00
func GetRawTorrentByID ( id uint ) ( torrent model . Torrent , err error ) {
2017-05-13 17:29:21 +02:00
err = nil
2017-05-20 16:26:22 +02:00
if db . ORM . Where ( "torrent_id = ?" , id ) . Find ( & torrent ) . RecordNotFound ( ) {
2017-05-28 01:29:46 +02:00
err = errors . New ( "Torrent is not found" )
}
return
}
// GetRawTorrentByHash : Get torrent with id without user or comments
// won't fetch user or comments
func GetRawTorrentByHash ( hash string ) ( torrent model . Torrent , err error ) {
err = nil
if db . ORM . Where ( "torrent_hash = ?" , hash ) . Find ( & torrent ) . RecordNotFound ( ) {
err = errors . New ( "Torrent is not found" )
2017-05-13 17:29:21 +02:00
}
return
}
2017-05-26 12:12:52 +02:00
// GetTorrentsOrderByNoCount : Get torrents based on search without counting and user
2017-05-10 20:42:20 +02:00
func GetTorrentsOrderByNoCount ( parameters * serviceBase . WhereParams , orderBy string , limit int , offset int ) ( torrents [ ] model . Torrent , err error ) {
2017-05-25 02:19:05 +02:00
torrents , _ , err = getTorrentsOrderBy ( parameters , orderBy , limit , offset , false , false , false )
2017-05-09 17:07:42 +02:00
return
}
2017-05-26 12:12:52 +02:00
// GetTorrentsOrderBy : Get torrents based on search without user
2017-05-10 20:42:20 +02:00
func GetTorrentsOrderBy ( parameters * serviceBase . WhereParams , orderBy string , limit int , offset int ) ( torrents [ ] model . Torrent , count int , err error ) {
2017-05-25 02:19:05 +02:00
torrents , count , err = getTorrentsOrderBy ( parameters , orderBy , limit , offset , true , false , false )
2017-05-09 17:07:42 +02:00
return
}
2017-05-26 12:12:52 +02:00
// GetTorrentsWithUserOrderBy : Get torrents based on search with user
2017-05-20 13:45:15 +02:00
func GetTorrentsWithUserOrderBy ( parameters * serviceBase . WhereParams , orderBy string , limit int , offset int ) ( torrents [ ] model . Torrent , count int , err error ) {
2017-05-25 02:19:05 +02:00
torrents , count , err = getTorrentsOrderBy ( parameters , orderBy , limit , offset , true , true , false )
2017-05-20 13:45:15 +02:00
return
}
2017-05-25 02:19:05 +02:00
func getTorrentsOrderBy ( parameters * serviceBase . WhereParams , orderBy string , limit int , offset int , countAll bool , withUser bool , deleted bool ) (
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
torrents [ ] model . Torrent , count int , err error ,
2017-05-09 19:23:21 +02:00
) {
2017-05-08 22:46:49 +02:00
var conditionArray [ ] string
2017-05-05 14:20:51 +02:00
var params [ ] interface { }
if parameters != nil { // if there is where parameters
2017-05-08 16:53:15 +02:00
if len ( parameters . Conditions ) > 0 {
2017-05-08 22:46:49 +02:00
conditionArray = append ( conditionArray , parameters . Conditions )
2017-05-08 16:53:15 +02:00
}
2017-05-05 14:51:19 +02:00
params = parameters . Params
2017-05-05 14:20:51 +02:00
}
2017-05-25 02:19:05 +02:00
if ! deleted {
conditionArray = append ( conditionArray , "deleted_at IS NULL" )
} else {
2017-05-25 23:35:37 +02:00
conditionArray = append ( conditionArray , "deleted_at NOT NULL" )
2017-05-25 02:19:05 +02:00
}
2017-05-25 23:35:37 +02:00
2017-05-08 22:46:49 +02:00
conditions := strings . Join ( conditionArray , " AND " )
2017-05-25 02:19:05 +02:00
2017-05-09 17:07:42 +02:00
if countAll {
2017-05-25 02:19:05 +02:00
err = db . ORM . Unscoped ( ) . Model ( & torrents ) . Where ( conditions , params ... ) . Count ( & count ) . Error
2017-05-09 17:07:42 +02:00
if err != nil {
return
}
2017-05-09 13:31:58 +02:00
}
2017-05-08 22:46:49 +02:00
// build custom db query for performance reasons
2017-05-31 04:21:57 +02:00
dbQuery := "SELECT * FROM " + config . Conf . Models . TorrentsTableName
2017-05-08 15:50:18 +02:00
if conditions != "" {
dbQuery = dbQuery + " WHERE " + conditions
}
2017-05-12 19:38:08 +02:00
/ * This makes all queries take roughly the same amount of time ( lots ) ...
2017-05-12 17:54:08 +02:00
if strings . Contains ( conditions , "torrent_name" ) && offset > 0 {
2017-05-08 15:50:18 +02:00
dbQuery = "WITH t AS (SELECT * FROM torrents WHERE " + conditions + ") SELECT * FROM t"
2017-05-12 19:38:08 +02:00
} * /
2017-05-05 14:20:51 +02:00
2017-05-07 21:04:21 +02:00
if orderBy == "" { // default OrderBy
2017-05-05 14:20:51 +02:00
orderBy = "torrent_id DESC"
2017-05-07 21:04:21 +02:00
}
2017-05-08 15:50:18 +02:00
dbQuery = dbQuery + " ORDER BY " + orderBy
2017-05-05 14:20:51 +02:00
if limit != 0 || offset != 0 { // if limits provided
2017-05-08 15:50:18 +02:00
dbQuery = dbQuery + " LIMIT " + strconv . Itoa ( limit ) + " OFFSET " + strconv . Itoa ( offset )
2017-05-05 14:20:51 +02:00
}
2017-05-20 13:45:15 +02:00
dbQ := db . ORM
if withUser {
dbQ = dbQ . Preload ( "Uploader" )
}
if countAll {
dbQ = dbQ . Preload ( "Comments" )
}
2017-06-15 21:54:06 +02:00
err = dbQ . Preload ( "FileList" ) . Raw ( dbQuery , params ... ) . Find ( & torrents ) . Error
2017-05-09 13:31:58 +02:00
return
2017-05-05 14:20:51 +02:00
}
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
// GetTorrents obtain a list of torrents matching 'parameters' from the
// database. The list will be of length 'limit' and in default order.
// GetTorrents returns the first records found. Later records may be retrieved
// by providing a positive 'offset'
2017-05-10 20:42:20 +02:00
func GetTorrents ( parameters serviceBase . WhereParams , limit int , offset int ) ( [ ] model . Torrent , int , error ) {
2017-05-05 14:20:51 +02:00
return GetTorrentsOrderBy ( & parameters , "" , limit , offset )
}
2017-05-26 12:12:52 +02:00
// GetTorrentsDB : Get Torrents with where parameters but no limit and order by default (get all the torrents corresponding in the db)
2017-05-10 20:42:20 +02:00
func GetTorrentsDB ( parameters serviceBase . WhereParams ) ( [ ] model . Torrent , int , error ) {
2017-05-05 14:20:51 +02:00
return GetTorrentsOrderBy ( & parameters , "" , 0 , 0 )
}
2017-05-26 12:12:52 +02:00
// GetAllTorrentsOrderBy : Get all torrents ordered by parameters
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
func GetAllTorrentsOrderBy ( orderBy string , limit int , offset int ) ( [ ] model . Torrent , int , error ) {
2017-05-05 14:20:51 +02:00
return GetTorrentsOrderBy ( nil , orderBy , limit , offset )
}
2017-05-26 12:12:52 +02:00
// GetAllTorrents : Get all torrents without order
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
func GetAllTorrents ( limit int , offset int ) ( [ ] model . Torrent , int , error ) {
2017-05-05 14:20:51 +02:00
return GetTorrentsOrderBy ( nil , "" , limit , offset )
}
2017-05-26 12:12:52 +02:00
// GetAllTorrentsDB : Get all torrents
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
func GetAllTorrentsDB ( ) ( [ ] model . Torrent , int , error ) {
2017-05-05 14:20:51 +02:00
return GetTorrentsOrderBy ( nil , "" , 0 , 0 )
}
2017-05-26 12:12:52 +02:00
// DeleteTorrent : delete a torrent based on id
2017-06-15 04:44:46 +02:00
func DeleteTorrent ( id string ) ( * model . Torrent , int , error ) {
2017-05-10 05:54:12 +02:00
var torrent model . Torrent
2017-05-20 16:26:22 +02:00
if db . ORM . First ( & torrent , id ) . RecordNotFound ( ) {
2017-06-15 04:44:46 +02:00
return & torrent , http . StatusNotFound , errors . New ( "Torrent is not found" )
2017-05-10 04:03:25 +02:00
}
2017-05-20 16:26:22 +02:00
if db . ORM . Delete ( & torrent ) . Error != nil {
2017-06-15 04:44:46 +02:00
return & torrent , http . StatusInternalServerError , errors . New ( "Torrent was not deleted" )
2017-05-10 04:03:25 +02:00
}
2017-05-26 01:48:14 +02:00
2017-06-05 03:33:02 +02:00
if db . ElasticSearchClient != nil {
err := torrent . DeleteFromESIndex ( db . ElasticSearchClient )
2017-05-26 01:48:14 +02:00
if err == nil {
log . Infof ( "Successfully deleted torrent to ES index." )
} else {
log . Errorf ( "Unable to delete torrent to ES index: %s" , err )
}
}
2017-06-15 04:44:46 +02:00
return & torrent , http . StatusOK , nil
2017-05-10 05:24:18 +02:00
}
2017-05-26 12:12:52 +02:00
// DefinitelyDeleteTorrent : deletes definitely a torrent based on id
2017-06-15 04:44:46 +02:00
func DefinitelyDeleteTorrent ( id string ) ( * model . Torrent , int , error ) {
2017-05-25 02:19:05 +02:00
var torrent model . Torrent
if db . ORM . Unscoped ( ) . Model ( & torrent ) . First ( & torrent , id ) . RecordNotFound ( ) {
2017-06-15 04:44:46 +02:00
return & torrent , http . StatusNotFound , errors . New ( "Torrent is not found" )
2017-05-25 02:19:05 +02:00
}
if db . ORM . Unscoped ( ) . Model ( & torrent ) . Delete ( & torrent ) . Error != nil {
2017-06-15 04:44:46 +02:00
return & torrent , http . StatusInternalServerError , errors . New ( "Torrent was not deleted" )
2017-05-25 02:19:05 +02:00
}
2017-05-26 01:48:14 +02:00
2017-06-05 03:33:02 +02:00
if db . ElasticSearchClient != nil {
err := torrent . DeleteFromESIndex ( db . ElasticSearchClient )
2017-05-26 01:48:14 +02:00
if err == nil {
log . Infof ( "Successfully deleted torrent to ES index." )
} else {
log . Errorf ( "Unable to delete torrent to ES index: %s" , err )
}
}
2017-06-15 04:44:46 +02:00
return & torrent , http . StatusOK , nil
2017-05-25 02:19:05 +02:00
}
2017-05-26 12:12:52 +02:00
// ToggleBlockTorrent ; Lock/Unlock a torrent based on id
2017-05-25 02:19:05 +02:00
func ToggleBlockTorrent ( id string ) ( model . Torrent , int , error ) {
var torrent model . Torrent
if db . ORM . Unscoped ( ) . Model ( & torrent ) . First ( & torrent , id ) . RecordNotFound ( ) {
2017-05-26 12:12:52 +02:00
return torrent , http . StatusNotFound , errors . New ( "Torrent is not found" )
2017-05-25 02:19:05 +02:00
}
if torrent . Status == model . TorrentStatusBlocked {
torrent . Status = model . TorrentStatusNormal
} else {
2017-05-25 23:35:37 +02:00
torrent . Status = model . TorrentStatusBlocked
2017-05-25 02:19:05 +02:00
}
if db . ORM . Unscoped ( ) . Model ( & torrent ) . UpdateColumn ( & torrent ) . Error != nil {
2017-05-26 12:12:52 +02:00
return torrent , http . StatusInternalServerError , errors . New ( "Torrent was not updated" )
2017-05-25 02:19:05 +02:00
}
return torrent , http . StatusOK , nil
}
2017-05-26 12:12:52 +02:00
// UpdateTorrent : Update a torrent based on model
2017-06-15 04:44:46 +02:00
func UpdateTorrent ( torrent * model . Torrent ) ( int , error ) {
if db . ORM . Model ( torrent ) . UpdateColumn ( torrent ) . Error != nil {
return http . StatusInternalServerError , errors . New ( "Torrent was not updated" )
}
// TODO Don't create a new client for each request
if db . ElasticSearchClient != nil {
err := torrent . AddToESIndex ( db . ElasticSearchClient )
if err == nil {
log . Infof ( "Successfully updated torrent to ES index." )
} else {
log . Errorf ( "Unable to update torrent to ES index: %s" , err )
}
}
return http . StatusOK , nil
}
// UpdateUnscopeTorrent : Update a torrent based on model
func UpdateUnscopeTorrent ( torrent * model . Torrent ) ( int , error ) {
if db . ORM . Unscoped ( ) . Model ( torrent ) . UpdateColumn ( torrent ) . Error != nil {
2017-05-26 12:12:52 +02:00
return http . StatusInternalServerError , errors . New ( "Torrent was not updated" )
2017-05-10 05:24:18 +02:00
}
2017-06-14 08:36:38 +02:00
// TODO Don't create a new client for each request
2017-06-05 03:33:02 +02:00
if db . ElasticSearchClient != nil {
err := torrent . AddToESIndex ( db . ElasticSearchClient )
2017-05-26 01:48:14 +02:00
if err == nil {
log . Infof ( "Successfully updated torrent to ES index." )
} else {
log . Errorf ( "Unable to update torrent to ES index: %s" , err )
}
}
2017-05-10 04:03:25 +02:00
return http . StatusOK , nil
2017-05-10 05:40:47 +02:00
}
2017-05-25 02:19:05 +02:00
2017-05-26 12:12:52 +02:00
// GetDeletedTorrents : Gets deleted torrents based on search params
2017-05-25 02:19:05 +02:00
func GetDeletedTorrents ( parameters * serviceBase . WhereParams , orderBy string , limit int , offset int ) ( torrents [ ] model . Torrent , count int , err error ) {
torrents , count , err = getTorrentsOrderBy ( parameters , orderBy , limit , offset , true , true , true )
return
2017-05-25 23:35:37 +02:00
}
2017-06-05 15:19:25 +02:00
// ExistOrDelete : Check if a torrent exist with the same hash and if it can be replaced, it is replaced
func ExistOrDelete ( hash string , user * model . User ) error {
torrentIndb := model . Torrent { }
db . ORM . Unscoped ( ) . Model ( & model . Torrent { } ) . Where ( "torrent_hash = ?" , hash ) . First ( & torrentIndb )
if torrentIndb . ID > 0 {
if userPermission . CurrentUserIdentical ( user , torrentIndb . UploaderID ) && torrentIndb . IsDeleted ( ) && ! torrentIndb . IsBlocked ( ) { // if torrent is not locked and is deleted and the user is the actual owner
2017-06-15 04:44:46 +02:00
torrent , _ , err := DefinitelyDeleteTorrent ( strconv . Itoa ( int ( torrentIndb . ID ) ) )
2017-06-05 15:19:25 +02:00
if err != nil {
return err
}
2017-06-15 04:44:46 +02:00
activity . Log ( & model . User { } , torrent . Identifier ( ) , "delete" , "torrent_deleted_by" , strconv . Itoa ( int ( torrent . ID ) ) , torrent . Uploader . Username , user . Username )
2017-06-05 15:19:25 +02:00
} else {
return errors . New ( "Torrent already in database" )
}
}
return nil
}
// NewTorrentEvent : Should be called when you create a new torrent
func NewTorrentEvent ( router * mux . Router , user * model . User , torrent * model . Torrent ) error {
url , err := router . Get ( "view_torrent" ) . URL ( "id" , strconv . FormatUint ( uint64 ( torrent . ID ) , 10 ) )
if err != nil {
return err
}
if user . ID > 0 && config . Conf . Users . DefaultUserSettings [ "new_torrent" ] { // If we are a member and notifications for new torrents are enabled
userService . GetFollowers ( user ) // We populate the liked field for users
if len ( user . Followers ) > 0 { // If we are followed by at least someone
for _ , follower := range user . Followers {
follower . ParseSettings ( ) // We need to call it before checking settings
if follower . Settings . Get ( "new_torrent" ) {
T , _ , _ := publicSettings . TfuncAndLanguageWithFallback ( follower . Language , follower . Language ) // We need to send the notification to every user in their language
notifierService . NotifyUser ( & follower , torrent . Identifier ( ) , fmt . Sprintf ( T ( "new_torrent_uploaded" ) , torrent . Name , user . Username ) , url . String ( ) , follower . Settings . Get ( "new_torrent_email" ) )
}
}
}
}
return nil
}
2017-06-15 04:44:46 +02:00
// HideTorrentUser : hides a torrent user for hidden torrents
func HideTorrentUser ( uploaderID uint , uploaderName string , torrentHidden bool ) ( uint , string ) {
if torrentHidden {
return 0 , "れんちょん"
}
if uploaderID == 0 {
2017-06-16 00:57:52 +02:00
return 0 , uploaderName
2017-06-15 04:44:46 +02:00
}
return uploaderID , uploaderName
}
// TorrentsToAPI : Map Torrents for API usage without reallocations
func TorrentsToAPI ( t [ ] model . Torrent ) [ ] model . TorrentJSON {
json := make ( [ ] model . TorrentJSON , len ( t ) )
for i := range t {
json [ i ] = t [ i ] . ToJSON ( )
uploaderID , username := HideTorrentUser ( json [ i ] . UploaderID , string ( json [ i ] . UploaderName ) , json [ i ] . Hidden )
json [ i ] . UploaderName = template . HTML ( username )
json [ i ] . UploaderID = uploaderID
}
return json
}