Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

  1. package Database
  2. import (
  3. "github.com/gofrs/uuid"
  4. "gorm.io/gorm"
  5. "gorm.io/gorm/clause"
  6. )
  7. // MessageData holds the content of the message
  8. // encrypted through the Message.SymmetricKey
  9. type MessageData struct {
  10. Base
  11. Data string ` json:"data"` // Stored encrypted
  12. AttachmentID *uuid.UUID ` json:"attachment_id"`
  13. Attachment Attachment ` json:"attachment"`
  14. SenderID string `gorm:"not null" json:"sender_id"` // Stored encrypted
  15. SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted
  16. }
  17. func GetMessageDataById(id string) (MessageData, error) {
  18. var (
  19. messageData MessageData
  20. err error
  21. )
  22. err = DB.Preload(clause.Associations).
  23. First(&messageData, "id = ?", id).
  24. Error
  25. return messageData, err
  26. }
  27. func (messageData *MessageData) CreateMessageData() error {
  28. var (
  29. err error
  30. )
  31. err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
  32. Create(messageData).
  33. Error
  34. return err
  35. }
  36. func (messageData *MessageData) DeleteMessageData() error {
  37. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  38. Delete(messageData).
  39. Error
  40. }