From 44cc1692a977320e3b52fcf9d8490704dd971389 Mon Sep 17 00:00:00 2001 From: Brett Profitt Date: Fri, 10 May 2024 20:29:57 -0400 Subject: [PATCH] Add endpoint support for read receipts. --- src/api/api.go | 53 +++++++++++++++++++++++++++++++++++++++++++- src/client/client.go | 38 +++++++++++++++++++++++++++++++ src/main.go | 8 +++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/api/api.go b/src/api/api.go index 26d8044..af02243 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -91,6 +91,12 @@ type Reaction struct { Timestamp int64 `json:"timestamp"` } +type Receipt struct { + Recipient string `json:"recipient"` + ReceiptType string `json:"receipt_type" enums:"read,viewed"` + Timestamp int64 `json:"timestamp"` +} + type SendMessageV1 struct { Number string `json:"number"` Recipients []string `json:"recipients"` @@ -1472,6 +1478,51 @@ func (a *Api) RemoveReaction(c *gin.Context) { c.Status(http.StatusNoContent) } + +// @Summary Send a receipt. +// @Tags Receipts +// @Description Send a read or viewed receipt +// @Accept json +// @Produce json +// @Success 204 {string} OK +// @Failure 400 {object} Error +// @Param data body Receipt true "Receipt" +// @Router /v1/receipts/{number} [post] +func (a *Api) SendReceipt(c *gin.Context) { + var req Receipt + err := c.BindJSON(&req) + if err != nil { + c.JSON(400, Error{Msg: "Couldn't process request - invalid request"}) + log.Error(err.Error()) + return + } + + number := c.Param("number") + + if req.Recipient == "" { + c.JSON(400, Error{Msg: "Couldn't process request - recipient missing"}) + return + } + + // if req.ReceiptType != "viewed" && req.ReceiptType != "read" { + if !utils.StringInSlice(req.ReceiptType, []string{"read", "viewed"}) { + c.JSON(400, Error{Msg: "Couldn't process request - receipt type must be read or viewed"}) + return + } + + if req.Timestamp == 0 { + c.JSON(400, Error{Msg: "Couldn't process request - timestamp missing"}) + return + } + + err = a.signalClient.SendReceipt(number, req.Recipient, req.ReceiptType, req.Timestamp) + if err != nil { + c.JSON(400, Error{Msg: err.Error()}) + return + } + c.Status(http.StatusNoContent) +} + // @Summary Show Typing Indicator. // @Tags Messages // @Description Show Typing Indicator. @@ -1883,7 +1934,7 @@ func (a *Api) ListInstalledStickerPacks(c *gin.Context) { // @Summary Add Sticker Pack. // @Tags Sticker Packs -// @Description In order to add a sticker pack, browse to https://signalstickers.org/ and select the sticker pack you want to add. Then, press the "Add to Signal" button. If you look at the address bar in your browser you should see an URL in this format: https://signal.art/addstickers/#pack_id=XXX&pack_key=YYY, where XXX is the pack_id and YYY is the pack_key. +// @Description In order to add a sticker pack, browse to https://signalstickers.org/ and select the sticker pack you want to add. Then, press the "Add to Signal" button. If you look at the address bar in your browser you should see an URL in this format: https://signal.art/addstickers/#pack_id=XXX&pack_key=YYY, where XXX is the pack_id and YYY is the pack_key. // @Accept json // @Produce json // @Param number path string true "Registered Phone Number" diff --git a/src/client/client.go b/src/client/client.go index 1421828..849d469 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -1637,6 +1637,44 @@ func (s *SignalClient) SendReaction(number string, recipient string, emoji strin return err } + +func (s *SignalClient) SendReceipt(number string, recipient string, receipt_type string, timestamp int64) error { + // see https://github.com/AsamK/signal-cli/blob/master/man/signal-cli.1.adoc#sendreceipt + var err error + recp := recipient + + if s.signalCliMode == JsonRpc { + type Request struct { + Recipient string `json:"recipient,omitempty"` + ReceiptType string `json:"receipt-type"` + Timestamp int64 `json:"target-timestamp"` + } + request := Request{} + request.Recipient = recp + request.ReceiptType = receipt_type + request.Timestamp = timestamp + + jsonRpc2Client, err := s.getJsonRpc2Client() + if err != nil { + return err + } + _, err = jsonRpc2Client.getRaw("sendReceipt", &number, request) + return err + } + + cmd := []string{ + "--config", s.signalCliConfig, + "-a", number, + "sendReceipt", + recp, + } + + cmd = append(cmd, []string{"-t", strconv.FormatInt(timestamp, 10)}...) + + _, err = s.cliClient.Execute(true, cmd, "") + return err +} + func (s *SignalClient) SendStartTyping(number string, recipient string) error { var err error recp := recipient diff --git a/src/main.go b/src/main.go index 282ee50..6e2199b 100644 --- a/src/main.go +++ b/src/main.go @@ -49,6 +49,9 @@ import ( // @tag.name Reactions // @tag.description React to messages. +// @tag.name Receipts +// @tag.description Send receipts for messages. + // @tag.name Search // @tag.description Search the Signal Service. @@ -254,6 +257,11 @@ func main() { reactions.DELETE(":number", api.RemoveReaction) } + receipts := v1.Group("/receipts") + { + receipts.POST(":number", api.SendReceipt) + } + search := v1.Group("/search") { search.GET("", api.SearchForNumbers)