Alt Text Tweaks + Video site link detection

This commit is contained in:
Noah Petherbridge 2024-03-15 23:19:26 -07:00
parent 9c4ec85f8a
commit 04a7616299
5 changed files with 64 additions and 3 deletions

View File

@ -17,6 +17,7 @@ import (
"code.nonshy.com/nonshy/website/pkg/models" "code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/redis" "code.nonshy.com/nonshy/website/pkg/redis"
"code.nonshy.com/nonshy/website/pkg/session" "code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/spam"
"code.nonshy.com/nonshy/website/pkg/templates" "code.nonshy.com/nonshy/website/pkg/templates"
"code.nonshy.com/nonshy/website/pkg/utility" "code.nonshy.com/nonshy/website/pkg/utility"
"code.nonshy.com/nonshy/website/pkg/worker" "code.nonshy.com/nonshy/website/pkg/worker"
@ -114,7 +115,15 @@ func Settings() http.HandlerFunc {
// Set profile attributes. // Set profile attributes.
for _, attr := range config.ProfileFields { for _, attr := range config.ProfileFields {
user.SetProfileField(attr, r.PostFormValue(attr)) var value = strings.TrimSpace(r.PostFormValue(attr))
// Look for spammy links to restricted video sites or things.
if err := spam.DetectSpamMessage(value); err != nil {
session.FlashError(w, r, "On field '%s': %s", attr, err.Error())
continue
}
user.SetProfileField(attr, value)
} }
// "Looking For" checkbox list. // "Looking For" checkbox list.

View File

@ -15,6 +15,7 @@ import (
"code.nonshy.com/nonshy/website/pkg/models" "code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/photo" "code.nonshy.com/nonshy/website/pkg/photo"
"code.nonshy.com/nonshy/website/pkg/session" "code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/spam"
"code.nonshy.com/nonshy/website/pkg/templates" "code.nonshy.com/nonshy/website/pkg/templates"
) )
@ -183,6 +184,19 @@ func NewPost() http.HandlerFunc {
// Submitting the form. // Submitting the form.
if r.Method == http.MethodPost { if r.Method == http.MethodPost {
// Look for spammy links to video sites or things.
if err := spam.DetectSpamMessage(title + message); err != nil {
session.FlashError(w, r, err.Error())
if thread != nil {
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID))
} else if forum != nil {
templates.Redirect(w, fmt.Sprintf("/f/%s", forum.Fragment))
} else {
templates.Redirect(w, "/forum")
}
return
}
// Polls: parse form parameters into a neat list of answers. // Polls: parse form parameters into a neat list of answers.
pollExpires, _ = strconv.Atoi(r.FormValue("poll_expires")) pollExpires, _ = strconv.Atoi(r.FormValue("poll_expires"))
var distinctPollChoices = map[string]interface{}{} var distinctPollChoices = map[string]interface{}{}

32
pkg/spam/spam.go Normal file
View File

@ -0,0 +1,32 @@
package spam
import (
"errors"
"strings"
)
// SpamWebsites to third-party video hosting apps: we already have our own chat room, and third-party links shared in
// public places can pose a risk to user privacy/safety.
var SpamWebsites = []string{
"join.skype.com",
"zoom.us",
"whereby.com",
"meet.jit.si",
"https://t.me",
}
// DetectSpamMessage searches a message (such as a comment, forum post, etc.) for spammy contents such as Skype invite links
// and returns an error if found.
func DetectSpamMessage(message string) error {
for _, link := range SpamWebsites {
if strings.Contains(message, link) {
return errors.New(
"Your message could not be posted because it contains a link to a third-party video chat website. " +
"In the interest of protecting our community, we do not allow linking to third-party video conferencing apps where user " +
"privacy and security may not hold up to our standards, or where the content may run against our terms of service.",
)
}
}
return nil
}

View File

@ -648,6 +648,7 @@
<!-- GIF video? --> <!-- GIF video? -->
{{if HasSuffix $Body.Photo.Filename ".mp4"}} {{if HasSuffix $Body.Photo.Filename ".mp4"}}
<video loop controls controlsList="nodownload" <video loop controls controlsList="nodownload"
{{if $Body.Photo.AltText}}title="{{$Body.Photo.AltText}}"{{end}}
{{if BlurExplicit $Body.Photo}}class="blurred-explicit" {{if BlurExplicit $Body.Photo}}class="blurred-explicit"
{{else if (not (eq ($Root.CurrentUser.GetProfileField "autoplay_gif") "false"))}}autoplay {{else if (not (eq ($Root.CurrentUser.GetProfileField "autoplay_gif") "false"))}}autoplay
{{end}}> {{end}}>
@ -660,7 +661,9 @@
</div> </div>
{{else}} {{else}}
<a href="/photo/view?id={{$Body.Photo.ID}}"> <a href="/photo/view?id={{$Body.Photo.ID}}">
<img src="{{PhotoURL $Body.Photo.Filename}}" loading="lazy"{{if BlurExplicit $Body.Photo}} class="blurred-explicit"{{end}}> <img src="{{PhotoURL $Body.Photo.Filename}}" loading="lazy"
{{if BlurExplicit $Body.Photo}} class="blurred-explicit"{{end}}
{{if $Body.Photo.AltText}}title="{{$Body.Photo.AltText}}" alt="{{$Body.Photo.AltText}}"{{end}}>
</a> </a>
{{end}} {{end}}

View File

@ -72,13 +72,16 @@
<!-- GIF video? --> <!-- GIF video? -->
{{if HasSuffix .Photo.Filename ".mp4"}} {{if HasSuffix .Photo.Filename ".mp4"}}
<video loop controls controlsList="nodownload" <video loop controls controlsList="nodownload"
{{if .Photo.AltText}}title="{{.Photo.AltText}}"{{end}}
{{if BlurExplicit .Photo}}class="blurred-explicit" {{if BlurExplicit .Photo}}class="blurred-explicit"
{{else if (not (eq ($Root.CurrentUser.GetProfileField "autoplay_gif") "false"))}}autoplay {{else if (not (eq ($Root.CurrentUser.GetProfileField "autoplay_gif") "false"))}}autoplay
{{end}}> {{end}}>
<source src="{{PhotoURL .Photo.Filename}}" type="video/mp4"> <source src="{{PhotoURL .Photo.Filename}}" type="video/mp4">
</video> </video>
{{else}} {{else}}
<img src="{{PhotoURL .Photo.Filename}}"{{if BlurExplicit .Photo}} class="blurred-explicit"{{end}}> <img src="{{PhotoURL .Photo.Filename}}"
{{if BlurExplicit .Photo}} class="blurred-explicit"{{end}}
{{if .Photo.AltText}}alt="{{.Photo.AltText}}" title="{{.Photo.AltText}}"{{end}}>
{{end}} {{end}}
</div> </div>