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.

63 lines
2.2 KiB

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