2019-05-06 01:40:41 +03:00
|
|
|
package models
|
|
|
|
|
2020-05-30 23:19:05 +03:00
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
2021-02-07 14:37:51 +03:00
|
|
|
"encoding/json"
|
2020-05-30 23:19:05 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-11-01 22:14:10 +03:00
|
|
|
"gorm.io/gorm"
|
2020-08-30 02:42:00 +03:00
|
|
|
"math"
|
2020-05-30 23:19:05 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
2020-05-30 21:41:27 +03:00
|
|
|
|
2019-05-06 01:40:41 +03:00
|
|
|
const (
|
2020-05-24 14:41:19 +03:00
|
|
|
UserKey = "user"
|
2020-05-30 21:41:27 +03:00
|
|
|
ImprintKey = "imprint"
|
2020-05-24 14:41:19 +03:00
|
|
|
AuthCookieKey = "wakapi_auth"
|
2019-05-06 01:40:41 +03:00
|
|
|
)
|
2020-05-30 21:41:27 +03:00
|
|
|
|
|
|
|
type MigrationFunc func(db *gorm.DB) error
|
|
|
|
|
|
|
|
type KeyStringValue struct {
|
|
|
|
Key string `gorm:"primary_key"`
|
2020-05-30 22:10:44 +03:00
|
|
|
Value string `gorm:"type:text"`
|
2020-05-30 21:41:27 +03:00
|
|
|
}
|
2020-05-30 23:19:05 +03:00
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
type Interval struct {
|
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
}
|
|
|
|
|
2020-08-30 02:42:00 +03:00
|
|
|
type CustomTime time.Time
|
|
|
|
|
2021-02-07 14:37:51 +03:00
|
|
|
func (j *CustomTime) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(j.String())
|
|
|
|
}
|
|
|
|
|
2020-05-30 23:19:05 +03:00
|
|
|
func (j *CustomTime) UnmarshalJSON(b []byte) error {
|
2020-08-30 02:45:01 +03:00
|
|
|
s := strings.Replace(strings.Trim(string(b), "\""), ".", "", 1)
|
2020-05-30 23:19:05 +03:00
|
|
|
i, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-30 02:42:00 +03:00
|
|
|
t := time.Unix(0, i*int64(math.Pow10(19-len(s))))
|
2020-05-30 23:19:05 +03:00
|
|
|
*j = CustomTime(t)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-16 13:49:36 +03:00
|
|
|
// heartbeat timestamps arrive as strings for sqlite and as time.Time for postgres
|
2020-05-30 23:19:05 +03:00
|
|
|
func (j *CustomTime) Scan(value interface{}) error {
|
2020-10-16 13:49:36 +03:00
|
|
|
var (
|
|
|
|
t time.Time
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-05-30 23:19:05 +03:00
|
|
|
switch value.(type) {
|
|
|
|
case string:
|
2020-10-16 13:49:36 +03:00
|
|
|
t, err = time.Parse("2006-01-02 15:04:05-07:00", value.(string))
|
2020-05-30 23:19:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("unsupported date time format: %s", value))
|
|
|
|
}
|
|
|
|
case int64:
|
2020-10-16 13:49:36 +03:00
|
|
|
t = time.Unix(0, value.(int64))
|
2020-05-30 23:19:05 +03:00
|
|
|
break
|
|
|
|
case time.Time:
|
2020-10-16 13:49:36 +03:00
|
|
|
t = value.(time.Time)
|
2020-05-30 23:19:05 +03:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
return errors.New(fmt.Sprintf("unsupported type: %T", value))
|
|
|
|
}
|
2020-10-16 13:49:36 +03:00
|
|
|
|
|
|
|
t = time.Unix(0, (t.UnixNano()/int64(time.Millisecond))*int64(time.Millisecond)) // round to millisecond precision
|
|
|
|
*j = CustomTime(t)
|
|
|
|
|
2020-05-30 23:19:05 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:29:50 +03:00
|
|
|
func (j *CustomTime) Hash() (uint64, error) {
|
|
|
|
return uint64((j.T().UnixNano() / 1000) / 1000), nil
|
|
|
|
}
|
|
|
|
|
2020-05-30 23:19:05 +03:00
|
|
|
func (j CustomTime) Value() (driver.Value, error) {
|
2020-10-16 13:49:36 +03:00
|
|
|
t := time.Unix(0, j.T().UnixNano()/int64(time.Millisecond)*int64(time.Millisecond)) // round to millisecond precision
|
|
|
|
return t, nil
|
2020-05-30 23:19:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j CustomTime) String() string {
|
|
|
|
t := time.Time(j)
|
2020-08-30 02:42:00 +03:00
|
|
|
return t.Format("2006-01-02 15:04:05.000")
|
2020-05-30 23:19:05 +03:00
|
|
|
}
|
|
|
|
|
2020-10-16 13:49:36 +03:00
|
|
|
func (j CustomTime) T() time.Time {
|
2020-05-30 23:19:05 +03:00
|
|
|
return time.Time(j)
|
|
|
|
}
|
2020-11-07 14:01:35 +03:00
|
|
|
|
|
|
|
func (j CustomTime) Valid() bool {
|
|
|
|
return j.T().Unix() >= 0
|
|
|
|
}
|