Skip to content

Commit 2989dcd

Browse files
committed
Add sender attribute to messages
1 parent d4318ef commit 2989dcd

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

internal/conversations/service.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,29 @@ func (s *service) messageToView(ctx context.Context, message models.Message) (*M
443443
return nil, err
444444
}
445445

446+
if message.SenderPerspective == nil {
447+
perspective := models.MessageSenderPerspectiveVolunteer
448+
message.SenderPerspective = &perspective
449+
}
450+
446451
view := &MessageView{
447-
ID: message.ID,
448-
ConversationID: message.ConversationID,
449-
SenderID: message.SenderID,
450-
Timestamp: message.Timestamp,
451-
Type: message.Type,
452-
Edited: message.Edited,
453-
EditedTimestamp: message.EditedTimestamp,
454-
Body: body,
452+
ID: message.ID,
453+
ConversationID: message.ConversationID,
454+
SenderID: message.SenderID,
455+
SenderPerspective: *message.SenderPerspective,
456+
Timestamp: message.Timestamp,
457+
Type: message.Type,
458+
Edited: message.Edited,
459+
EditedTimestamp: message.EditedTimestamp,
460+
Body: body,
461+
}
462+
463+
if message.Sender != nil {
464+
view.Sender = &MessageSenderView{
465+
FirstName: message.Sender.FirstName,
466+
LastName: message.Sender.LastName,
467+
ProfilePicture: message.Sender.ProfilePicture,
468+
}
455469
}
456470

457471
return view, nil

internal/conversations/view.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ type ConversationView struct {
1616

1717
// MessageView represents a view of a message.
1818
type MessageView struct {
19-
ID int64 `json:"id"`
20-
ConversationID int64 `json:"conversationId"`
21-
SenderID int64 `json:"senderId"`
22-
SenderPerspective uint `json:"senderPerspective"`
23-
Timestamp time.Time `json:"timestamp"`
24-
Type string `json:"type"`
25-
Edited bool `json:"edited"`
26-
EditedTimestamp time.Time `json:"editedTimestamp"`
27-
Body interface{} `json:"body"`
19+
ID int64 `json:"id"`
20+
ConversationID int64 `json:"conversationId"`
21+
SenderID int64 `json:"senderId"`
22+
SenderPerspective uint `json:"senderPerspective"`
23+
Timestamp time.Time `json:"timestamp"`
24+
Type string `json:"type"`
25+
Edited bool `json:"edited"`
26+
EditedTimestamp time.Time `json:"editedTimestamp"`
27+
Body interface{} `json:"body"`
28+
Sender *MessageSenderView `json:"sender"`
29+
}
30+
31+
// MessageSenderView represents the sender of a message.
32+
type MessageSenderView struct {
33+
FirstName string `json:"firstName"`
34+
LastName string `json:"lastName"`
35+
ProfilePicture string `json:"profilePicture"`
2836
}
2937

3038
// MessageVolunteerRequestProfileView represents a view of a message containing a user's profile.

internal/database/postgres/conversation_repository.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (r *conversationRepository) FindByID(id int64) (*models.Conversation, error
2525
var conversation models.Conversation
2626
if err := r.db.Preload("LastMessage", func(db *gorm.DB) *gorm.DB {
2727
return db.Order("timestamp desc")
28-
}).Preload("Organization").First(&conversation, id).Error; err != nil {
28+
}).Preload("LastMessage.Sender").Preload("Organization").First(&conversation, id).Error; err != nil {
2929
return &conversation, err
3030
}
3131
return &conversation, nil
@@ -41,7 +41,7 @@ func (r *conversationRepository) FindByIDs(ctx context.Context, ids []int64) (*m
4141
Model(&models.Conversation{}).
4242
Preload("LastMessage", func(db *gorm.DB) *gorm.DB {
4343
return db.Order("timestamp desc")
44-
}).
44+
}).Preload("LastMessage.Sender").
4545
Preload("Organization").
4646
Limit(dbctx.Limit).
4747
Joins("LEFT JOIN (select distinct on (timestamp) * from messages order by timestamp desc limit 1) as message ON message.conversation_id = conversations.id").
@@ -67,7 +67,7 @@ func (r *conversationRepository) FindByOrganizationID(ctx context.Context, organ
6767
Model(&models.Conversation{}).
6868
Preload("LastMessage", func(db *gorm.DB) *gorm.DB {
6969
return db.Order("timestamp desc")
70-
}).
70+
}).Preload("LastMessage.Sender").
7171
Limit(dbctx.Limit).
7272
Joins("LEFT JOIN (select distinct on (timestamp) * from messages order by timestamp desc limit 1) as message ON message.conversation_id = conversations.id").
7373
Where("conversations.organization_id = ? AND active = True", organizationID).

internal/database/postgres/message_repository.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (r *messageRepository) FindByConversationID(ctx context.Context, conversati
3636
dbctx := dbctx.Get(ctx)
3737

3838
db := r.db.
39+
Preload("Sender").
3940
Model(&models.Message{}).
4041
Limit(dbctx.Limit).
4142
Where("conversation_id = ?", conversationID).Order("timestamp DESC", true).
@@ -63,6 +64,7 @@ func (r *messageRepository) FindBySenderID(ctx context.Context, senderID int64)
6364
dbctx := dbctx.Get(ctx)
6465

6566
db := r.db.
67+
Preload("Sender").
6668
Model(&models.Message{}).
6769
Limit(dbctx.Limit).
6870
Where("sender_id = ?", senderID).Order("timestamp DESC", true).
@@ -89,6 +91,7 @@ func (r *messageRepository) FindInConversationBySenderID(ctx context.Context, co
8991
dbctx := dbctx.Get(ctx)
9092

9193
db := r.db.
94+
Preload("Sender").
9295
Model(&models.Message{}).
9396
Limit(dbctx.Limit).
9497
Where("conversation_id = ? AND sender_id = ?", conversationID, senderID).Order("timestamp DESC", true).

internal/models/message.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Message struct {
2929
Model
3030
Timestamp time.Time `json:"timestamp"`
3131
ConversationID int64 `json:"conversationId"`
32+
Sender *User `json:"-" gorm:"foreignkey:SenderID"`
3233
SenderID int64 `json:"senderId"`
3334
SenderPerspective *uint `json:"senderPerspective"`
3435
Type string `json:"type"`

0 commit comments

Comments
 (0)