2017-06-28 13:42:38 +02:00
package messages
2017-05-24 09:11:13 +02:00
2017-05-20 13:45:15 +02:00
import (
2017-07-01 01:25:11 +02:00
"errors"
2017-05-20 13:45:15 +02:00
"fmt"
2017-05-26 12:12:52 +02:00
2017-05-27 19:08:47 +02:00
"github.com/NyaaPantsu/nyaa/util/publicSettings"
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
2017-05-24 09:11:13 +02:00
"github.com/gorilla/context"
"github.com/nicksnyder/go-i18n/i18n"
2017-05-20 13:45:15 +02:00
)
2017-05-26 12:12:52 +02:00
// MessagesKey : use for context
2017-05-29 17:07:18 +02:00
const MessagesKey = "nyaapantsu.messages"
2017-05-20 13:45:15 +02:00
2017-05-26 12:12:52 +02:00
// Messages struct
2017-05-20 13:45:15 +02:00
type Messages struct {
Errors map [ string ] [ ] string
2017-05-24 09:11:13 +02:00
Infos map [ string ] [ ] string
2017-06-28 13:42:38 +02:00
c * gin . Context
2017-05-24 09:11:13 +02:00
T i18n . TranslateFunc
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
// GetMessages : Initialize or return the messages object from context
2017-06-28 13:42:38 +02:00
func GetMessages ( c * gin . Context ) * Messages {
if rv := context . Get ( c . Request , MessagesKey ) ; rv != nil {
2017-05-24 09:11:13 +02:00
mes := rv . ( * Messages )
2017-06-28 13:42:38 +02:00
T , _ := publicSettings . GetTfuncAndLanguageFromRequest ( c )
2017-05-24 09:11:13 +02:00
mes . T = T
2017-06-28 13:42:38 +02:00
mes . c = c
2017-05-24 09:11:13 +02:00
return mes
}
2017-06-28 13:42:38 +02:00
context . Set ( c . Request , MessagesKey , & Messages { } )
T , _ := publicSettings . GetTfuncAndLanguageFromRequest ( c )
return & Messages { make ( map [ string ] [ ] string ) , make ( map [ string ] [ ] string ) , c , T }
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
// AddError : Add an error in category name with message msg
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) AddError ( name string , msg string ) error {
2017-05-24 09:11:13 +02:00
if mes . Errors == nil {
2017-05-22 00:22:42 +02:00
mes . Errors = make ( map [ string ] [ ] string )
}
2017-05-20 13:45:15 +02:00
mes . Errors [ name ] = append ( mes . Errors [ name ] , msg )
2017-05-20 17:01:13 +02:00
mes . setMessagesInContext ( )
2017-07-01 01:25:11 +02:00
return errors . New ( msg )
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
// AddErrorf : Add an error in category name with message msg formatted with args
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) AddErrorf ( name string , msg string , args ... interface { } ) error {
return mes . AddError ( name , fmt . Sprintf ( msg , args ... ) )
2017-05-20 17:01:13 +02:00
}
2017-05-26 12:12:52 +02:00
// AddErrorTf : Add an error in category name with translation string id formatted with args
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) AddErrorTf ( name string , id string , args ... interface { } ) error {
return mes . AddErrorf ( name , mes . T ( id ) , args ... )
2017-05-23 04:05:33 +02:00
}
2017-05-26 12:12:52 +02:00
// AddErrorT : Add an error in category name with translation string id
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) AddErrorT ( name string , id string ) error {
return mes . AddError ( name , mes . T ( id ) )
2017-05-23 04:05:33 +02:00
}
2017-05-26 12:12:52 +02:00
// ImportFromError : Add an error in category name with message msg imported from type error
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) ImportFromError ( name string , err error ) error {
return mes . AddError ( name , err . Error ( ) )
2017-05-20 13:45:15 +02:00
}
2017-06-28 13:42:38 +02:00
// ImportFromErrorT : Add an error in category name with message msg imported from type error
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) ImportFromErrorT ( name string , err error ) error {
return mes . AddError ( name , mes . T ( err . Error ( ) ) )
2017-06-28 13:42:38 +02:00
}
// ImportFromErrorTf : Add an error in category name with message msg imported from type error
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) ImportFromErrorTf ( name string , err error , args ... interface { } ) error {
return mes . AddError ( name , fmt . Sprintf ( mes . T ( err . Error ( ) ) , args ... ) )
2017-06-28 13:42:38 +02:00
}
// ImportFromError : Aliases to import directly an error in "errors" map index
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) Error ( err error ) error {
return mes . ImportFromError ( "errors" , err )
2017-06-28 13:42:38 +02:00
}
// ErrorT : Aliases to import directly an error in "errors" map index and translate the error
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) ErrorT ( err error ) error {
return mes . ImportFromErrorT ( "errors" , err )
2017-06-28 13:42:38 +02:00
}
// ErrorTf : Aliases to import directly an error in "errors" map index and translate the error with args
2017-07-01 01:25:11 +02:00
func ( mes * Messages ) ErrorTf ( err error , args ... interface { } ) error {
return mes . ImportFromErrorTf ( "errors" , err )
2017-06-28 13:42:38 +02:00
}
2017-05-26 12:12:52 +02:00
// AddInfo : Add an info in category name with message msg
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) AddInfo ( name string , msg string ) {
2017-05-24 09:11:13 +02:00
if mes . Infos == nil {
2017-05-22 00:22:42 +02:00
mes . Infos = make ( map [ string ] [ ] string )
}
2017-05-20 13:45:15 +02:00
mes . Infos [ name ] = append ( mes . Infos [ name ] , msg )
2017-05-20 17:01:13 +02:00
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
// AddInfof : Add an info in category name with message msg formatted with args
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) AddInfof ( name string , msg string , args ... interface { } ) {
2017-05-20 17:01:13 +02:00
mes . AddInfo ( name , fmt . Sprintf ( msg , args ... ) )
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
// AddInfoTf : Add an info in category name with translation string id formatted with args
2017-05-23 04:05:33 +02:00
func ( mes * Messages ) AddInfoTf ( name string , id string , args ... interface { } ) {
mes . AddInfof ( name , mes . T ( id ) , args ... )
}
2017-05-26 12:12:52 +02:00
// AddInfoT : Add an info in category name with translation string id
2017-05-23 04:05:33 +02:00
func ( mes * Messages ) AddInfoT ( name string , id string ) {
mes . AddInfo ( name , mes . T ( id ) )
}
2017-05-20 13:45:15 +02:00
2017-06-10 00:58:28 +02:00
// ClearAllErrors : Erase all errors in messages
func ( mes * Messages ) ClearAllErrors ( ) {
mes . Errors = nil
mes . setMessagesInContext ( )
}
// ClearAllInfos : Erase all infos in messages
func ( mes * Messages ) ClearAllInfos ( ) {
2017-05-20 17:01:13 +02:00
mes . Infos = nil
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
2017-06-10 00:58:28 +02:00
// ClearErrors : Erase all errors in messages
func ( mes * Messages ) ClearErrors ( name string ) {
2017-06-14 09:00:50 +02:00
delete ( mes . Errors , name )
2017-06-10 00:58:28 +02:00
mes . setMessagesInContext ( )
}
2017-05-26 12:12:52 +02:00
// ClearInfos : Erase all infos in messages
2017-06-10 00:58:28 +02:00
func ( mes * Messages ) ClearInfos ( name string ) {
2017-06-14 09:00:50 +02:00
delete ( mes . Infos , name )
2017-05-20 17:01:13 +02:00
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
// GetAllErrors : Get all errors
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) GetAllErrors ( ) map [ string ] [ ] string {
2017-06-28 13:42:38 +02:00
mes = GetMessages ( mes . c ) // We need to look if any new errors from other functions has updated context
2017-05-20 17:01:13 +02:00
return mes . Errors
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
// GetErrors : Get all errors in category name
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) GetErrors ( name string ) [ ] string {
2017-06-28 13:42:38 +02:00
mes = GetMessages ( mes . c ) // We need to look if any new errors from other functions has updated context
2017-05-20 13:45:15 +02:00
return mes . Errors [ name ]
}
2017-05-20 17:01:13 +02:00
2017-05-26 12:12:52 +02:00
// GetAllInfos : Get all infos
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) GetAllInfos ( ) map [ string ] [ ] string {
2017-06-28 13:42:38 +02:00
mes = GetMessages ( mes . c ) // We need to look if any new errors from other functions has updated context
2017-05-20 13:45:15 +02:00
return mes . Infos
}
2017-05-26 12:12:52 +02:00
// GetInfos : Get all infos in category name
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) GetInfos ( name string ) [ ] string {
2017-06-28 13:42:38 +02:00
mes = GetMessages ( mes . c ) // We need to look if any new errors from other functions has updated context
2017-05-20 17:01:13 +02:00
return mes . Infos [ name ]
2017-05-20 13:45:15 +02:00
}
2017-05-26 12:12:52 +02:00
// HasErrors : Check if there are errors
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) HasErrors ( ) bool {
2017-06-28 13:42:38 +02:00
mes = GetMessages ( mes . c ) // We need to look if any new errors from other functions has updated context
2017-05-20 13:45:15 +02:00
return len ( mes . Errors ) > 0
}
2017-05-26 12:12:52 +02:00
// HasInfos : Check if there are infos
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) HasInfos ( ) bool {
2017-06-28 13:42:38 +02:00
mes = GetMessages ( mes . c ) // We need to look if any new errors from other functions has updated context
2017-05-20 13:45:15 +02:00
return len ( mes . Infos ) > 0
2017-05-20 17:01:13 +02:00
}
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) setMessagesInContext ( ) {
2017-06-28 13:42:38 +02:00
context . Set ( mes . c . Request , MessagesKey , mes )
2017-05-24 09:11:13 +02:00
}