Files

117 lines
3.6 KiB
Go

package httpapi
import (
"errors"
"net/http"
"strconv"
"strings"
"xhchat/server/internal/account"
"xhchat/server/internal/delivery"
)
func (a *API) sendEnvelope(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
var input struct {
MessageID string `json:"messageId"`
RecipientDevice string `json:"recipientDevice"`
Ciphertext []byte `json:"ciphertext"`
Silent bool `json:"silent"`
}
if !parseLimit(w, r, &input, 96<<10) {
return
}
if !uuidPattern.MatchString(input.MessageID) || !uuidPattern.MatchString(input.RecipientDevice) || !mlsFrame(input.Ciphertext) {
problem(w, 400, "invalid_envelope", "请输入有效的 MLS 密文封包")
return
}
if !a.limit(w, r, "envelope-send:"+identity.Device.ID, 180) {
return
}
recipient := strings.ToLower(input.RecipientDevice)
seq, notify, err := a.delivery.Send(r.Context(), identity.Device.ID, recipient, input.MessageID, input.Ciphertext,
input.Ciphertext[3] == 2 && !input.Silent)
switch {
case errors.Is(err, delivery.ErrUnavailable):
problem(w, 404, "delivery_unavailable", "无法向指定设备投递")
case errors.Is(err, delivery.ErrConflict):
problem(w, 409, "message_id_conflict", "消息 ID 已关联其他密文")
case err != nil:
unavailable(w, err)
default:
hint := "changed"
if notify {
hint = "private-notify:" + identity.User.ID
}
_ = a.redis.Publish(r.Context(), "xhchat:inbox:"+recipient, hint).Err()
respond(w, 200, map[string]string{"sequence": seq})
}
}
func mlsFrame(wire []byte) bool {
// RFC 9420 MLSMessage header. The delivery service routes PrivateMessage and
// Welcome. Recipients validate the complete message and authenticate its contents.
return len(wire) >= 16 && len(wire) <= 65536 && wire[0] == 0 && wire[1] == 1 &&
wire[2] == 0 && (wire[3] == 2 || wire[3] == 3)
}
func (a *API) inbox(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
var after int64
if raw := r.URL.Query().Get("after"); raw != "" {
var err error
after, err = strconv.ParseInt(raw, 10, 64)
if err != nil || after < 0 {
problem(w, 400, "invalid_cursor", "无效的投递游标")
return
}
}
if !a.limit(w, r, "inbox:"+identity.Device.ID, 180) {
return
}
items, err := a.delivery.Inbox(r.Context(), identity.Device.ID, after)
if err != nil {
unavailable(w, err)
return
}
respond(w, 200, items)
}
func (a *API) privateUnread(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
if !a.limit(w, r, "private-unread:"+identity.Device.ID, 180) {
return
}
counts, err := a.delivery.Unread(r.Context(), identity.Device.ID)
if err != nil {
unavailable(w, err)
return
}
respond(w, 200, counts)
}
func (a *API) markPrivateSeen(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
var input struct {
SenderDevice string `json:"senderDevice"`
Sequence string `json:"sequence"`
}
if !parse(w, r, &input) {
return
}
sequence, err := strconv.ParseInt(input.Sequence, 10, 64)
if !uuidPattern.MatchString(input.SenderDevice) || err != nil || sequence <= 0 || strconv.FormatInt(sequence, 10) != input.Sequence {
problem(w, 400, "invalid_cursor", "无效的设备或已查看游标")
return
}
if !a.limit(w, r, "private-seen:"+identity.Device.ID, 180) {
return
}
err = a.delivery.MarkSeen(r.Context(), identity.Device.ID, strings.ToLower(input.SenderDevice), sequence)
if errors.Is(err, delivery.ErrUnavailable) {
problem(w, 404, "delivery_unavailable", "找不到当前设备可见的投递记录")
return
}
if err != nil {
unavailable(w, err)
return
}
w.WriteHeader(204)
}