firend.go 22.8 KB
Newer Older
W
wenxu12345 已提交
1 2 3
package friend

import (
W
wenxu12345 已提交
4
	chat "Open_IM/internal/rpc/msg"
W
wenxu12345 已提交
5 6 7 8 9
	"Open_IM/pkg/common/config"
	"Open_IM/pkg/common/constant"
	imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
	"Open_IM/pkg/common/log"
	"Open_IM/pkg/common/token_verify"
W
wenxu12345 已提交
10
	cp "Open_IM/pkg/common/utils"
W
wenxu12345 已提交
11 12
	"Open_IM/pkg/grpc-etcdv3/getcdv3"
	pbFriend "Open_IM/pkg/proto/friend"
W
wenxu12345 已提交
13
	sdkws "Open_IM/pkg/proto/sdk_ws"
W
wenxu12345 已提交
14 15 16 17 18 19
	"Open_IM/pkg/utils"
	"context"
	"google.golang.org/grpc"
	"net"
	"strconv"
	"strings"
W
wenxu12345 已提交
20
	"time"
W
wenxu12345 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
)

type friendServer struct {
	rpcPort         int
	rpcRegisterName string
	etcdSchema      string
	etcdAddr        []string
}

func NewFriendServer(port int) *friendServer {
	log.NewPrivateLog("friend")
	return &friendServer{
		rpcPort:         port,
		rpcRegisterName: config.Config.RpcRegisterName.OpenImFriendName,
		etcdSchema:      config.Config.Etcd.EtcdSchema,
		etcdAddr:        config.Config.Etcd.EtcdAddr,
	}
}

func (s *friendServer) Run() {
W
wenxu12345 已提交
41
	log.NewInfo("0", "friendServer run...")
W
wenxu12345 已提交
42 43 44 45 46 47

	ip := utils.ServerIP
	registerAddress := ip + ":" + strconv.Itoa(s.rpcPort)
	//listener network
	listener, err := net.Listen("tcp", registerAddress)
	if err != nil {
W
wenxu12345 已提交
48
		log.NewError("0", "Listen failed ", err.Error(), registerAddress)
W
wenxu12345 已提交
49 50
		return
	}
W
wenxu12345 已提交
51
	log.NewInfo("0", "listen ok ", registerAddress)
W
wenxu12345 已提交
52 53 54 55 56 57 58 59
	defer listener.Close()
	//grpc server
	srv := grpc.NewServer()
	defer srv.GracefulStop()
	//User friend related services register to etcd
	pbFriend.RegisterFriendServer(srv, s)
	err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), ip, s.rpcPort, s.rpcRegisterName, 10)
	if err != nil {
W
wenxu12345 已提交
60
		log.NewError("0", "RegisterEtcd failed ", err.Error(), s.etcdSchema, strings.Join(s.etcdAddr, ","), ip, s.rpcPort, s.rpcRegisterName)
W
wenxu12345 已提交
61 62 63 64
		return
	}
	err = srv.Serve(listener)
	if err != nil {
W
wenxu12345 已提交
65
		log.NewError("0", "Serve failed ", err.Error(), listener)
W
wenxu12345 已提交
66 67 68 69
		return
	}
}

W
wenxu12345 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
////
//func (s *friendServer) GetFriendsInfo(ctx context.Context, req *pbFriend.GetFriendsInfoReq) (*pbFriend.GetFriendInfoResp, error) {
//	return nil, nil
////	log.NewInfo(req.CommID.OperationID, "GetFriendsInfo args ", req.String())
////	var (
////		isInBlackList int32
////		//	isFriend      int32
////		comment string
////	)
////
////	friendShip, err := imdb.FindFriendRelationshipFromFriend(req.CommID.FromUserID, req.CommID.ToUserID)
////	if err != nil {
////		log.NewError(req.CommID.OperationID, "FindFriendRelationshipFromFriend failed ", err.Error())
////		return &pbFriend.GetFriendInfoResp{ErrCode: constant.ErrSearchUserInfo.ErrCode, ErrMsg: constant.ErrSearchUserInfo.ErrMsg}, nil
////		//	isFriend = constant.FriendFlag
////	}
////	comment = friendShip.Remark
////
////	friendUserInfo, err := imdb.FindUserByUID(req.CommID.ToUserID)
////	if err != nil {
////		log.NewError(req.CommID.OperationID, "FindUserByUID failed ", err.Error())
////		return &pbFriend.GetFriendInfoResp{ErrCode: constant.ErrSearchUserInfo.ErrCode, ErrMsg: constant.ErrSearchUserInfo.ErrMsg}, nil
////	}
////
////	err = imdb.FindRelationshipFromBlackList(req.CommID.FromUserID, req.CommID.ToUserID)
////	if err == nil {
////		isInBlackList = constant.BlackListFlag
////	}
////
////	resp := pbFriend.GetFriendInfoResp{ErrCode: 0, ErrMsg:  "",}
////
////	utils.CopyStructFields(resp.FriendInfoList, friendUserInfo)
////	resp.Data.IsBlack = isInBlackList
////	resp.Data.OwnerUserID = req.CommID.FromUserID
////	resp.Data.Remark = comment
////	resp.Data.CreateTime = friendUserInfo.CreateTime
////
////	log.NewInfo(req.CommID.OperationID, "GetFriendsInfo ok ", resp)
////	return &resp, nil
////
//}

func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlacklistReq) (*pbFriend.AddBlacklistResp, error) {
W
wenxu12345 已提交
113 114 115
	log.NewInfo(req.CommID.OperationID, "AddBlacklist args ", req.String())
	ok := token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID)
	if !ok {
W
wenxu12345 已提交
116 117
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
		return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
118
	}
W
wenxu12345 已提交
119
	black := imdb.Black{OwnerUserID: req.CommID.FromUserID, BlockUserID: req.CommID.ToUserID, OperatorUserID: req.CommID.OpUserID}
W
wenxu12345 已提交
120

W
wenxu12345 已提交
121
	err := imdb.InsertInToUserBlackList(black)
W
wenxu12345 已提交
122 123
	if err != nil {
		log.NewError(req.CommID.OperationID, "InsertInToUserBlackList failed ", err.Error())
W
wenxu12345 已提交
124
		return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
W
wenxu12345 已提交
125
	}
W
wenxu12345 已提交
126
	log.NewInfo(req.CommID.OperationID, "AddBlacklist rpc ok ", req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
127
	chat.BlackAddedNotification(req)
W
wenxu12345 已提交
128
	return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
129 130
}

W
wenxu12345 已提交
131
func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq) (*pbFriend.AddFriendResp, error) {
W
wenxu12345 已提交
132 133 134
	log.NewInfo(req.CommID.OperationID, "AddFriend args ", req.String())
	ok := token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID)
	if !ok {
W
wenxu12345 已提交
135 136
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
		return &pbFriend.AddFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
137 138
	}
	//Cannot add non-existent users
W
wenxu12345 已提交
139 140
	if _, err := imdb.GetUserByUserID(req.CommID.ToUserID); err != nil {
		log.NewError(req.CommID.OperationID, "GetUserByUserID failed ", err.Error(), req.CommID.ToUserID)
W
wenxu12345 已提交
141
		return &pbFriend.AddFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
W
wenxu12345 已提交
142 143 144
	}

	//Establish a latest relationship in the friend request table
W
wenxu12345 已提交
145
	friendRequest := imdb.FriendRequest{ReqMsg: req.ReqMsg}
W
wenxu12345 已提交
146
	utils.CopyStructFields(&friendRequest, req.CommID)
W
wenxu12345 已提交
147
	// {openIM001 openIM002 0 test add friend 0001-01-01 00:00:00 +0000 UTC   0001-01-01 00:00:00 +0000 UTC }]
W
wenxu12345 已提交
148
	log.NewDebug(req.CommID.OperationID, "UpdateFriendApplication args ", friendRequest)
W
wenxu12345 已提交
149
	err := imdb.UpdateFriendApplication(&friendRequest)
W
wenxu12345 已提交
150
	if err != nil {
W
wenxu12345 已提交
151
		log.NewError(req.CommID.OperationID, "UpdateFriendApplication failed ", err.Error(), friendRequest)
W
wenxu12345 已提交
152
		return &pbFriend.AddFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
W
wenxu12345 已提交
153 154
	}

W
wenxu12345 已提交
155
	chat.FriendApplicationAddedNotification(req)
W
wenxu12345 已提交
156
	return &pbFriend.AddFriendResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
157 158 159
}

func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFriendReq) (*pbFriend.ImportFriendResp, error) {
W
wenxu12345 已提交
160
	log.NewInfo(req.OperationID, "ImportFriend args ", req.String())
W
wenxu12345 已提交
161
	resp := pbFriend.ImportFriendResp{CommonResp: &pbFriend.CommonResp{}}
W
wenxu12345 已提交
162 163 164
	var c pbFriend.CommonResp

	if !utils.IsContain(req.OpUserID, config.Config.Manager.AppManagerUid) {
W
wenxu12345 已提交
165
		log.NewError(req.OperationID, "not authorized", req.OpUserID, config.Config.Manager.AppManagerUid)
W
wenxu12345 已提交
166 167
		c.ErrCode = constant.ErrAccess.ErrCode
		c.ErrMsg = constant.ErrAccess.ErrMsg
W
wenxu12345 已提交
168 169 170 171 172
		for _, v := range req.FriendUserIDList {
			resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
		}
		resp.CommonResp = &c
		return &resp, nil
W
wenxu12345 已提交
173
	}
W
wenxu12345 已提交
174
	if _, err := imdb.GetUserByUserID(req.FromUserID); err != nil {
W
wenxu12345 已提交
175
		log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), req.FromUserID)
W
wenxu12345 已提交
176
		c.ErrCode = constant.ErrDB.ErrCode
W
wenxu12345 已提交
177
		c.ErrMsg = "this user not exists,cant not add friend"
W
wenxu12345 已提交
178 179 180 181 182
		for _, v := range req.FriendUserIDList {
			resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
		}
		resp.CommonResp = &c
		return &resp, nil
W
wenxu12345 已提交
183 184 185
	}

	for _, v := range req.FriendUserIDList {
W
wenxu12345 已提交
186
		if _, fErr := imdb.GetUserByUserID(v); fErr != nil {
W
wenxu12345 已提交
187
			resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
W
wenxu12345 已提交
188
		} else {
W
wenxu12345 已提交
189
			if _, err := imdb.GetFriendRelationshipFromFriend(req.FromUserID, v); err != nil {
W
wenxu12345 已提交
190
				//Establish two single friendship
W
wenxu12345 已提交
191 192
				toInsertFollow := imdb.Friend{OwnerUserID: req.FromUserID, FriendUserID: v}
				err1 := imdb.InsertToFriend(&toInsertFollow)
W
wenxu12345 已提交
193 194
				if err1 != nil {
					log.NewError(req.OperationID, "InsertToFriend failed", req.FromUserID, v, err1.Error())
W
wenxu12345 已提交
195
					resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
W
wenxu12345 已提交
196 197
					continue
				}
W
wenxu12345 已提交
198 199
				toInsertFollow = imdb.Friend{OwnerUserID: v, FriendUserID: req.FromUserID}
				err2 := imdb.InsertToFriend(&toInsertFollow)
W
wenxu12345 已提交
200 201
				if err2 != nil {
					log.NewError(req.OperationID, "InsertToFriend failed", v, req.FromUserID, err2.Error())
W
wenxu12345 已提交
202
					resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
W
wenxu12345 已提交
203 204
					continue
				}
W
wenxu12345 已提交
205 206
				for _, v := range req.FriendUserIDList {
					chat.FriendAddedNotification(req.OperationID, req.OpUserID, req.FromUserID, v)
W
wenxu12345 已提交
207
					resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: 0})
W
wenxu12345 已提交
208
				}
W
wenxu12345 已提交
209 210 211
			}
		}
	}
W
wenxu12345 已提交
212 213
	resp.CommonResp.ErrCode = 0
	log.NewInfo(req.OperationID, "ImportFriend rpc ok ", resp.String())
W
wenxu12345 已提交
214 215 216
	return &resp, nil
}

W
wenxu12345 已提交
217
//process Friend application
W
wenxu12345 已提交
218
func (s *friendServer) AddFriendResponse(ctx context.Context, req *pbFriend.AddFriendResponseReq) (*pbFriend.AddFriendResponseResp, error) {
W
wenxu12345 已提交
219 220
	log.NewInfo(req.CommID.OperationID, "AddFriendResponse args ", req.String())

W
wenxu12345 已提交
221 222
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
223
		return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
224 225 226 227
	}

	//Check there application before agreeing or refuse to a friend's application
	//req.CommID.FromUserID process req.CommID.ToUserID
W
wenxu12345 已提交
228 229 230
	friendRequest, err := imdb.GetFriendApplicationByBothUserID(req.CommID.ToUserID, req.CommID.FromUserID)
	if err != nil {
		log.NewError(req.CommID.OperationID, "GetFriendApplicationByBothUserID failed ", err.Error(), req.CommID.ToUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
231
		return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
W
wenxu12345 已提交
232
	}
W
wenxu12345 已提交
233
	friendRequest.HandleResult = req.Flag
W
wenxu12345 已提交
234
	friendRequest.HandleTime = time.Now()
W
wenxu12345 已提交
235
	//friendRequest.HandleTime.Unix()
W
wenxu12345 已提交
236 237
	friendRequest.HandleMsg = req.HandleMsg
	friendRequest.HandlerUserID = req.CommID.OpUserID
W
wenxu12345 已提交
238
	err = imdb.UpdateFriendApplication(friendRequest)
W
wenxu12345 已提交
239
	if err != nil {
W
wenxu12345 已提交
240
		log.NewError(req.CommID.OperationID, "UpdateFriendApplication failed ", err.Error(), friendRequest)
W
wenxu12345 已提交
241
		return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
W
wenxu12345 已提交
242
	}
W
wenxu12345 已提交
243

W
wenxu12345 已提交
244 245 246
	//Change the status of the friend request form
	if req.Flag == constant.FriendFlag {
		//Establish friendship after find friend relationship not exists
W
wenxu12345 已提交
247
		_, err := imdb.GetFriendRelationshipFromFriend(req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
248
		if err == nil {
W
wenxu12345 已提交
249
			log.NewWarn(req.CommID.OperationID, "GetFriendRelationshipFromFriend exist", req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
250 251
		} else {
			//Establish two single friendship
W
wenxu12345 已提交
252 253
			toInsertFollow := imdb.Friend{OwnerUserID: req.CommID.FromUserID, FriendUserID: req.CommID.ToUserID, OperatorUserID: req.CommID.OpUserID}
			err = imdb.InsertToFriend(&toInsertFollow)
W
wenxu12345 已提交
254
			if err != nil {
W
wenxu12345 已提交
255
				log.NewError(req.CommID.OperationID, "InsertToFriend failed ", err.Error(), toInsertFollow)
W
wenxu12345 已提交
256
				return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
W
wenxu12345 已提交
257 258 259
			}
		}

W
wenxu12345 已提交
260
		_, err = imdb.GetFriendRelationshipFromFriend(req.CommID.ToUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
261
		if err == nil {
W
wenxu12345 已提交
262
			log.NewWarn(req.CommID.OperationID, "GetFriendRelationshipFromFriend exist", req.CommID.ToUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
263 264 265 266 267 268 269 270
		} else {
			toInsertFollow := imdb.Friend{OwnerUserID: req.CommID.ToUserID, FriendUserID: req.CommID.FromUserID, OperatorUserID: req.CommID.OpUserID}
			err = imdb.InsertToFriend(&toInsertFollow)
			if err != nil {
				log.NewError(req.CommID.OperationID, "InsertToFriend failed ", err.Error(), toInsertFollow)
				return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
			}
			chat.FriendAddedNotification(req.CommID.OperationID, req.CommID.OpUserID, req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
271 272 273
		}
	}

W
wenxu12345 已提交
274
	chat.FriendApplicationProcessedNotification(req)
W
wenxu12345 已提交
275
	log.NewInfo(req.CommID.OperationID, "rpc AddFriendResponse ok")
W
wenxu12345 已提交
276
	return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
277 278
}

W
wenxu12345 已提交
279
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFriendReq) (*pbFriend.DeleteFriendResp, error) {
W
wenxu12345 已提交
280 281 282 283
	log.NewInfo(req.CommID.OperationID, "DeleteFriend args ", req.String())
	//Parse token, to find current user information
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
284
		return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
285 286 287 288 289
	}

	err := imdb.DeleteSingleFriendInfo(req.CommID.FromUserID, req.CommID.ToUserID)
	if err != nil {
		log.NewError(req.CommID.OperationID, "DeleteSingleFriendInfo failed", err.Error(), req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
290
		return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
291 292
	}
	log.NewInfo(req.CommID.OperationID, "DeleteFriend rpc ok")
W
wenxu12345 已提交
293
	chat.FriendDeletedNotification(req)
W
wenxu12345 已提交
294
	return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
295 296 297 298 299 300 301
}

func (s *friendServer) GetBlacklist(ctx context.Context, req *pbFriend.GetBlacklistReq) (*pbFriend.GetBlacklistResp, error) {
	log.NewInfo(req.CommID.OperationID, "GetBlacklist args ", req.String())

	//Parse token, to find current user information
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
W
wenxu12345 已提交
302
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
303
		return &pbFriend.GetBlacklistResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
304 305
	}

W
wenxu12345 已提交
306
	blackListInfo, err := imdb.GetBlackListByUserID(req.CommID.FromUserID)
W
wenxu12345 已提交
307 308
	if err != nil {
		log.NewError(req.CommID.OperationID, "GetBlackListByUID failed ", err.Error(), req.CommID.FromUserID)
W
wenxu12345 已提交
309
		return &pbFriend.GetBlacklistResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
W
wenxu12345 已提交
310 311 312
	}

	var (
W
wenxu12345 已提交
313
		userInfoList []*sdkws.PublicUserInfo
W
wenxu12345 已提交
314 315
	)
	for _, blackUser := range blackListInfo {
W
wenxu12345 已提交
316
		var blackUserInfo sdkws.PublicUserInfo
W
wenxu12345 已提交
317
		//Find black user information
W
wenxu12345 已提交
318
		us, err := imdb.GetUserByUserID(blackUser.BlockUserID)
W
wenxu12345 已提交
319
		if err != nil {
W
wenxu12345 已提交
320
			log.NewError(req.CommID.OperationID, "GetUserByUserID failed ", err.Error(), blackUser.BlockUserID)
W
wenxu12345 已提交
321 322 323 324 325
			continue
		}
		utils.CopyStructFields(&blackUserInfo, us)
		userInfoList = append(userInfoList, &blackUserInfo)
	}
W
wenxu12345 已提交
326
	log.NewInfo(req.CommID.OperationID, "rpc GetBlacklist ok ", pbFriend.GetBlacklistResp{BlackUserInfoList: userInfoList})
W
wenxu12345 已提交
327
	return &pbFriend.GetBlacklistResp{BlackUserInfoList: userInfoList}, nil
W
wenxu12345 已提交
328 329
}

W
wenxu12345 已提交
330
func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbFriend.SetFriendRemarkReq) (*pbFriend.SetFriendRemarkResp, error) {
W
wenxu12345 已提交
331 332 333
	log.NewInfo(req.CommID.OperationID, "SetFriendComment args ", req.String())
	//Parse token, to find current user information
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
W
wenxu12345 已提交
334
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
335
		return &pbFriend.SetFriendRemarkResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
336 337
	}

W
wenxu12345 已提交
338
	err := imdb.UpdateFriendComment(req.CommID.FromUserID, req.CommID.ToUserID, req.Remark)
W
wenxu12345 已提交
339
	if err != nil {
W
wenxu12345 已提交
340
		log.NewError(req.CommID.OperationID, "UpdateFriendComment failed ", req.CommID.FromUserID, req.CommID.ToUserID, req.Remark)
W
wenxu12345 已提交
341
		return &pbFriend.SetFriendRemarkResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
W
wenxu12345 已提交
342 343
	}
	log.NewInfo(req.CommID.OperationID, "rpc SetFriendComment ok")
W
wenxu12345 已提交
344
	chat.FriendInfoChangedNotification(req.CommID.OperationID, req.CommID.OpUserID, req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
345
	return &pbFriend.SetFriendRemarkResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
346 347
}

W
wenxu12345 已提交
348
func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.RemoveBlacklistReq) (*pbFriend.RemoveBlacklistResp, error) {
W
wenxu12345 已提交
349 350 351
	log.NewInfo(req.CommID.OperationID, "RemoveBlacklist args ", req.String())
	//Parse token, to find current user information
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
W
wenxu12345 已提交
352
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
353
		return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
354 355 356 357
	}
	err := imdb.RemoveBlackList(req.CommID.FromUserID, req.CommID.ToUserID)
	if err != nil {
		log.NewError(req.CommID.OperationID, "RemoveBlackList failed", err.Error(), req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
358 359
		return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil

W
wenxu12345 已提交
360
	}
W
wenxu12345 已提交
361
	log.NewInfo(req.CommID.OperationID, "rpc RemoveBlacklist ok ")
W
wenxu12345 已提交
362
	chat.BlackDeletedNotification(req)
W
wenxu12345 已提交
363
	return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
364 365 366 367 368
}

func (s *friendServer) IsInBlackList(ctx context.Context, req *pbFriend.IsInBlackListReq) (*pbFriend.IsInBlackListResp, error) {
	log.NewInfo("IsInBlackList args ", req.String())
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
W
wenxu12345 已提交
369
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
370
		return &pbFriend.IsInBlackListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
371 372 373
	}

	var isInBlacklist = false
W
wenxu12345 已提交
374
	err := imdb.CheckBlack(req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
375 376 377
	if err == nil {
		isInBlacklist = true
	}
W
wenxu12345 已提交
378
	log.NewInfo(req.CommID.OperationID, "IsInBlackList rpc ok ", pbFriend.IsInBlackListResp{Response: isInBlacklist})
W
wenxu12345 已提交
379 380 381 382 383
	return &pbFriend.IsInBlackListResp{Response: isInBlacklist}, nil
}

func (s *friendServer) IsFriend(ctx context.Context, req *pbFriend.IsFriendReq) (*pbFriend.IsFriendResp, error) {
	log.NewInfo("IsFriend args ", req.String())
W
wenxu12345 已提交
384
	var isFriend bool
W
wenxu12345 已提交
385 386
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
387
		return &pbFriend.IsFriendResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
388
	}
W
wenxu12345 已提交
389
	_, err := imdb.GetFriendRelationshipFromFriend(req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
390
	if err == nil {
W
wenxu12345 已提交
391
		isFriend = true
W
wenxu12345 已提交
392
	} else {
W
wenxu12345 已提交
393
		isFriend = false
W
wenxu12345 已提交
394
	}
W
wenxu12345 已提交
395
	log.NewInfo("IsFriend rpc ok ", pbFriend.IsFriendResp{Response: isFriend})
W
wenxu12345 已提交
396
	return &pbFriend.IsFriendResp{Response: isFriend}, nil
W
wenxu12345 已提交
397 398 399 400 401 402
}

func (s *friendServer) GetFriendList(ctx context.Context, req *pbFriend.GetFriendListReq) (*pbFriend.GetFriendListResp, error) {
	log.NewInfo("GetFriendList args ", req.String())
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
403
		return &pbFriend.GetFriendListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
404 405
	}

W
wenxu12345 已提交
406
	friends, err := imdb.GetFriendListByUserID(req.CommID.FromUserID)
W
wenxu12345 已提交
407
	if err != nil {
W
wenxu12345 已提交
408
		log.NewError(req.CommID.OperationID, "FindUserInfoFromFriend failed ", err.Error(), req.CommID.FromUserID)
W
wenxu12345 已提交
409
		return &pbFriend.GetFriendListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
W
wenxu12345 已提交
410
	}
W
wenxu12345 已提交
411
	var userInfoList []*sdkws.FriendInfo
W
wenxu12345 已提交
412
	for _, friendUser := range friends {
W
wenxu12345 已提交
413
		friendUserInfo := sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
W
wenxu12345 已提交
414 415
		cp.FriendDBCopyOpenIM(&friendUserInfo, &friendUser)
		log.NewDebug(req.CommID.OperationID, "friends : ", friendUser, "openim friends: ", friendUserInfo)
W
wenxu12345 已提交
416 417
		userInfoList = append(userInfoList, &friendUserInfo)
	}
W
wenxu12345 已提交
418 419
	log.NewInfo(req.CommID.OperationID, "rpc GetFriendList ok", pbFriend.GetFriendListResp{FriendInfoList: userInfoList})
	return &pbFriend.GetFriendListResp{FriendInfoList: userInfoList}, nil
W
wenxu12345 已提交
420 421
}

W
wenxu12345 已提交
422
//received
W
wenxu12345 已提交
423
func (s *friendServer) GetFriendApplyList(ctx context.Context, req *pbFriend.GetFriendApplyListReq) (*pbFriend.GetFriendApplyListResp, error) {
W
wenxu12345 已提交
424 425 426 427
	log.NewInfo(req.CommID.OperationID, "GetFriendApplyList args ", req.String())
	//Parse token, to find current user information
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
428
		return &pbFriend.GetFriendApplyListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
429 430
	}
	//	Find the  current user friend applications received
W
wenxu12345 已提交
431
	ApplyUsersInfo, err := imdb.GetReceivedFriendsApplicationListByUserID(req.CommID.FromUserID)
W
wenxu12345 已提交
432
	if err != nil {
W
wenxu12345 已提交
433
		log.NewError(req.CommID.OperationID, "GetReceivedFriendsApplicationListByUserID ", err.Error(), req.CommID.FromUserID)
W
wenxu12345 已提交
434
		return &pbFriend.GetFriendApplyListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
W
wenxu12345 已提交
435 436
	}

W
wenxu12345 已提交
437
	var appleUserList []*sdkws.FriendRequest
W
wenxu12345 已提交
438
	for _, applyUserInfo := range ApplyUsersInfo {
W
wenxu12345 已提交
439 440
		var userInfo sdkws.FriendRequest
		utils.CopyStructFields(&userInfo, applyUserInfo)
W
wenxu12345 已提交
441 442
		appleUserList = append(appleUserList, &userInfo)
	}
W
wenxu12345 已提交
443 444
	log.NewInfo(req.CommID.OperationID, "rpc GetFriendApplyList ok", pbFriend.GetFriendApplyListResp{FriendRequestList: appleUserList})
	return &pbFriend.GetFriendApplyListResp{FriendRequestList: appleUserList}, nil
W
wenxu12345 已提交
445 446
}

W
wenxu12345 已提交
447
func (s *friendServer) GetSelfApplyList(ctx context.Context, req *pbFriend.GetSelfApplyListReq) (*pbFriend.GetSelfApplyListResp, error) {
W
wenxu12345 已提交
448
	log.NewInfo(req.CommID.OperationID, "GetSelfApplyList args ", req.String())
W
wenxu12345 已提交
449

W
wenxu12345 已提交
450 451 452
	//Parse token, to find current user information
	if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
453
		return &pbFriend.GetSelfApplyListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
454 455
	}
	//	Find the self add other userinfo
W
wenxu12345 已提交
456
	usersInfo, err := imdb.GetSendFriendApplicationListByUserID(req.CommID.FromUserID)
W
wenxu12345 已提交
457
	if err != nil {
W
wenxu12345 已提交
458
		log.NewError(req.CommID.OperationID, "GetSendFriendApplicationListByUserID failed ", err.Error(), req.CommID.FromUserID)
W
wenxu12345 已提交
459
		return &pbFriend.GetSelfApplyListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
W
wenxu12345 已提交
460
	}
W
wenxu12345 已提交
461
	var selfApplyOtherUserList []*sdkws.FriendRequest
W
wenxu12345 已提交
462
	for _, selfApplyOtherUserInfo := range usersInfo {
W
wenxu12345 已提交
463
		var userInfo sdkws.FriendRequest // pbFriend.ApplyUserInfo
W
wenxu12345 已提交
464
		cp.FriendRequestDBCopyOpenIM(&userInfo, &selfApplyOtherUserInfo)
W
wenxu12345 已提交
465 466
		selfApplyOtherUserList = append(selfApplyOtherUserList, &userInfo)
	}
W
wenxu12345 已提交
467 468
	log.NewInfo(req.CommID.OperationID, "rpc GetSelfApplyList ok", pbFriend.GetSelfApplyListResp{FriendRequestList: selfApplyOtherUserList})
	return &pbFriend.GetSelfApplyListResp{FriendRequestList: selfApplyOtherUserList}, nil
W
wenxu12345 已提交
469
}