Files
open-im-server/pkg/utils/utils.go
T

255 lines
5.5 KiB
Go
Raw Normal View History

2021-12-07 15:44:12 +08:00
package utils
import (
2023-02-15 15:52:32 +08:00
"hash/crc32"
2022-03-17 13:49:21 +08:00
"math/rand"
2022-01-04 12:56:10 +08:00
"runtime"
2023-05-06 10:52:29 +08:00
"sort"
2022-01-04 12:56:10 +08:00
"strconv"
2022-01-15 18:36:40 +08:00
"strings"
2022-03-17 13:49:21 +08:00
"time"
2023-04-28 18:33:33 +08:00
2023-05-06 10:52:29 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2023-04-28 18:33:33 +08:00
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
2021-12-07 15:44:12 +08:00
)
// copy a by b b->a
func CopyStructFields(a interface{}, b interface{}, fields ...string) (err error) {
2021-12-28 17:49:52 +08:00
return copier.Copy(a, b)
2021-12-07 15:44:12 +08:00
}
2022-01-04 12:56:10 +08:00
2023-02-23 18:17:17 +08:00
func Wrap1(err error) error {
2023-04-28 18:33:33 +08:00
return errors.Wrap(err, "==> "+printCallerNameAndLine())
2023-02-23 18:17:17 +08:00
}
func Wrap2[T any](a T, err error) (T, error) {
if err != nil {
2023-04-28 18:33:33 +08:00
return a, errors.Wrap(err, "==> "+printCallerNameAndLine())
2023-02-23 18:17:17 +08:00
}
return a, nil
}
func Wrap3[T any, V any](a T, b V, err error) (T, V, error) {
if err != nil {
2023-04-28 18:33:33 +08:00
return a, b, errors.Wrap(err, "==> "+printCallerNameAndLine())
2023-02-23 18:17:17 +08:00
}
return a, b, nil
}
2022-01-04 12:56:10 +08:00
func Wrap(err error, message string) error {
return errors.Wrap(err, "==> "+printCallerNameAndLine()+message)
}
func WithMessage(err error, message string) error {
return errors.WithMessage(err, "==> "+printCallerNameAndLine()+message)
}
func printCallerNameAndLine() string {
pc, _, line, _ := runtime.Caller(2)
return runtime.FuncForPC(pc).Name() + "()@" + strconv.Itoa(line) + ": "
}
2022-01-15 18:36:40 +08:00
func GetSelfFuncName() string {
pc, _, _, _ := runtime.Caller(1)
return cleanUpFuncName(runtime.FuncForPC(pc).Name())
}
2023-01-05 10:47:41 +08:00
2023-02-09 14:33:35 +08:00
func GetFuncName(skips ...int) string {
skip := 1
if len(skips) > 0 {
skip = skips[0] + 1
}
pc, _, _, _ := runtime.Caller(skip)
2023-01-05 10:47:41 +08:00
return cleanUpFuncName(runtime.FuncForPC(pc).Name())
}
2022-01-15 18:36:40 +08:00
func cleanUpFuncName(funcName string) string {
end := strings.LastIndex(funcName, ".")
if end == -1 {
return ""
}
return funcName[end+1:]
}
2022-04-08 17:55:44 +08:00
2023-01-05 10:47:41 +08:00
// Get the intersection of two slices
2023-02-16 15:20:59 +08:00
func Intersect(slice1, slice2 []int64) []int64 {
m := make(map[int64]bool)
n := make([]int64, 0)
2022-01-20 11:36:43 +08:00
for _, v := range slice1 {
m[v] = true
}
for _, v := range slice2 {
flag, _ := m[v]
if flag {
n = append(n, v)
}
}
return n
}
2022-04-08 17:55:44 +08:00
2023-01-05 10:47:41 +08:00
// Get the diff of two slices
2023-02-16 15:20:59 +08:00
func Difference(slice1, slice2 []int64) []int64 {
m := make(map[int64]bool)
n := make([]int64, 0)
2022-01-20 11:36:43 +08:00
inter := Intersect(slice1, slice2)
for _, v := range inter {
m[v] = true
}
for _, v := range slice1 {
if !m[v] {
n = append(n, v)
}
}
for _, v := range slice2 {
if !m[v] {
n = append(n, v)
}
}
return n
}
2022-04-21 16:46:48 +08:00
2023-01-05 10:47:41 +08:00
// Get the intersection of two slices
2022-04-21 16:46:48 +08:00
func IntersectString(slice1, slice2 []string) []string {
m := make(map[string]bool)
n := make([]string, 0)
for _, v := range slice1 {
m[v] = true
}
for _, v := range slice2 {
flag, _ := m[v]
if flag {
n = append(n, v)
}
}
return n
}
2023-01-05 10:47:41 +08:00
// Get the diff of two slices
2022-04-21 16:46:48 +08:00
func DifferenceString(slice1, slice2 []string) []string {
m := make(map[string]bool)
n := make([]string, 0)
inter := IntersectString(slice1, slice2)
for _, v := range inter {
m[v] = true
}
for _, v := range slice1 {
if !m[v] {
n = append(n, v)
}
}
for _, v := range slice2 {
if !m[v] {
n = append(n, v)
}
}
return n
}
2023-02-22 19:51:14 +08:00
2022-03-17 13:49:21 +08:00
func OperationIDGenerator() string {
return strconv.FormatInt(time.Now().UnixNano()+int64(rand.Uint32()), 10)
}
2022-03-28 12:17:07 +08:00
2022-05-24 19:24:11 +08:00
func Pb2String(pb proto.Message) (string, error) {
2023-05-04 15:06:23 +08:00
s, err := proto.Marshal(pb)
if err != nil {
return "", err
2022-04-28 15:13:39 +08:00
}
2023-05-04 15:06:23 +08:00
return string(s), nil
2022-05-25 15:09:17 +08:00
}
2022-05-24 19:24:11 +08:00
2022-05-25 15:09:17 +08:00
func String2Pb(s string, pb proto.Message) error {
return proto.Unmarshal([]byte(s), pb)
2022-05-24 19:24:11 +08:00
}
2022-05-24 19:25:49 +08:00
2023-02-15 15:52:32 +08:00
func GetHashCode(s string) uint32 {
return crc32.ChecksumIEEE([]byte(s))
}
2023-05-06 10:52:29 +08:00
func GetConversationIDByMsg(msg *sdkws.MsgData) string {
2023-05-06 11:31:04 +08:00
options := Options(msg.Options)
2023-05-06 10:52:29 +08:00
switch msg.SessionType {
case constant.SingleChatType:
l := []string{msg.SendID, msg.RecvID}
sort.Strings(l)
2023-05-06 11:31:04 +08:00
if options.IsNotification() {
return "n_" + strings.Join(l, "_")
}
2023-05-06 11:04:28 +08:00
return "si_" + strings.Join(l, "_") // single chat
2023-05-06 10:52:29 +08:00
case constant.GroupChatType:
2023-05-06 11:31:04 +08:00
if options.IsNotification() {
return "n_" + msg.GroupID // group chat
}
2023-05-06 11:04:28 +08:00
return "g_" + msg.GroupID // group chat
2023-05-06 10:52:29 +08:00
case constant.SuperGroupChatType:
2023-05-06 11:31:04 +08:00
if options.IsNotification() {
return "n_" + msg.GroupID // super group chat
}
2023-05-06 10:52:29 +08:00
return "sg_" + msg.GroupID // super group chat
case constant.NotificationChatType:
2023-05-06 11:31:04 +08:00
if options.IsNotification() {
return "n_" + msg.SendID + "_" + msg.RecvID // super group chat
}
2023-05-06 10:52:29 +08:00
return "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat
}
return ""
}
func GetConversationIDBySessionType(sessionType int, ids ...string) string {
sort.Strings(ids)
if len(ids) > 2 || len(ids) < 1 {
return ""
}
switch sessionType {
case constant.SingleChatType:
return "si_" + strings.Join(ids, "_") // single chat
case constant.GroupChatType:
return "g_" + ids[0] // group chat
case constant.SuperGroupChatType:
return "sg_" + ids[0] // super group chat
case constant.NotificationChatType:
return "sn_" + ids[0] // server notification chat
}
return ""
}
func GetNotificationConversationIDBySessionType(sessionType int, ids ...string) string {
sort.Strings(ids)
if len(ids) > 2 || len(ids) < 1 {
return ""
}
switch sessionType {
case constant.SingleChatType:
return "n_" + strings.Join(ids, "_") // single chat
case constant.GroupChatType:
return "n_" + ids[0] // group chat
case constant.SuperGroupChatType:
return "n_" + ids[0] // super group chat
case constant.NotificationChatType:
return "n_" + ids[0] // server notification chat
}
return ""
}
func IsNotification(conversationID string) bool {
return strings.HasPrefix(conversationID, "n_")
}
2023-05-06 18:51:10 +08:00
type MsgBySeq []*sdkws.MsgData
func (s MsgBySeq) Len() int {
return len(s)
}
func (s MsgBySeq) Less(i, j int) bool {
return s[i].Seq < s[j].Seq
}
func (s MsgBySeq) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}