mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-01 07:35:58 +08:00
del files
This commit is contained in:
+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{
|
||||
|
||||
@@ -25,6 +25,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.GetSelfFuncName(), 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
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user