Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add emoticon upload, remove, and get #283

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
7d09cc3
add email log in
AndrewZuo01 Nov 14, 2023
afd62c9
update
AndrewZuo01 Nov 15, 2023
f9ef9b1
update
AndrewZuo01 Nov 15, 2023
dff31c1
proto compile
AndrewZuo01 Nov 15, 2023
4742432
Merge remote-tracking branch 'upstream/main' into email-log-in
AndrewZuo01 Nov 20, 2023
fcde966
FIX BUG
AndrewZuo01 Nov 29, 2023
0fa34a9
FIX BUG
AndrewZuo01 Nov 29, 2023
6be297d
FIX BUG
AndrewZuo01 Nov 29, 2023
4d03a00
FIX BUG
AndrewZuo01 Nov 29, 2023
f08722b
FIX BUG
AndrewZuo01 Nov 29, 2023
ed513bb
add feature add and delete emoticon
AndrewZuo01 Dec 5, 2023
2d8796b
fix bug
AndrewZuo01 Dec 5, 2023
6a506aa
fix bug
AndrewZuo01 Dec 5, 2023
43134f8
fix bug
AndrewZuo01 Dec 5, 2023
a453f53
fix bug
AndrewZuo01 Dec 5, 2023
473ecbd
fix bug
AndrewZuo01 Dec 5, 2023
931041d
fix bug
AndrewZuo01 Dec 5, 2023
3d3949a
fix bug
AndrewZuo01 Dec 5, 2023
846128b
fix bug
AndrewZuo01 Dec 5, 2023
215d355
fix bug
AndrewZuo01 Dec 5, 2023
9d1a10a
fix bug
AndrewZuo01 Dec 5, 2023
23d80e2
Merge branch 'openimsdk:main' into email-log-in
AndrewZuo01 Dec 5, 2023
fe79b25
fix bug
AndrewZuo01 Dec 5, 2023
e0e4dfd
Merge remote-tracking branch 'origin/email-log-in' into email-log-in
AndrewZuo01 Dec 5, 2023
b6a5829
fix bug
AndrewZuo01 Dec 5, 2023
91fabaa
fix bug
AndrewZuo01 Dec 5, 2023
0b6bd52
fix bug
AndrewZuo01 Dec 5, 2023
605cc79
fix bug
AndrewZuo01 Dec 5, 2023
a120c06
fix bug
AndrewZuo01 Dec 5, 2023
51c6fe7
fix bug
AndrewZuo01 Dec 5, 2023
9da9bfb
fix bug
AndrewZuo01 Dec 5, 2023
6baf177
fix bug
AndrewZuo01 Dec 6, 2023
aa90bb1
fix bug
AndrewZuo01 Dec 6, 2023
9571b22
fix bug
AndrewZuo01 Dec 6, 2023
82c7540
fix bug
AndrewZuo01 Dec 6, 2023
698edcc
fix bug
AndrewZuo01 Dec 6, 2023
19cdf72
fix bug add check for send verify code
AndrewZuo01 Dec 13, 2023
89c1c31
fix bug add check for send verify code
AndrewZuo01 Dec 13, 2023
c727355
fix bug add check for send verify code
AndrewZuo01 Dec 13, 2023
a1e3f82
Merge branch 'openimsdk:main' into email-log-in
AndrewZuo01 Dec 13, 2023
e19e18b
Merge branch 'main' into email-log-in
AndrewZuo01 Jan 3, 2024
c12ead2
update pb
AndrewZuo01 Jan 3, 2024
f170e36
update pb
AndrewZuo01 Jan 3, 2024
3530da9
update pb
AndrewZuo01 Jan 3, 2024
19decbd
update pb
AndrewZuo01 Jan 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion internal/api/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ import (
)

func NewChat(chatConn, adminConn grpc.ClientConnInterface) *ChatApi {
return &ChatApi{chatClient: chat.NewChatClient(chatConn), adminClient: admin.NewAdminClient(adminConn), imApiCaller: apicall.NewCallerInterface()}
return &ChatApi{
chatClient: chat.NewChatClient(chatConn),
adminClient: admin.NewAdminClient(adminConn),
imApiCaller: apicall.NewCallerInterface(),
}
}

type ChatApi struct {
Expand Down Expand Up @@ -347,3 +351,14 @@ func (o *ChatApi) SearchFriend(c *gin.Context) {
}
apiresp.GinSuccess(c, resp)
}

func (o *ChatApi) AddEmoticon(c *gin.Context) {
log.ZDebug(c, "hello here api")
a2r.Call(chat.ChatClient.AddEmoticon, o.chatClient, c)
}
func (o *ChatApi) RemoveEmoticon(c *gin.Context) {
a2r.Call(chat.ChatClient.RemoveEmoticon, o.chatClient, c)
}
func (o *ChatApi) GetEmoticon(c *gin.Context) {
a2r.Call(chat.ChatClient.GetEmoticon, o.chatClient, c)
}
7 changes: 6 additions & 1 deletion internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package api

import (
"context"

"github.com/OpenIMSDK/chat/pkg/common/config"
"github.com/OpenIMSDK/tools/discoveryregistry"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -57,7 +56,13 @@ func NewChatRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegis
router.Group("/callback").POST("/open_im", chat.OpenIMCallback) // Callback

logs := router.Group("/logs", mw.CheckToken)

logs.POST("/upload", chat.UploadLogs)

emoticon := router.Group("/emoticon", mw.CheckToken)
emoticon.POST("/upload", chat.AddEmoticon)
emoticon.POST("/remove", chat.RemoveEmoticon)
emoticon.POST("/get", chat.GetEmoticon)
}

func NewAdminRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegistry) {
Expand Down
2 changes: 2 additions & 0 deletions internal/rpc/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func Start(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
chat2.VerifyCode{},
chat2.UserLoginRecord{},
chat2.Log{},
chat2.Emoticon{},
chat2.Image{},
}
if err := db.AutoMigrate(tables...); err != nil {
return err
Expand Down
123 changes: 123 additions & 0 deletions internal/rpc/chat/emoticon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package chat

import (
"context"
table "github.com/OpenIMSDK/chat/pkg/common/db/table/chat"
"github.com/OpenIMSDK/chat/pkg/proto/chat"
"github.com/OpenIMSDK/tools/errs"
"sync"
"time"
)

const (
epoch int64 = 1609459200000 // 设置起始时间 (例如 2021-01-01)
machineIDBits uint8 = 5 // 机器ID所占的位数
datacenterIDBits uint8 = 5 // 数据中心ID所占的位数
sequenceBits uint8 = 12 // 序列号所占的位数

maxMachineID int64 = -1 ^ (-1 << machineIDBits) // 最大机器ID
maxDatacenterID int64 = -1 ^ (-1 << datacenterIDBits) // 最大数据中心ID
maxSequence int64 = -1 ^ (-1 << sequenceBits) // 最大序列号

timeLeft uint8 = 22 // 时间戳向左的位移
dataLeft uint8 = 17 // 数据中心ID向左的位移
machineLeft uint8 = 12 // 机器ID向左的位移
)

type Snowflake struct {
mutex sync.Mutex // 保护同时访问
lastTimestamp int64
datacenterID int64
machineID int64
sequence int64
}

func NewSnowflake(datacenterID, machineID int64) (*Snowflake, error) {
if datacenterID < 0 || datacenterID > maxDatacenterID {
return nil, errs.ErrData.Wrap("datacenter ID out of rang")
}
if machineID < 0 || machineID > maxMachineID {
return nil, errs.ErrData.Wrap("machine ID out of range")
}

return &Snowflake{
lastTimestamp: 0,
datacenterID: datacenterID,
machineID: machineID,
sequence: 0,
}, nil
}

func (s *Snowflake) Generate() (int64, error) {
s.mutex.Lock()
defer s.mutex.Unlock()

now := time.Now().UnixNano() / 1e6 // 当前时间的毫秒时间戳
if s.lastTimestamp == now {
s.sequence = (s.sequence + 1) & maxSequence
if s.sequence == 0 {
for now <= s.lastTimestamp {
now = time.Now().UnixNano() / 1e6
}
}
} else {
s.sequence = 0
}

if now < s.lastTimestamp {
return 0, errs.ErrData.Wrap("clock moved backwards")
}
s.lastTimestamp = now

id := ((now - epoch) << timeLeft) | (s.datacenterID << dataLeft) | (s.machineID << machineLeft) | s.sequence
return id, nil
}

func (o *chatSvr) AddEmoticon(ctx context.Context, req *chat.AddEmoticonReq) (*chat.AddEmoticonResp, error) {
sf, err := NewSnowflake(1, 1)
if err != nil {
return nil, err
}
result, err := sf.Generate()
if err != nil {
return nil, err
}
image := &table.Image{
ID: result,
ImageURL: req.ImageData,
OwnerID: req.OwnerId,
}
err = o.Database.AddImage(ctx, image)
if err != nil {
return nil, err
}

return &chat.AddEmoticonResp{}, nil
}
func (o *chatSvr) RemoveEmoticon(ctx context.Context, req *chat.RemoveEmoticonReq) (*chat.RemoveEmoticonResp, error) {

err := o.Database.RemoveImage(ctx, req.UserId, req.EmoticonId)
if err != nil {
return nil, err
}

return &chat.RemoveEmoticonResp{}, nil
}
func (o *chatSvr) GetEmoticon(ctx context.Context, req *chat.GetEmoticonReq) (*chat.GetEmoticonResp, error) {

results, err := o.Database.GetImages(ctx, req.UserId)
if err != nil {
return nil, err
}

pbEmoticons := make([]*chat.Emoticon, 0)
for _, result := range results {
pbEmoticons = append(pbEmoticons, &chat.Emoticon{
ImageURL: result.ImageURL,
EmoticonId: result.ID,
UserId: result.OwnerID,
})
}

return &chat.GetEmoticonResp{E: pbEmoticons}, nil
}
4 changes: 2 additions & 2 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe
if req.Email == "" {
_, err := o.Database.TakeAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
if o.Database.IsNotFound(err) {
return nil, eerrs.ErrAccountNotFound.Wrap("phone unregistered")
return nil, errs.ErrArgs.Wrap("phone unregistered")
} else if err != nil {
return nil, err
}
} else {
_, err := o.Database.TakeAttributeByEmail(ctx, req.Email)
if o.Database.IsNotFound(err) {
return nil, eerrs.ErrAccountNotFound.Wrap("email unregistered")
return nil, errs.ErrArgs.Wrap("email unregistered")
} else if err != nil {
return nil, err
}
Expand Down
13 changes: 13 additions & 0 deletions internal/rpc/chat/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ func (o *chatSvr) ResetPassword(ctx context.Context, req *chat.ResetPasswordReq)
if req.Password == "" {
return nil, errs.ErrArgs.Wrap("password must be set")
}
if req.Email == "" {
_, err := o.Database.GetAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
if err != nil {
return nil, err
}
} else {
_, err := o.Database.GetAttributeByEmail(ctx, req.Email)
if err != nil {
return nil, err
}
}


var verifyCodeID uint
var err error
if req.Email == "" {
Expand Down
18 changes: 18 additions & 0 deletions pkg/common/db/database/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type ChatDatabaseInterface interface {
DeleteLogs(ctx context.Context, logID []string, userID string) error
SearchLogs(ctx context.Context, keyword string, start time.Time, end time.Time, pageNumber int32, showNumber int32) (uint32, []*table.Log, error)
GetLogs(ctx context.Context, LogIDs []string, userID string) ([]*table.Log, error)

AddImage(ctx context.Context, image *table.Image) error
RemoveImage(ctx context.Context, userID string, imageID int64) error
GetImages(ctx context.Context, userID string) ([]*table.Image, error)
}

func NewChatDatabase(db *gorm.DB) ChatDatabaseInterface {
Expand All @@ -73,6 +77,7 @@ func NewChatDatabase(db *gorm.DB) ChatDatabaseInterface {
verifyCode: chat.NewVerifyCode(db),
forbiddenAccount: admin2.NewForbiddenAccount(db),
log: chat.NewLogs(db),
emoticon: chat.NewEmoticons(db),
}
}

Expand All @@ -85,6 +90,19 @@ type ChatDatabase struct {
verifyCode table.VerifyCodeInterface
forbiddenAccount admin.ForbiddenAccountInterface
log table.LogInterface
emoticon table.EmoticonInterface
}

func (o *ChatDatabase) AddImage(ctx context.Context, image *table.Image) error {
return o.emoticon.AddImage(ctx, image)
}

func (o *ChatDatabase) RemoveImage(ctx context.Context, userID string, imageID int64) error {
return o.emoticon.DeleteImage(ctx, userID, imageID)
}

func (o *ChatDatabase) GetImages(ctx context.Context, userID string) ([]*table.Image, error) {
return o.emoticon.GetImages(ctx, userID)
}

func (o *ChatDatabase) GetLogs(ctx context.Context, LogIDs []string, userID string) ([]*table.Log, error) {
Expand Down
52 changes: 52 additions & 0 deletions pkg/common/db/model/chat/emoticon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package chat

import (
"context"
"github.com/OpenIMSDK/chat/pkg/common/db/table/chat"
"github.com/OpenIMSDK/tools/errs"
"gorm.io/gorm"
)

type Emoticons struct {
db *gorm.DB
}

func (e *Emoticons) AddEmoticon(ctx context.Context, emoticon *chat.Emoticon) error {
result := e.db.WithContext(ctx).Create(emoticon)
return errs.Wrap(result.Error) // Wrap the error using the errs package
}

func (e *Emoticons) DeleteEmoticon(ctx context.Context, userID string, emoticonID int64) error {
result := e.db.WithContext(ctx).Where("id = ? AND owner_id = ?", emoticonID, userID).Delete(&chat.Emoticon{})
return errs.Wrap(result.Error)
}

func (e *Emoticons) GetEmoticon(ctx context.Context, userID string, emoticonID int64) (*chat.Emoticon, error) {
var emoticon chat.Emoticon
result := e.db.WithContext(ctx).Where("id = ? AND owner_id = ?", emoticonID, userID).First(&emoticon)
if result.Error != nil {
return nil, errs.Wrap(result.Error)
}
return &emoticon, nil
}

func (e *Emoticons) AddImage(ctx context.Context, image *chat.Image) error {
result := e.db.WithContext(ctx).Create(image)
return errs.Wrap(result.Error)
}
func (e *Emoticons) DeleteImage(ctx context.Context, userID string, imageID int64) error {
result := e.db.WithContext(ctx).Where("id = ? AND owner_id = ?", imageID, userID).Delete(&chat.Image{})
return errs.Wrap(result.Error)
}
func (e *Emoticons) GetImages(ctx context.Context, userID string) ([]*chat.Image, error) {
var images []*chat.Image
result := e.db.WithContext(ctx).Where("owner_id = ?", userID).Find(&images)
if result.Error != nil {
return nil, errs.Wrap(result.Error)
}
return images, nil
}

func NewEmoticons(db *gorm.DB) chat.EmoticonInterface {
return &Emoticons{db: db}
}
35 changes: 35 additions & 0 deletions pkg/common/db/table/chat/emoticon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package chat

import (
"context"
)

type Image struct {
ID int64 `gorm:"column:id;primary_key;"`
ImageURL string `gorm:"column:image_url;type:varchar(255)"`
//EmoticonID string `gorm:"column:owner_id;type:char(64)"`
OwnerID string `gorm:"column:owner_id;type:char(64)"`
}

func (Image) TableName() string {
return "images"
}

type Emoticon struct {
EmoticonID int64 `gorm:"column:id;primary_key;"`
OwnerID string `gorm:"column:owner_id;type:char(64)"`
}

func (Emoticon) TableName() string {
return "emoticons"
}

type EmoticonInterface interface {
AddEmoticon(ctx context.Context, emoticon *Emoticon) error
DeleteEmoticon(ctx context.Context, userID string, emoticonID int64) error
GetEmoticon(ctx context.Context, userID string, emoticonID int64) (*Emoticon, error)

AddImage(ctx context.Context, image *Image) error
DeleteImage(ctx context.Context, userID string, imageID int64) error
GetImages(ctx context.Context, userID string) ([]*Image, error)
}
26 changes: 25 additions & 1 deletion pkg/proto/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,31 @@ func (x *SearchUserInfoReq) Check() error {
return nil
}

func (e *AddEmoticonReq) Check() error {
if e.OwnerId == "" {
return errs.ErrArgs.Wrap("emoticon ID is empty")
}
if e.ImageData == "" {
return errs.ErrArgs.Wrap("image URL is empty")
}
return nil
}

// Check RemoveEmoticon
func (e *RemoveEmoticonReq) Check() error {
if e.EmoticonId == 0 {
return errs.ErrArgs.Wrap("emoticon ID is empty")
}
return nil
}

// Check GetEmoticon
func (e *GetEmoticonReq) Check() error {
if e.UserId == "" {
return errs.ErrArgs.Wrap("User ID is empty")
}
return nil
}
func (x *AddUserAccountReq) Check() error {
if x.User == nil {
return errs.ErrArgs.Wrap("user is empty")
Expand All @@ -340,6 +365,5 @@ func (x *AddUserAccountReq) Check() error {
return errs.ErrArgs.Wrap("email must be right")
}
}

return nil
}
Loading
Loading