1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-17 10:56:24 -07:00

Add custom flag usage during post processing

This commit is contained in:
Eggbertx 2024-01-21 17:16:27 -08:00
parent 19e27da5a6
commit f78bcd3704
4 changed files with 53 additions and 19 deletions

View file

@ -237,15 +237,15 @@ type BoardConfig struct {
isGlobal bool
}
// CheckFlag returns true if the given flag and name are configured for
// CheckCustomFlag returns true if the given flag and name are configured for
// the board (or are globally set)
func (bc *BoardConfig) CheckFlag(flag string, name string) bool {
func (bc *BoardConfig) CheckCustomFlag(flag string) (string, bool) {
for _, country := range bc.CustomFlags {
if flag == country.Flag && name == country.Name {
return true
if flag == country.Flag {
return country.Name, true
}
}
return false
return "", false
}
// IsGlobal returns true if this is the global configuration applied to all

View file

@ -31,6 +31,7 @@ const (
var (
ErrorPostTooLong = errors.New("post is too long")
ErrInvalidFlag = errors.New("invalid selected flag")
)
// MakePost is called when a user accesses /post. Parse form data, then insert and build
@ -248,24 +249,57 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
return
}
if boardConfig.EnableGeoIP {
geoipInfo, err := geoip.GetCountry(request, postBoard.Dir, errEv)
if err != nil {
// GetCountry logs the error
server.ServeError(writer, "Unable to get post info", wantsJSON, nil)
return
}
post.Country = geoipInfo.Name
post.Flag = strings.ToLower(geoipInfo.Flag)
}
captchaSuccess, err := submitCaptchaResponse(request)
if err != nil {
server.ServeError(writer, "Error submitting captcha response:"+err.Error(), wantsJSON, nil)
errEv.Err(err).
Caller().Send()
server.ServeError(writer, "Error submitting captcha response:"+err.Error(), wantsJSON, nil)
return
}
flag := request.PostFormValue("post-flag")
if flag != "" {
errEv.Str("flag", flag)
}
switch flag {
case "geoip":
if boardConfig.EnableGeoIP {
geoipInfo, err := geoip.GetCountry(request, postBoard.Dir, errEv)
if err != nil {
// GetCountry logs the error
break
}
post.Country = geoipInfo.Name
post.Flag = strings.ToLower(geoipInfo.Flag)
} else {
err = ErrInvalidFlag
errEv.Caller().
Msg("User selected 'geoip' on a non-geoip board")
}
case "":
// "No flag"
if !boardConfig.EnableNoFlag {
err = ErrInvalidFlag
errEv.Caller().
Msg("User submitted 'No flag' on a board without it enabled")
}
default:
// custom flag
var validFlag bool
post.Country, validFlag = boardConfig.CheckCustomFlag(flag)
if !validFlag {
err = ErrInvalidFlag
errEv.Caller().Msg("User submitted invalid custom flag")
}
post.Flag = flag
}
if err != nil {
server.ServeError(writer, err.Error(), wantsJSON, map[string]any{
"flag": flag,
})
return
}
if !captchaSuccess {
server.ServeError(writer, "Missing or invalid captcha response", wantsJSON, nil)
errEv.Msg("Missing or invalid captcha response")

View file

@ -2,7 +2,7 @@
<img {{if .IsGeoIP -}}
class="flag-geoip flag-{{.Flag}}"
{{- else -}}
class="flag-geoip" src="{{webPath `/static/flags/` .Flag}}"
src="{{webPath `/static/flags/` .Flag}}"
{{- end -}}
title="{{.Name}}" />
{{- end -}}

View file

@ -19,7 +19,7 @@
<tr>
<th class="postblock">Flag</th>
<td>
<select name="flag" id="post-flag">
<select name="post-flag" id="post-flag">
{{- if $.boardConfig.EnableGeoIP -}}
<option value="geoip">Local flag</option>
{{- end -}}