/// <summary> /// 拒绝添加好友 /// </summary> public async Task RejectFriend(string friendId) { var model = FriendNotificationRepository.Find(f => f.Id == new Guid(friendId)).FirstOrDefault(); string userId = string.Empty; string connId = string.Empty; string to = model.ToUserId.ToString().ToLower(); if (UserIdByClient.ContainsKey(Context.ConnectionId)) { userId = UserIdByClient[Context.ConnectionId]; } if (ClientByUserId.ContainsKey(to)) { connId = ClientByUserId[to]; } model.State = 2; UserGroupRelationshipRepository.SaveChanges(); if (!string.IsNullOrEmpty(connId)) { await Clients.Client(connId).SendAsync("ReceiveRejectFriendByFromUser", "对方拒绝添加你为好友"); } }
/// <summary> /// 同意成为好友 /// </summary> public async Task AgreeFriend(string friendId) { var model = FriendNotificationRepository.Find(f => f.Id == new Guid(friendId)).FirstOrDefault(); string userId = string.Empty; string connId = string.Empty; string to = model.ToUserId.ToString().ToLower(); if (UserIdByClient.ContainsKey(Context.ConnectionId)) { userId = UserIdByClient[Context.ConnectionId]; } if (ClientByUserId.ContainsKey(to)) { connId = ClientByUserId[to]; } //发起人添加关联 UserGroupRelationship fromUgp = new UserGroupRelationship(); fromUgp.Id = Guid.NewGuid(); fromUgp.UserId = model.ToUserId; fromUgp.GroupId = model.FromUser.GroupList[0].Id; UserGroupRelationshipRepository.Add(fromUgp); //接收人添加关联 UserGroupRelationship toUgp = new UserGroupRelationship(); toUgp.Id = Guid.NewGuid(); toUgp.UserId = model.FromUserId; toUgp.GroupId = model.ToUser.GroupList[0].Id; UserGroupRelationshipRepository.Add(toUgp); model.State = 3; UserGroupRelationshipRepository.SaveChanges(); if (!string.IsNullOrEmpty(connId)) { await Clients.Client(connId).SendAsync("ReceiveAgreeFriendByFromUser", "对方同意添加你为好友"); } await Clients.Client(Context.ConnectionId).SendAsync("ReceiveAgreeFriendByToUser", "同意成为好友"); }
public async Task SendAddPerson(string from, string to, string msg) { string uid = string.Empty; string connId = string.Empty; if (UserIdByClient.ContainsKey(Context.ConnectionId)) { uid = UserIdByClient[Context.ConnectionId]; } if (ClientByUserId.ContainsKey(to)) { connId = ClientByUserId[to]; } var model = FriendNotificationRepository.Find(f => (f.FromUserId == new Guid(from) && f.ToUserId == new Guid(to)) || (f.FromUserId == new Guid(to) && f.ToUserId == new Guid(from)) ).FirstOrDefault(); if (model == null) { //通知添加到数据库 model = new FriendNotification(); model.Id = Guid.NewGuid(); model.State = 1; model.FromUserId = new Guid(uid); model.FromUser = UsersRepository.Load(model.FromUserId); model.ToUserId = new Guid(to); model.ToUser = UsersRepository.Load(model.ToUserId); model.Message = msg; model.CreateTime = DateTime.Now; model.UpdateTime = DateTime.Now; FriendNotificationRepository.Add(model); FriendNotificationRepository.SaveChanges(); } else { if (model.State != 3) { model.State = 1; model.Message = msg; model.UpdateTime = DateTime.Now; FriendNotificationRepository.SaveChanges(); } } if (new Guid(uid) == model.ToUserId) { await Clients.Client(Context.ConnectionId).SendAsync("OnlineCallbackFunc", "你们已经是好友了,无法重复添加"); } else { AddFriend friend = new AddFriend(); friend.Id = model.Id.ToString().ToLower(); friend.Type = MessageType.AddUser; friend.FromUid = uid; friend.ToUid = to; friend.Message = msg; friend.State = model.State; friend.UserName = model.FromUser.UserName; friend.NickName = model.FromUser.NickName; friend.HeadImg = model.FromUser.HeadImg; friend.CreateTime = model.FromUser.CreateTime; friend.UpdateTime = model.FromUser.UpdateTime; if (!string.IsNullOrEmpty(connId) && model.State == 1) { await Clients.Client(connId).SendAsync("ClientNoticeRemind", friend.ToJsonString()); } } }