复制项目
This commit is contained in:
519
pkg/protocol/chat/chat.go
Normal file
519
pkg/protocol/chat/chat.go
Normal file
@@ -0,0 +1,519 @@
|
||||
// Copyright © 2023 OpenIM open source community. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chat
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"git.imall.cloud/openim/chat/pkg/common/constant"
|
||||
constantpb "git.imall.cloud/openim/protocol/constant"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
)
|
||||
|
||||
func (x *UpdateUserInfoReq) Check() error {
|
||||
if x.UserID == "" {
|
||||
return errs.ErrArgs.WrapMsg("userID is empty")
|
||||
}
|
||||
if x.Email != nil && x.Email.Value != "" {
|
||||
if err := EmailCheck(x.Email.Value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *FindUserPublicInfoReq) Check() error {
|
||||
if x.UserIDs == nil {
|
||||
return errs.ErrArgs.WrapMsg("userIDs is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *SearchUserPublicInfoReq) Check() error {
|
||||
if x.Pagination == nil {
|
||||
return errs.ErrArgs.WrapMsg("pagination is empty")
|
||||
}
|
||||
if x.Pagination.PageNumber < 1 {
|
||||
return errs.ErrArgs.WrapMsg("pageNumber is invalid")
|
||||
}
|
||||
if x.Pagination.ShowNumber < 1 {
|
||||
return errs.ErrArgs.WrapMsg("showNumber is invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *FindUserFullInfoReq) Check() error {
|
||||
if x.UserIDs == nil {
|
||||
return errs.ErrArgs.WrapMsg("userIDs is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *SendVerifyCodeReq) Check() error {
|
||||
if x.UsedFor < constant.VerificationCodeForRegister || x.UsedFor > constant.VerificationCodeForH5Register {
|
||||
return errs.ErrArgs.WrapMsg("usedFor flied is empty")
|
||||
}
|
||||
if x.Email == "" {
|
||||
if x.AreaCode == "" {
|
||||
return errs.ErrArgs.WrapMsg("AreaCode is empty")
|
||||
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
|
||||
return err
|
||||
}
|
||||
if x.PhoneNumber == "" {
|
||||
return errs.ErrArgs.WrapMsg("PhoneNumber is empty")
|
||||
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := EmailCheck(x.Email); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *VerifyCodeReq) Check() error {
|
||||
// 如果提供了account,验证account格式,此时不需要AreaCode和PhoneNumber
|
||||
if x.Account != "" {
|
||||
if err := AccountCheck(x.Account); err != nil {
|
||||
return err
|
||||
}
|
||||
// account验证时,VerifyCode是必需的
|
||||
if x.VerifyCode == "" {
|
||||
return errs.ErrArgs.WrapMsg("VerifyCode is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果没有提供account,则验证phone或email
|
||||
if x.Email == "" {
|
||||
if x.AreaCode == "" {
|
||||
return errs.ErrArgs.WrapMsg("AreaCode is empty")
|
||||
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
|
||||
return err
|
||||
}
|
||||
if x.PhoneNumber == "" {
|
||||
return errs.ErrArgs.WrapMsg("PhoneNumber is empty")
|
||||
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := EmailCheck(x.Email); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if x.VerifyCode == "" {
|
||||
return errs.ErrArgs.WrapMsg("VerifyCode is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *RegisterUserReq) Check() error {
|
||||
//if x.VerifyCode == "" {
|
||||
// return errs.ErrArgs.WrapMsg("VerifyCode is empty")
|
||||
//}
|
||||
if x.User.Nickname == "" {
|
||||
return errs.ErrArgs.WrapMsg("Nickname is nil")
|
||||
}
|
||||
if x.Platform < constantpb.IOSPlatformID || x.Platform > constantpb.HarmonyOSPlatformID {
|
||||
return errs.ErrArgs.WrapMsg("platform is invalid")
|
||||
}
|
||||
if x.User == nil {
|
||||
return errs.ErrArgs.WrapMsg("user is empty")
|
||||
}
|
||||
|
||||
// 如果提供了account,验证account格式,此时不需要AreaCode和PhoneNumber
|
||||
if x.User.Account != "" {
|
||||
if err := AccountCheck(x.User.Account); err != nil {
|
||||
return err
|
||||
}
|
||||
// account注册时,不需要验证phone和email
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果没有提供account,则验证phone或email
|
||||
if x.User.Email == "" {
|
||||
if x.User.AreaCode == "" {
|
||||
return errs.ErrArgs.WrapMsg("AreaCode is empty")
|
||||
} else if err := AreaCodeCheck(x.User.AreaCode); err != nil {
|
||||
return err
|
||||
}
|
||||
if x.User.PhoneNumber == "" {
|
||||
return errs.ErrArgs.WrapMsg("PhoneNumber is empty")
|
||||
} else if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := EmailCheck(x.User.Email); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *LoginReq) Check() error {
|
||||
if x.Platform < constantpb.IOSPlatformID || x.Platform > constantpb.HarmonyOSPlatformID {
|
||||
return errs.ErrArgs.WrapMsg("platform is invalid")
|
||||
}
|
||||
// 支持三种登录方式:account、phone、email
|
||||
if x.Account != "" {
|
||||
// 使用account登录,不需要验证phone和email
|
||||
return nil
|
||||
} else if x.Email != "" {
|
||||
// 使用email登录
|
||||
if err := EmailCheck(x.Email); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// 使用phone登录
|
||||
if x.AreaCode == "" {
|
||||
return errs.ErrArgs.WrapMsg("AreaCode is empty")
|
||||
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
|
||||
return err
|
||||
}
|
||||
if x.PhoneNumber == "" {
|
||||
return errs.ErrArgs.WrapMsg("PhoneNumber is empty")
|
||||
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *ResetPasswordReq) Check() error {
|
||||
if x.Password == "" {
|
||||
return errs.ErrArgs.WrapMsg("password is empty")
|
||||
}
|
||||
if x.Email == "" {
|
||||
if x.AreaCode == "" {
|
||||
return errs.ErrArgs.WrapMsg("AreaCode is empty")
|
||||
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
|
||||
return err
|
||||
}
|
||||
if x.PhoneNumber == "" {
|
||||
return errs.ErrArgs.WrapMsg("PhoneNumber is empty")
|
||||
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := EmailCheck(x.Email); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if x.VerifyCode == "" {
|
||||
return errs.ErrArgs.WrapMsg("VerifyCode is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *ChangePasswordReq) Check() error {
|
||||
if x.UserID == "" {
|
||||
return errs.ErrArgs.WrapMsg("userID is empty")
|
||||
}
|
||||
|
||||
if x.NewPassword == "" {
|
||||
return errs.ErrArgs.WrapMsg("newPassword is empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *FindUserAccountReq) Check() error {
|
||||
if x.UserIDs == nil {
|
||||
return errs.ErrArgs.WrapMsg("userIDs is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *FindAccountUserReq) Check() error {
|
||||
if x.Accounts == nil {
|
||||
return errs.ErrArgs.WrapMsg("Accounts is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *SearchUserFullInfoReq) Check() error {
|
||||
if x.Pagination == nil {
|
||||
return errs.ErrArgs.WrapMsg("pagination is empty")
|
||||
}
|
||||
if x.Pagination.PageNumber < 1 {
|
||||
return errs.ErrArgs.WrapMsg("pageNumber is invalid")
|
||||
}
|
||||
if x.Pagination.ShowNumber < 1 {
|
||||
return errs.ErrArgs.WrapMsg("showNumber is invalid")
|
||||
}
|
||||
if x.Normal < constant.FinDAllUser || x.Normal > constant.FindNormalUser {
|
||||
return errs.ErrArgs.WrapMsg("normal flied is invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *GetTokenForVideoMeetingReq) Check() error {
|
||||
if x.Room == "" {
|
||||
errs.ErrArgs.WrapMsg("Room is empty")
|
||||
}
|
||||
if x.Identity == "" {
|
||||
errs.ErrArgs.WrapMsg("User Identity is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func EmailCheck(email string) error {
|
||||
pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
|
||||
if err := regexMatch(pattern, email); err != nil {
|
||||
return errs.WrapMsg(err, "Email is invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AreaCodeCheck(areaCode string) error {
|
||||
//pattern := `\+[1-9][0-9]{1,2}`
|
||||
//if err := regexMatch(pattern, areaCode); err != nil {
|
||||
// return errs.WrapMsg(err, "AreaCode is invalid")
|
||||
//}
|
||||
return nil
|
||||
}
|
||||
|
||||
func PhoneNumberCheck(phoneNumber string) error {
|
||||
if phoneNumber == "" {
|
||||
return errs.ErrArgs.WrapMsg("phoneNumber is empty")
|
||||
}
|
||||
_, err := strconv.ParseUint(phoneNumber, 10, 64)
|
||||
if err != nil {
|
||||
return errs.ErrArgs.WrapMsg("phoneNumber is invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AccountCheck(account string) error {
|
||||
if account == "" {
|
||||
return errs.ErrArgs.WrapMsg("account is empty")
|
||||
}
|
||||
// 验证长度:6到20位
|
||||
if len(account) < 6 || len(account) > 20 {
|
||||
return errs.ErrArgs.WrapMsg("account must be between 6 and 20 characters")
|
||||
}
|
||||
// 验证格式:只能包含数字、字母、下划线(_)、横线(-)
|
||||
pattern := `^[a-zA-Z0-9_-]+$`
|
||||
if err := regexMatch(pattern, account); err != nil {
|
||||
return errs.WrapMsg(err, "account must contain only letters, numbers, underscores, and hyphens")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func regexMatch(pattern string, target string) error {
|
||||
reg := regexp.MustCompile(pattern)
|
||||
ok := reg.MatchString(target)
|
||||
if !ok {
|
||||
return errs.ErrArgs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *SearchUserInfoReq) Check() error {
|
||||
if x.Pagination == nil {
|
||||
return errs.ErrArgs.WrapMsg("Pagination is nil")
|
||||
}
|
||||
if x.Pagination.PageNumber < 1 {
|
||||
return errs.ErrArgs.WrapMsg("pageNumber is invalid")
|
||||
}
|
||||
if x.Pagination.ShowNumber < 1 {
|
||||
return errs.ErrArgs.WrapMsg("showNumber is invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *AddUserAccountReq) Check() error {
|
||||
if x.User == nil {
|
||||
return errs.ErrArgs.WrapMsg("user is empty")
|
||||
}
|
||||
|
||||
if x.User.Email == "" {
|
||||
if x.User.AreaCode == "" || x.User.PhoneNumber == "" {
|
||||
return errs.ErrArgs.WrapMsg("area code or phone number is empty")
|
||||
}
|
||||
if x.User.AreaCode[0] != '+' {
|
||||
x.User.AreaCode = "+" + x.User.AreaCode
|
||||
}
|
||||
if _, err := strconv.ParseUint(x.User.AreaCode[1:], 10, 64); err != nil {
|
||||
return errs.ErrArgs.WrapMsg("area code must be number")
|
||||
}
|
||||
if _, err := strconv.ParseUint(x.User.PhoneNumber, 10, 64); err != nil {
|
||||
return errs.ErrArgs.WrapMsg("phone number must be number")
|
||||
}
|
||||
} else {
|
||||
if err := EmailCheck(x.User.Email); err != nil {
|
||||
return errs.ErrArgs.WrapMsg("email must be right")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 敏感词检测相关 Check() 方法
|
||||
func (x *GetSensitiveWordsReq) Check() error {
|
||||
// 获取敏感词列表不需要参数验证
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *CheckSensitiveWordsReq) Check() error {
|
||||
if x.Content == "" {
|
||||
return errs.ErrArgs.WrapMsg("content is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ==================== 敏感词管理相关 Check() 方法 ====================
|
||||
|
||||
func (x *AddSensitiveWordReq) Check() error {
|
||||
if x.Word == "" {
|
||||
return errs.ErrArgs.WrapMsg("word is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *UpdateSensitiveWordReq) Check() error {
|
||||
if x.Id == "" {
|
||||
return errs.ErrArgs.WrapMsg("id is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *DeleteSensitiveWordReq) Check() error {
|
||||
if len(x.Ids) == 0 {
|
||||
return errs.ErrArgs.WrapMsg("ids is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *GetSensitiveWordReq) Check() error {
|
||||
if x.Id == "" {
|
||||
return errs.ErrArgs.WrapMsg("id is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *SearchSensitiveWordsReq) Check() error {
|
||||
if x.Pagination == nil {
|
||||
return errs.ErrArgs.WrapMsg("pagination is nil")
|
||||
}
|
||||
if x.Pagination.ShowNumber == 0 {
|
||||
return errs.ErrArgs.WrapMsg("showNumber is empty")
|
||||
}
|
||||
if x.Pagination.PageNumber == 0 {
|
||||
return errs.ErrArgs.WrapMsg("pageNumber is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *BatchAddSensitiveWordsReq) Check() error {
|
||||
if len(x.Words) == 0 {
|
||||
return errs.ErrArgs.WrapMsg("words is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *BatchUpdateSensitiveWordsReq) Check() error {
|
||||
if len(x.Updates) == 0 {
|
||||
return errs.ErrArgs.WrapMsg("updates is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *BatchDeleteSensitiveWordsReq) Check() error {
|
||||
if len(x.Ids) == 0 {
|
||||
return errs.ErrArgs.WrapMsg("ids is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *AddSensitiveWordGroupReq) Check() error {
|
||||
if x.Name == "" {
|
||||
return errs.ErrArgs.WrapMsg("name is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *UpdateSensitiveWordGroupReq) Check() error {
|
||||
if x.Id == "" {
|
||||
return errs.ErrArgs.WrapMsg("id is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *DeleteSensitiveWordGroupReq) Check() error {
|
||||
if len(x.Ids) == 0 {
|
||||
return errs.ErrArgs.WrapMsg("ids is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *GetSensitiveWordGroupReq) Check() error {
|
||||
if x.Id == "" {
|
||||
return errs.ErrArgs.WrapMsg("id is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *GetAllSensitiveWordGroupsReq) Check() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *GetSensitiveWordConfigReq) Check() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *UpdateSensitiveWordConfigReq) Check() error {
|
||||
if x.Config == nil {
|
||||
return errs.ErrArgs.WrapMsg("config is nil")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *GetSensitiveWordLogsReq) Check() error {
|
||||
if x.Pagination == nil {
|
||||
return errs.ErrArgs.WrapMsg("pagination is nil")
|
||||
}
|
||||
if x.Pagination.ShowNumber == 0 {
|
||||
return errs.ErrArgs.WrapMsg("showNumber is empty")
|
||||
}
|
||||
if x.Pagination.PageNumber == 0 {
|
||||
return errs.ErrArgs.WrapMsg("pageNumber is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *DeleteSensitiveWordLogsReq) Check() error {
|
||||
if len(x.Ids) == 0 {
|
||||
return errs.ErrArgs.WrapMsg("ids is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *GetSensitiveWordStatsReq) Check() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *GetSensitiveWordLogStatsReq) Check() error {
|
||||
if x.StartTime == 0 || x.EndTime == 0 {
|
||||
return errs.ErrArgs.WrapMsg("startTime or endTime is empty")
|
||||
}
|
||||
if x.StartTime > x.EndTime {
|
||||
return errs.ErrArgs.WrapMsg("startTime must be less than endTime")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user