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.

112 lines
2.7 KiB

2 years ago
2 years ago
  1. package Database
  2. import (
  3. "time"
  4. "github.com/gofrs/uuid"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/clause"
  7. )
  8. // UserConversation Used to link the current user to their conversations
  9. type UserConversation struct {
  10. Base
  11. UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;" json:"user_id"`
  12. User User ` json:"user"`
  13. ConversationDetailID string `gorm:"not null" json:"conversation_detail_id"` // Stored encrypted
  14. Admin string `gorm:"not null" json:"admin"` // Bool if user is admin of thread, stored encrypted
  15. SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted
  16. CreatedAt time.Time `gorm:"not null" json:"created_at"`
  17. }
  18. type UserConversationList []UserConversation
  19. func GetUserConversationById(id string) (UserConversation, error) {
  20. var (
  21. message UserConversation
  22. err error
  23. )
  24. err = DB.First(&message, "id = ?", id).
  25. Error
  26. return message, err
  27. }
  28. func GetUserConversationsByUserId(id string, page int) ([]UserConversation, error) {
  29. var (
  30. conversations []UserConversation
  31. offset int
  32. err error
  33. )
  34. offset = page * PageSize
  35. err = DB.Offset(offset).
  36. Limit(PageSize).
  37. Find(&conversations, "user_id = ?", id).
  38. Order("created_at DESC").
  39. Error
  40. return conversations, err
  41. }
  42. func (userConversation *UserConversation) CreateUserConversation() error {
  43. var err error
  44. err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
  45. Create(userConversation).
  46. Error
  47. return err
  48. }
  49. func (userConversations *UserConversationList) CreateUserConversations() error {
  50. var err error
  51. err = DB.Create(userConversations).
  52. Error
  53. return err
  54. }
  55. func (userConversation *UserConversation) UpdateUserConversation() error {
  56. var err error
  57. err = DB.Model(UserConversation{}).
  58. Updates(userConversation).
  59. Error
  60. return err
  61. }
  62. func (userConversations *UserConversationList) UpdateUserConversations() error {
  63. var err error
  64. err = DB.Model(UserConversation{}).
  65. Updates(userConversations).
  66. Error
  67. return err
  68. }
  69. func (userConversations *UserConversationList) UpdateOrCreateUserConversations() error {
  70. var err error
  71. err = DB.Model(UserConversation{}).
  72. Clauses(clause.OnConflict{
  73. Columns: []clause.Column{{Name: "id"}},
  74. DoUpdates: clause.AssignmentColumns([]string{"admin"}),
  75. }).
  76. Create(userConversations).
  77. Error
  78. return err
  79. }
  80. func (userConversation *UserConversation) DeleteUserConversation() error {
  81. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  82. Delete(userConversation).
  83. Error
  84. }