private void OnAddResponse(IMClientPeer peer, OperationResponse response) { const SubCode subCode = SubCode.Contact_Add_Response; if (!TryInitResponse(subCode, peer, response, out var parameters, ParameterKeys.CONTACT_ADD_CLIENT_RESPONSE, out ContactAddClientResponseModel model)) { return; } if (peer.LoginUser == null) { mLogger.ErrorFormat("响应失败!客户端:{0}未登陆!", peer); return; } var contactAddRequest = ContactAddRequestManager.GetContactAddRequest(model.RequestUsername, peer.LoginUser.Username); if (contactAddRequest == null) { peer.SendResponse(ReturnCode.UsernameDoesNotExist, parameters); return; } var responseCode = model.Accept ? ContactAddRequest.ContactAddResponseCode.Accept : ContactAddRequest.ContactAddResponseCode.Refuse; //DB更新 contactAddRequest.ResponseCode = (int)responseCode; ContactAddRequestManager.UpdateContactAddRequest(contactAddRequest); var requestUser = UserManager.GetUser(model.RequestUsername); if (model.Accept) { //DB添加 ContactManager.AddContact(new Contact { Username = model.RequestUsername, ContactUsername = peer.LoginUser.Username }); ContactManager.AddContact(new Contact { Username = peer.LoginUser.Username, ContactUsername = requestUser.Username }); } //响应 ContactAddServerResponseModel responseModel1 = new ContactAddServerResponseModel(false, responseCode, new UserModel(requestUser)); peer.SendResponse(ReturnCode.Success, parameters.AddParameter(ParameterKeys.CONTACT_ADD_SERVER_RESPONSE, responseModel1)); //如果请求方在线,发送响应 if (IMApplication.Instance.TryGetPeerByUsername(model.RequestUsername, out var requestPeer)) { ContactAddServerResponseModel responseModel2 = new ContactAddServerResponseModel(true, responseCode, new UserModel(peer.LoginUser)); parameters[ParameterKeys.CONTACT_ADD_SERVER_RESPONSE] = responseModel2; requestPeer.SendRequest(parameters); } }
public NewContactItem(ContactAddServerResponseModel serverResponseModel) : this(serverResponseModel.IsRequest, serverResponseModel.ResponseCode, serverResponseModel.ContactUser.Username, serverResponseModel.ContactUser.Nickname) { }