Skip to content

Commit

Permalink
fix(GODT-3036): Keep contentID order for attachment in FakeServer.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlejeune74 committed Oct 23, 2023
1 parent 2b3fddd commit f4421b7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 40 additions & 1 deletion server/backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"errors"
"fmt"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -727,7 +728,10 @@ func (b *Backend) SendMessage(userID, messageID string, packages []*proton.Messa
newMsg.unread = true
messages[newMsg.messageID] = newMsg

for _, attID := range msg.attIDs {
// collect attachment with contentID
attIDs := sortAttachment(atts, msg.attIDs)

for _, attID := range attIDs {
attKey, err := base64.StdEncoding.DecodeString(recipient.AttachmentKeyPackets[attID])
if err != nil {
return err
Expand All @@ -737,6 +741,7 @@ func (b *Backend) SendMessage(userID, messageID string, packages []*proton.Messa
atts[attID].filename,
atts[attID].mimeType,
atts[attID].disposition,
atts[attID].contentID,
attKey,
atts[attID].attDataID,
atts[attID].armSig,
Expand Down Expand Up @@ -767,6 +772,39 @@ func (b *Backend) SendMessage(userID, messageID string, packages []*proton.Messa
})
}

func sortAttachment(atts map[string]*attachment, attIDs []string) []string {
// collect attachment with contentID
attContentId := make(map[string]string, len(attIDs))
for _, attID := range attIDs {
if atts[attID].contentID != "" {
attContentId[atts[attID].contentID] = attID
}
}

// sort contentID
contentIDs := make([]string, 0, len(attContentId))
for k := range attContentId {
contentIDs = append(contentIDs, k)
}
sort.Strings(contentIDs)

// set contentID attachment first
attachments := make([]string, 0)
for _, id := range contentIDs {
attachments = append(attachments, attContentId[id])
}

// follow with attachment without contentID
for _, attID := range attIDs {
if atts[attID].contentID != "" {
continue
}
attachments = append(attachments, attContentId[attID])
}

return attachments
}

func (b *Backend) CreateAttachment(
userID string,
messageID string,
Expand All @@ -792,6 +830,7 @@ func (b *Backend) CreateAttachment(
filename,
mimeType,
disposition,
contentID,
keyPackets,
b.createAttData(dataPacket),
armSig,
Expand Down
3 changes: 3 additions & 0 deletions server/backend/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type attachment struct {
filename string
mimeType rfc822.MIMEType
disposition proton.Disposition
contentID string

keyPackets []byte
armSig string
Expand All @@ -35,6 +36,7 @@ func newAttachment(
filename string,
mimeType rfc822.MIMEType,
disposition proton.Disposition,
contentID string,
keyPackets []byte,
dataPacketID string,
armSig string,
Expand All @@ -46,6 +48,7 @@ func newAttachment(
filename: filename,
mimeType: mimeType,
disposition: disposition,
contentID: contentID,

keyPackets: keyPackets,
armSig: armSig,
Expand Down

0 comments on commit f4421b7

Please sign in to comment.