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.

105 lines
3.1 KiB

  1. package Models
  2. import (
  3. "database/sql/driver"
  4. "errors"
  5. "github.com/gofrs/uuid"
  6. "gorm.io/gorm"
  7. )
  8. // BeforeUpdate prevents updating the email if it has not changed
  9. // This stops a unique constraint error
  10. func (u *User) BeforeUpdate(tx *gorm.DB) (err error) {
  11. if !tx.Statement.Changed("Username") {
  12. tx.Statement.Omit("Username")
  13. }
  14. return nil
  15. }
  16. // MessageExpiry holds values for how long messages should expire by default
  17. type MessageExpiry []uint8
  18. const (
  19. // MessageExpiryFifteenMin expires after 15 minutes
  20. MessageExpiryFifteenMin = "fifteen_min"
  21. // MessageExpiryThirtyMin expires after 30 minutes
  22. MessageExpiryThirtyMin = "thirty_min"
  23. // MessageExpiryOneHour expires after one hour
  24. MessageExpiryOneHour = "one_hour"
  25. // MessageExpiryThreeHour expires after three hours
  26. MessageExpiryThreeHour = "three_hour"
  27. // MessageExpirySixHour expires after six hours
  28. MessageExpirySixHour = "six_hour"
  29. // MessageExpiryTwelveHour expires after twelve hours
  30. MessageExpiryTwelveHour = "twelve_hour"
  31. // MessageExpiryOneDay expires after one day
  32. MessageExpiryOneDay = "one_day"
  33. // MessageExpiryThreeDay expires after three days
  34. MessageExpiryThreeDay = "three_day"
  35. // MessageExpiryNoExpiry never expires
  36. MessageExpiryNoExpiry = "no_expiry"
  37. )
  38. // MessageExpiryValues list of all expiry values for validation
  39. var MessageExpiryValues = []string{
  40. MessageExpiryFifteenMin,
  41. MessageExpiryThirtyMin,
  42. MessageExpiryOneHour,
  43. MessageExpiryThreeHour,
  44. MessageExpirySixHour,
  45. MessageExpiryTwelveHour,
  46. MessageExpiryOneDay,
  47. MessageExpiryThreeDay,
  48. MessageExpiryNoExpiry,
  49. }
  50. // Scan new value into MessageExpiry
  51. func (e *MessageExpiry) Scan(value interface{}) error {
  52. var (
  53. strValue = value.(string)
  54. m string
  55. )
  56. for _, m = range MessageExpiryValues {
  57. if strValue != m {
  58. continue
  59. }
  60. *e = MessageExpiry(strValue)
  61. return nil
  62. }
  63. return errors.New("Invalid MessageExpiry value")
  64. }
  65. // Value gets value out of MessageExpiry column
  66. func (e MessageExpiry) Value() (driver.Value, error) {
  67. return string(e), nil
  68. }
  69. func (e MessageExpiry) String() string {
  70. return string(e)
  71. }
  72. // User holds user data
  73. type User struct {
  74. Base
  75. Username string `gorm:"not null;unique" json:"username"`
  76. Password string `gorm:"not null" json:"password"`
  77. ConfirmPassword string `gorm:"-" json:"confirm_password"`
  78. AsymmetricPrivateKey string `gorm:"not null" json:"asymmetric_private_key"` // Stored encrypted
  79. AsymmetricPublicKey string `gorm:"not null" json:"asymmetric_public_key"`
  80. SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted
  81. AttachmentID *uuid.UUID ` json:"attachment_id"`
  82. Attachment Attachment ` json:"attachment"`
  83. MessageExpiryDefault MessageExpiry `gorm:"default:no_expiry" json:"-" sql:"type:ENUM(
  84. 'fifteen_min',
  85. 'thirty_min',
  86. 'one_hour',
  87. 'three_hour',
  88. 'six_hour',
  89. 'twelve_hour',
  90. 'one_day',
  91. 'three_day'
  92. )"` // Stored encrypted
  93. }