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

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

W
wenxu12345 已提交
222 223
	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 已提交
224
		return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
225 226 227 228
	}

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

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

W
wenxu12345 已提交
261
		_, err = imdb.GetFriendRelationshipFromFriend(req.CommID.ToUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
262
		if err == nil {
W
wenxu12345 已提交
263
			log.NewWarn(req.CommID.OperationID, "GetFriendRelationshipFromFriend exist", req.CommID.ToUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
264 265 266 267 268 269 270 271
		} 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 已提交
272 273 274
		}
	}

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

W
wenxu12345 已提交
280
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFriendReq) (*pbFriend.DeleteFriendResp, error) {
W
wenxu12345 已提交
281 282 283 284
	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 已提交
285
		return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
286 287 288 289 290
	}

	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 已提交
291
		return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
292 293
	}
	log.NewInfo(req.CommID.OperationID, "DeleteFriend rpc ok")
W
wenxu12345 已提交
294
	chat.FriendDeletedNotification(req)
W
wenxu12345 已提交
295
	return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
296 297 298 299 300 301 302
}

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 已提交
303
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
304
		return &pbFriend.GetBlacklistResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
305 306
	}

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

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

W
wenxu12345 已提交
331
func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbFriend.SetFriendRemarkReq) (*pbFriend.SetFriendRemarkResp, error) {
W
wenxu12345 已提交
332 333 334
	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 已提交
335
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
336
		return &pbFriend.SetFriendRemarkResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
337 338
	}

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

W
wenxu12345 已提交
349
func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.RemoveBlacklistReq) (*pbFriend.RemoveBlacklistResp, error) {
W
wenxu12345 已提交
350 351 352
	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 已提交
353
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
354
		return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
355 356 357 358
	}
	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 已提交
359 360
		return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil

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

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 已提交
370
		log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
371
		return &pbFriend.IsInBlackListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
372 373 374
	}

	var isInBlacklist = false
W
wenxu12345 已提交
375
	err := imdb.CheckBlack(req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
376 377 378
	if err == nil {
		isInBlacklist = true
	}
W
wenxu12345 已提交
379
	log.NewInfo(req.CommID.OperationID, "IsInBlackList rpc ok ", pbFriend.IsInBlackListResp{Response: isInBlacklist})
W
wenxu12345 已提交
380 381 382 383 384
	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 已提交
385
	var isFriend bool
W
wenxu12345 已提交
386 387
	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 已提交
388
		return &pbFriend.IsFriendResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
389
	}
W
wenxu12345 已提交
390
	_, err := imdb.GetFriendRelationshipFromFriend(req.CommID.FromUserID, req.CommID.ToUserID)
W
wenxu12345 已提交
391
	if err == nil {
W
wenxu12345 已提交
392
		isFriend = true
W
wenxu12345 已提交
393
	} else {
W
wenxu12345 已提交
394
		isFriend = false
W
wenxu12345 已提交
395
	}
W
wenxu12345 已提交
396
	log.NewInfo("IsFriend rpc ok ", pbFriend.IsFriendResp{Response: isFriend})
W
wenxu12345 已提交
397
	return &pbFriend.IsFriendResp{Response: isFriend}, nil
W
wenxu12345 已提交
398 399 400 401 402 403
}

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 已提交
404
		return &pbFriend.GetFriendListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
405 406
	}

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

W
wenxu12345 已提交
423
//received
W
wenxu12345 已提交
424
func (s *friendServer) GetFriendApplyList(ctx context.Context, req *pbFriend.GetFriendApplyListReq) (*pbFriend.GetFriendApplyListResp, error) {
W
wenxu12345 已提交
425 426 427 428
	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 已提交
429
		return &pbFriend.GetFriendApplyListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
430 431
	}
	//	Find the  current user friend applications received
W
wenxu12345 已提交
432
	ApplyUsersInfo, err := imdb.GetReceivedFriendsApplicationListByUserID(req.CommID.FromUserID)
W
wenxu12345 已提交
433
	if err != nil {
W
wenxu12345 已提交
434
		log.NewError(req.CommID.OperationID, "GetReceivedFriendsApplicationListByUserID ", err.Error(), req.CommID.FromUserID)
W
wenxu12345 已提交
435
		return &pbFriend.GetFriendApplyListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
W
wenxu12345 已提交
436 437
	}

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

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

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