Instantly block user in chat, search improvements
This commit is contained in:
parent
e5b5b9435b
commit
a19e52c03f
|
@ -6,6 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.nonshy.com/nonshy/website/pkg/config"
|
"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/log"
|
||||||
"code.nonshy.com/nonshy/website/pkg/models"
|
"code.nonshy.com/nonshy/website/pkg/models"
|
||||||
"code.nonshy.com/nonshy/website/pkg/session"
|
"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)
|
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")
|
templates.Redirect(w, "/users/blocked")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,3 +240,48 @@ func SendBlocklist(user *models.User) error {
|
||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -224,8 +224,8 @@ func SearchUsers(user *User, search *UserSearch, pager *Pagination) ([]*User, er
|
||||||
|
|
||||||
if search.Username != "" {
|
if search.Username != "" {
|
||||||
ilike := "%" + strings.TrimSpace(strings.ToLower(search.Username)) + "%"
|
ilike := "%" + strings.TrimSpace(strings.ToLower(search.Username)) + "%"
|
||||||
wheres = append(wheres, "username LIKE ?")
|
wheres = append(wheres, "username LIKE ? OR name ILIKE ?")
|
||||||
placeholders = append(placeholders, ilike)
|
placeholders = append(placeholders, ilike, ilike)
|
||||||
}
|
}
|
||||||
|
|
||||||
if search.Gender != "" {
|
if search.Gender != "" {
|
||||||
|
|
|
@ -98,7 +98,7 @@
|
||||||
|
|
||||||
<div class="column px-1">
|
<div class="column px-1">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Partial username:</label>
|
<label class="label">Name or username:</label>
|
||||||
<input type="text" class="input"
|
<input type="text" class="input"
|
||||||
name="username"
|
name="username"
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
|
|
|
@ -141,4 +141,8 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="block p-2">
|
||||||
|
{{SimplePager .Pager}}
|
||||||
|
</div>
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user