Request and send to Telegram XEP-0333 displayed markers by "receipts" option

This commit is contained in:
Bohdan Horbeshko 2024-01-27 06:13:45 -05:00
parent 81fc3ea370
commit 599cf16cdb
6 changed files with 62 additions and 16 deletions

View file

@ -42,6 +42,7 @@ type Session struct {
OOBMode bool `yaml:":oobmode"` OOBMode bool `yaml:":oobmode"`
Carbons bool `yaml:":carbons"` Carbons bool `yaml:":carbons"`
HideIds bool `yaml:":hideids"` HideIds bool `yaml:":hideids"`
Receipts bool `yaml:":receipts"`
} }
var configKeys = []string{ var configKeys = []string{
@ -52,6 +53,7 @@ var configKeys = []string{
"oobmode", "oobmode",
"carbons", "carbons",
"hideids", "hideids",
"receipts",
} }
var sessionDB *SessionsYamlDB var sessionDB *SessionsYamlDB
@ -130,6 +132,8 @@ func (s *Session) Get(key string) (string, error) {
return fromBool(s.Carbons), nil return fromBool(s.Carbons), nil
case "hideids": case "hideids":
return fromBool(s.HideIds), nil return fromBool(s.HideIds), nil
case "receipts":
return fromBool(s.Receipts), nil
} }
return "", errors.New("Unknown session property") return "", errors.New("Unknown session property")
@ -194,6 +198,13 @@ func (s *Session) Set(key string, value string) (string, error) {
} }
s.HideIds = b s.HideIds = b
return value, nil return value, nil
case "receipts":
b, err := toBool(value)
if err != nil {
return "", err
}
s.Receipts = b
return value, nil
} }
return "", errors.New("Unknown session property") return "", errors.New("Unknown session property")

View file

@ -202,6 +202,7 @@ func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
c.xmpp, c.xmpp,
reply, reply,
false, false,
false,
) )
} }
} }

View file

@ -298,7 +298,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
markupFunction, markupFunction,
)) ))
for _, jid := range jids { for _, jid := range jids {
gateway.SendMessage(jid, strconv.FormatInt(update.ChatId, 10), text, "e"+strconv.FormatInt(update.MessageId, 10), c.xmpp, nil, false) gateway.SendMessage(jid, strconv.FormatInt(update.ChatId, 10), text, "e"+strconv.FormatInt(update.MessageId, 10), c.xmpp, nil, false, false)
} }
} }
} }

View file

@ -1074,24 +1074,31 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
} }
// mark message as read // mark message as read
c.client.ViewMessages(&client.ViewMessagesRequest{ if !c.Session.Receipts {
ChatId: chatId, c.MarkAsRead(chatId, message.Id)
MessageIds: []int64{message.Id}, }
ForceRead: true,
})
// forward message to XMPP // forward message to XMPP
sId := strconv.FormatInt(message.Id, 10) sId := strconv.FormatInt(message.Id, 10)
sChatId := strconv.FormatInt(chatId, 10) sChatId := strconv.FormatInt(chatId, 10)
for _, jid := range jids { for _, jid := range jids {
gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, isCarbon) gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, isCarbon, c.Session.Receipts)
if auxText != "" { if auxText != "" {
gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, isCarbon) gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, isCarbon, c.Session.Receipts)
} }
} }
} }
// MarkAsRead marks a message as read
func (c *Client) MarkAsRead(chatId, messageId int64) {
c.client.ViewMessages(&client.ViewMessagesRequest{
ChatId: chatId,
MessageIds: []int64{messageId},
ForceRead: true,
})
}
// PrepareMessageContent creates a simple text message // PrepareMessageContent creates a simple text message
func (c *Client) PrepareOutgoingMessageContent(text string) client.InputMessageContent { func (c *Client) PrepareOutgoingMessageContent(text string) client.InputMessageContent {
return c.prepareOutgoingMessageContent(text, nil) return c.prepareOutgoingMessageContent(text, nil)

View file

@ -54,23 +54,23 @@ var DirtySessions = false
var MessageOutgoingPermissionVersion = 0 var MessageOutgoingPermissionVersion = 0
// SendMessage creates and sends a message stanza // SendMessage creates and sends a message stanza
func SendMessage(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, isCarbon bool) { func SendMessage(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, isCarbon, requestReceipt bool) {
sendMessageWrapper(to, from, body, id, component, reply, nil, "", isCarbon) sendMessageWrapper(to, from, body, id, component, reply, nil, "", isCarbon, requestReceipt)
} }
// SendServiceMessage creates and sends a simple message stanza from transport // SendServiceMessage creates and sends a simple message stanza from transport
func SendServiceMessage(to string, body string, component *xmpp.Component) { func SendServiceMessage(to string, body string, component *xmpp.Component) {
sendMessageWrapper(to, "", body, "", component, nil, nil, "", false) sendMessageWrapper(to, "", body, "", component, nil, nil, "", false, false)
} }
// SendTextMessage creates and sends a simple message stanza // SendTextMessage creates and sends a simple message stanza
func SendTextMessage(to string, from string, body string, component *xmpp.Component) { func SendTextMessage(to string, from string, body string, component *xmpp.Component) {
sendMessageWrapper(to, from, body, "", component, nil, nil, "", false) sendMessageWrapper(to, from, body, "", component, nil, nil, "", false, false)
} }
// SendMessageWithOOB creates and sends a message stanza with OOB URL // SendMessageWithOOB creates and sends a message stanza with OOB URL
func SendMessageWithOOB(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, oob string, isCarbon bool) { func SendMessageWithOOB(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, oob string, isCarbon, requestReceipt bool) {
sendMessageWrapper(to, from, body, id, component, reply, nil, oob, isCarbon) sendMessageWrapper(to, from, body, id, component, reply, nil, oob, isCarbon, requestReceipt)
} }
// SendMessageMarker creates and sends a message stanza with a XEP-0333 marker // SendMessageMarker creates and sends a message stanza with a XEP-0333 marker
@ -78,10 +78,10 @@ func SendMessageMarker(to string, from string, component *xmpp.Component, marker
sendMessageWrapper(to, from, "", "", component, nil, &marker{ sendMessageWrapper(to, from, "", "", component, nil, &marker{
Type: markerType, Type: markerType,
Id: markerId, Id: markerId,
}, "", false) }, "", false, false)
} }
func sendMessageWrapper(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, marker *marker, oob string, isCarbon bool) { func sendMessageWrapper(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, marker *marker, oob string, isCarbon, requestReceipt bool) {
toJid, err := stanza.NewJid(to) toJid, err := stanza.NewJid(to)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -150,6 +150,9 @@ func sendMessageWrapper(to string, from string, body string, id string, componen
if !isCarbon && toJid.Resource != "" { if !isCarbon && toJid.Resource != "" {
message.Extensions = append(message.Extensions, stanza.HintNoCopy{}) message.Extensions = append(message.Extensions, stanza.HintNoCopy{})
} }
if requestReceipt {
message.Extensions = append(message.Extensions, stanza.ReceiptRequest{})
}
if isCarbon { if isCarbon {
carbonMessage := extensions.ClientMessage{ carbonMessage := extensions.ClientMessage{

View file

@ -254,6 +254,30 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
gateway.MessageOutgoingPermissionVersion = 2 gateway.MessageOutgoingPermissionVersion = 2
} }
} }
var displayed stanza.MarkDisplayed
msg.Get(&displayed)
if displayed.ID != "" {
log.Debugf("displayed: %#v", displayed)
bare, _, ok := gateway.SplitJID(msg.From)
if !ok {
return
}
session, ok := sessions[bare]
if !ok {
return
}
toID, ok := toToID(msg.To)
if !ok {
return
}
msgId, err := strconv.ParseInt(displayed.ID, 10, 64)
if err == nil {
session.MarkAsRead(toID, msgId)
}
return
}
} }
if msg.Type == "error" { if msg.Type == "error" {