Skip to content

Commit 5a8f39a

Browse files
committed
Preview works, download not
1 parent c49709c commit 5a8f39a

File tree

3 files changed

+68
-45
lines changed

3 files changed

+68
-45
lines changed

main_test.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,8 @@ func TestIntegration_SendAndFetchImageMessages(t *testing.T) {
614614
// Find the image or text message from our test
615615
for i := range fetchResp.ReceivedSMSs {
616616
msg := &fetchResp.ReceivedSMSs[i]
617-
if (msg.ContentType == "application/x-acro-filetransfer+json" || msg.SMSText == "Test text message before image" || msg.SMSText == "Test image message") && len(msg.Attachments) > 0 {
618-
t.Logf("Attempt %d: Found image message with %d attachments", attempts+1, len(msg.Attachments))
617+
if msg.ContentType == "application/x-acro-filetransfer+json" {
618+
t.Logf("Attempt %d: Found image message with content type %s", attempts+1, msg.ContentType)
619619
imageMsg = msg
620620
break
621621
}
@@ -639,23 +639,29 @@ func TestIntegration_SendAndFetchImageMessages(t *testing.T) {
639639
}
640640

641641
// Verify attachment structure
642-
if len(imageMsg.Attachments) == 0 {
643-
t.Fatalf("image message has no attachments")
642+
var ft models.FileTransfer
643+
err = json.Unmarshal([]byte(imageMsg.SMSText), &ft)
644+
if err != nil {
645+
t.Fatalf("failed to unmarshal SMSText as FileTransfer: %v. SMSText: %s", err, imageMsg.SMSText)
646+
}
647+
648+
if len(ft.Attachments) == 0 {
649+
t.Fatalf("image message has no attachments in FileTransfer")
644650
}
645651

646-
attachment := imageMsg.Attachments[0]
647-
t.Logf("Attachment type: %s, url: %s, size: %d", attachment.Type, attachment.URL, attachment.Size)
652+
attachment := ft.Attachments[0]
653+
t.Logf("Attachment content-type: %s, url: %s, size: %d", attachment.ContentType, attachment.ContentURL, attachment.ContentSize)
648654

649-
if attachment.Type == "" {
650-
t.Errorf("attachment type is empty")
655+
if attachment.ContentType == "" {
656+
t.Errorf("attachment content-type is empty")
651657
}
652658

653-
if attachment.URL == "" {
654-
t.Errorf("attachment url is empty")
659+
if attachment.ContentURL == "" {
660+
t.Errorf("attachment content-url is empty")
655661
}
656662

657-
if attachment.Size == 0 {
658-
t.Errorf("attachment size is 0")
663+
if attachment.ContentSize == 0 {
664+
t.Errorf("attachment content-size is 0")
659665
}
660666

661667
// Verify preview exists
@@ -666,13 +672,13 @@ func TestIntegration_SendAndFetchImageMessages(t *testing.T) {
666672
// Step 3: Download the image and verify it matches
667673
t.Run("VerifyImagePreview", func(t *testing.T) {
668674
// Verify that the attachment has a preview with the correct structure
669-
if imageMsg.Attachments[0].Preview == nil {
675+
if ft.Attachments[0].Preview == nil {
670676
t.Fatalf("attachment has no preview")
671677
}
672678

673-
preview := imageMsg.Attachments[0].Preview
674-
if preview.Type != "image/jpeg" {
675-
t.Errorf("preview type is %s, expected image/jpeg", preview.Type)
679+
preview := ft.Attachments[0].Preview
680+
if preview.ContentType != "image/jpeg" {
681+
t.Errorf("preview content-type is %s, expected image/jpeg", preview.ContentType)
676682
}
677683

678684
if preview.Content == "" {
@@ -690,11 +696,11 @@ func TestIntegration_SendAndFetchImageMessages(t *testing.T) {
690696
}
691697

692698
// Verify the attachment structure
693-
t.Logf("Attachment structure verified: Type=%s, URL=%s, Size=%d, HasPreview=%v",
694-
imageMsg.Attachments[0].Type,
695-
imageMsg.Attachments[0].URL,
696-
imageMsg.Attachments[0].Size,
697-
imageMsg.Attachments[0].Preview != nil)
699+
t.Logf("Attachment structure verified: ContentType=%s, ContentURL=%s, ContentSize=%d, HasPreview=%v",
700+
ft.Attachments[0].ContentType,
701+
ft.Attachments[0].ContentURL,
702+
ft.Attachments[0].ContentSize,
703+
ft.Attachments[0].Preview != nil)
698704
})
699705
})
700706
}

models/messages.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,39 @@ type FetchMessagesResponse struct {
3333

3434
// SMS represents a message in the Acrobits Modern API format.
3535
type SMS struct {
36-
SMSID string `json:"sms_id"`
37-
SendingDate string `json:"sending_date"`
38-
Sender string `json:"sender,omitempty"`
39-
Recipient string `json:"recipient,omitempty"`
40-
SMSText string `json:"sms_text"`
41-
ContentType string `json:"content_type,omitempty"`
42-
DispositionNotification string `json:"disposition_notification,omitempty"`
43-
Displayed bool `json:"displayed,omitempty"`
44-
StreamID string `json:"stream_id"`
45-
Attachments []Attachment `json:"attachments,omitempty"`
36+
SMSID string `json:"sms_id"`
37+
SendingDate string `json:"sending_date"`
38+
Sender string `json:"sender,omitempty"`
39+
Recipient string `json:"recipient,omitempty"`
40+
SMSText string `json:"sms_text"`
41+
ContentType string `json:"content_type,omitempty"`
42+
DispositionNotification string `json:"disposition_notification,omitempty"`
43+
Displayed bool `json:"displayed,omitempty"`
44+
StreamID string `json:"stream_id"`
45+
}
46+
47+
// FileTransfer represents the application/x-acro-filetransfer+json format.
48+
type FileTransfer struct {
49+
Body string `json:"body,omitempty"`
50+
Attachments []Attachment `json:"attachments"`
4651
}
4752

4853
// Attachment represents a file attachment in the Acrobits x-acro-filetransfer format.
4954
type Attachment struct {
50-
Type string `json:"type,omitempty"`
51-
URL string `json:"url"`
52-
Size int `json:"size,omitempty"`
55+
ContentType string `json:"content-type,omitempty"`
56+
ContentURL string `json:"content-url"`
57+
ContentSize int `json:"content-size,omitempty"`
5358
Filename string `json:"filename,omitempty"`
5459
Description string `json:"description,omitempty"`
55-
EncryptionKey string `json:"encryption_key,omitempty"`
60+
EncryptionKey string `json:"encryption-key,omitempty"`
5661
Hash string `json:"hash,omitempty"`
5762
Preview *AttachmentPreview `json:"preview,omitempty"`
5863
}
5964

6065
// AttachmentPreview represents a low-quality preview of an attachment.
6166
type AttachmentPreview struct {
62-
Type string `json:"type,omitempty"`
63-
Content string `json:"content"` // BASE64 encoded
67+
ContentType string `json:"content-type,omitempty"`
68+
Content string `json:"content"` // BASE64 encoded
6469
}
6570

6671
// Message is a helper struct for internal use.

service/messages.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"strconv"
@@ -328,21 +329,32 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
328329
publicURL := s.buildMediaURL(mxcURL)
329330

330331
attachment := models.Attachment{
331-
Type: contentType,
332-
URL: publicURL,
333-
Size: contentSize,
334-
Filename: body, // Use body as filename if available
332+
ContentType: contentType,
333+
ContentURL: publicURL,
334+
ContentSize: contentSize,
335+
Filename: body, // Use body as filename if available
335336
}
336337

337338
if preview != "" {
338339
attachment.Preview = &models.AttachmentPreview{
339-
Type: "image/jpeg",
340-
Content: preview,
340+
ContentType: "image/jpeg",
341+
Content: preview,
341342
}
342343
}
343344

344-
sms.Attachments = []models.Attachment{attachment}
345-
sms.ContentType = "application/x-acro-filetransfer+json"
345+
// Create the FileTransfer object and marshal it to JSON for SMSText
346+
ft := models.FileTransfer{
347+
Body: body,
348+
Attachments: []models.Attachment{attachment},
349+
}
350+
351+
ftJSON, err := json.Marshal(ft)
352+
if err != nil {
353+
logger.Error().Err(err).Msg("failed to marshal file transfer JSON")
354+
} else {
355+
sms.SMSText = string(ftJSON)
356+
sms.ContentType = "application/x-acro-filetransfer+json"
357+
}
346358

347359
logger.Debug().
348360
Str("event_id", string(evt.ID)).

0 commit comments

Comments
 (0)