Refactoring: merge handleGetDiscoInfo/handleGetDiscoItems back into one function

This commit is contained in:
Bohdan Horbeshko 2022-07-08 07:54:30 -04:00
parent 6abb7ff9c2
commit 63f12202d0

View file

@ -24,6 +24,12 @@ const (
) )
const NodeVCard4 string = "urn:xmpp:vcard4" const NodeVCard4 string = "urn:xmpp:vcard4"
type discoType int
const (
discoTypeInfo discoType = iota
discoTypeItems
)
func logPacketType(p stanza.Packet) { func logPacketType(p stanza.Packet) {
log.Warnf("Ignoring packet: %T\n", p) log.Warnf("Ignoring packet: %T\n", p)
} }
@ -52,12 +58,12 @@ func HandleIq(s xmpp.Sender, p stanza.Packet) {
} }
_, ok = iq.Payload.(*stanza.DiscoInfo) _, ok = iq.Payload.(*stanza.DiscoInfo)
if ok { if ok {
go handleGetDiscoInfo(s, iq) go handleGetDisco(discoTypeInfo, s, iq)
return return
} }
_, ok = iq.Payload.(*stanza.DiscoItems) _, ok = iq.Payload.(*stanza.DiscoItems)
if ok { if ok {
go handleGetDiscoItems(s, iq) go handleGetDisco(discoTypeItems, s, iq)
return return
} }
} }
@ -318,7 +324,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
_ = gateway.ResumableSend(component, &answer) _ = gateway.ResumableSend(component, &answer)
} }
func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ) { func handleGetDisco(dt discoType, s xmpp.Sender, iq *stanza.IQ) {
answer, err := stanza.NewIQ(stanza.Attrs{ answer, err := stanza.NewIQ(stanza.Attrs{
Type: stanza.IQTypeResult, Type: stanza.IQTypeResult,
From: iq.To, From: iq.To,
@ -331,67 +337,45 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ) {
return return
} }
disco := answer.DiscoInfo() if dt == discoTypeInfo {
_, ok := toToID(iq.To) disco := answer.DiscoInfo()
if ok { _, ok := toToID(iq.To)
disco.AddIdentity("", "account", "registered") if ok {
} else { disco.AddIdentity("", "account", "registered")
disco.AddIdentity("Telegram Gateway", "gateway", "telegram") } else {
} disco.AddIdentity("Telegram Gateway", "gateway", "telegram")
bare, _, ok := splitFrom(iq.From)
if ok {
session, ok := sessions[bare]
if ok && session.Session.MUC {
disco.AddFeatures(stanza.NSDiscoItems)
disco.AddIdentity("Telegram group chats", "conference", "text")
} }
}
answer.Payload = disco
log.Debugf("%#v", answer)
component, ok := s.(*xmpp.Component)
if !ok {
log.Error("Not a component")
return
}
_ = gateway.ResumableSend(component, answer)
}
func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ) {
answer, err := stanza.NewIQ(stanza.Attrs{
Type: stanza.IQTypeResult,
From: iq.To,
To: iq.From,
Id: iq.Id,
Lang: "en",
})
if err != nil {
log.Errorf("Failed to create answer IQ: %v", err)
return
}
disco := answer.DiscoItems()
_, ok := toToID(iq.To)
if !ok {
bare, _, ok := splitFrom(iq.From) bare, _, ok := splitFrom(iq.From)
if ok { if ok {
// raw access, no need to create a new instance if not connected
session, ok := sessions[bare] session, ok := sessions[bare]
if ok && session.Session.MUC { if ok && session.Session.MUC {
bareJid := gateway.Jid.Bare() disco.AddFeatures(stanza.NSDiscoItems)
disco.AddItem(bareJid, "", "Telegram group chats") disco.AddIdentity("Telegram group chats", "conference", "text")
for _, chat := range session.GetGroupChats() { }
jid := strconv.FormatInt(chat.Id, 10) + "@" + bareJid }
disco.AddItem(jid, "", chat.Title) answer.Payload = disco
} else if dt == discoTypeItems {
disco := answer.DiscoItems()
_, ok := toToID(iq.To)
if !ok {
bare, _, ok := splitFrom(iq.From)
if ok {
// raw access, no need to create a new instance if not connected
session, ok := sessions[bare]
if ok && session.Session.MUC {
bareJid := gateway.Jid.Bare()
disco.AddItem(bareJid, "", "Telegram group chats")
for _, chat := range session.GetGroupChats() {
jid := strconv.FormatInt(chat.Id, 10) + "@" + bareJid
disco.AddItem(jid, "", chat.Title)
}
} }
} }
} }
}
answer.Payload = disco answer.Payload = disco
}
log.Debugf("%#v", answer) log.Debugf("%#v", answer)