mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-10-07 08:41:47 +00:00
69 lines
2 KiB
Go
69 lines
2 KiB
Go
package xmpp
|
|
|
|
import (
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"gosrc.io/xmpp"
|
|
"gosrc.io/xmpp/stanza"
|
|
|
|
"dev.narayana.im/narayana/telegabber/telegram"
|
|
"dev.narayana.im/narayana/telegabber/xmpp/extensions"
|
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
|
)
|
|
|
|
// hasReactionsPayload reports whether msg carries a XEP-0444 <reactions/>
|
|
// element - such stanzas are body-less by design, so HandleMessage's
|
|
// body-gate (there to skip typing notifications etc.) needs this alongside
|
|
// hasOMEMOPayload to let them through.
|
|
func hasReactionsPayload(msg stanza.Message) bool {
|
|
var reactions extensions.Reactions
|
|
return msg.Get(&reactions)
|
|
}
|
|
|
|
// handleIncomingReaction applies an incoming XEP-0444 <reactions/> stanza
|
|
// to Telegram, rejecting it (S3.3, type='error'/not-acceptable) if any
|
|
// requested emoji isn't allowed. bare and toID are already resolved by
|
|
// HandleMessage's caller.
|
|
func handleIncomingReaction(component *xmpp.Component, msg stanza.Message, session *telegram.Client, bare string, toID int64, isGroupchat bool, reactions extensions.Reactions) {
|
|
var msgId int64
|
|
chatId, foundMsgId, err := gateway.IdsDB.GetByXmppId(session.Session.Login, bare, reactions.Id)
|
|
if err == nil {
|
|
if chatId != toID {
|
|
log.Warnf("Reaction chat mismatch: %v != %v", chatId, toID)
|
|
return
|
|
}
|
|
msgId = foundMsgId
|
|
} else {
|
|
var ok bool
|
|
msgId, ok = parseMessageId(reactions.Id)
|
|
if !ok {
|
|
return
|
|
}
|
|
}
|
|
|
|
emojis := make([]string, 0, len(reactions.Reactions))
|
|
for _, r := range reactions.Reactions {
|
|
if r.Text != "" {
|
|
emojis = append(emojis, r.Text)
|
|
}
|
|
}
|
|
|
|
_, rejected := session.SetOwnMessageReactions(toID, msgId, emojis)
|
|
if len(rejected) == 0 {
|
|
return
|
|
}
|
|
|
|
var from string
|
|
if isGroupchat {
|
|
from = gateway.MUCJID(toID)
|
|
} else {
|
|
from = gateway.CHATNODE(toID)
|
|
}
|
|
gateway.SendMessage(msg.From, from, component,
|
|
gateway.SMErrorText("Reaction(s) not allowed: "+strings.Join(rejected, " ")),
|
|
gateway.SMErrorCode(406),
|
|
gateway.SMId(msg.Id),
|
|
gateway.SMIsGroupchat(isGroupchat),
|
|
)
|
|
}
|