交付双端基础、实时联系人和隔离浏览器
This commit is contained in:
1 parent
43687d5e7e
commit
67fef3ff87
59 files changed
+5094
-1
No files matched your search
@@ -0,0 +1,86 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"xhchat/server/internal/account"
|
||||
"xhchat/server/internal/contacts"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (a *API) listContacts(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
result, err := a.contacts.List(r.Context(), identity.User.ID)
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, result)
|
||||
}
|
||||
|
||||
func (a *API) searchContact(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
username := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("username")))
|
||||
if !usernamePattern.MatchString(username) {
|
||||
problem(w, 400, "invalid_username", "请输入完整用户名")
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "contact-search:"+identity.User.ID, 60) {
|
||||
return
|
||||
}
|
||||
result, err := a.contacts.Search(r.Context(), identity.User.ID, username)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
problem(w, 404, "contact_not_found", "找不到该用户")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, result)
|
||||
}
|
||||
|
||||
func (a *API) changeContact(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
target := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(target) {
|
||||
problem(w, 404, "contact_not_found", "找不到该用户")
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Action string `json:"action"`
|
||||
Alias string `json:"alias"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
if !parse(w, r, &input) {
|
||||
return
|
||||
}
|
||||
input.Alias = strings.TrimSpace(input.Alias)
|
||||
input.Category = strings.TrimSpace(input.Category)
|
||||
if utf8.RuneCountInString(input.Alias) > 64 || utf8.RuneCountInString(input.Category) > 32 ||
|
||||
strings.ContainsFunc(input.Alias, unicode.IsControl) || strings.ContainsFunc(input.Category, unicode.IsControl) {
|
||||
problem(w, 400, "invalid_contact_preferences", "请检查备注和分组")
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "contact-change:"+identity.User.ID, 60) {
|
||||
return
|
||||
}
|
||||
err := a.contacts.Change(r.Context(), identity.User.ID, target, input.Action, input.Alias, input.Category)
|
||||
if errors.Is(err, contacts.ErrUnavailable) {
|
||||
problem(w, 409, "contact_action_unavailable", "当前无法执行此联系人操作,请刷新后重试")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
pipe := a.redis.Pipeline()
|
||||
pipe.Publish(r.Context(), "xhchat:contacts:"+identity.User.ID, "changed")
|
||||
if input.Action != "preferences" {
|
||||
pipe.Publish(r.Context(), "xhchat:contacts:"+strings.ToLower(target), "changed")
|
||||
}
|
||||
_, _ = pipe.Exec(r.Context())
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
Reference in new issue
Block a user