From 7f815b31848abac827c4005c3ca70d7aed2cea1f Mon Sep 17 00:00:00 2001 From: yuanjay <2218773049@qq.com> Date: Wed, 15 Nov 2023 01:51:09 +0800 Subject: [PATCH] aes key db --- pkg/common/db/controller/aes_key.go | 43 +++++++++++++++++++++++++ pkg/common/db/relation/aes_key_model.go | 29 +++++++++++++++++ pkg/common/db/table/relation/aes_key.go | 26 +++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 pkg/common/db/controller/aes_key.go create mode 100644 pkg/common/db/relation/aes_key_model.go create mode 100644 pkg/common/db/table/relation/aes_key.go diff --git a/pkg/common/db/controller/aes_key.go b/pkg/common/db/controller/aes_key.go new file mode 100644 index 000000000..42b80670f --- /dev/null +++ b/pkg/common/db/controller/aes_key.go @@ -0,0 +1,43 @@ +package controller + +import ( + "context" + "crypto/md5" + "fmt" + "github.com/OpenIMSDK/tools/tx" + "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" + "sort" + "strings" +) + +type AesKeyDatabase interface { + AcquireAesKey(ctx context.Context, conversationType int32, userId, friendId, groupId string) (key *relation.AesKeyModel, err error) + AcquireAesKeys(ctx context.Context, userId string) (key []*relation.AesKeyModel, err error) +} + +type aesKeyDatabase struct { + key relation.AesKeyModelInterface + tx tx.Tx +} + +func newAesKeyDatabase(key relation.AesKeyModelInterface, tx tx.Tx) *aesKeyDatabase { + return &aesKeyDatabase{key: key, tx: tx} +} + +func (a *aesKeyDatabase) AcquireAesKey(ctx context.Context, conversationType int32, userId, friendId, groupId string) (key *relation.AesKeyModel, err error) { + //TODO implement me + panic("implement me") +} + +func (a *aesKeyDatabase) AcquireAesKeys(ctx context.Context, userId string) (key []*relation.AesKeyModel, err error) { + //TODO implement me + panic("implement me") +} + +func (a *aesKeyDatabase) generateKeyConversationsID(args ...string) string { + sort.Strings(args) + combinedStr := strings.Join(args, "") + md5Value := md5.Sum([]byte(combinedStr)) + md5Str := fmt.Sprintf("%x", md5Value) + return md5Str[:16] +} diff --git a/pkg/common/db/relation/aes_key_model.go b/pkg/common/db/relation/aes_key_model.go new file mode 100644 index 000000000..73ebabeca --- /dev/null +++ b/pkg/common/db/relation/aes_key_model.go @@ -0,0 +1,29 @@ +package relation + +import ( + "context" + "github.com/OpenIMSDK/tools/utils" + "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" + "gorm.io/gorm" +) + +type AesKeyGorm struct { + *MetaDB +} + +func NewAesKeyGorm(db *gorm.DB) *AesKeyGorm { + return &AesKeyGorm{NewMetaDB(db, &relation.AesKeyModel{})} +} + +func (a *AesKeyGorm) Installs(ctx context.Context, keys []*relation.AesKeyModel) (err error) { + return utils.Wrap(a.db(ctx).Create(&keys).Error, "") +} + +func (a *AesKeyGorm) GetAesKey(tx context.Context, KeyConversationsID string) (key *relation.AesKeyModel, err error) { + key = &relation.AesKeyModel{} + return key, utils.Wrap(a.db(tx).Where("key_conversations_id = ? ", KeyConversationsID).Take(key).Error, "") +} + +func (a *AesKeyGorm) GetAllAesKey(tx context.Context, UserID string) (keys []*relation.AesKeyModel, err error) { + return keys, utils.Wrap(a.db(tx).Where("owner_user_id = ? or friend_user_id = ? ", UserID, UserID).Take(keys).Error, "") +} diff --git a/pkg/common/db/table/relation/aes_key.go b/pkg/common/db/table/relation/aes_key.go new file mode 100644 index 000000000..847b71665 --- /dev/null +++ b/pkg/common/db/table/relation/aes_key.go @@ -0,0 +1,26 @@ +package relation + +import "context" + +const ( + AesKeyModelTableName = "aes_keys" +) + +type AesKeyModel struct { + KeyConversationsID string `gorm:"column:key_conversations_id;primary_key;size:64" json:"keyConversationsID"` + Key string `gorm:"column:key" json:"key"` + ConversationType int32 `gorm:"column:conversation_type" json:"conversationType"` + OwnerUserID string `gorm:"column:owner_user_id;size:64" json:"ownerUserID"` + FriendUserID string `gorm:"column:friend_user_id;size:64" json:"friendUserID"` + GroupID string `gorm:"column:group_id;size:64" json:"groupID"` +} + +func (AesKeyModel) TableName() string { + return AesKeyModelTableName +} + +type AesKeyModelInterface interface { + Installs(ctx context.Context, friends []*AesKeyModel) (err error) + GetAesKey(tx context.Context, KeyConversationsID string) (key *AesKeyModel, err error) + GetAllAesKey(tx context.Context, UserID string) (key []*AesKeyModel, err error) +}