implement message updates

This commit is contained in:
2025-03-27 14:41:02 -04:00
parent b86ee0dac4
commit a7466e5c77
2 changed files with 40 additions and 1 deletions

View File

@@ -66,6 +66,7 @@ func EditMessage(w http.ResponseWriter, r *http.Request) {
}
message.Body = body
dbDeleteMessage(message.ID)
message.Edited = time.Now()
err = dbAddMessage(message)
if err != nil {
}
@@ -125,6 +126,7 @@ func NewMessage(w http.ResponseWriter, r *http.Request) {
UserID: userID,
Body: body,
Timestamp: time.Now(),
Edited: time.Time{},
}
err = dbAddMessage(&msg)
@@ -143,6 +145,7 @@ type Message struct {
UserID int64 `json:"user_id"`
Body string `json:"body"`
Timestamp time.Time `json:"timestamp"`
Edited time.Time `json:"edited"`
}
type MessageRequest struct {
@@ -167,15 +170,23 @@ func (m MessageResponse) MarshalJSON() ([]byte, error) {
UserID int64 `json:"user_id"`
Body string `json:"body"`
Timestamp string `json:"timestamp"`
Edited *string `json:"edited,omitempty"` // Use a pointer to allow null values
User *UserPayload `json:"user,omitempty"`
Elapsed int64 `json:"elapsed"`
}
var edited *string
if !m.Message.Edited.IsZero() { // Check if Edited is not the zero value
editedStr := m.Message.Edited.Format(time.RFC3339)
edited = &editedStr
}
ordered := OrderedMessageResponse{
ID: m.Message.ID,
UserID: m.Message.UserID,
Body: m.Message.Body,
Timestamp: m.Message.Timestamp.Format(time.RFC3339),
Edited: edited, // Null if Edited is zero
User: m.User,
Elapsed: m.Elapsed,
}