Skip to content

Commit

Permalink
msgconv/from-slack: merge captions and handle caption edits properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 11, 2024
1 parent b5a4398 commit 3c78512
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/yuin/goldmark v1.7.4
go.mau.fi/util v0.5.1-0.20240708204011-043c35cda49c
gopkg.in/yaml.v3 v3.0.1
maunium.net/go/mautrix v0.19.0-beta.1.0.20240710204602-dd16a8d1d90b
maunium.net/go/mautrix v0.19.0-beta.1.0.20240711083823-7f18d6b7358e
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240710204602-dd16a8d1d90b h1:xYEIkM0OzirvHPpTwYiDQrh6PHjDny5Ox84SIkD2aXc=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240710204602-dd16a8d1d90b/go.mod h1:bNQrvIftiwJ+7OjSh+Gza5xcncq1ooHk6oyDWq4B4sg=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240711083823-7f18d6b7358e h1:DdWudfvcrUGs8BFre8ER00pfN1ROSWUi4z+JchNY+3s=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240711083823-7f18d6b7358e/go.mod h1:bNQrvIftiwJ+7OjSh+Gza5xcncq1ooHk6oyDWq4B4sg=
34 changes: 22 additions & 12 deletions pkg/msgconv/from-slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (mc *MessageConverter) ToMatrix(
partID := slackid.MakePartID(slackid.PartTypeFile, i, file.ID)
output.Parts = append(output.Parts, mc.slackFileToMatrix(ctx, portal, intent, client, partID, &file))
}
output.MergeCaption()
return output
}

Expand All @@ -80,29 +81,38 @@ func (mc *MessageConverter) EditToMatrix(
) *bridgev2.ConvertedEdit {
ctx = context.WithValue(ctx, contextKeyPortal, portal)
ctx = context.WithValue(ctx, contextKeySource, source)
client := source.Client.(SlackClientProvider).GetClient()
existingMap := make(map[networkid.PartID]*database.Message, len(existing))
for _, part := range existing {
existingMap[part.PartID] = part
}
editTargetPart := existing[0]
output := &bridgev2.ConvertedEdit{}
textPart := mc.makeTextPart(ctx, msg, portal, intent)
if textPart != nil {
output.ModifiedParts = append(output.ModifiedParts, &bridgev2.ConvertedEditPart{
Part: existingMap[""],
Type: textPart.Type,
Content: textPart.Content,
Extra: textPart.Extra,
})
}
modifiedPart := mc.makeTextPart(ctx, msg, portal, intent)
captionMerged := false
for i, file := range msg.Files {
partID := slackid.MakePartID(slackid.PartTypeFile, i, file.ID)
existingPart, ok := existingMap[partID]
if file.Mode == "tombstone" {
partID := slackid.MakePartID(slackid.PartTypeFile, i, file.ID)
deletedPart, ok := existingMap[partID]
if ok {
output.DeletedParts = append(output.DeletedParts, deletedPart)
output.DeletedParts = append(output.DeletedParts, existingPart)
}
} else {
// For edits where there's either only one media part, or there was no text part,
// we'll need to fetch the first media part to merge it in
if !captionMerged && modifiedPart != nil && (len(msg.Files) == 1 || editTargetPart.PartID != "") {
if editTargetPart.PartID != "" {
editTargetPart = existingPart
}
filePart := mc.slackFileToMatrix(ctx, portal, intent, client, partID, &file)
modifiedPart = bridgev2.MergeCaption(modifiedPart, filePart)
captionMerged = true
}
}
}
if modifiedPart != nil {
output.ModifiedParts = append(output.ModifiedParts, modifiedPart.ToEditPart(editTargetPart))
}
return output
}

Expand Down

0 comments on commit 3c78512

Please sign in to comment.