Skip to content

Add endpoint support for individual read receipts #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down
38 changes: 38 additions & 0 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)
Expand Down
Loading