You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
5.9 KiB
157 lines
5.9 KiB
2 years ago
|
package relation
|
||
4 years ago
|
|
||
|
import (
|
||
2 years ago
|
"Open_IM/pkg/common/trace_log"
|
||
3 years ago
|
"Open_IM/pkg/utils"
|
||
2 years ago
|
"context"
|
||
2 years ago
|
"gorm.io/gorm"
|
||
3 years ago
|
"time"
|
||
4 years ago
|
)
|
||
|
|
||
2 years ago
|
var FriendRequestDB *gorm.DB
|
||
|
|
||
|
type FriendRequest struct {
|
||
|
FromUserID string `gorm:"column:from_user_id;primary_key;size:64"`
|
||
|
ToUserID string `gorm:"column:to_user_id;primary_key;size:64"`
|
||
|
HandleResult int32 `gorm:"column:handle_result"`
|
||
|
ReqMsg string `gorm:"column:req_msg;size:255"`
|
||
|
CreateTime time.Time `gorm:"column:create_time"`
|
||
|
HandlerUserID string `gorm:"column:handler_user_id;size:64"`
|
||
|
HandleMsg string `gorm:"column:handle_msg;size:255"`
|
||
|
HandleTime time.Time `gorm:"column:handle_time"`
|
||
|
Ex string `gorm:"column:ex;size:1024"`
|
||
2 years ago
|
DB *gorm.DB `gorm:"-"`
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func NewFriendRequest(db *gorm.DB) *FriendRequest {
|
||
2 years ago
|
var fr FriendRequest
|
||
|
fr.DB = initModel(db, &fr)
|
||
|
return &fr
|
||
2 years ago
|
}
|
||
|
|
||
|
func (f *FriendRequest) Create(ctx context.Context, friends []*FriendRequest) (err error) {
|
||
|
defer func() {
|
||
|
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||
|
}()
|
||
2 years ago
|
return utils.Wrap(f.DB.Create(&friends).Error, "")
|
||
2 years ago
|
}
|
||
|
|
||
|
func (f *FriendRequest) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
|
||
|
defer func() {
|
||
|
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID)
|
||
|
}()
|
||
2 years ago
|
return utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&FriendRequest{}).Error, "")
|
||
2 years ago
|
}
|
||
|
|
||
|
func (f *FriendRequest) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) {
|
||
|
defer func() {
|
||
|
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "args", args)
|
||
|
}()
|
||
2 years ago
|
return utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||
2 years ago
|
}
|
||
|
|
||
|
func (f *FriendRequest) Update(ctx context.Context, friends []*FriendRequest) (err error) {
|
||
|
defer func() {
|
||
|
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||
|
}()
|
||
2 years ago
|
return utils.Wrap(f.DB.Updates(&friends).Error, "")
|
||
2 years ago
|
}
|
||
|
|
||
|
func (f *FriendRequest) Find(ctx context.Context, ownerUserID string) (friends []*FriendRequest, err error) {
|
||
|
defer func() {
|
||
|
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends)
|
||
|
}()
|
||
2 years ago
|
return friends, utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (f *FriendRequest) Take(ctx context.Context, fromUserID, toUserID string) (friend *FriendRequest, err error) {
|
||
2 years ago
|
friend = &FriendRequest{}
|
||
2 years ago
|
defer trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID, "friend", friend)
|
||
2 years ago
|
return friend, utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friend).Error, "")
|
||
2 years ago
|
}
|
||
4 years ago
|
|
||
2 years ago
|
func (f *FriendRequest) FindToUserID(ctx context.Context, toUserID string) (friends []*FriendRequest, err error) {
|
||
|
defer func() {
|
||
|
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "toUserID", toUserID, "friends", friends)
|
||
|
}()
|
||
2 years ago
|
return friends, utils.Wrap(f.DB.Where("to_user_id = ?", toUserID).Find(&friends).Error, "")
|
||
2 years ago
|
}
|
||
|
|
||
|
func (f *FriendRequest) FindFromUserID(ctx context.Context, fromUserID string) (friends []*FriendRequest, err error) {
|
||
|
defer func() {
|
||
|
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "friends", friends)
|
||
|
}()
|
||
2 years ago
|
return friends, utils.Wrap(f.DB.Where("from_user_id = ?", fromUserID).Find(&friends).Error, "")
|
||
2 years ago
|
}
|
||
|
|
||
3 years ago
|
// who apply to add me
|
||
2 years ago
|
func GetReceivedFriendsApplicationListByUserID(ToUserID string) ([]FriendRequest, error) {
|
||
|
var usersInfo []FriendRequest
|
||
2 years ago
|
err := FriendRequestDB.Table("friend_requests").Where("to_user_id=?", ToUserID).Find(&usersInfo).Error
|
||
4 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return usersInfo, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
// I apply to add somebody
|
||
|
func GetSendFriendApplicationListByUserID(FromUserID string) ([]FriendRequest, error) {
|
||
|
var usersInfo []FriendRequest
|
||
2 years ago
|
err := FriendRequestDB.Table("friend_requests").Where("from_user_id=?", FromUserID).Find(&usersInfo).Error
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return usersInfo, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
// FromUserId apply to add ToUserID
|
||
|
func GetFriendApplicationByBothUserID(FromUserID, ToUserID string) (*FriendRequest, error) {
|
||
|
var friendRequest FriendRequest
|
||
2 years ago
|
err := FriendRequestDB.Table("friend_requests").Where("from_user_id=? and to_user_id=?", FromUserID, ToUserID).Take(&friendRequest).Error
|
||
4 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &friendRequest, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func UpdateFriendApplication(friendRequest *FriendRequest) error {
|
||
3 years ago
|
friendRequest.CreateTime = time.Now()
|
||
2 years ago
|
return FriendRequestDB.Table("friend_requests").Where("from_user_id=? and to_user_id=?",
|
||
2 years ago
|
friendRequest.FromUserID, friendRequest.ToUserID).Updates(&friendRequest).Error
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
2 years ago
|
func InsertFriendApplication(friendRequest *FriendRequest, args map[string]interface{}) error {
|
||
2 years ago
|
if err := FriendRequestDB.Table("friend_requests").Create(friendRequest).Error; err == nil {
|
||
3 years ago
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
//t := dbConn.Debug().Table("friend_requests").Where("from_user_id = ? and to_user_id = ?", friendRequest.FromUserID, friendRequest.ToUserID).Select("*").Updates(*friendRequest)
|
||
|
//if t.RowsAffected == 0 {
|
||
|
// return utils.Wrap(errors.New("RowsAffected == 0"), "no update")
|
||
|
//}
|
||
|
//return utils.Wrap(t.Error, "")
|
||
|
|
||
|
friendRequest.CreateTime = time.Now()
|
||
|
args["create_time"] = friendRequest.CreateTime
|
||
2 years ago
|
u := FriendRequestDB.Model(friendRequest).Updates(args)
|
||
3 years ago
|
//u := dbConn.Table("friend_requests").Where("from_user_id=? and to_user_id=?",
|
||
|
// friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest)
|
||
|
//u := dbConn.Table("friend_requests").Where("from_user_id=? and to_user_id=?",
|
||
|
// friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest)
|
||
|
if u.RowsAffected != 0 {
|
||
|
return nil
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
if friendRequest.CreateTime.Unix() < 0 {
|
||
|
friendRequest.CreateTime = time.Now()
|
||
|
}
|
||
|
if friendRequest.HandleTime.Unix() < 0 {
|
||
|
friendRequest.HandleTime = utils.UnixSecondToTime(0)
|
||
|
}
|
||
2 years ago
|
err := FriendRequestDB.Table("friend_requests").Create(friendRequest).Error
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
3 years ago
|
}
|