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.

120 lines
3.7 KiB

  1. package Database
  2. import (
  3. "database/sql"
  4. "time"
  5. "github.com/gofrs/uuid"
  6. "gorm.io/gorm"
  7. "gorm.io/gorm/clause"
  8. )
  9. // FriendRequest Set with Friend being the requestee, and UserID being the requester
  10. type FriendRequest struct {
  11. Base
  12. UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;" json:"user_id"`
  13. User User ` json:"user"`
  14. FriendID string `gorm:"not null" json:"friend_id"` // Stored encrypted
  15. FriendUsername string ` json:"friend_username"` // Stored encrypted
  16. FriendImagePath string ` json:"friend_image_path"`
  17. FriendPublicAsymmetricKey string ` json:"asymmetric_public_key"` // Stored encrypted
  18. FriendRequestDeviceTokens []FriendRequestDeviceToken ` json:"tokens"`
  19. SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted
  20. AcceptedAt sql.NullTime ` json:"accepted_at"`
  21. CreatedAt time.Time `gorm:"not null" json:"created_at"`
  22. }
  23. type FriendRequestList []FriendRequest
  24. // GetFriendRequestByID gets friend request
  25. func GetFriendRequestByID(id string) (FriendRequest, error) {
  26. var (
  27. friendRequest FriendRequest
  28. err error
  29. )
  30. err = DB.Preload(clause.Associations).
  31. First(&friendRequest, "id = ?", id).
  32. Error
  33. return friendRequest, err
  34. }
  35. // GetFriendRequestsByUserID gets friend request by user id
  36. func GetFriendRequestsByUserID(userID string, page int, acceptedAt time.Time) ([]FriendRequest, error) {
  37. var (
  38. query *gorm.DB
  39. friends []FriendRequest
  40. offset int
  41. err error
  42. )
  43. offset = page * PageSize
  44. query = DB.Model(FriendRequest{}).
  45. Preload("FriendRequestDeviceTokens.DeviceToken").
  46. Where("user_id = ?", userID)
  47. if !acceptedAt.IsZero() {
  48. query = query.Where("accepted_at > ?", acceptedAt).
  49. Or("created_at > ?", acceptedAt)
  50. } else {
  51. query = query.
  52. Offset(offset).
  53. Limit(PageSize)
  54. }
  55. err = query.
  56. Order("created_at DESC").
  57. Find(&friends).
  58. Error
  59. return friends, err
  60. }
  61. // CreateFriendRequest creates friend request
  62. func (friendRequest *FriendRequest) CreateFriendRequest() error {
  63. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  64. Create(friendRequest).
  65. Error
  66. }
  67. // CreateFriendRequests creates multiple friend requests
  68. func (friendRequest *FriendRequestList) CreateFriendRequests() error {
  69. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  70. Create(friendRequest).
  71. Error
  72. }
  73. // UpdateFriendRequest Updates friend request
  74. func (friendRequest *FriendRequest) UpdateFriendRequest() error {
  75. return DB.Where("id = ?", friendRequest.ID).
  76. Updates(friendRequest).
  77. Error
  78. }
  79. // DeleteFriendRequest deletes friend request
  80. func (friendRequest *FriendRequest) DeleteFriendRequest() error {
  81. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  82. Delete(friendRequest).
  83. Error
  84. }
  85. func (friendRequest *FriendRequest) AttachDeviceToken(deviceToken DeviceToken, requester bool) error {
  86. var (
  87. requestToken FriendRequestDeviceToken
  88. err error
  89. )
  90. requestToken = FriendRequestDeviceToken{
  91. DeviceToken: deviceToken,
  92. FriendRequestID: friendRequest.ID,
  93. }
  94. err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
  95. Create(&requestToken).
  96. Error
  97. return err
  98. }