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

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

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 已提交
42
	log.NewInfo("0", "friendServer run...")
W
wenxu12345 已提交
43 44 45 46 47 48

	ip := utils.ServerIP
	registerAddress := ip + ":" + strconv.Itoa(s.rpcPort)
	//listener network
	listener, err := net.Listen("tcp", registerAddress)
	if err != nil {
W
wenxu12345 已提交
49
		log.NewError("0", "Listen failed ", err.Error(), registerAddress)
W
wenxu12345 已提交
50 51
		return
	}
W
wenxu12345 已提交
52
	log.NewInfo("0", "listen ok ", registerAddress)
W
wenxu12345 已提交
53 54 55 56 57 58 59 60
	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 已提交
61
		log.NewError("0", "RegisterEtcd failed ", err.Error(), s.etcdSchema, strings.Join(s.etcdAddr, ","), ip, s.rpcPort, s.rpcRegisterName)
W
wenxu12345 已提交
62 63 64 65
		return
	}
	err = srv.Serve(listener)
	if err != nil {
W
wenxu12345 已提交
66
		log.NewError("0", "Serve failed ", err.Error(), listener)
W
wenxu12345 已提交
67 68 69 70
		return
	}
}

W
wenxu12345 已提交
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 113
////
//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 已提交
114 115 116
	log.NewInfo(req.CommID.OperationID, "AddBlacklist args ", req.String())
	ok := token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID)
	if !ok {
W
wenxu12345 已提交
117 118
		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 已提交
119
	}
W
wenxu12345 已提交
120
	black := db.Black{OwnerUserID: req.CommID.FromUserID, BlockUserID: req.CommID.ToUserID, OperatorUserID: req.CommID.OpUserID}
W
wenxu12345 已提交
121

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

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

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

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

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

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

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

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

W
wenxu12345 已提交
226 227
	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 已提交
228
		return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
229 230 231 232
	}

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

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

W
wenxu12345 已提交
265
		_, err = imdb.GetFriendRelationshipFromFriend(req.CommID.ToUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
266
		if err == nil {
W
wenxu12345 已提交
267
			log.NewWarn(req.CommID.OperationID, "GetFriendRelationshipFromFriend exist", req.CommID.ToUserID, req.CommID.FromUserID)
W
wenxu12345 已提交
268
		} else {
W
wenxu12345 已提交
269
			toInsertFollow := db.Friend{OwnerUserID: req.CommID.ToUserID, FriendUserID: req.CommID.FromUserID, OperatorUserID: req.CommID.OpUserID}
W
wenxu12345 已提交
270 271 272 273 274 275
			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 已提交
276 277 278
		}
	}

W
wenxu12345 已提交
279
	chat.FriendApplicationProcessedNotification(req)
W
wenxu12345 已提交
280
	log.NewInfo(req.CommID.OperationID, "rpc AddFriendResponse ok")
W
wenxu12345 已提交
281
	return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
282 283
}

W
wenxu12345 已提交
284
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFriendReq) (*pbFriend.DeleteFriendResp, error) {
W
wenxu12345 已提交
285 286 287 288
	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 已提交
289
		return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
290 291 292 293
	}
	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 已提交
294
		return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
W
wenxu12345 已提交
295 296
	}
	log.NewInfo(req.CommID.OperationID, "DeleteFriend rpc ok")
W
wenxu12345 已提交
297
	chat.FriendDeletedNotification(req)
W
wenxu12345 已提交
298
	return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{}}, nil
W
wenxu12345 已提交
299 300 301 302 303 304 305
}

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

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

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

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

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

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

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

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

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

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

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

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

W
wenxu12345 已提交
441
	var appleUserList []*sdkws.FriendRequest
W
wenxu12345 已提交
442
	for _, applyUserInfo := range ApplyUsersInfo {
W
wenxu12345 已提交
443 444
		var userInfo sdkws.FriendRequest
		utils.CopyStructFields(&userInfo, applyUserInfo)
W
wenxu12345 已提交
445 446 447 448 449 450 451 452
		u, err := imdb.GetUserByUserID(userInfo.FromUserID)
		if err != nil {
			log.Error(req.CommID.OperationID, "GetUserByUserID", userInfo.FromUserID)
			continue
		}
		userInfo.FromNickname = u.Nickname
		userInfo.FromFaceURL = u.FaceURL
		userInfo.FromGender = u.Gend
W
wenxu12345 已提交
453 454
		appleUserList = append(appleUserList, &userInfo)
	}
W
wenxu12345 已提交
455

W
wenxu12345 已提交
456 457
	log.NewInfo(req.CommID.OperationID, "rpc GetFriendApplyList ok", pbFriend.GetFriendApplyListResp{FriendRequestList: appleUserList})
	return &pbFriend.GetFriendApplyListResp{FriendRequestList: appleUserList}, nil
W
wenxu12345 已提交
458 459
}

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

W
wenxu12345 已提交
463 464 465
	//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 已提交
466
		return &pbFriend.GetSelfApplyListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
W
wenxu12345 已提交
467 468
	}
	//	Find the self add other userinfo
W
wenxu12345 已提交
469
	usersInfo, err := imdb.GetSendFriendApplicationListByUserID(req.CommID.FromUserID)
W
wenxu12345 已提交
470
	if err != nil {
W
wenxu12345 已提交
471
		log.NewError(req.CommID.OperationID, "GetSendFriendApplicationListByUserID failed ", err.Error(), req.CommID.FromUserID)
W
wenxu12345 已提交
472
		return &pbFriend.GetSelfApplyListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
W
wenxu12345 已提交
473
	}
W
wenxu12345 已提交
474
	var selfApplyOtherUserList []*sdkws.FriendRequest
W
wenxu12345 已提交
475
	for _, selfApplyOtherUserInfo := range usersInfo {
W
wenxu12345 已提交
476
		var userInfo sdkws.FriendRequest // pbFriend.ApplyUserInfo
W
wenxu12345 已提交
477
		cp.FriendRequestDBCopyOpenIM(&userInfo, &selfApplyOtherUserInfo)
W
wenxu12345 已提交
478 479
		selfApplyOtherUserList = append(selfApplyOtherUserList, &userInfo)
	}
W
wenxu12345 已提交
480 481
	log.NewInfo(req.CommID.OperationID, "rpc GetSelfApplyList ok", pbFriend.GetSelfApplyListResp{FriendRequestList: selfApplyOtherUserList})
	return &pbFriend.GetSelfApplyListResp{FriendRequestList: selfApplyOtherUserList}, nil
W
wenxu12345 已提交
482
}