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.

75 lines
2.5 KiB

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