From 8fbc18bfd33e13678e1657551c49cb388df3bd75 Mon Sep 17 00:00:00 2001 From: Manoj Malik Date: Mon, 19 Jun 2023 21:04:54 +0530 Subject: [PATCH] [MI-3172] Fixed the issue of attachments being removed when post is edited in MM (#175) Added a TODO comment in the update function for the channels to handle attachments Added the logic for getting the existing attachments from a post and send them in the update API --- server/message_hooks.go | 2 ++ server/msteams/client.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/server/message_hooks.go b/server/message_hooks.go index fcd0bc953..b18022698 100644 --- a/server/message_hooks.go +++ b/server/message_hooks.go @@ -628,6 +628,8 @@ func (p *Plugin) Update(teamID, channelID string, user *model.User, newPost, old return errors.New("post not found") } + // TODO: Add the logic of processing the attachments and uploading new files to Teams + // once Mattermost comes up with the feature of editing attachments md := markdown.New(markdown.XHTMLOutput(true), markdown.LangPrefix("CodeMirror language-")) content := md.RenderToString([]byte(emoji.Parse(text))) diff --git a/server/msteams/client.go b/server/msteams/client.go index 2334e25ac..0c8d3d255 100644 --- a/server/msteams/client.go +++ b/server/msteams/client.go @@ -577,6 +577,25 @@ func (tc *ClientImpl) UpdateMessage(teamID, channelID, parentID, msgID, message contentType := models.HTML_BODYTYPE rmsg.SetMentions(mentions) + var originalMessage models.ChatMessageable + var err error + if parentID != "" { + originalMessage, err = tc.client.TeamsById(teamID).ChannelsById(channelID).MessagesById(parentID).RepliesById(msgID).Get(tc.ctx, nil) + } else { + originalMessage, err = tc.client.TeamsById(teamID).ChannelsById(channelID).MessagesById(msgID).Get(tc.ctx, nil) + } + if err != nil { + tc.logError("Error in getting original message from Teams", "error", NormalizeGraphAPIError(err)) + } + + if originalMessage != nil { + attachments := originalMessage.GetAttachments() + for _, a := range attachments { + message = fmt.Sprintf(" %s", *a.GetId(), message) + } + rmsg.SetAttachments(attachments) + } + body := models.NewItemBody() body.SetContentType(&contentType) body.SetContent(&message) @@ -597,6 +616,19 @@ func (tc *ClientImpl) UpdateMessage(teamID, channelID, parentID, msgID, message func (tc *ClientImpl) UpdateChatMessage(chatID, msgID, message string, mentions []models.ChatMessageMentionable) error { rmsg := models.NewChatMessage() + originalMessage, err := tc.client.ChatsById(chatID).MessagesById(msgID).Get(tc.ctx, nil) + if err != nil { + tc.logError("Error in getting original message from Teams", "error", NormalizeGraphAPIError(err)) + } + + if originalMessage != nil { + attachments := originalMessage.GetAttachments() + for _, a := range attachments { + message = fmt.Sprintf(" %s", *a.GetId(), message) + } + rmsg.SetAttachments(attachments) + } + contentType := models.HTML_BODYTYPE rmsg.SetMentions(mentions) @@ -605,10 +637,10 @@ func (tc *ClientImpl) UpdateChatMessage(chatID, msgID, message string, mentions body.SetContentType(&contentType) body.SetContent(&message) rmsg.SetBody(body) - if _, err := tc.client.ChatsById(chatID).MessagesById(msgID).Patch(tc.ctx, rmsg, nil); err != nil { return NormalizeGraphAPIError(err) } + return nil }