From a19e52c03fe5d0bf9e943730f8fb27c998249cb1 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sat, 30 Sep 2023 15:24:14 -0700 Subject: [PATCH] Instantly block user in chat, search improvements --- pkg/controller/block/block.go | 4 +++ pkg/controller/chat/chat.go | 45 ++++++++++++++++++++++++++++ pkg/models/user.go | 4 +-- web/templates/account/search.html | 2 +- web/templates/forum/board_index.html | 4 +++ 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/pkg/controller/block/block.go b/pkg/controller/block/block.go index b83b12c..a77ca86 100644 --- a/pkg/controller/block/block.go +++ b/pkg/controller/block/block.go @@ -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") }) } diff --git a/pkg/controller/chat/chat.go b/pkg/controller/chat/chat.go index 555396c..a352bf6 100644 --- a/pkg/controller/chat/chat.go +++ b/pkg/controller/chat/chat.go @@ -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 +} diff --git a/pkg/models/user.go b/pkg/models/user.go index 14b5d17..ed3b28a 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -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 != "" { diff --git a/web/templates/account/search.html b/web/templates/account/search.html index 2ea8c97..c0bdc32 100644 --- a/web/templates/account/search.html +++ b/web/templates/account/search.html @@ -98,7 +98,7 @@
- + +
+ {{SimplePager .Pager}} +
+ {{end}}