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() {
for msg := range portal.messages {
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")
continue
}
@ -217,53 +217,33 @@ func (portal *Portal) handleMessageLoop() {
}
}
func (portal *Portal) shouldCreateRoom(msg PortalMessage) bool {
if msg.undecryptable != nil {
return true
func containsSupportedMessage(waMsg *waProto.Message) bool {
if waMsg == nil {
return false
}
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 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
// (so a warning should be logged if they are encountered).
potentiallyInterestingThings := []interface{}{
waMsg.Call, waMsg.Chat, waMsg.ContactsArrayMessage, waMsg.HighlyStructuredMessage,
waMsg.SendPaymentMessage, waMsg.LiveLocationMessage, waMsg.RequestPaymentMessage,
waMsg.DeclinePaymentRequestMessage, waMsg.CancelPaymentRequestMessage, waMsg.TemplateMessage,
waMsg.TemplateButtonReplyMessage, waMsg.ProductMessage, waMsg.ListMessage, waMsg.OrderMessage,
waMsg.ListResponseMessage, waMsg.InvoiceMessage, waMsg.ButtonsResponseMessage,
waMsg.ButtonsResponseMessage, waMsg.PaymentInviteMessage, waMsg.InteractiveMessage,
waMsg.ReactionMessage, waMsg.StickerSyncRmrMessage,
}
for _, thing := range potentiallyInterestingThings {
if thing != nil {
return true
}
}
return false
return waMsg.Call != nil || waMsg.Chat != nil || waMsg.ContactsArrayMessage != nil ||
waMsg.HighlyStructuredMessage != nil || waMsg.SendPaymentMessage != nil || waMsg.LiveLocationMessage != nil ||
waMsg.RequestPaymentMessage != nil || waMsg.DeclinePaymentRequestMessage != nil ||
waMsg.CancelPaymentRequestMessage != nil || waMsg.TemplateMessage != nil ||
waMsg.TemplateButtonReplyMessage != nil || waMsg.ProductMessage != nil || waMsg.ListMessage != nil ||
waMsg.OrderMessage != nil || waMsg.ListResponseMessage != nil || waMsg.InvoiceMessage != nil ||
waMsg.ButtonsMessage != nil || waMsg.ButtonsResponseMessage != nil || waMsg.PaymentInviteMessage != nil ||
waMsg.InteractiveMessage != nil || waMsg.ReactionMessage != nil || waMsg.StickerSyncRmrMessage != nil
}
func (portal *Portal) getMessageType(waMsg *waProto.Message) string {
func getMessageType(waMsg *waProto.Message) string {
switch {
case waMsg == nil:
return "ignore"
@ -294,7 +274,7 @@ func (portal *Portal) getMessageType(waMsg *waProto.Message) string {
default:
return "unknown_protocol"
}
case portal.isPotentiallyInteresting(waMsg):
case isPotentiallyInteresting(waMsg):
return "unknown"
default:
return "ignore"
@ -362,7 +342,7 @@ func (portal *Portal) handleMessage(source *User, evt *events.Message) {
return
}
msgID := evt.Info.ID
msgType := portal.getMessageType(evt.Message)
msgType := getMessageType(evt.Message)
if msgType == "ignore" {
return
} 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-- {
wrappedMsg := messages[i]
webMsg := wrappedMsg.GetMessage()
msgType := portal.getMessageType(webMsg.GetMessage())
if msgType == "unknown" || msgType == "ignore" {
if msgType == "unknown" {
msgType := getMessageType(webMsg.GetMessage())
if msgType == "unknown" || msgType == "ignore" || msgType == "unknown_protocol" {
if msgType != "ignore" {
portal.log.Debugfln("Skipping message %s with unknown type in backfill", webMsg.GetKey().GetId())
}
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) {
if evt.GetSyncType() != waProto.HistorySync_RECENT && evt.GetSyncType() != waProto.HistorySync_FULL {
return
@ -376,7 +385,10 @@ func (user *User) handleHistorySync(evt *waProto.HistorySync) {
lastMsg := time.Unix(int64(conv.GetConversationTimestamp()), 0)
portal := user.GetPortalByJID(jid)
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)
continue
}