Skip to content

Commit 153c26c

Browse files
committed
fix: reformat messages
1 parent ca2a21d commit 153c26c

File tree

2 files changed

+37
-59
lines changed

2 files changed

+37
-59
lines changed

models/messages.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,6 @@ type SMS struct {
4545
}
4646

4747
// Message is a helper struct for internal use.
48-
type Message struct {
49-
ID string
50-
SendingDate string
51-
Sender string
52-
Recipient string
53-
Text string
54-
ContentType string
55-
StreamID string
56-
}
5748

5849
// PushTokenReportRequest mirrors the Acrobits push token reporter POST JSON schema.
5950
type PushTokenReportRequest struct {

service/messages.go

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
139139
logger.Error().Str("user_id", string(userID)).Err(err).Msg("failed to list joined rooms")
140140
} else {
141141
logger.Debug().Str("user_id", string(userID)).Int("joined_room_count", len(rooms)).Msg("fetched joined rooms")
142-
143142
// Print each joined room (stdout) and also log them for debug
144143
for _, r := range rooms {
145-
fmt.Println(r)
146144
logger.Debug().Str("room_id", string(r)).Msg("joined room")
147145
}
148146
}
@@ -170,54 +168,65 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
170168
if evt.Type != event.EventMessage {
171169
continue
172170
}
173-
msg := convertEvent(evt)
171+
// return models.Message{
172+
// ID: string(evt.ID),
173+
// SendingDate: sendingDate,
174+
// Sender: string(evt.Sender),
175+
// Recipient: string(evt.RoomID),
176+
// Text: body,
177+
// ContentType: contentType,
178+
// StreamID: string(evt.RoomID),
179+
// }
180+
181+
// Debug entire raw event JSON for troubleshooting
182+
if b, err := evt.MarshalJSON(); err != nil {
183+
logger.Debug().Str("event", string(b)).Msg("processing event")
184+
} else {
185+
logger.Debug().Err(err).Msg("failed to marshal event for debug")
186+
}
187+
body := ""
188+
if b, ok := evt.Content.Raw["body"].(string); ok {
189+
body = b
190+
}
191+
192+
sms := models.SMS{
193+
SMSID: string(evt.ID),
194+
SendingDate: time.UnixMilli(evt.Timestamp).UTC().Format(time.RFC3339),
195+
SMSText: body,
196+
ContentType: "text/plain",
197+
// StreamID: msg.StreamID,
198+
}
174199

175200
// Determine if I sent the message
176-
senderMatrixID := msg.Sender
201+
senderMatrixID := string(evt.Sender)
177202
isSent := isSentBy(senderMatrixID, string(userID))
178203

179204
// Remap sender to identifier (e.g. "202" or "91201")
180-
msg.Sender = string(s.resolveMatrixUser(senderMatrixID))
205+
sms.Sender = string(s.resolveMatrixIDToIdentifier(senderMatrixID))
181206

182207
// Determine Recipient
183208
if isSent {
184209
// I sent it. Recipient is the other person in the room.
185210
other := s.resolveRoomIDToOtherIdentifier(ctx, evt.RoomID, string(userID))
186211
if other != "" {
187-
msg.Recipient = other
212+
sms.Recipient = other
188213
}
189-
// If other not found, msg.Recipient remains RoomID (default from convertEvent)
214+
sent = append(sent, sms)
215+
190216
} else {
191217
// I received it. Recipient is me.
192-
msg.Recipient = callerIdentifier
193-
}
218+
sms.Recipient = callerIdentifier
219+
received = append(received, sms)
194220

195-
// Convert internal Message to SMS
196-
sms := models.SMS{
197-
SMSID: msg.ID,
198-
SendingDate: msg.SendingDate,
199-
SMSText: msg.Text,
200-
ContentType: msg.ContentType,
201-
StreamID: msg.StreamID,
202221
}
203-
204222
// Debug each processed message
205223
logger.Debug().
206-
Str("event_id", msg.ID).
207-
Str("room_id", msg.StreamID).
208-
Str("sender", msg.Sender).
209-
Str("recipient", msg.Recipient).
224+
Str("sender", sms.Sender).
225+
Str("recipient", sms.Recipient).
210226
Bool("is_sent", isSent).
211227
Interface("sms", sms).
212228
Msg("processed message from sync")
213229

214-
if isSent {
215-
sms.Recipient = msg.Recipient
216-
sent = append(sent, sms)
217-
} else {
218-
sms.Sender = msg.Sender
219-
received = append(received, sms)
220-
}
221230
}
222231
}
223232

@@ -498,28 +507,6 @@ func isSentBy(sender, username string) bool {
498507
return strings.EqualFold(normalizeMatrixID(sender), normalizeMatrixID(username))
499508
}
500509

501-
func convertEvent(evt *event.Event) models.Message {
502-
body := ""
503-
if b, ok := evt.Content.Raw["body"].(string); ok {
504-
body = b
505-
}
506-
contentType := "text/plain"
507-
if mt, ok := evt.Content.Raw["msgtype"].(string); ok {
508-
contentType = mt
509-
}
510-
511-
sendingDate := time.UnixMilli(evt.Timestamp).UTC().Format(time.RFC3339)
512-
return models.Message{
513-
ID: string(evt.ID),
514-
SendingDate: sendingDate,
515-
Sender: string(evt.Sender),
516-
Recipient: string(evt.RoomID),
517-
Text: body,
518-
ContentType: contentType,
519-
StreamID: string(evt.RoomID),
520-
}
521-
}
522-
523510
func mapAuthErr(err error) error {
524511
if errors.Is(err, ErrAuthentication) {
525512
return err

0 commit comments

Comments
 (0)