Fix checking if message contains interesting things

This commit is contained in:
Tulir Asokan 2021-11-01 15:30:56 +02:00
parent ce8a29b46f
commit 4ac445a868
2 changed files with 39 additions and 47 deletions

View file

@ -196,7 +196,7 @@ type Portal struct {
func (portal *Portal) handleMessageLoop() { func (portal *Portal) handleMessageLoop() {
for msg := range portal.messages { for msg := range portal.messages {
if len(portal.MXID) == 0 { if len(portal.MXID) == 0 {
if !portal.shouldCreateRoom(msg) { if msg.evt == nil || !containsSupportedMessage(msg.evt.Message) {
portal.log.Debugln("Not creating portal room for incoming message: message is not a chat message") portal.log.Debugln("Not creating portal room for incoming message: message is not a chat message")
continue continue
} }
@ -217,53 +217,33 @@ func (portal *Portal) handleMessageLoop() {
} }
} }
func (portal *Portal) shouldCreateRoom(msg PortalMessage) bool { func containsSupportedMessage(waMsg *waProto.Message) bool {
if msg.undecryptable != nil { if waMsg == nil {
return true
}
waMsg := msg.evt.Message
supportedMessages := []interface{}{
waMsg.Conversation,
waMsg.ExtendedTextMessage,
waMsg.ImageMessage,
waMsg.StickerMessage,
waMsg.VideoMessage,
waMsg.AudioMessage,
waMsg.VideoMessage,
waMsg.DocumentMessage,
waMsg.ContactMessage,
waMsg.LocationMessage,
waMsg.GroupInviteMessage,
}
for _, message := range supportedMessages {
if message != nil {
return true
}
}
return false return false
} }
return waMsg.Conversation != nil || waMsg.ExtendedTextMessage != nil || waMsg.ImageMessage != nil ||
waMsg.StickerMessage != nil || waMsg.AudioMessage != nil || waMsg.VideoMessage != nil ||
waMsg.DocumentMessage != nil || waMsg.ContactMessage != nil || waMsg.LocationMessage != nil ||
waMsg.GroupInviteMessage != nil
}
func (portal *Portal) isPotentiallyInteresting(waMsg *waProto.Message) bool { func isPotentiallyInteresting(waMsg *waProto.Message) bool {
if waMsg == nil {
return false
}
// List of message types that aren't supported, but might potentially be interesting // List of message types that aren't supported, but might potentially be interesting
// (so a warning should be logged if they are encountered). // (so a warning should be logged if they are encountered).
potentiallyInterestingThings := []interface{}{ return waMsg.Call != nil || waMsg.Chat != nil || waMsg.ContactsArrayMessage != nil ||
waMsg.Call, waMsg.Chat, waMsg.ContactsArrayMessage, waMsg.HighlyStructuredMessage, waMsg.HighlyStructuredMessage != nil || waMsg.SendPaymentMessage != nil || waMsg.LiveLocationMessage != nil ||
waMsg.SendPaymentMessage, waMsg.LiveLocationMessage, waMsg.RequestPaymentMessage, waMsg.RequestPaymentMessage != nil || waMsg.DeclinePaymentRequestMessage != nil ||
waMsg.DeclinePaymentRequestMessage, waMsg.CancelPaymentRequestMessage, waMsg.TemplateMessage, waMsg.CancelPaymentRequestMessage != nil || waMsg.TemplateMessage != nil ||
waMsg.TemplateButtonReplyMessage, waMsg.ProductMessage, waMsg.ListMessage, waMsg.OrderMessage, waMsg.TemplateButtonReplyMessage != nil || waMsg.ProductMessage != nil || waMsg.ListMessage != nil ||
waMsg.ListResponseMessage, waMsg.InvoiceMessage, waMsg.ButtonsResponseMessage, waMsg.OrderMessage != nil || waMsg.ListResponseMessage != nil || waMsg.InvoiceMessage != nil ||
waMsg.ButtonsResponseMessage, waMsg.PaymentInviteMessage, waMsg.InteractiveMessage, waMsg.ButtonsMessage != nil || waMsg.ButtonsResponseMessage != nil || waMsg.PaymentInviteMessage != nil ||
waMsg.ReactionMessage, waMsg.StickerSyncRmrMessage, waMsg.InteractiveMessage != nil || waMsg.ReactionMessage != nil || waMsg.StickerSyncRmrMessage != nil
}
for _, thing := range potentiallyInterestingThings {
if thing != nil {
return true
}
}
return false
} }
func (portal *Portal) getMessageType(waMsg *waProto.Message) string { func getMessageType(waMsg *waProto.Message) string {
switch { switch {
case waMsg == nil: case waMsg == nil:
return "ignore" return "ignore"
@ -294,7 +274,7 @@ func (portal *Portal) getMessageType(waMsg *waProto.Message) string {
default: default:
return "unknown_protocol" return "unknown_protocol"
} }
case portal.isPotentiallyInteresting(waMsg): case isPotentiallyInteresting(waMsg):
return "unknown" return "unknown"
default: default:
return "ignore" return "ignore"
@ -362,7 +342,7 @@ func (portal *Portal) handleMessage(source *User, evt *events.Message) {
return return
} }
msgID := evt.Info.ID msgID := evt.Info.ID
msgType := portal.getMessageType(evt.Message) msgType := getMessageType(evt.Message)
if msgType == "ignore" { if msgType == "ignore" {
return return
} else if portal.isRecentlyHandled(msgID, false) { } else if portal.isRecentlyHandled(msgID, false) {
@ -1072,9 +1052,9 @@ func (portal *Portal) backfill(source *User, messages []*waProto.HistorySyncMsg)
for i := len(messages) - 1; i >= 0; i-- { for i := len(messages) - 1; i >= 0; i-- {
wrappedMsg := messages[i] wrappedMsg := messages[i]
webMsg := wrappedMsg.GetMessage() webMsg := wrappedMsg.GetMessage()
msgType := portal.getMessageType(webMsg.GetMessage()) msgType := getMessageType(webMsg.GetMessage())
if msgType == "unknown" || msgType == "ignore" { if msgType == "unknown" || msgType == "ignore" || msgType == "unknown_protocol" {
if msgType == "unknown" { if msgType != "ignore" {
portal.log.Debugfln("Skipping message %s with unknown type in backfill", webMsg.GetKey().GetId()) portal.log.Debugfln("Skipping message %s with unknown type in backfill", webMsg.GetKey().GetId())
} }
continue continue

14
user.go
View file

@ -347,6 +347,15 @@ func (user *User) sendMarkdownBridgeAlert(formatString string, args ...interface
} }
} }
func containsSupportedMessages(conv *waProto.Conversation) bool {
for _, msg := range conv.GetMessages() {
if containsSupportedMessage(msg.GetMessage().GetMessage()) {
return true
}
}
return false
}
func (user *User) handleHistorySync(evt *waProto.HistorySync) { func (user *User) handleHistorySync(evt *waProto.HistorySync) {
if evt.GetSyncType() != waProto.HistorySync_RECENT && evt.GetSyncType() != waProto.HistorySync_FULL { if evt.GetSyncType() != waProto.HistorySync_RECENT && evt.GetSyncType() != waProto.HistorySync_FULL {
return return
@ -376,7 +385,10 @@ func (user *User) handleHistorySync(evt *waProto.HistorySync) {
lastMsg := time.Unix(int64(conv.GetConversationTimestamp()), 0) lastMsg := time.Unix(int64(conv.GetConversationTimestamp()), 0)
portal := user.GetPortalByJID(jid) portal := user.GetPortalByJID(jid)
if createRooms && len(portal.MXID) == 0 { if createRooms && len(portal.MXID) == 0 {
if maxAge > 0 && !lastMsg.After(minLastMsgToCreate) { if !containsSupportedMessages(conv) {
user.log.Debugfln("Not creating portal for %s: no interesting messages found", portal.Key.JID)
continue
} else if maxAge > 0 && !lastMsg.After(minLastMsgToCreate) {
user.log.Debugfln("Not creating portal for %s: last message older than limit (%s)", portal.Key.JID, lastMsg) user.log.Debugfln("Not creating portal for %s: last message older than limit (%s)", portal.Key.JID, lastMsg)
continue continue
} }