2017-07-16 17:14:21 +02:00
package userController
2017-06-28 13:42:38 +02:00
import (
"strconv"
"time"
2017-10-04 15:46:06 +02:00
"fmt"
2017-06-28 13:42:38 +02:00
2017-07-16 17:14:21 +02:00
"net/http"
"github.com/NyaaPantsu/nyaa/controllers/router"
2017-06-29 13:15:23 +02:00
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/models/notifications"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/models/users"
2017-07-16 15:35:24 +02:00
"github.com/NyaaPantsu/nyaa/templates"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/cookies"
"github.com/NyaaPantsu/nyaa/utils/crypto"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/utils/email"
2017-07-02 16:54:55 +02:00
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/utils/validator"
"github.com/NyaaPantsu/nyaa/utils/validator/user"
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
)
2017-10-12 02:54:01 +02:00
// UserProfileDelete : Deleting User Profile
func UserProfileDelete ( c * gin . Context ) {
id , _ := strconv . ParseUint ( c . Param ( "id" ) , 10 , 32 )
userProfile , _ , errorUser := users . FindForAdmin ( uint ( id ) )
if errorUser == nil {
currentUser := router . GetUser ( c )
if ( currentUser . CurrentOrAdmin ( userProfile . ID ) ) {
_ , err := userProfile . Delete ( currentUser )
if err == nil && currentUser . CurrentUserIdentical ( userProfile . ID ) {
cookies . Clear ( c )
}
}
templates . Static ( c , "site/static/delete_success.jet.html" )
}
}
2017-06-28 13:42:38 +02:00
// UserProfileHandler : Getting User Profile
func UserProfileHandler ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseUint ( c . Param ( "id" ) , 10 , 32 )
2017-06-28 13:42:38 +02:00
Ts , _ := publicSettings . GetTfuncAndLanguageFromRequest ( c )
messages := msg . GetMessages ( c )
2017-10-12 02:54:01 +02:00
if c . Param ( "id" ) != "0" && id == 0 && ContainsNonNumbersChars ( c . Param ( "id" ) ) {
2017-10-04 15:46:06 +02:00
c . Redirect ( http . StatusSeeOther , fmt . Sprintf ( "/username/%s" , c . Param ( "id" ) ) )
return
}
2017-07-02 23:53:23 +02:00
userProfile , _ , errorUser := users . FindForAdmin ( uint ( id ) )
2017-06-28 13:42:38 +02:00
if errorUser == nil {
2017-07-16 17:14:21 +02:00
currentUser := router . GetUser ( c )
2017-06-28 13:42:38 +02:00
follow := c . Request . URL . Query ( ) [ "followed" ]
unfollow := c . Request . URL . Query ( ) [ "unfollowed" ]
deleteVar := c . Request . URL . Query ( ) [ "delete" ]
2017-10-12 02:54:01 +02:00
if ! ( ( deleteVar != nil ) && ( currentUser . CurrentOrAdmin ( userProfile . ID ) ) ) {
2017-06-28 13:42:38 +02:00
if follow != nil {
messages . AddInfof ( "infos" , Ts ( "user_followed_msg" ) , userProfile . Username )
}
if unfollow != nil {
messages . AddInfof ( "infos" , Ts ( "user_unfollowed_msg" ) , userProfile . Username )
}
userProfile . ParseSettings ( )
2017-07-20 20:21:57 +02:00
2017-07-20 13:33:50 +02:00
templates . UserProfile ( c , userProfile )
2017-06-28 13:42:38 +02:00
}
} else {
2017-10-04 15:46:06 +02:00
variables := templates . Commonvariables ( c )
templates . Render ( c , "errors/user_not_found.jet.html" , variables )
}
}
func ContainsNonNumbersChars ( source string ) bool {
for char := range source {
if char < 30 || char > 39 {
return true
}
2017-06-28 13:42:38 +02:00
}
2017-10-04 15:46:06 +02:00
return false
2017-06-28 13:42:38 +02:00
}
2017-09-22 06:54:19 +02:00
func UserGetFromName ( c * gin . Context ) {
username := c . Param ( "username" )
2017-10-03 01:44:33 +02:00
Ts , _ := publicSettings . GetTfuncAndLanguageFromRequest ( c )
messages := msg . GetMessages ( c )
userProfile , _ , _ , err := users . FindByUsername ( username )
if err == nil {
currentUser := router . GetUser ( c )
follow := c . Request . URL . Query ( ) [ "followed" ]
unfollow := c . Request . URL . Query ( ) [ "unfollowed" ]
deleteVar := c . Request . URL . Query ( ) [ "delete" ]
if ( deleteVar != nil ) && ( currentUser . CurrentOrAdmin ( userProfile . ID ) ) {
_ , err := userProfile . Delete ( currentUser )
if err == nil && currentUser . CurrentUserIdentical ( userProfile . ID ) {
cookies . Clear ( c )
}
templates . Static ( c , "site/static/delete_success.jet.html" )
} else {
if follow != nil {
messages . AddInfof ( "infos" , Ts ( "user_followed_msg" ) , userProfile . Username )
}
if unfollow != nil {
messages . AddInfof ( "infos" , Ts ( "user_unfollowed_msg" ) , userProfile . Username )
}
userProfile . ParseSettings ( )
templates . UserProfile ( c , userProfile )
}
} else {
2017-10-04 15:46:06 +02:00
variables := templates . Commonvariables ( c )
searchForm := templates . NewSearchForm ( c )
searchForm . User = username
variables . Set ( "Search" , searchForm )
templates . Render ( c , "errors/user_not_found.jet.html" , variables )
}
}
func RedirectToUserSearch ( c * gin . Context ) {
username := c . Query ( "username" )
if username == "" {
variables := templates . Commonvariables ( c )
templates . Render ( c , "errors/user_not_found.jet.html" , variables )
} else {
c . Redirect ( http . StatusSeeOther , fmt . Sprintf ( "/username/%s" , username ) )
2017-10-03 01:44:33 +02:00
}
2017-09-22 06:54:19 +02:00
}
2017-06-28 13:42:38 +02:00
// UserDetailsHandler : Getting User Profile Details View
func UserDetailsHandler ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseUint ( c . Param ( "id" ) , 10 , 32 )
2017-07-16 17:14:21 +02:00
currentUser := router . GetUser ( c )
2017-06-28 13:42:38 +02:00
2017-07-02 23:53:23 +02:00
userProfile , _ , errorUser := users . FindForAdmin ( uint ( id ) )
if errorUser == nil && currentUser . CurrentOrAdmin ( userProfile . ID ) {
b := userValidator . UserForm { }
c . Bind ( & b )
availableLanguages := publicSettings . GetAvailableLanguages ( )
userProfile . ParseSettings ( )
2017-07-16 15:35:24 +02:00
templates . UserProfileEdit ( c , userProfile , b , availableLanguages )
2017-06-28 13:42:38 +02:00
} else {
2017-10-04 15:46:06 +02:00
variables := templates . Commonvariables ( c )
templates . Render ( c , "errors/user_not_found.jet.html" , variables )
2017-06-28 13:42:38 +02:00
}
}
// UserProfileFormHandler : Getting View User Profile Update
func UserProfileFormHandler ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseUint ( c . Param ( "id" ) , 10 , 32 )
2017-07-16 17:14:21 +02:00
currentUser := router . GetUser ( c )
2017-07-02 23:53:23 +02:00
userProfile , _ , errorUser := users . FindForAdmin ( uint ( id ) )
if errorUser != nil || ! currentUser . CurrentOrAdmin ( userProfile . ID ) || userProfile . ID == 0 {
2017-07-16 17:14:21 +02:00
c . Status ( http . StatusNotFound )
2017-06-28 13:42:38 +02:00
return
}
userProfile . ParseSettings ( )
messages := msg . GetMessages ( c )
2017-07-02 23:53:23 +02:00
userForm := userValidator . UserForm { }
userSettingsForm := userValidator . UserSettingsForm { }
2017-06-28 13:42:38 +02:00
if len ( c . PostForm ( "email" ) ) > 0 {
2017-07-03 02:16:39 +02:00
if ! userValidator . EmailValidation ( c . PostForm ( "email" ) ) {
messages . AddErrorT ( "email" , "email_not_valid" )
2017-07-02 23:53:23 +02:00
}
2017-06-28 13:42:38 +02:00
}
if len ( c . PostForm ( "username" ) ) > 0 {
2017-07-03 02:16:39 +02:00
if ! userValidator . ValidateUsername ( c . PostForm ( "username" ) ) {
messages . AddErrorT ( "username" , "username_illegal" )
}
2017-06-28 13:42:38 +02:00
}
if ! messages . HasErrors ( ) {
c . Bind ( & userForm )
c . Bind ( & userSettingsForm )
2017-07-02 23:53:23 +02:00
if ! currentUser . HasAdmin ( ) {
2017-06-28 13:42:38 +02:00
userForm . Username = userProfile . Username
userForm . Status = userProfile . Status
} else {
if userProfile . Status != userForm . Status && userForm . Status == 2 {
messages . AddErrorT ( "errors" , "elevating_user_error" )
}
}
2017-07-02 23:53:23 +02:00
validator . ValidateForm ( & userForm , messages )
2017-06-28 13:42:38 +02:00
if ! messages . HasErrors ( ) {
if userForm . Email != userProfile . Email {
2017-07-02 23:53:23 +02:00
email . SendVerificationToUser ( currentUser , userForm . Email )
2017-06-28 13:42:38 +02:00
messages . AddInfoTf ( "infos" , "email_changed" , userForm . Email )
userForm . Email = userProfile . Email // reset, it will be set when user clicks verification
}
2017-07-02 23:53:23 +02:00
user , _ , err := users . UpdateFromRequest ( c , & userForm , & userSettingsForm , currentUser , uint ( id ) )
if err != nil {
messages . Error ( err )
}
if userForm . Email != user . Email {
// send verification to new email and keep old
email . SendVerificationToUser ( user , userForm . Email )
}
2017-06-28 13:42:38 +02:00
if ! messages . HasErrors ( ) {
messages . AddInfoT ( "infos" , "profile_updated" )
}
}
}
availableLanguages := publicSettings . GetAvailableLanguages ( )
2017-07-16 15:35:24 +02:00
templates . UserProfileEdit ( c , userProfile , userForm , availableLanguages )
2017-06-28 13:42:38 +02:00
}
// UserNotificationsHandler : Controller to show user notifications
func UserNotificationsHandler ( c * gin . Context ) {
2017-07-16 17:14:21 +02:00
currentUser := router . GetUser ( c )
2017-06-28 13:42:38 +02:00
if currentUser . ID > 0 {
messages := msg . GetMessages ( c )
if c . Request . URL . Query ( ) [ "clear" ] != nil {
2017-07-02 23:53:23 +02:00
notifications . DeleteAllNotifications ( currentUser . ID )
2017-06-28 13:42:38 +02:00
messages . AddInfoT ( "infos" , "notifications_cleared" )
2017-07-01 23:09:35 +02:00
currentUser . Notifications = [ ] models . Notification { }
2017-06-28 13:42:38 +02:00
}
2017-07-16 15:35:24 +02:00
templates . UserProfileNotifications ( c , currentUser )
2017-06-28 13:42:38 +02:00
} else {
2017-07-16 17:14:21 +02:00
c . Status ( http . StatusNotFound )
2017-06-28 13:42:38 +02:00
}
}
// UserAPIKeyResetHandler : Controller to reset user api key
func UserAPIKeyResetHandler ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseUint ( c . Param ( "id" ) , 10 , 32 )
2017-07-16 17:14:21 +02:00
currentUser := router . GetUser ( c )
2017-06-28 13:42:38 +02:00
messages := msg . GetMessages ( c )
2017-07-02 23:53:23 +02:00
userProfile , _ , errorUser := users . FindForAdmin ( uint ( id ) )
if errorUser != nil || ! currentUser . CurrentOrAdmin ( userProfile . ID ) || userProfile . ID == 0 {
2017-07-16 17:14:21 +02:00
c . Status ( http . StatusNotFound )
2017-06-28 13:42:38 +02:00
return
}
userProfile . APIToken , _ = crypto . GenerateRandomToken32 ( )
userProfile . APITokenExpiry = time . Unix ( 0 , 0 )
2017-07-02 23:53:23 +02:00
_ , errorUser = userProfile . UpdateRaw ( )
2017-06-28 13:42:38 +02:00
if errorUser != nil {
messages . Error ( errorUser )
} else {
messages . AddInfoT ( "infos" , "profile_updated" )
}
2017-10-11 03:24:09 +02:00
UserDetailsHandler ( c )
2017-06-28 13:42:38 +02:00
}