Instantly block user in chat, search improvements

face-detect
Noah Petherbridge 2023-09-30 15:24:14 -07:00
parent e5b5b9435b
commit a19e52c03f
5 changed files with 56 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"strings"
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/controller/chat"
"code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/session"
@ -140,6 +141,9 @@ func BlockUser() http.HandlerFunc {
session.Flash(w, r, "You have added %s to your block list.", user.Username)
}
// Sync the block to the BareRTC chat server now, in case either user is currently online.
go chat.BlockUserNow(currentUser, user)
templates.Redirect(w, "/users/blocked")
})
}

View File

@ -240,3 +240,48 @@ func SendBlocklist(user *models.User) error {
return nil
}
// BlockUserNow syncs the new block action to the chat server now, in case the user is already online.
func BlockUserNow(currentUser, user *models.User) error {
// API request struct for BareRTC /api/block/now endpoint.
var request = struct {
APIKey string
Usernames []string
}{
config.Current.CronAPIKey,
[]string{
currentUser.Username,
user.Username,
},
}
// JSON request body.
jsonStr, err := json.Marshal(request)
if err != nil {
return err
}
// Make the API request to BareRTC.
var url = strings.TrimSuffix(config.Current.BareRTC.URL, "/") + "/api/block/now"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
log.Error("BlockUserNow: error syncing block to BareRTC: status %d body %s", resp.StatusCode, body)
}
return nil
}

View File

@ -224,8 +224,8 @@ func SearchUsers(user *User, search *UserSearch, pager *Pagination) ([]*User, er
if search.Username != "" {
ilike := "%" + strings.TrimSpace(strings.ToLower(search.Username)) + "%"
wheres = append(wheres, "username LIKE ?")
placeholders = append(placeholders, ilike)
wheres = append(wheres, "username LIKE ? OR name ILIKE ?")
placeholders = append(placeholders, ilike, ilike)
}
if search.Gender != "" {

View File

@ -98,7 +98,7 @@
<div class="column px-1">
<div class="field">
<label class="label">Partial username:</label>
<label class="label">Name or username:</label>
<input type="text" class="input"
name="username"
autocomplete="off"

View File

@ -141,4 +141,8 @@
{{end}}
</div>
<div class="block p-2">
{{SimplePager .Pager}}
</div>
{{end}}