@@ -441,18 +441,96 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
441441
442442 logger .Debug ().Str ("event_id" , string (evt .ID )).Str ("room_id" , string (eventRoomID )).Msg ("processing message event" )
443443
444- body := ""
444+ // Extract content fields
445+ var msgType string
446+ var body string
447+ var url string
448+ var info map [string ]interface {}
449+ var filename string
450+
451+ if t , ok := evt .Content .Raw ["msgtype" ].(string ); ok {
452+ msgType = t
453+ }
445454 if b , ok := evt .Content .Raw ["body" ].(string ); ok {
446455 body = b
447456 }
457+ if fn , ok := evt .Content .Raw ["filename" ].(string ); ok {
458+ filename = fn
459+ }
460+ if u , ok := evt .Content .Raw ["url" ].(string ); ok {
461+ url = u
462+ }
463+ if i , ok := evt .Content .Raw ["info" ].(map [string ]interface {}); ok {
464+ info = i
465+ }
466+
448467 sms := models.SMS {
449468 SMSID : string (evt .ID ),
450469 SendingDate : time .UnixMilli (evt .Timestamp ).UTC ().Format (time .RFC3339 ),
451- SMSText : body ,
452- ContentType : "text/plain" ,
453470 StreamID : string (roomID ),
454471 }
455472
473+ // Check if it's a media message
474+ if msgType == "m.image" || msgType == "m.video" || msgType == "m.audio" || msgType == "m.file" {
475+ // Convert to Acrobits file transfer format
476+ // Resolve MXC URI to HTTP URL
477+ httpURL := s .matrixClient .ResolveMXC (url )
478+ if httpURL == "" {
479+ logger .Warn ().Str ("event_id" , string (evt .ID )).Msg ("media event missing URL, falling back to text" )
480+ sms .SMSText = body
481+ sms .ContentType = "text/plain"
482+ } else {
483+ // Extract metadata
484+ mimetype := ""
485+ var size int64
486+ thumbnailURL := ""
487+ thumbnailMime := ""
488+
489+ if info != nil {
490+ if m , ok := info ["mimetype" ].(string ); ok {
491+ mimetype = m
492+ }
493+ if sVal , ok := info ["size" ].(float64 ); ok { // JSON numbers are float64
494+ size = int64 (sVal )
495+ } else if sVal , ok := info ["size" ].(int64 ); ok {
496+ size = sVal
497+ } else if sVal , ok := info ["size" ].(int ); ok {
498+ size = int64 (sVal )
499+ }
500+ if tURL , ok := info ["thumbnail_url" ].(string ); ok {
501+ thumbnailURL = s .matrixClient .ResolveMXC (tURL )
502+ }
503+ if tInfo , ok := info ["thumbnail_info" ].(map [string ]interface {}); ok {
504+ if tm , ok := tInfo ["mimetype" ].(string ); ok {
505+ thumbnailMime = tm
506+ }
507+ }
508+ }
509+
510+ // Convert to JSON
511+ // Prefer explicit filename if present; fall back to body
512+ effectiveFilename := filename
513+ if effectiveFilename == "" {
514+ effectiveFilename = body
515+ }
516+
517+ ftJSON , err := models .MatrixMediaToFileTransfer (msgType , body , httpURL , mimetype , effectiveFilename , size , thumbnailURL , thumbnailMime )
518+ if err != nil {
519+ logger .Warn ().Err (err ).Msg ("failed to convert matrix media to file transfer" )
520+ // Fallback to text
521+ sms .SMSText = body
522+ sms .ContentType = "text/plain"
523+ } else {
524+ sms .SMSText = ftJSON
525+ sms .ContentType = models .FileTransferContentType
526+ }
527+ }
528+ } else {
529+ // Regular text message
530+ sms .SMSText = body
531+ sms .ContentType = "text/plain"
532+ }
533+
456534 // Determine if I sent the message
457535 senderMatrixID := string (evt .Sender )
458536 isSent := isSentBy (senderMatrixID , string (userID ))
0 commit comments