github.com/safing/portbase@v0.19.5/database/record/meta.go (about)

     1  package record
     2  
     3  import "time"
     4  
     5  // Meta holds metadata about the record.
     6  type Meta struct {
     7  	Created   int64
     8  	Modified  int64
     9  	Expires   int64
    10  	Deleted   int64
    11  	secret    bool // secrets must not be sent to the UI, only synced between nodes
    12  	cronjewel bool // crownjewels must never leave the instance, but may be read by the UI
    13  }
    14  
    15  // SetAbsoluteExpiry sets an absolute expiry time (in seconds), that is not affected when the record is updated.
    16  func (m *Meta) SetAbsoluteExpiry(seconds int64) {
    17  	m.Expires = seconds
    18  	m.Deleted = 0
    19  }
    20  
    21  // SetRelativateExpiry sets a relative expiry time (ie. TTL in seconds) that is automatically updated whenever the record is updated/saved.
    22  func (m *Meta) SetRelativateExpiry(seconds int64) {
    23  	if seconds >= 0 {
    24  		m.Deleted = -seconds
    25  	}
    26  }
    27  
    28  // GetAbsoluteExpiry returns the absolute expiry time.
    29  func (m *Meta) GetAbsoluteExpiry() int64 {
    30  	return m.Expires
    31  }
    32  
    33  // GetRelativeExpiry returns the current relative expiry time - ie. seconds until expiry.
    34  // A negative value signifies that the record does not expire.
    35  func (m *Meta) GetRelativeExpiry() int64 {
    36  	if m.Expires == 0 {
    37  		return -1
    38  	}
    39  
    40  	abs := m.Expires - time.Now().Unix()
    41  	if abs < 0 {
    42  		return 0
    43  	}
    44  	return abs
    45  }
    46  
    47  // MakeCrownJewel marks the database records as a crownjewel, meaning that it will not be sent/synced to other devices.
    48  func (m *Meta) MakeCrownJewel() {
    49  	m.cronjewel = true
    50  }
    51  
    52  // MakeSecret sets the database record as secret, meaning that it may only be used internally, and not by interfacing processes, such as the UI.
    53  func (m *Meta) MakeSecret() {
    54  	m.secret = true
    55  }
    56  
    57  // Update updates the internal meta states and should be called before writing the record to the database.
    58  func (m *Meta) Update() {
    59  	now := time.Now().Unix()
    60  	m.Modified = now
    61  	if m.Created == 0 {
    62  		m.Created = now
    63  	}
    64  	if m.Deleted < 0 {
    65  		m.Expires = now - m.Deleted
    66  	}
    67  }
    68  
    69  // Reset resets all metadata, except for the secret and crownjewel status.
    70  func (m *Meta) Reset() {
    71  	m.Created = 0
    72  	m.Modified = 0
    73  	m.Expires = 0
    74  	m.Deleted = 0
    75  }
    76  
    77  // Delete marks the record as deleted.
    78  func (m *Meta) Delete() {
    79  	m.Deleted = time.Now().Unix()
    80  }
    81  
    82  // IsDeleted returns whether the record is deleted.
    83  func (m *Meta) IsDeleted() bool {
    84  	return m.Deleted > 0
    85  }
    86  
    87  // CheckValidity checks whether the database record is valid.
    88  func (m *Meta) CheckValidity() (valid bool) {
    89  	if m == nil {
    90  		return false
    91  	}
    92  
    93  	switch {
    94  	case m.Deleted > 0:
    95  		return false
    96  	case m.Expires > 0 && m.Expires < time.Now().Unix():
    97  		return false
    98  	default:
    99  		return true
   100  	}
   101  }
   102  
   103  // CheckPermission checks whether the database record may be accessed with the following scope.
   104  func (m *Meta) CheckPermission(local, internal bool) (permitted bool) {
   105  	if m == nil {
   106  		return false
   107  	}
   108  
   109  	switch {
   110  	case !local && m.cronjewel:
   111  		return false
   112  	case !internal && m.secret:
   113  		return false
   114  	default:
   115  		return true
   116  	}
   117  }
   118  
   119  // Duplicate returns a new copy of Meta.
   120  func (m *Meta) Duplicate() *Meta {
   121  	return &Meta{
   122  		Created:   m.Created,
   123  		Modified:  m.Modified,
   124  		Expires:   m.Expires,
   125  		Deleted:   m.Deleted,
   126  		secret:    m.secret,
   127  		cronjewel: m.cronjewel,
   128  	}
   129  }