mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-07 10:35:59 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
This commit is contained in:
+56
-52
@@ -20,10 +20,10 @@ const ConfName = "openIMConf"
|
||||
|
||||
var Config config
|
||||
|
||||
type callBackConfig struct {
|
||||
Enable bool `yaml:"enable"`
|
||||
CallbackTimeOut int `yaml:"callbackTimeOut"`
|
||||
CallbackFailedContinue bool `yaml:"callbackFailedContinue"`
|
||||
type CallBackConfig struct {
|
||||
Enable bool `yaml:"enable"`
|
||||
CallbackTimeOut int `yaml:"callbackTimeOut"`
|
||||
CallbackFailedContinue *bool `yaml:"callbackFailedContinue"`
|
||||
}
|
||||
|
||||
type config struct {
|
||||
@@ -283,21 +283,21 @@ type config struct {
|
||||
|
||||
Callback struct {
|
||||
CallbackUrl string `yaml:"callbackUrl"`
|
||||
CallbackBeforeSendSingleMsg callBackConfig `yaml:"callbackBeforeSendSingleMsg"`
|
||||
CallbackAfterSendSingleMsg callBackConfig `yaml:"callbackAfterSendSingleMsg"`
|
||||
CallbackBeforeSendGroupMsg callBackConfig `yaml:"callbackBeforeSendGroupMsg"`
|
||||
CallbackAfterSendGroupMsg callBackConfig `yaml:"callbackAfterSendGroupMsg"`
|
||||
CallbackMsgModify callBackConfig `yaml:"callbackMsgModify"`
|
||||
CallbackUserOnline callBackConfig `yaml:"callbackUserOnline"`
|
||||
CallbackUserOffline callBackConfig `yaml:"callbackUserOffline"`
|
||||
CallbackUserKickOff callBackConfig `yaml:"callbackUserKickOff"`
|
||||
CallbackOfflinePush callBackConfig `yaml:"callbackOfflinePush"`
|
||||
CallbackOnlinePush callBackConfig `yaml:"callbackOnlinePush"`
|
||||
CallbackBeforeSuperGroupOnlinePush callBackConfig `yaml:"callbackSuperGroupOnlinePush"`
|
||||
CallbackBeforeAddFriend callBackConfig `yaml:"callbackBeforeAddFriend"`
|
||||
CallbackBeforeCreateGroup callBackConfig `yaml:"callbackBeforeCreateGroup"`
|
||||
CallbackBeforeMemberJoinGroup callBackConfig `yaml:"callbackBeforeMemberJoinGroup"`
|
||||
CallbackBeforeSetGroupMemberInfo callBackConfig `yaml:"callbackBeforeSetGroupMemberInfo"`
|
||||
CallbackBeforeSendSingleMsg CallBackConfig `yaml:"callbackBeforeSendSingleMsg"`
|
||||
CallbackAfterSendSingleMsg CallBackConfig `yaml:"callbackAfterSendSingleMsg"`
|
||||
CallbackBeforeSendGroupMsg CallBackConfig `yaml:"callbackBeforeSendGroupMsg"`
|
||||
CallbackAfterSendGroupMsg CallBackConfig `yaml:"callbackAfterSendGroupMsg"`
|
||||
CallbackMsgModify CallBackConfig `yaml:"callbackMsgModify"`
|
||||
CallbackUserOnline CallBackConfig `yaml:"callbackUserOnline"`
|
||||
CallbackUserOffline CallBackConfig `yaml:"callbackUserOffline"`
|
||||
CallbackUserKickOff CallBackConfig `yaml:"callbackUserKickOff"`
|
||||
CallbackOfflinePush CallBackConfig `yaml:"callbackOfflinePush"`
|
||||
CallbackOnlinePush CallBackConfig `yaml:"callbackOnlinePush"`
|
||||
CallbackBeforeSuperGroupOnlinePush CallBackConfig `yaml:"callbackSuperGroupOnlinePush"`
|
||||
CallbackBeforeAddFriend CallBackConfig `yaml:"callbackBeforeAddFriend"`
|
||||
CallbackBeforeCreateGroup CallBackConfig `yaml:"callbackBeforeCreateGroup"`
|
||||
CallbackBeforeMemberJoinGroup CallBackConfig `yaml:"callbackBeforeMemberJoinGroup"`
|
||||
CallbackBeforeSetGroupMemberInfo CallBackConfig `yaml:"callbackBeforeSetGroupMemberInfo"`
|
||||
} `yaml:"callback"`
|
||||
Notification struct {
|
||||
///////////////////////group/////////////////////////////
|
||||
@@ -629,41 +629,43 @@ type usualConfig struct {
|
||||
|
||||
var UsualConfig usualConfig
|
||||
|
||||
func unmarshalConfig(config interface{}, configName string) {
|
||||
var env string
|
||||
if configName == "config.yaml" {
|
||||
env = "CONFIG_NAME"
|
||||
} else if configName == "usualConfig.yaml" {
|
||||
env = "USUAL_CONFIG_NAME"
|
||||
func unmarshalConfig(config interface{}, configPath string) {
|
||||
bytes, err := ioutil.ReadFile(configPath)
|
||||
if err != nil {
|
||||
panic(err.Error() + configPath)
|
||||
}
|
||||
cfgName := os.Getenv(env)
|
||||
if len(cfgName) != 0 {
|
||||
bytes, err := ioutil.ReadFile(filepath.Join(cfgName, "config", configName))
|
||||
if err != nil {
|
||||
bytes, err = ioutil.ReadFile(filepath.Join(Root, "config", configName))
|
||||
if err != nil {
|
||||
panic(err.Error() + " config: " + filepath.Join(cfgName, "config", configName))
|
||||
}
|
||||
} else {
|
||||
Root = cfgName
|
||||
}
|
||||
if err = yaml.Unmarshal(bytes, config); err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
} else {
|
||||
bytes, err := ioutil.ReadFile(fmt.Sprintf("../config/%s", configName))
|
||||
if err != nil {
|
||||
panic(err.Error() + configName)
|
||||
}
|
||||
if err = yaml.Unmarshal(bytes, config); err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
if err = yaml.Unmarshal(bytes, config); err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
unmarshalConfig(&Config, "config.yaml")
|
||||
unmarshalConfig(&UsualConfig, "usualConfig.yaml")
|
||||
func initConfig(config interface{}, configName, configPath string) {
|
||||
if configPath == "" {
|
||||
var env string
|
||||
if configName == "config.yaml" {
|
||||
env = "CONFIG_NAME"
|
||||
} else if configName == "usualConfig.yaml" {
|
||||
env = "USUAL_CONFIG_NAME"
|
||||
}
|
||||
cfgName := os.Getenv(env)
|
||||
if len(cfgName) != 0 {
|
||||
configPath = filepath.Join(cfgName, "config", configName)
|
||||
_, err := os.Stat(configPath)
|
||||
if os.IsNotExist(err) {
|
||||
configPath = filepath.Join(Root, "config", configName)
|
||||
} else {
|
||||
Root = cfgName
|
||||
}
|
||||
} else {
|
||||
configPath = fmt.Sprintf("../config/%s", configName)
|
||||
}
|
||||
}
|
||||
unmarshalConfig(config, configPath)
|
||||
}
|
||||
|
||||
func InitConfig(configPath string) {
|
||||
initConfig(&Config, "config.yaml", configPath)
|
||||
initConfig(&UsualConfig, "usualConfig.yaml", configPath)
|
||||
if Config.Etcd.UserName == "" {
|
||||
Config.Etcd.UserName = UsualConfig.Etcd.UserName
|
||||
}
|
||||
@@ -728,11 +730,9 @@ func init() {
|
||||
if Config.Push.Getui.Enable == nil {
|
||||
Config.Push.Getui.Enable = &UsualConfig.Push.Getui.Enable
|
||||
}
|
||||
|
||||
if Config.Secret == "" {
|
||||
Config.Secret = UsualConfig.Secret
|
||||
}
|
||||
|
||||
if Config.TokenPolicy.AccessExpire == 0 {
|
||||
Config.TokenPolicy.AccessExpire = UsualConfig.Tokenpolicy.AccessExpire
|
||||
}
|
||||
@@ -740,3 +740,7 @@ func init() {
|
||||
Config.TokenPolicy.AccessSecret = UsualConfig.Tokenpolicy.AccessSecret
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
InitConfig("")
|
||||
}
|
||||
|
||||
@@ -16,6 +16,14 @@ type ErrInfo struct {
|
||||
DetailErrMsg string
|
||||
}
|
||||
|
||||
func NewErrInfo(code int32, msg, detail string) *ErrInfo {
|
||||
return &ErrInfo{
|
||||
ErrCode: code,
|
||||
ErrMsg: msg,
|
||||
DetailErrMsg: detail,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *ErrInfo) Error() string {
|
||||
return "errMsg: " + e.ErrMsg + " detail errMsg: " + e.DetailErrMsg
|
||||
}
|
||||
|
||||
+1
-35
@@ -37,7 +37,6 @@ func initMysqlDB() {
|
||||
panic(err1.Error() + " open failed " + dsn)
|
||||
}
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
|
||||
err = db.Exec(sql).Error
|
||||
if err != nil {
|
||||
@@ -112,39 +111,7 @@ func initMysqlDB() {
|
||||
if !db.Migrator().HasTable(&im_mysql_model.Conversation{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.Conversation{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.Department{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.Department{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.OrganizationUser{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.OrganizationUser{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.DepartmentMember{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.DepartmentMember{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.AppVersion{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.AppVersion{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.BlackList{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.BlackList{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.IpLimit{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.IpLimit{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.UserIpLimit{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.UserIpLimit{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.RegisterAddFriend{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.RegisterAddFriend{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.Invitation{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.Invitation{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.ClientInitConfig{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.ClientInitConfig{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&im_mysql_model.UserIpRecord{}) {
|
||||
db.Migrator().CreateTable(&im_mysql_model.UserIpRecord{})
|
||||
}
|
||||
|
||||
DB.MysqlDB.db = db
|
||||
im_mysql_model.GroupDB = db.Table("groups")
|
||||
im_mysql_model.GroupMemberDB = db.Table("group_members")
|
||||
@@ -158,7 +125,6 @@ func initMysqlDB() {
|
||||
im_mysql_model.FriendDB = db.Table("friends")
|
||||
im_mysql_model.FriendRequestDB = db.Table("friend_requests")
|
||||
im_mysql_model.GroupRequestDB = db.Table("group_requests")
|
||||
im_mysql_model.AppDB = db.Table("app_db")
|
||||
InitManager()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package im_mysql_model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var InitConfigDB *gorm.DB
|
||||
|
||||
func SetClientInitConfig(m map[string]interface{}) error {
|
||||
result := InitConfigDB.Model(&ClientInitConfig{}).Where("1=1").Updates(m)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
err := InitConfigDB.Model(&ClientInitConfig{}).Create(m).Error
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetClientInitConfig() (ClientInitConfig, error) {
|
||||
var config ClientInitConfig
|
||||
err := InitConfigDB.Model(&ClientInitConfig{}).First(&config).Error
|
||||
return config, err
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package im_mysql_model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -23,11 +22,6 @@ func GetRegister(account, areaCode, userID string) (*Register, error) {
|
||||
userID, "", account, account, areaCode).Take(&r).Error
|
||||
}
|
||||
|
||||
func GetRegisterInfo(userID string) (*Register, error) {
|
||||
var r Register
|
||||
return &r, RegisterDB.Table("registers").Where("user_id = ?", userID).Take(&r).Error
|
||||
}
|
||||
|
||||
func SetPassword(account, password, ex, userID, areaCode, ip string) error {
|
||||
r := Register{
|
||||
Account: account,
|
||||
@@ -46,49 +40,3 @@ func ResetPassword(account, password string) error {
|
||||
}
|
||||
return RegisterDB.Table("registers").Where("account = ?", account).Updates(&r).Error
|
||||
}
|
||||
|
||||
func GetRegisterAddFriendList(showNumber, pageNumber int32) ([]string, error) {
|
||||
var IDList []string
|
||||
var err error
|
||||
model := RegisterDB.Model(&RegisterAddFriend{})
|
||||
if showNumber == 0 {
|
||||
err = model.Pluck("user_id", &IDList).Error
|
||||
} else {
|
||||
err = model.Limit(int(showNumber)).Offset(int(showNumber*(pageNumber-1))).Pluck("user_id", &IDList).Error
|
||||
}
|
||||
return IDList, err
|
||||
}
|
||||
|
||||
func AddUserRegisterAddFriendIDList(userIDList ...string) error {
|
||||
var list []RegisterAddFriend
|
||||
for _, v := range userIDList {
|
||||
list = append(list, RegisterAddFriend{UserID: v})
|
||||
}
|
||||
result := RegisterDB.Create(list)
|
||||
if int(result.RowsAffected) < len(userIDList) {
|
||||
return errors.New("some line insert failed")
|
||||
}
|
||||
err := result.Error
|
||||
return err
|
||||
}
|
||||
|
||||
func ReduceUserRegisterAddFriendIDList(userIDList ...string) error {
|
||||
var list []RegisterAddFriend
|
||||
for _, v := range userIDList {
|
||||
list = append(list, RegisterAddFriend{UserID: v})
|
||||
}
|
||||
err := RegisterDB.Delete(list).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteAllRegisterAddFriendIDList() error {
|
||||
err := RegisterDB.Where("1 = 1").Delete(&RegisterAddFriend{}).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func GetUserIPLimit(userID string) (UserIpLimit, error) {
|
||||
var limit UserIpLimit
|
||||
limit.UserID = userID
|
||||
err := RegisterDB.Model(&UserIpLimit{}).Take(&limit).Error
|
||||
return limit, err
|
||||
}
|
||||
|
||||
@@ -7,6 +7,20 @@ import (
|
||||
|
||||
var AppDB *gorm.DB
|
||||
|
||||
type AppVersion struct {
|
||||
Version string `gorm:"column:version;size:64" json:"version"`
|
||||
Type int `gorm:"column:type;primary_key" json:"type"`
|
||||
UpdateTime int `gorm:"column:update_time" json:"update_time"`
|
||||
ForceUpdate bool `gorm:"column:force_update" json:"force_update"`
|
||||
FileName string `gorm:"column:file_name" json:"file_name"`
|
||||
YamlName string `gorm:"column:yaml_name" json:"yaml_name"`
|
||||
UpdateLog string `gorm:"column:update_log" json:"update_log"`
|
||||
}
|
||||
|
||||
func (AppVersion) TableName() string {
|
||||
return "app_version"
|
||||
}
|
||||
|
||||
func UpdateAppVersion(appType int, version string, forceUpdate bool, fileName, yamlName, updateLog string) error {
|
||||
updateTime := int(time.Now().Unix())
|
||||
app := AppVersion{
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var FriendDB *gorm.DB
|
||||
|
||||
type Friend struct {
|
||||
OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"`
|
||||
FriendUserID string `gorm:"column:friend_user_id;primary_key;size:64"`
|
||||
@@ -18,49 +16,54 @@ type Friend struct {
|
||||
AddSource int32 `gorm:"column:add_source"`
|
||||
OperatorUserID string `gorm:"column:operator_user_id;size:64"`
|
||||
Ex string `gorm:"column:ex;size:1024"`
|
||||
db *gorm.DB `gorm:"-"`
|
||||
}
|
||||
|
||||
func (*Friend) Create(ctx context.Context, friends []*Friend) (err error) {
|
||||
func NewFriend(db *gorm.DB) *Friend {
|
||||
return &Friend{db: db}
|
||||
}
|
||||
|
||||
func (f *Friend) Create(ctx context.Context, friends []*Friend) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
err = utils.Wrap(FriendDB.Create(&friends).Error, "")
|
||||
err = utils.Wrap(f.db.Create(&friends).Error, "")
|
||||
return err
|
||||
}
|
||||
|
||||
func (*Friend) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
|
||||
func (f *Friend) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs)
|
||||
}()
|
||||
err = utils.Wrap(FriendDB.Where("owner_user_id = ? and friend_user_id in (?)", ownerUserID, friendUserIDs).Delete(&Friend{}).Error, "")
|
||||
err = utils.Wrap(f.db.Where("owner_user_id = ? and friend_user_id in (?)", ownerUserID, friendUserIDs).Delete(&Friend{}).Error, "")
|
||||
return err
|
||||
}
|
||||
|
||||
func (*Friend) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) {
|
||||
func (f *Friend) 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)
|
||||
}()
|
||||
return utils.Wrap(FriendDB.Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
return utils.Wrap(f.db.Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (*Friend) Update(ctx context.Context, friends []*Friend) (err error) {
|
||||
func (f *Friend) Update(ctx context.Context, friends []*Friend) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(FriendDB.Updates(&friends).Error, "")
|
||||
return utils.Wrap(f.db.Updates(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (*Friend) Find(ctx context.Context, ownerUserID string) (friends []*Friend, err error) {
|
||||
func (f *Friend) Find(ctx context.Context, ownerUserID string) (friends []*Friend, err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends)
|
||||
}()
|
||||
err = utils.Wrap(FriendDB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
err = utils.Wrap(f.db.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
return friends, err
|
||||
}
|
||||
|
||||
func (*Friend) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *Friend, err error) {
|
||||
func (f *Friend) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *Friend, err error) {
|
||||
friend = &Friend{}
|
||||
defer trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "group", *friend)
|
||||
err = utils.Wrap(FriendDB.Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
|
||||
err = utils.Wrap(f.db.Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
|
||||
return friend, err
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package im_mysql_model
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/trace_log"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
@@ -18,10 +20,56 @@ type FriendRequest struct {
|
||||
HandleMsg string `gorm:"column:handle_msg;size:255"`
|
||||
HandleTime time.Time `gorm:"column:handle_time"`
|
||||
Ex string `gorm:"column:ex;size:1024"`
|
||||
db *gorm.DB `gorm:"-"`
|
||||
}
|
||||
|
||||
func (FriendRequest) TableName() string {
|
||||
return "friend_requests"
|
||||
func NewFriendRequest(db *gorm.DB) *FriendRequest {
|
||||
return &FriendRequest{db: db}
|
||||
}
|
||||
|
||||
func (f *FriendRequest) Create(ctx context.Context, friends []*FriendRequest) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
err = utils.Wrap(f.db.Create(&friends).Error, "")
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
}()
|
||||
err = utils.Wrap(f.db.Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&FriendRequest{}).Error, "")
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
}()
|
||||
return utils.Wrap(f.db.Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequest) Update(ctx context.Context, friends []*FriendRequest) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(f.db.Updates(&friends).Error, "")
|
||||
}
|
||||
|
||||
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)
|
||||
}()
|
||||
err = utils.Wrap(f.db.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
return friends, err
|
||||
}
|
||||
|
||||
func (f *FriendRequest) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *FriendRequest, err error) {
|
||||
friend = &FriendRequest{}
|
||||
defer trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "group", *friend)
|
||||
err = utils.Wrap(f.db.Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
|
||||
return friend, err
|
||||
}
|
||||
|
||||
// who apply to add me
|
||||
|
||||
@@ -24,6 +24,10 @@ type GroupRequest struct {
|
||||
Ex string `gorm:"column:ex;size:1024"`
|
||||
}
|
||||
|
||||
func (GroupRequest) TableName() string {
|
||||
return "friend_requests"
|
||||
}
|
||||
|
||||
func (*GroupRequest) Create(ctx context.Context, groupRequests []*GroupRequest) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
package im_mysql_model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var InvitationDB *gorm.DB
|
||||
|
||||
type Invitation struct {
|
||||
InvitationCode string `gorm:"column:invitation_code;primary_key;type:varchar(32)"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
UserID string `gorm:"column:user_id;index:userID"`
|
||||
LastTime time.Time `gorm:"column:last_time"`
|
||||
Status int32 `gorm:"column:status"`
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量生成邀请码
|
||||
*/
|
||||
func BatchCreateInvitationCodes(CodeNums int, CodeLen int) ([]string, error) {
|
||||
i := CodeNums
|
||||
var codes []string
|
||||
for {
|
||||
if i == 0 {
|
||||
break
|
||||
}
|
||||
code := CreateRandomString(CodeLen)
|
||||
invitation := new(Invitation)
|
||||
invitation.CreateTime = time.Now()
|
||||
invitation.InvitationCode = code
|
||||
invitation.LastTime = time.Now()
|
||||
invitation.Status = 0
|
||||
invitation.UserID = ""
|
||||
result := InvitationDB.Table("invitations").Create(&invitation)
|
||||
if result.Error != nil {
|
||||
continue
|
||||
}
|
||||
if result.RowsAffected > 0 {
|
||||
i = i - 1
|
||||
}
|
||||
codes = append(codes, code)
|
||||
}
|
||||
return codes, nil
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查邀请码
|
||||
*/
|
||||
func CheckInvitationCode(code string) error {
|
||||
var invitationCode Invitation
|
||||
err := InvitationDB.Table("invitations").Where("invitation_code=?", code).Take(&invitationCode).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if invitationCode.InvitationCode != code {
|
||||
return errors.New("邀请码不存在")
|
||||
}
|
||||
if invitationCode.Status != 0 {
|
||||
return errors.New("邀请码已经被使用")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试加锁模式解决邀请码抢占的问题
|
||||
*/
|
||||
func TryLockInvitationCode(Code string, UserID string) bool {
|
||||
Data := make(map[string]interface{}, 0)
|
||||
Data["user_id"] = UserID
|
||||
Data["status"] = 1
|
||||
Data["last_time"] = time.Now()
|
||||
result := InvitationDB.Table("invitations").Where("invitation_code=? and user_id=? and status=?", Code, "", 0).Updates(Data)
|
||||
if result.Error != nil {
|
||||
return false
|
||||
}
|
||||
return result.RowsAffected > 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 完成邀请码的状态
|
||||
*/
|
||||
func FinishInvitationCode(Code string, UserId string) bool {
|
||||
Data := make(map[string]interface{}, 0)
|
||||
Data["status"] = 2
|
||||
result := InvitationDB.Table("invitations").Where("invitation_code=? and user_id=? and status=?", Code, UserId, 1).Updates(Data)
|
||||
if result.Error != nil {
|
||||
return false
|
||||
}
|
||||
return result.RowsAffected > 0
|
||||
}
|
||||
|
||||
func GetInvitationCode(code string) (*Invitation, error) {
|
||||
invitation := &Invitation{
|
||||
InvitationCode: code,
|
||||
}
|
||||
err := InvitationDB.Model(invitation).Find(invitation).Error
|
||||
return invitation, err
|
||||
}
|
||||
|
||||
func CreateRandomString(strlen int) string {
|
||||
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
bytes := []byte(str)
|
||||
result := []byte{}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < strlen; i++ {
|
||||
result = append(result, bytes[r.Intn(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
func GetInvitationCodes(showNumber, pageNumber, status int32) ([]Invitation, int64, error) {
|
||||
var invitationList []Invitation
|
||||
db := InvitationDB.Model(Invitation{}).Where("status=?", status)
|
||||
var count int64
|
||||
err := db.Count(&count).Error
|
||||
err = db.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).
|
||||
Order("create_time desc").Find(&invitationList).Error
|
||||
return invitationList, count, err
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
package im_mysql_model
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var IPDB *gorm.DB
|
||||
|
||||
type UserIpRecord struct {
|
||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||
CreateIp string `gorm:"column:create_ip;size:15"`
|
||||
LastLoginTime time.Time `gorm:"column:last_login_time"`
|
||||
LastLoginIp string `gorm:"column:last_login_ip;size:15"`
|
||||
LoginTimes int32 `gorm:"column:login_times"`
|
||||
}
|
||||
|
||||
// ip limit login
|
||||
type IpLimit struct {
|
||||
Ip string `gorm:"column:ip;primary_key;size:15"`
|
||||
LimitRegister int32 `gorm:"column:limit_register;size:1"`
|
||||
LimitLogin int32 `gorm:"column:limit_login;size:1"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
LimitTime time.Time `gorm:"column:limit_time"`
|
||||
}
|
||||
|
||||
// ip login
|
||||
type UserIpLimit struct {
|
||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||
Ip string `gorm:"column:ip;primary_key;size:15"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
}
|
||||
|
||||
func IsLimitRegisterIp(RegisterIp string) (bool, error) {
|
||||
//如果已经存在则限制
|
||||
var count int64
|
||||
if err := IPDB.Table("ip_limits").Where("ip=? and limit_register=? and limit_time>now()", RegisterIp, 1).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func IsLimitLoginIp(LoginIp string) (bool, error) {
|
||||
//如果已经存在则限制
|
||||
var count int64
|
||||
if err := IPDB.Table("ip_limits").Where("ip=? and limit_login=? and limit_time>now()", LoginIp, 1).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func IsLimitUserLoginIp(userID string, loginIp string) (limit bool, err error) {
|
||||
//如果已经存在则放行
|
||||
var count int64
|
||||
result := IPDB.Table("user_ip_limits").Where("user_id=?", userID).Count(&count)
|
||||
if err := result.Error; err != nil {
|
||||
return true, err
|
||||
}
|
||||
if count < 1 {
|
||||
return false, nil
|
||||
}
|
||||
result = IPDB.Table("user_ip_limits").Where("user_id=? and ip = ?", userID, loginIp).Count(&count)
|
||||
if err := result.Error; err != nil {
|
||||
return true, err
|
||||
}
|
||||
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func QueryIPLimits(ip string) (*IpLimit, error) {
|
||||
var ipLimit IpLimit
|
||||
err := IPDB.Model(&IpLimit{}).Where("ip=?", ip).First(&ipLimit).Error
|
||||
return &ipLimit, err
|
||||
}
|
||||
|
||||
func QueryUserIPLimits(ip string) ([]UserIpLimit, error) {
|
||||
var ips []UserIpLimit
|
||||
err := IPDB.Model(&UserIpLimit{}).Where("ip=?", ip).Find(&ips).Error
|
||||
return ips, err
|
||||
}
|
||||
|
||||
func InsertOneIntoIpLimits(ipLimits IpLimit) error {
|
||||
return IPDB.Model(&IpLimit{}).Create(ipLimits).Error
|
||||
}
|
||||
|
||||
func DeleteOneFromIpLimits(ip string) error {
|
||||
ipLimits := &IpLimit{Ip: ip}
|
||||
return IPDB.Model(ipLimits).Where("ip=?", ip).Delete(ipLimits).Error
|
||||
}
|
||||
|
||||
func GetIpLimitsLoginByUserID(userID string) ([]UserIpLimit, error) {
|
||||
var ips []UserIpLimit
|
||||
err := IPDB.Model(&UserIpLimit{}).Where("user_id=?", userID).Find(&ips).Error
|
||||
return ips, err
|
||||
}
|
||||
|
||||
func InsertUserIpLimitsLogin(userIp *UserIpLimit) error {
|
||||
userIp.CreateTime = time.Now()
|
||||
return IPDB.Model(&UserIpLimit{}).Create(userIp).Error
|
||||
}
|
||||
|
||||
func DeleteUserIpLimitsLogin(userID, ip string) error {
|
||||
userIp := UserIpLimit{UserID: userID, Ip: ip}
|
||||
return IPDB.Model(&UserIpLimit{}).Delete(&userIp).Error
|
||||
}
|
||||
|
||||
func GetRegisterUserNum(ip string) ([]string, error) {
|
||||
var userIDList []string
|
||||
err := IPDB.Model(&Register{}).Where("register_ip=?", ip).Pluck("user_id", &userIDList).Error
|
||||
return userIDList, err
|
||||
}
|
||||
|
||||
func InsertIpRecord(userID, createIp string) error {
|
||||
record := &UserIpRecord{UserID: userID, CreateIp: createIp, LastLoginTime: time.Now(), LoginTimes: 1}
|
||||
err := IPDB.Model(&UserIpRecord{}).Create(record).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateIpReocord(userID, ip string) (err error) {
|
||||
record := &UserIpRecord{UserID: userID, LastLoginIp: ip, LastLoginTime: time.Now()}
|
||||
result := IPDB.Model(&UserIpRecord{}).Where("user_id=?", userID).Updates(record).Update("login_times", gorm.Expr("login_times+?", 1))
|
||||
if result.Error != nil {
|
||||
return utils.Wrap(result.Error, "")
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
err = InsertIpRecord(userID, ip)
|
||||
}
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
package im_mysql_model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//type Register struct {
|
||||
// Account string `gorm:"column:account;primary_key;type:char(255)" json:"account"`
|
||||
// Password string `gorm:"column:password;type:varchar(255)" json:"password"`
|
||||
@@ -277,82 +273,3 @@ import (
|
||||
//func (Conversation) TableName() string {
|
||||
// return "conversations"
|
||||
//}
|
||||
|
||||
type Department struct {
|
||||
DepartmentID string `gorm:"column:department_id;primary_key;size:64" json:"departmentID"`
|
||||
FaceURL string `gorm:"column:face_url;size:255" json:"faceURL"`
|
||||
Name string `gorm:"column:name;size:256" json:"name" binding:"required"`
|
||||
ParentID string `gorm:"column:parent_id;size:64" json:"parentID" binding:"required"` // "0" or Real parent id
|
||||
Order int32 `gorm:"column:order" json:"order" ` // 1, 2, ...
|
||||
DepartmentType int32 `gorm:"column:department_type" json:"departmentType"` //1, 2...
|
||||
RelatedGroupID string `gorm:"column:related_group_id;size:64" json:"relatedGroupID"`
|
||||
CreateTime time.Time `gorm:"column:create_time" json:"createTime"`
|
||||
Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"`
|
||||
}
|
||||
|
||||
func (Department) TableName() string {
|
||||
return "departments"
|
||||
}
|
||||
|
||||
type OrganizationUser struct {
|
||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||
Nickname string `gorm:"column:nickname;size:256"`
|
||||
EnglishName string `gorm:"column:english_name;size:256"`
|
||||
FaceURL string `gorm:"column:face_url;size:256"`
|
||||
Gender int32 `gorm:"column:gender"` //1 ,2
|
||||
Mobile string `gorm:"column:mobile;size:32"`
|
||||
Telephone string `gorm:"column:telephone;size:32"`
|
||||
Birth time.Time `gorm:"column:birth"`
|
||||
Email string `gorm:"column:email;size:64"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
Ex string `gorm:"column:ex;size:1024"`
|
||||
}
|
||||
|
||||
func (OrganizationUser) TableName() string {
|
||||
return "organization_users"
|
||||
}
|
||||
|
||||
type DepartmentMember struct {
|
||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||
DepartmentID string `gorm:"column:department_id;primary_key;size:64"`
|
||||
Order int32 `gorm:"column:order" json:"order"` //1,2
|
||||
Position string `gorm:"column:position;size:256" json:"position"`
|
||||
Leader int32 `gorm:"column:leader" json:"leader"` //-1, 1
|
||||
Status int32 `gorm:"column:status" json:"status"` //-1, 1
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"`
|
||||
}
|
||||
|
||||
func (DepartmentMember) TableName() string {
|
||||
return "department_members"
|
||||
}
|
||||
|
||||
type AppVersion struct {
|
||||
Version string `gorm:"column:version;size:64" json:"version"`
|
||||
Type int `gorm:"column:type;primary_key" json:"type"`
|
||||
UpdateTime int `gorm:"column:update_time" json:"update_time"`
|
||||
ForceUpdate bool `gorm:"column:force_update" json:"force_update"`
|
||||
FileName string `gorm:"column:file_name" json:"file_name"`
|
||||
YamlName string `gorm:"column:yaml_name" json:"yaml_name"`
|
||||
UpdateLog string `gorm:"column:update_log" json:"update_log"`
|
||||
}
|
||||
|
||||
func (AppVersion) TableName() string {
|
||||
return "app_version"
|
||||
}
|
||||
|
||||
type RegisterAddFriend struct {
|
||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||
}
|
||||
|
||||
func (RegisterAddFriend) TableName() string {
|
||||
return "register_add_friend"
|
||||
}
|
||||
|
||||
type ClientInitConfig struct {
|
||||
DiscoverPageURL string `gorm:"column:discover_page_url;size:64" json:"version"`
|
||||
}
|
||||
|
||||
func (ClientInitConfig) TableName() string {
|
||||
return "client_init_config"
|
||||
}
|
||||
|
||||
@@ -1,224 +0,0 @@
|
||||
package im_mysql_model
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
var OrgDB *gorm.DB
|
||||
|
||||
func CreateDepartment(department *Department) error {
|
||||
department.CreateTime = time.Now()
|
||||
return OrgDB.Table("departments").Create(department).Error
|
||||
}
|
||||
|
||||
func GetDepartment(departmentID string) (*Department, error) {
|
||||
var department Department
|
||||
err := OrgDB.Table("departments").Where("department_id=?", departmentID).Find(&department).Error
|
||||
return &department, err
|
||||
}
|
||||
|
||||
func UpdateDepartment(department *Department, args map[string]interface{}) error {
|
||||
if err := OrgDB.Table("departments").Where("department_id=?", department.DepartmentID).Updates(department).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if args != nil {
|
||||
return OrgDB.Table("departments").Where("department_id=?", department.DepartmentID).Updates(args).Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetSubDepartmentList(departmentID string) ([]Department, error) {
|
||||
var departmentList []Department
|
||||
var err error
|
||||
if departmentID == "-1" {
|
||||
err = OrgDB.Table("departments").Find(&departmentList).Error
|
||||
} else {
|
||||
err = OrgDB.Table("departments").Where("parent_id=?", departmentID).Find(&departmentList).Error
|
||||
}
|
||||
|
||||
return departmentList, err
|
||||
}
|
||||
|
||||
func DeleteDepartment(departmentID string) error {
|
||||
var err error
|
||||
if err = OrgDB.Table("departments").Where("department_id=?", departmentID).Delete(Department{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err = OrgDB.Table("department_members").Where("department_id=?", departmentID).Delete(DepartmentMember{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateOrganizationUser(organizationUser *OrganizationUser) error {
|
||||
organizationUser.CreateTime = time.Now()
|
||||
return OrgDB.Table("organization_users").Create(organizationUser).Error
|
||||
}
|
||||
|
||||
func GetOrganizationUser(userID string) (error, *OrganizationUser) {
|
||||
organizationUser := OrganizationUser{}
|
||||
err := OrgDB.Table("organization_users").Where("user_id=?", userID).Take(&organizationUser).Error
|
||||
return err, &organizationUser
|
||||
}
|
||||
|
||||
func GetOrganizationUsers(userIDList []string) ([]*OrganizationUser, error) {
|
||||
var organizationUserList []*OrganizationUser
|
||||
err := OrgDB.Table("organization_users").Where("user_id in (?)", userIDList).Find(&organizationUserList).Error
|
||||
return organizationUserList, err
|
||||
}
|
||||
|
||||
func UpdateOrganizationUser(organizationUser *OrganizationUser, args map[string]interface{}) error {
|
||||
if err := OrgDB.Table("organization_users").Where("user_id=?", organizationUser.UserID).Updates(organizationUser).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if args != nil {
|
||||
return OrgDB.Table("organization_users").Where("user_id=?", organizationUser.UserID).Updates(args).Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDepartmentMember(departmentMember *DepartmentMember) error {
|
||||
departmentMember.CreateTime = time.Now()
|
||||
return OrgDB.Table("department_members").Create(departmentMember).Error
|
||||
}
|
||||
|
||||
func GetUserInDepartment(userID string) (error, []DepartmentMember) {
|
||||
var departmentMemberList []DepartmentMember
|
||||
err := OrgDB.Where("user_id=?", userID).Find(&departmentMemberList).Error
|
||||
return err, departmentMemberList
|
||||
}
|
||||
|
||||
func UpdateUserInDepartment(departmentMember *DepartmentMember, args map[string]interface{}) error {
|
||||
if err := OrgDB.Where("department_id=? AND user_id=?", departmentMember.DepartmentID, departmentMember.UserID).
|
||||
Updates(departmentMember).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if args != nil {
|
||||
return OrgDB.Where("department_id=? AND user_id=?", departmentMember.DepartmentID, departmentMember.UserID).
|
||||
Updates(args).Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteUserInDepartment(departmentID, userID string) error {
|
||||
return OrgDB.Table("department_members").Where("department_id=? AND user_id=?", departmentID, userID).Delete(DepartmentMember{}).Error
|
||||
}
|
||||
|
||||
func DeleteUserInAllDepartment(userID string) error {
|
||||
return OrgDB.Table("department_members").Where("user_id=?", userID).Delete(DepartmentMember{}).Error
|
||||
}
|
||||
|
||||
func DeleteOrganizationUser(OrganizationUserID string) error {
|
||||
if err := DeleteUserInAllDepartment(OrganizationUserID); err != nil {
|
||||
return err
|
||||
}
|
||||
return OrgDB.Table("organization_users").Where("user_id=?", OrganizationUserID).Delete(OrganizationUser{}).Error
|
||||
}
|
||||
|
||||
func GetDepartmentMemberUserIDList(departmentID string) (error, []string) {
|
||||
var departmentMemberList []DepartmentMember
|
||||
err := OrgDB.Table("department_members").Where("department_id=?", departmentID).Take(&departmentMemberList).Error
|
||||
if err != nil {
|
||||
return err, nil
|
||||
}
|
||||
var userIDList []string = make([]string, 0)
|
||||
for _, v := range departmentMemberList {
|
||||
userIDList = append(userIDList, v.UserID)
|
||||
}
|
||||
return err, userIDList
|
||||
}
|
||||
|
||||
func GetDepartmentMemberList(departmentID string) ([]DepartmentMember, error) {
|
||||
var departmentMemberList []DepartmentMember
|
||||
var err error
|
||||
if departmentID == "-1" {
|
||||
err = OrgDB.Table("department_members").Find(&departmentMemberList).Error
|
||||
} else {
|
||||
err = OrgDB.Table("department_members").Where("department_id=?", departmentID).Find(&departmentMemberList).Error
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return departmentMemberList, err
|
||||
}
|
||||
|
||||
func GetAllOrganizationUserID() (error, []string) {
|
||||
var OrganizationUser OrganizationUser
|
||||
var result []string
|
||||
return OrgDB.Model(&OrganizationUser).Pluck("user_id", &result).Error, result
|
||||
}
|
||||
|
||||
func GetDepartmentMemberNum(departmentID string) (error, uint32) {
|
||||
var number int64
|
||||
err := OrgDB.Table("department_members").Where("department_id=?", departmentID).Count(&number).Error
|
||||
if err != nil {
|
||||
return utils.Wrap(err, ""), 0
|
||||
}
|
||||
return nil, uint32(number)
|
||||
|
||||
}
|
||||
|
||||
func GetSubDepartmentNum(departmentID string) (error, uint32) {
|
||||
var number int64
|
||||
err := OrgDB.Table("departments").Where("parent_id=?", departmentID).Count(&number).Error
|
||||
if err != nil {
|
||||
return utils.Wrap(err, ""), 0
|
||||
}
|
||||
return nil, uint32(number)
|
||||
}
|
||||
|
||||
func SetDepartmentRelatedGroupID(groupID, departmentID string) error {
|
||||
department := &Department{RelatedGroupID: groupID}
|
||||
return OrgDB.Model(&department).Where("department_id=?", departmentID).Updates(department).Error
|
||||
}
|
||||
|
||||
func GetDepartmentRelatedGroupIDList(departmentIDList []string) ([]string, error) {
|
||||
var groupIDList []string
|
||||
err := OrgDB.Table("departments").Where("department_id IN (?) ", departmentIDList).Pluck("related_group_id", &groupIDList).Error
|
||||
return groupIDList, err
|
||||
}
|
||||
|
||||
func getDepartmentParent(departmentID string, dbConn *gorm.DB) (*Department, error) {
|
||||
var department Department
|
||||
var parentDepartment Department
|
||||
//var parentID string
|
||||
err := OrgDB.Model(&department).Where("department_id=?", departmentID).Select("parent_id").First(&department).Error
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
if department.ParentID != "" {
|
||||
err = dbConn.Model(&parentDepartment).Where("department_id = ?", department.ParentID).Find(&parentDepartment).Error
|
||||
}
|
||||
return &parentDepartment, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func GetDepartmentParent(departmentID string, dbConn *gorm.DB, parentIDList *[]string) error {
|
||||
department, err := getDepartmentParent(departmentID, dbConn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if department.DepartmentID != "" {
|
||||
*parentIDList = append(*parentIDList, department.DepartmentID)
|
||||
err = GetDepartmentParent(department.DepartmentID, dbConn, parentIDList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDepartmentParentIDList(departmentID string) ([]string, error) {
|
||||
dbConn := OrgDB
|
||||
var parentIDList []string
|
||||
err := GetDepartmentParent(departmentID, dbConn, &parentIDList)
|
||||
return parentIDList, err
|
||||
}
|
||||
|
||||
func GetRandomDepartmentID() (string, error) {
|
||||
department := &Department{}
|
||||
err := OrgDB.Model(department).Order("RAND()").Where("related_group_id != ? AND department_id != ? AND department_type = ?", "", "0", 1).First(department).Error
|
||||
return department.DepartmentID, err
|
||||
}
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var BlackDB *gorm.DB
|
||||
|
||||
type Black struct {
|
||||
OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"`
|
||||
BlockUserID string `gorm:"column:block_user_id;primary_key;size:64"`
|
||||
@@ -17,37 +15,42 @@ type Black struct {
|
||||
AddSource int32 `gorm:"column:add_source"`
|
||||
OperatorUserID string `gorm:"column:operator_user_id;size:64"`
|
||||
Ex string `gorm:"column:ex;size:1024"`
|
||||
db *gorm.DB `gorm:"-"`
|
||||
}
|
||||
|
||||
func (*Black) Create(ctx context.Context, blacks []*Black) (err error) {
|
||||
func NewBlack(db *gorm.DB) *Black {
|
||||
return &Black{db: db}
|
||||
}
|
||||
|
||||
func (b *Black) Create(ctx context.Context, blacks []*Black) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
|
||||
}()
|
||||
return utils.Wrap(BlackDB.Create(&blacks).Error, "")
|
||||
return utils.Wrap(b.db.Create(&blacks).Error, "")
|
||||
}
|
||||
|
||||
func (*Black) Delete(ctx context.Context, blacks []*Black) (err error) {
|
||||
func (b *Black) Delete(ctx context.Context, blacks []*Black) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
|
||||
}()
|
||||
return utils.Wrap(GroupMemberDB.Delete(blacks).Error, "")
|
||||
}
|
||||
|
||||
func (*Black) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) {
|
||||
func (b *Black) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blockUserID", blockUserID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(BlackDB.Where("block_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Updates(args).Error, "")
|
||||
return utils.Wrap(b.db.Where("block_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (*Black) Update(ctx context.Context, blacks []*Black) (err error) {
|
||||
func (b *Black) Update(ctx context.Context, blacks []*Black) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
|
||||
}()
|
||||
return utils.Wrap(BlackDB.Updates(&blacks).Error, "")
|
||||
return utils.Wrap(b.db.Updates(&blacks).Error, "")
|
||||
}
|
||||
|
||||
func (*Black) Find(ctx context.Context, blacks []Black) (blackList []*Black, err error) {
|
||||
func (b *Black) Find(ctx context.Context, blacks []Black) (blackList []*Black, err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks, "blackList", blackList)
|
||||
}()
|
||||
@@ -58,15 +61,15 @@ func (*Black) Find(ctx context.Context, blacks []Black) (blackList []*Black, err
|
||||
return blackList, utils.Wrap(GroupMemberDB.Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error, "")
|
||||
}
|
||||
|
||||
func (*Black) Take(ctx context.Context, blackID string) (black *Black, err error) {
|
||||
func (b *Black) Take(ctx context.Context, blackID string) (black *Black, err error) {
|
||||
black = &Black{}
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blackID", blackID, "black", *black)
|
||||
}()
|
||||
return black, utils.Wrap(BlackDB.Where("black_id = ?", blackID).Take(black).Error, "")
|
||||
return black, utils.Wrap(b.db.Where("black_id = ?", blackID).Take(black).Error, "")
|
||||
}
|
||||
|
||||
func (*Black) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*Black, err error) {
|
||||
func (b *Black) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*Black, err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blackList", blackList)
|
||||
}()
|
||||
@@ -101,7 +104,7 @@ func GetBlackListByUserID(ownerUserID string) ([]Black, error) {
|
||||
|
||||
func GetBlackIDListByUserID(ownerUserID string) ([]string, error) {
|
||||
var blackIDList []string
|
||||
err := BlackDB.Where("owner_user_id=?", ownerUserID).Pluck("block_user_id", &blackIDList).Error
|
||||
err := b.db.Where("owner_user_id=?", ownerUserID).Pluck("block_user_id", &blackIDList).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -27,8 +27,6 @@ const (
|
||||
groupMemberInfoCache = "GROUP_MEMBER_INFO_CACHE:"
|
||||
groupAllMemberInfoCache = "GROUP_ALL_MEMBER_INFO_CACHE:"
|
||||
allFriendInfoCache = "ALL_FRIEND_INFO_CACHE:"
|
||||
allDepartmentCache = "ALL_DEPARTMENT_CACHE:"
|
||||
allDepartmentMemberCache = "ALL_DEPARTMENT_MEMBER_CACHE:"
|
||||
joinedSuperGroupListCache = "JOINED_SUPER_GROUP_LIST_CACHE:"
|
||||
groupMemberListHashCache = "GROUP_MEMBER_LIST_HASH_CACHE:"
|
||||
groupMemberNumCache = "GROUP_MEMBER_NUM_CACHE:"
|
||||
@@ -39,7 +37,7 @@ const (
|
||||
)
|
||||
|
||||
func DelKeys() {
|
||||
fmt.Println("init to del old keys")
|
||||
fmt.Println("cache init to del old keys")
|
||||
for _, key := range []string{groupCache, friendRelationCache, blackListCache, userInfoCache, groupInfoCache, groupOwnerIDCache, joinedGroupListCache,
|
||||
groupMemberInfoCache, groupAllMemberInfoCache, allFriendInfoCache} {
|
||||
fName := utils.GetSelfFuncName()
|
||||
@@ -100,27 +98,27 @@ func DelFriendIDListFromCache(ctx context.Context, userID string) (err error) {
|
||||
return db.DB.Rc.TagAsDeleted(friendRelationCache + userID)
|
||||
}
|
||||
|
||||
func GetBlackListFromCache(ctx context.Context, userID string) (blackIDList []string, err error) {
|
||||
func GetBlackListFromCache(ctx context.Context, userID string) (blackIDs []string, err error) {
|
||||
getBlackIDList := func() (string, error) {
|
||||
blackIDList, err := imdb.GetBlackIDListByUserID(userID)
|
||||
blackIDs, err := imdb.GetBlackIDListByUserID(userID)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
bytes, err := json.Marshal(blackIDList)
|
||||
bytes, err := json.Marshal(blackIDs)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "blackIDList", blackIDList)
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "blackIDList", blackIDs)
|
||||
}()
|
||||
blackIDListStr, err := db.DB.Rc.Fetch(blackListCache+userID, time.Second*30*60, getBlackIDList)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
err = json.Unmarshal([]byte(blackIDListStr), &blackIDList)
|
||||
return blackIDList, utils.Wrap(err, "")
|
||||
err = json.Unmarshal([]byte(blackIDListStr), &blackIDs)
|
||||
return blackIDs, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func DelBlackIDListFromCache(ctx context.Context, userID string) (err error) {
|
||||
@@ -415,56 +413,6 @@ func DelAllFriendsInfoFromCache(ctx context.Context, userID string) (err error)
|
||||
return db.DB.Rc.TagAsDeleted(allFriendInfoCache + userID)
|
||||
}
|
||||
|
||||
func GetAllDepartmentsFromCache() ([]imdb.Department, error) {
|
||||
getAllDepartments := func() (string, error) {
|
||||
departmentList, err := imdb.GetSubDepartmentList("-1")
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
bytes, err := json.Marshal(departmentList)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
allDepartmentsStr, err := db.DB.Rc.Fetch(allDepartmentCache, time.Second*30*60, getAllDepartments)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
var allDepartments []imdb.Department
|
||||
err = json.Unmarshal([]byte(allDepartmentsStr), &allDepartments)
|
||||
return allDepartments, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func DelAllDepartmentsFromCache() error {
|
||||
return db.DB.Rc.TagAsDeleted(allDepartmentCache)
|
||||
}
|
||||
|
||||
func GetAllDepartmentMembersFromCache() ([]imdb.DepartmentMember, error) {
|
||||
getAllDepartmentMembers := func() (string, error) {
|
||||
departmentMembers, err := imdb.GetDepartmentMemberList("-1")
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
bytes, err := json.Marshal(departmentMembers)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
allDepartmentMembersStr, err := db.DB.Rc.Fetch(allDepartmentMemberCache, time.Second*30*60, getAllDepartmentMembers)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
var allDepartmentMembers []imdb.DepartmentMember
|
||||
err = json.Unmarshal([]byte(allDepartmentMembersStr), &allDepartmentMembers)
|
||||
return allDepartmentMembers, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func DelAllDepartmentMembersFromCache() error {
|
||||
return db.DB.Rc.TagAsDeleted(allDepartmentMemberCache)
|
||||
}
|
||||
|
||||
func GetJoinedSuperGroupListFromCache(ctx context.Context, userID string) (joinedSuperGroupIDs []string, err error) {
|
||||
getJoinedSuperGroupIDList := func() (string, error) {
|
||||
userToSuperGroup, err := db.DB.GetSuperGroupByUserID(userID)
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
cbApi "Open_IM/pkg/call_back_struct"
|
||||
cbApi "Open_IM/pkg/callback_struct"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
@@ -58,19 +59,19 @@ func Post(url string, data interface{}, timeOutSecond int) (content []byte, err
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func CallBackPostReturn(url, callbackCommand string, input interface{}, output cbApi.CallbackResp, timeOut int, failedContinue *bool) error {
|
||||
func CallBackPostReturn(url, callbackCommand string, input interface{}, output cbApi.CallbackResp, callbackConfig config.CallBackConfig) error {
|
||||
v := urlLib.Values{}
|
||||
v.Set("callbackCommand", callbackCommand)
|
||||
url = url + "?" + v.Encode()
|
||||
b, err := Post(url, input, timeOut)
|
||||
b, err := Post(url, input, callbackConfig.CallbackTimeOut)
|
||||
if err != nil {
|
||||
if failedContinue != nil && *failedContinue {
|
||||
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
||||
return constant.ErrCallbackContinue
|
||||
}
|
||||
return constant.NewErrNetwork(err)
|
||||
}
|
||||
if err = json.Unmarshal(b, output); err != nil {
|
||||
if failedContinue != nil && *failedContinue {
|
||||
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
||||
return constant.ErrCallbackContinue
|
||||
}
|
||||
return constant.NewErrData(err)
|
||||
|
||||
Reference in New Issue
Block a user