Skip to content

Commit

Permalink
[MI-3172] Fixed the issue of attachments being removed when post is e…
Browse files Browse the repository at this point in the history
…dited 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
  • Loading branch information
manojmalik20 authored Jun 19, 2023
1 parent 8baee56 commit 8fbc18b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions server/message_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
34 changes: 33 additions & 1 deletion server/msteams/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<attachment id=%q></attachment> %s", *a.GetId(), message)
}
rmsg.SetAttachments(attachments)
}

body := models.NewItemBody()
body.SetContentType(&contentType)
body.SetContent(&message)
Expand All @@ -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("<attachment id=%q></attachment> %s", *a.GetId(), message)
}
rmsg.SetAttachments(attachments)
}

contentType := models.HTML_BODYTYPE

rmsg.SetMentions(mentions)
Expand All @@ -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
}

Expand Down

0 comments on commit 8fbc18b

Please sign in to comment.