This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
package torrents
import (
"errors"
"fmt"
"strconv"
2017-07-02 23:53:23 +02:00
"html/template"
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
2017-06-29 01:06:30 +02:00
"github.com/NyaaPantsu/nyaa/models/activities"
"github.com/NyaaPantsu/nyaa/models/notifications"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +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 * models . User ) error {
2017-07-20 02:20:07 +02:00
torrentIndb := & models . Torrent { }
models . ORM . Unscoped ( ) . Model ( & models . Torrent { } ) . Where ( "torrent_hash = ?" , hash ) . Preload ( "Uploader" ) . First ( torrentIndb )
2017-06-29 01:06:30 +02:00
if torrentIndb . ID > 0 {
if user . CurrentUserIdentical ( torrentIndb . UploaderID ) && torrentIndb . IsDeleted ( ) && ! torrentIndb . IsBlocked ( ) { // if torrent is not locked and is deleted and the user is the actual owner
2017-07-20 02:20:07 +02:00
_ , _ , err := torrentIndb . DefinitelyDelete ( )
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
if err != nil {
return err
}
2017-07-20 02:20:07 +02:00
activities . Log ( & models . User { } , torrentIndb . Identifier ( ) , "delete" , "torrent_deleted_by" , strconv . Itoa ( int ( torrentIndb . ID ) ) , torrentIndb . Uploader . Username , user . Username )
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
} else {
return errors . New ( "Torrent already in database" )
}
}
return nil
}
// NewTorrentEvent : Should be called when you create a new torrent
func NewTorrentEvent ( user * models . User , torrent * models . Torrent ) error {
url := "/view/" + strconv . FormatUint ( uint64 ( torrent . ID ) , 10 )
2017-07-10 14:11:05 +02:00
if user . ID > 0 && config . Get ( ) . Users . DefaultUserSettings [ "new_torrent" ] { // If we are a member and notifications for new torrents are enabled
2017-06-29 01:06:30 +02:00
user . GetFollowers ( ) // We populate the liked field for users
if len ( user . Followers ) > 0 { // If we are followed by at least someone
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
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
2017-06-29 01:06:30 +02:00
notifications . NotifyUser ( & follower , torrent . Identifier ( ) , fmt . Sprintf ( T ( "new_torrent_uploaded" ) , torrent . Name , user . Username ) , url , follower . Settings . Get ( "new_torrent_email" ) )
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
}
}
}
}
return nil
}
2017-07-02 23:53:23 +02:00
// HideUser : hides a torrent user for hidden torrents
func HideUser ( uploaderID uint , uploaderName string , torrentHidden bool ) ( uint , string ) {
2017-07-15 01:44:09 +02:00
if uploaderName == "" || torrentHidden {
2017-07-02 23:53:23 +02:00
return 0 , "れんちょん"
}
if uploaderID == 0 {
return 0 , uploaderName
}
return uploaderID , uploaderName
}
// APITorrentsToJSON : Map Torrents to TorrentsToJSON for API request without reallocations
func APITorrentsToJSON ( t [ ] models . Torrent ) [ ] models . TorrentJSON {
json := make ( [ ] models . TorrentJSON , len ( t ) )
for i := range t {
json [ i ] = t [ i ] . ToJSON ( )
uploaderID , username := HideUser ( json [ i ] . UploaderID , string ( json [ i ] . UploaderName ) , json [ i ] . Hidden )
json [ i ] . UploaderName = template . HTML ( username )
json [ i ] . UploaderID = uploaderID
}
return json
}
// TorrentsToAPI : Map Torrents for API usage without reallocations
func TorrentsToAPI ( t [ ] models . Torrent ) [ ] models . TorrentJSON {
return APITorrentsToJSON ( t )
}