2020-11-09 14:19:03 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2021-09-16 21:31:02 +02:00
|
|
|
"encoding/json"
|
2022-03-26 01:21:56 +02:00
|
|
|
"reflect"
|
2021-07-08 16:36:43 +02:00
|
|
|
"time"
|
2021-10-05 15:52:59 +02:00
|
|
|
|
2022-06-15 12:17:44 +02:00
|
|
|
mmModel "github.com/mattermost/mattermost-server/v6/model"
|
2020-11-09 14:19:03 +02:00
|
|
|
)
|
|
|
|
|
2021-10-05 15:52:59 +02:00
|
|
|
type IDType byte
|
|
|
|
|
|
|
|
const (
|
2023-02-03 12:54:14 +02:00
|
|
|
IDTypeNone IDType = '7'
|
|
|
|
IDTypeTeam IDType = 't'
|
|
|
|
IDTypeBoard IDType = 'b'
|
|
|
|
IDTypeCard IDType = 'c'
|
|
|
|
IDTypeView IDType = 'v'
|
|
|
|
IDTypeSession IDType = 's'
|
|
|
|
IDTypeUser IDType = 'u'
|
|
|
|
IDTypeToken IDType = 'k'
|
|
|
|
IDTypeBlock IDType = 'a'
|
|
|
|
IDTypeAttachment IDType = 'i'
|
2021-10-05 15:52:59 +02:00
|
|
|
)
|
2020-11-09 14:19:03 +02:00
|
|
|
|
2021-10-05 15:52:59 +02:00
|
|
|
// NewId is a globally unique identifier. It is a [A-Z0-9] string 27
|
|
|
|
// characters long. It is a UUID version 4 Guid that is zbased32 encoded
|
|
|
|
// with the padding stripped off, and a one character alpha prefix indicating the
|
|
|
|
// type of entity or a `7` if unknown type.
|
|
|
|
func NewID(idType IDType) string {
|
2022-06-15 12:17:44 +02:00
|
|
|
return string(idType) + mmModel.NewId()
|
2020-11-09 14:19:03 +02:00
|
|
|
}
|
2021-07-08 16:36:43 +02:00
|
|
|
|
|
|
|
// GetMillis is a convenience method to get milliseconds since epoch.
|
|
|
|
func GetMillis() int64 {
|
2022-06-15 12:17:44 +02:00
|
|
|
return mmModel.GetMillis()
|
2021-10-05 15:52:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMillisForTime is a convenience method to get milliseconds since epoch for provided Time.
|
|
|
|
func GetMillisForTime(thisTime time.Time) int64 {
|
2022-06-15 12:17:44 +02:00
|
|
|
return mmModel.GetMillisForTime(thisTime)
|
2021-10-05 15:52:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTimeForMillis is a convenience method to get time.Time for milliseconds since epoch.
|
|
|
|
func GetTimeForMillis(millis int64) time.Time {
|
2022-06-15 12:17:44 +02:00
|
|
|
return mmModel.GetTimeForMillis(millis)
|
2021-10-05 15:52:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SecondsToMillis is a convenience method to convert seconds to milliseconds.
|
|
|
|
func SecondsToMillis(seconds int64) int64 {
|
|
|
|
return seconds * 1000
|
2021-07-08 16:36:43 +02:00
|
|
|
}
|
2021-09-16 21:31:02 +02:00
|
|
|
|
|
|
|
func StructToMap(v interface{}) (m map[string]interface{}) {
|
|
|
|
b, _ := json.Marshal(v)
|
|
|
|
_ = json.Unmarshal(b, &m)
|
|
|
|
return
|
|
|
|
}
|
2022-03-26 01:21:56 +02:00
|
|
|
|
|
|
|
func intersection(a []interface{}, b []interface{}) []interface{} {
|
|
|
|
set := make([]interface{}, 0)
|
|
|
|
hash := make(map[interface{}]bool)
|
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
bv := reflect.ValueOf(b)
|
|
|
|
|
|
|
|
for i := 0; i < av.Len(); i++ {
|
|
|
|
el := av.Index(i).Interface()
|
|
|
|
hash[el] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < bv.Len(); i++ {
|
|
|
|
el := bv.Index(i).Interface()
|
|
|
|
if _, found := hash[el]; found {
|
|
|
|
set = append(set, el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
func Intersection(x ...[]interface{}) []interface{} {
|
|
|
|
if len(x) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(x) == 1 {
|
|
|
|
return x[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
result := x[0]
|
|
|
|
i := 1
|
|
|
|
for i < len(x) {
|
|
|
|
result = intersection(result, x[i])
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2022-06-15 12:17:44 +02:00
|
|
|
|
|
|
|
func IsCloudLicense(license *mmModel.License) bool {
|
|
|
|
return license != nil &&
|
|
|
|
license.Features != nil &&
|
|
|
|
license.Features.Cloud != nil &&
|
|
|
|
*license.Features.Cloud
|
|
|
|
}
|
2023-01-24 12:11:54 +02:00
|
|
|
|
|
|
|
func DedupeStringArr(arr []string) []string {
|
|
|
|
hashMap := map[string]bool{}
|
|
|
|
|
|
|
|
for _, item := range arr {
|
|
|
|
hashMap[item] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
dedupedArr := make([]string, len(hashMap))
|
|
|
|
i := 0
|
|
|
|
for key := range hashMap {
|
|
|
|
dedupedArr[i] = key
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return dedupedArr
|
|
|
|
}
|