From 1e8ef05338deedc02d35547cf1dbfbc953d38a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Wed, 25 Sep 2024 12:07:24 +0200 Subject: [PATCH] Fix missing profile messages on notification dashboards (#28297) --- server/channels/app/notification.go | 42 ++++++++++++++++++++++------- server/public/model/channel.go | 30 +++++++++++++-------- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/server/channels/app/notification.go b/server/channels/app/notification.go index fe6bf5eeae..23cc0b05e5 100644 --- a/server/channels/app/notification.go +++ b/server/channels/app/notification.go @@ -1027,15 +1027,29 @@ func (a *App) getExplicitMentionsAndKeywords(c request.CTX, post *model.Post, ch var keywords MentionKeywords if channel.Type == model.ChannelTypeDirect { - otherUserId := channel.GetOtherUserIdForDM(post.UserId) + isWebhook := post.GetProp("from_webhook") == "true" - _, ok := profileMap[otherUserId] - if ok { - mentions.addMention(otherUserId, DMMention) + // A bot can post in a DM where it doesn't belong to. + // Therefore, we cannot "guess" who is the other user, + // so we add the mention to any user that is not the + // poster unless the post comes from a webhook. + user1, user2 := channel.GetBothUsersForDM() + if (post.UserId != user1) || isWebhook { + if _, ok := profileMap[user1]; ok { + mentions.addMention(user1, DMMention) + } else { + a.Log().Debug("missing profile: DM user not in profiles", mlog.String("userId", user1), mlog.String("channelId", channel.Id)) + } } - if post.GetProp("from_webhook") == "true" { - mentions.addMention(post.UserId, DMMention) + if user2 != "" { + if (post.UserId != user2) || isWebhook { + if _, ok := profileMap[user2]; ok { + mentions.addMention(user2, DMMention) + } else { + a.Log().Debug("missing profile: DM user not in profiles", mlog.String("userId", user2), mlog.String("channelId", channel.Id)) + } + } } } else { allowChannelMentions = a.allowChannelMentions(c, post, len(profileMap)) @@ -1046,16 +1060,23 @@ func (a *App) getExplicitMentionsAndKeywords(c request.CTX, post *model.Post, ch // Add a GM mention to all members of a GM channel if channel.Type == model.ChannelTypeGroup { for id := range channelMemberNotifyPropsMap { - mentions.addMention(id, GMMention) + if _, ok := profileMap[id]; ok { + mentions.addMention(id, GMMention) + } else { + a.Log().Debug("missing profile: GM user not in profiles", mlog.String("userId", id), mlog.String("channelId", channel.Id)) + } } } // Add an implicit mention when a user is added to a channel // even if the user has set 'username mentions' to false in account settings. if post.Type == model.PostTypeAddToChannel { - addedUserId, ok := post.GetProp(model.PostPropsAddedUserId).(string) - if ok { - mentions.addMention(addedUserId, KeywordMention) + if addedUserId, ok := post.GetProp(model.PostPropsAddedUserId).(string); ok { + if _, ok := profileMap[addedUserId]; ok { + mentions.addMention(addedUserId, KeywordMention) + } else { + a.Log().Debug("missing profile: user added to channel not in profiles", mlog.String("userId", addedUserId), mlog.String("channelId", channel.Id)) + } } } @@ -1064,6 +1085,7 @@ func (a *App) getExplicitMentionsAndKeywords(c request.CTX, post *model.Post, ch for _, threadPost := range parentPostList.Posts { profile := profileMap[threadPost.UserId] if profile == nil { + // Not logging missing profile since this is relatively expected continue } diff --git a/server/public/model/channel.go b/server/public/model/channel.go index 193baa182c..d70f01197f 100644 --- a/server/public/model/channel.go +++ b/server/public/model/channel.go @@ -334,26 +334,34 @@ func (o *Channel) IsShared() bool { } func (o *Channel) GetOtherUserIdForDM(userId string) string { - if o.Type != ChannelTypeDirect { + user1, user2 := o.GetBothUsersForDM() + + if user2 == "" { return "" } + if user1 == userId { + return user2 + } + + return user1 +} + +func (o *Channel) GetBothUsersForDM() (string, string) { + if o.Type != ChannelTypeDirect { + return "", "" + } + userIds := strings.Split(o.Name, "__") if len(userIds) != 2 { - return "" + return "", "" } - var otherUserId string - - if userIds[0] != userIds[1] { - if userIds[0] == userId { - otherUserId = userIds[1] - } else { - otherUserId = userIds[0] - } + if userIds[0] == userIds[1] { + return userIds[0], "" } - return otherUserId + return userIds[0], userIds[1] } func (o *Channel) Sanitize() Channel {