2017-05-20 13:45:15 +02:00
package Messages
2017-05-24 09:11:13 +02:00
2017-05-20 13:45:15 +02:00
import (
"fmt"
2017-05-23 04:05:33 +02:00
"github.com/NyaaPantsu/nyaa/util/languages"
2017-05-24 09:11:13 +02:00
"github.com/gorilla/context"
"github.com/nicksnyder/go-i18n/i18n"
"net/http"
2017-05-20 13:45:15 +02:00
)
const MessagesKey = "messages"
type Messages struct {
Errors map [ string ] [ ] string
2017-05-24 09:11:13 +02:00
Infos map [ string ] [ ] string
r * http . Request
T i18n . TranslateFunc
2017-05-20 13:45:15 +02:00
}
2017-05-22 10:15:18 +02:00
func GetMessages ( r * http . Request ) * Messages {
2017-05-20 13:45:15 +02:00
if rv := context . Get ( r , MessagesKey ) ; rv != nil {
2017-05-24 09:11:13 +02:00
mes := rv . ( * Messages )
T , _ := languages . GetTfuncAndLanguageFromRequest ( r )
mes . T = T
mes . r = r
return mes
} else {
context . Set ( r , MessagesKey , & Messages { } )
T , _ := languages . GetTfuncAndLanguageFromRequest ( r )
return & Messages { make ( map [ string ] [ ] string ) , make ( map [ string ] [ ] string ) , r , T }
}
2017-05-20 13:45:15 +02:00
}
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) AddError ( name string , msg string ) {
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-05-20 13:45:15 +02:00
}
2017-05-24 09:11:13 +02:00
func ( mes * Messages ) AddErrorf ( name string , msg string , args ... interface { } ) {
2017-05-20 17:01:13 +02:00
mes . AddError ( name , fmt . Sprintf ( msg , args ... ) )
}
2017-05-24 09:11:13 +02:00
func ( mes * Messages ) AddErrorTf ( name string , id string , args ... interface { } ) {
2017-05-23 04:05:33 +02:00
mes . AddErrorf ( name , mes . T ( id ) , args ... )
}
2017-05-24 09:11:13 +02:00
func ( mes * Messages ) AddErrorT ( name string , id string ) {
2017-05-23 04:05:33 +02:00
mes . AddError ( name , mes . T ( id ) )
}
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) ImportFromError ( name string , err error ) {
2017-05-20 17:01:13 +02:00
mes . AddError ( name , err . Error ( ) )
2017-05-20 13:45:15 +02:00
}
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-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-23 04:05:33 +02:00
func ( mes * Messages ) AddInfoTf ( name string , id string , args ... interface { } ) {
mes . AddInfof ( name , mes . T ( id ) , args ... )
}
func ( mes * Messages ) AddInfoT ( name string , id string ) {
mes . AddInfo ( name , mes . T ( id ) )
}
2017-05-20 13:45:15 +02:00
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) ClearErrors ( ) {
2017-05-20 17:01:13 +02:00
mes . Infos = nil
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) ClearInfos ( ) {
2017-05-20 13:45:15 +02:00
mes . Errors = nil
2017-05-20 17:01:13 +02:00
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) GetAllErrors ( ) map [ string ] [ ] string {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // We need to look if any new errors from other functions has updated context
return mes . Errors
2017-05-20 13:45:15 +02:00
}
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) GetErrors ( name string ) [ ] string {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // 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-22 10:15:18 +02:00
func ( mes * Messages ) GetAllInfos ( ) map [ string ] [ ] string {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // 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-22 10:15:18 +02:00
func ( mes * Messages ) GetInfos ( name string ) [ ] string {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // We need to look if any new errors from other functions has updated context
return mes . Infos [ name ]
2017-05-20 13:45:15 +02:00
}
2017-05-22 10:15:18 +02:00
func ( mes * Messages ) HasErrors ( ) bool {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // 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-22 10:15:18 +02:00
func ( mes * Messages ) HasInfos ( ) bool {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // 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-05-24 09:11:13 +02:00
context . Set ( mes . r , MessagesKey , mes )
}