Beispiel #1
0
        public void AddFriendshipInvitation(FriendshipInvitation invitation, User friend)
        {
            long invitationId = invitation.FriendshipInvitationId.Value;

            if (oldInvitationIds.Contains(invitationId) || outgoingFriendInvitations.Any((IInternalOutgoingFriendInvitation i) => i.InvitationId == invitationId) || incomingFriendInvitations.Any((IInternalIncomingFriendInvitation i) => i.InvitationId == invitationId))
            {
                return;
            }
            if (invitation.IsInviter.Value)
            {
                IInternalOutgoingFriendInvitation internalOutgoingFriendInvitation = outgoingFriendInvitations.FirstOrDefault((IInternalOutgoingFriendInvitation i) => i.Invitee.DisplayName.Text == invitation.FriendDisplayName && i.RequestTrust != invitation.IsTrusted);
                if (internalOutgoingFriendInvitation != null)
                {
                    RemoveOutgoingFriendInvitation(internalOutgoingFriendInvitation.InvitationId);
                }
                IInternalUnidentifiedUser invitee = RemoteUserFactory.CreateUnidentifiedUser(friend.DisplayName, friend.FirstName, userDatabase);
                OutgoingFriendInvitation  outgoingFriendInvitation = new OutgoingFriendInvitation(this, invitee, invitation.IsTrusted.Value);
                outgoingFriendInvitation.SendComplete(invitationId);
                AddOutgoingFriendInvitation(outgoingFriendInvitation);
            }
            else
            {
                IInternalIncomingFriendInvitation internalIncomingFriendInvitation = incomingFriendInvitations.FirstOrDefault((IInternalIncomingFriendInvitation i) => i.Inviter.DisplayName.Text == invitation.FriendDisplayName && i.RequestTrust != invitation.IsTrusted);
                if (internalIncomingFriendInvitation != null)
                {
                    RemoveIncomingFriendInvitation(internalIncomingFriendInvitation.InvitationId);
                }
                IInternalUnidentifiedUser inviter = RemoteUserFactory.CreateUnidentifiedUser(friend.DisplayName, friend.FirstName, userDatabase);
                IncomingFriendInvitation  incomingFriendInvitation = new IncomingFriendInvitation(inviter, this, invitation.IsTrusted.Value);
                incomingFriendInvitation.SendComplete(invitationId);
                AddIncomingFriendInvitation(incomingFriendInvitation);
            }
        }
Beispiel #2
0
 public void ParseFriendshipInvitations(GetStateResponse response, IUserDatabase userDatabase, IInternalLocalUser localUser, out IList <IInternalIncomingFriendInvitation> incomingFriendInvitations, out IList <IInternalOutgoingFriendInvitation> outgoingFriendInvitations)
 {
     incomingFriendInvitations = new List <IInternalIncomingFriendInvitation>();
     outgoingFriendInvitations = new List <IInternalOutgoingFriendInvitation>();
     if (response.FriendshipInvitations == null)
     {
         return;
     }
     foreach (FriendshipInvitation invitation in response.FriendshipInvitations)
     {
         List <User>       users     = response.Users;
         Func <User, bool> predicate = (User user) => user.DisplayName == invitation.FriendDisplayName;
         string            firstName = users.FirstOrDefault(predicate)?.FirstName;
         if (invitation.IsInviter.Value)
         {
             IInternalUnidentifiedUser invitee = RemoteUserFactory.CreateUnidentifiedUser(invitation.FriendDisplayName, firstName, userDatabase);
             OutgoingFriendInvitation  outgoingFriendInvitation = new OutgoingFriendInvitation(localUser, invitee, invitation.IsTrusted.Value);
             outgoingFriendInvitation.SendComplete(invitation.FriendshipInvitationId.Value);
             outgoingFriendInvitations.Add(outgoingFriendInvitation);
         }
         else
         {
             IInternalUnidentifiedUser inviter = RemoteUserFactory.CreateUnidentifiedUser(invitation.FriendDisplayName, firstName, userDatabase);
             IncomingFriendInvitation  incomingFriendInvitation = new IncomingFriendInvitation(inviter, localUser, invitation.IsTrusted.Value);
             incomingFriendInvitation.SendComplete(invitation.FriendshipInvitationId.Value);
             incomingFriendInvitations.Add(incomingFriendInvitation);
         }
     }
 }
Beispiel #3
0
        public IList <IInternalFriend> ParseFriendships(GetStateResponse response, IUserDatabase userDatabase)
        {
            List <IInternalFriend> list  = new List <IInternalFriend>();
            List <User>            users = response.Users;

            if (response.Friendships != null)
            {
                foreach (Friendship friendship in response.Friendships)
                {
                    string userId          = friendship.FriendUserId;
                    bool   value           = friendship.IsTrusted.Value;
                    string displayNameText = string.Empty;
                    string firstName       = string.Empty;
                    if (users != null)
                    {
                        User user = users.FirstOrDefault((User u) => u.UserId == userId);
                        if (user != null)
                        {
                            displayNameText = user.DisplayName;
                            firstName       = user.FirstName;
                        }
                    }
                    IInternalFriend item = RemoteUserFactory.CreateFriend(userId, value, displayNameText, firstName, userDatabase);
                    list.Add(item);
                }
            }
            return(list);
        }
Beispiel #4
0
        private void ReconcileFriendInvitations(GetStateResponse response, IInternalLocalUser localUser, IUserDatabase userDatabase)
        {
            List <FriendshipInvitation> friendshipInvitations = response.FriendshipInvitations;
            List <IInternalIncomingFriendInvitation> list     = localUser.InternalIncomingFriendInvitations.ToList();
            List <IInternalOutgoingFriendInvitation> list2    = localUser.InternalOutgoingFriendInvitations.ToList();

            if (friendshipInvitations != null)
            {
                foreach (FriendshipInvitation friendInvitation in friendshipInvitations)
                {
                    Func <IInternalIncomingFriendInvitation, bool> predicate           = (IInternalIncomingFriendInvitation i) => i.InvitationId == friendInvitation.FriendshipInvitationId;
                    IInternalIncomingFriendInvitation internalIncomingFriendInvitation = list.FirstOrDefault(predicate);
                    if (internalIncomingFriendInvitation != null)
                    {
                        if (internalIncomingFriendInvitation.RequestTrust != friendInvitation.IsTrusted)
                        {
                            internalIncomingFriendInvitation.RequestTrust = friendInvitation.IsTrusted.Value;
                        }
                        list.Remove(internalIncomingFriendInvitation);
                        continue;
                    }
                    IInternalOutgoingFriendInvitation internalOutgoingFriendInvitation = list2.FirstOrDefault((IInternalOutgoingFriendInvitation i) => i.InvitationId == friendInvitation.FriendshipInvitationId);
                    if (internalOutgoingFriendInvitation != null)
                    {
                        if (internalOutgoingFriendInvitation.RequestTrust != friendInvitation.IsTrusted)
                        {
                            internalOutgoingFriendInvitation.RequestTrust = friendInvitation.IsTrusted.Value;
                        }
                        list2.Remove(internalOutgoingFriendInvitation);
                        continue;
                    }
                    string firstName = response.Users.FirstOrDefault((User user) => user.DisplayName == friendInvitation.FriendDisplayName)?.FirstName;
                    if (friendInvitation.IsInviter.Value)
                    {
                        IInternalUnidentifiedUser invitee = RemoteUserFactory.CreateUnidentifiedUser(friendInvitation.FriendDisplayName, firstName, userDatabase);
                        OutgoingFriendInvitation  outgoingFriendInvitation = new OutgoingFriendInvitation(localUser, invitee, friendInvitation.IsTrusted.Value);
                        outgoingFriendInvitation.SendComplete(friendInvitation.FriendshipInvitationId.Value);
                        localUser.AddOutgoingFriendInvitation(outgoingFriendInvitation);
                    }
                    else
                    {
                        IInternalUnidentifiedUser inviter = RemoteUserFactory.CreateUnidentifiedUser(friendInvitation.FriendDisplayName, firstName, userDatabase);
                        IncomingFriendInvitation  incomingFriendInvitation = new IncomingFriendInvitation(inviter, localUser, friendInvitation.IsTrusted.Value);
                        incomingFriendInvitation.SendComplete(friendInvitation.FriendshipInvitationId.Value);
                        localUser.AddIncomingFriendInvitation(incomingFriendInvitation);
                    }
                }
            }
            foreach (IInternalIncomingFriendInvitation item in list)
            {
                localUser.RemoveIncomingFriendInvitation(item);
                item.Rejected();
            }
            foreach (IInternalOutgoingFriendInvitation item2 in list2)
            {
                localUser.RemoveOutgoingFriendInvitation(item2);
                item2.Rejected();
            }
        }
Beispiel #5
0
 private static IList <IInternalFriend> CreateFriends(IUserDatabase userDatabase)
 {
     return(userDatabase.GetAllFriendDocuments().Select(delegate(FriendDocument friendDoc)
     {
         UserDocument userBySwid = userDatabase.GetUserBySwid(friendDoc.Swid);
         return RemoteUserFactory.CreateFriend(friendDoc.Swid, friendDoc.IsTrusted, userBySwid.DisplayName, userBySwid.FirstName, userDatabase);
     }).ToList());
 }
Beispiel #6
0
        private void ReconcileFriends(GetStateResponse response, IInternalLocalUser localUser, IUserDatabase userDatabase)
        {
            List <IInternalFriend> list = localUser.InternalFriends.ToList();
            List <IInternalIncomingFriendInvitation> list2 = localUser.InternalIncomingFriendInvitations.ToList();
            List <IInternalOutgoingFriendInvitation> list3 = localUser.InternalOutgoingFriendInvitations.ToList();

            if (response.Friendships != null)
            {
                foreach (Friendship friendship in response.Friendships)
                {
                    string          friendUserId   = friendship.FriendUserId;
                    IInternalFriend internalFriend = list.FirstOrDefault((IInternalFriend f) => f.Swid == friendUserId);
                    User            user           = response.Users.First((User u) => u.UserId == friendUserId);
                    if (internalFriend != null)
                    {
                        if (internalFriend.IsTrusted != friendship.IsTrusted)
                        {
                            if (friendship.IsTrusted.Value)
                            {
                                internalFriend.ChangeTrust(isTrusted: true);
                            }
                            else
                            {
                                localUser.UntrustFriend(internalFriend);
                            }
                        }
                        list.Remove(internalFriend);
                        continue;
                    }
                    IInternalFriend internalFriend2 = RemoteUserFactory.CreateFriend(friendship.FriendUserId, friendship.IsTrusted.Value, user.DisplayName, user.FirstName, userDatabase);
                    IInternalIncomingFriendInvitation internalIncomingFriendInvitation = list2.FirstOrDefault((IInternalIncomingFriendInvitation i) => i.InternalInviter.DisplayName.Text == user.DisplayName);
                    if (internalIncomingFriendInvitation != null)
                    {
                        localUser.AddFriend(internalFriend2);
                        internalIncomingFriendInvitation.Accepted(internalFriend2.IsTrusted, internalFriend2);
                        list2.Remove(internalIncomingFriendInvitation);
                        localUser.RemoveIncomingFriendInvitation(internalIncomingFriendInvitation);
                        continue;
                    }
                    IInternalOutgoingFriendInvitation internalOutgoingFriendInvitation = list3.FirstOrDefault((IInternalOutgoingFriendInvitation i) => i.InternalInvitee.DisplayName.Text == user.DisplayName);
                    if (internalOutgoingFriendInvitation != null)
                    {
                        localUser.AddFriend(internalFriend2);
                        internalOutgoingFriendInvitation.Accepted(internalFriend2.IsTrusted, internalFriend2);
                        list3.Remove(internalOutgoingFriendInvitation);
                        localUser.RemoveOutgoingFriendInvitation(internalOutgoingFriendInvitation);
                    }
                    else
                    {
                        localUser.AddFriend(internalFriend2);
                    }
                }
            }
            foreach (IInternalFriend item in list)
            {
                localUser.RemoveFriend(item);
            }
        }
Beispiel #7
0
        public IOutgoingFriendInvitation SendFriendInvitation(IFriend user, bool requestTrust, Action <ISendFriendInvitationResult> callback)
        {
            IInternalFriend internalFriend = friends.FirstOrDefault((IInternalFriend f) => f.Id == user.Id);

            if (internalFriend == null)
            {
                logger.Critical("User to send friend invitation to not found");
                callback(new SendFriendInvitationResult(success: false, null));
                return(null);
            }
            IInternalUnidentifiedUser internalUser = RemoteUserFactory.CreateUnidentifiedUser(internalFriend.DisplayName.Text, internalFriend.FirstName, userDatabase);

            return(InternalSendFriendInvitation(internalUser, requestTrust, callback));
        }
Beispiel #8
0
 private static IEnumerable <OutgoingFriendInvitation> GetOutgoingFriendInvitations(IUserDatabase userDatabase, IInternalLocalUser localUser)
 {
     return((from friendInvitationDoc in userDatabase.GetFriendInvitationDocuments(isInviter : true)
             where userDatabase.GetUserByDisplayName(friendInvitationDoc.DisplayName) != null
             select friendInvitationDoc).Select(delegate(FriendInvitationDocument friendInvitationDoc)
     {
         UserDocument userByDisplayName = userDatabase.GetUserByDisplayName(friendInvitationDoc.DisplayName);
         string displayName = userByDisplayName.DisplayName;
         IInternalUnidentifiedUser invitee = RemoteUserFactory.CreateUnidentifiedUser(displayName, userByDisplayName.FirstName, userDatabase);
         bool isTrusted = friendInvitationDoc.IsTrusted;
         OutgoingFriendInvitation outgoingFriendInvitation = new OutgoingFriendInvitation(localUser, invitee, isTrusted);
         outgoingFriendInvitation.SendComplete(friendInvitationDoc.FriendInvitationId);
         return outgoingFriendInvitation;
     }).ToList());
 }
Beispiel #9
0
 public static void Search(AbstractLogger logger, IMixWebCallFactory mixWebCallFactory, string displayName, IUserDatabase userDatabase, Action <IInternalUnidentifiedUser> successCallback, Action failureCallback)
 {
     try
     {
         DisplayNameSearchRequest displayNameSearchRequest = new DisplayNameSearchRequest();
         displayNameSearchRequest.DisplayName = displayName;
         DisplayNameSearchRequest request = displayNameSearchRequest;
         IWebCall <DisplayNameSearchRequest, DisplayNameSearchResponse> webCall = mixWebCallFactory.SearchDisplaynamePost(request);
         webCall.OnResponse += delegate(object sender, WebCallEventArgs <DisplayNameSearchResponse> e)
         {
             DisplayNameSearchResponse response = e.Response;
             if (ValidateResponse(response))
             {
                 userDatabase.InsertUserDocument(new UserDocument
                 {
                     DisplayName = response.DisplayName,
                     FirstName   = response.FirstName,
                     Swid        = null,
                     HashedSwid  = null
                 });
                 IInternalUnidentifiedUser obj = RemoteUserFactory.CreateUnidentifiedUser(response.DisplayName, response.FirstName, userDatabase);
                 successCallback(obj);
             }
             else
             {
                 logger.Critical("Failed to validate display name search response: " + JsonParser.ToJson(response));
                 failureCallback();
             }
         };
         webCall.OnError += delegate(object sender, WebCallErrorEventArgs e)
         {
             logger.Debug("Failed to find user: "******"Unhandled exception: " + ex);
         failureCallback();
     }
 }
Beispiel #10
0
        public void AddFriend(User domainFriend, bool isTrusted, long invitationId)
        {
            IInternalFriend friend         = RemoteUserFactory.CreateFriend(domainFriend.UserId, isTrusted, domainFriend.DisplayName, domainFriend.FirstName, userDatabase);
            IInternalFriend internalFriend = friends.FirstOrDefault((IInternalFriend f) => f.Id == friend.Id && f.IsTrusted != friend.IsTrusted);

            if (internalFriend != null)
            {
                friends.Remove(internalFriend);
            }
            if (!friends.Any((IInternalFriend f) => f.Id == friend.Id))
            {
                friends.Add(friend);
                IInternalIncomingFriendInvitation internalIncomingFriendInvitation = RemoveIncomingFriendInvitation(invitationId);
                if (internalIncomingFriendInvitation != null)
                {
                    internalIncomingFriendInvitation.Accepted(isTrusted, friend);
                }
                else
                {
                    RemoveOutgoingFriendInvitation(invitationId)?.Accepted(isTrusted, friend);
                }
            }
        }
Beispiel #11
0
 public static void Recommend(AbstractLogger logger, IMixWebCallFactory mixWebCallFactory, IUserDatabase userDatabase, Action <IEnumerable <IInternalUnidentifiedUser> > successCallback, Action failureCallback)
 {
     try
     {
         BaseUserRequest request = new BaseUserRequest();
         IWebCall <BaseUserRequest, GetFriendshipRecommendationResponse> webCall = mixWebCallFactory.FriendshipRecommendPost(request);
         webCall.OnResponse += delegate(object sender, WebCallEventArgs <GetFriendshipRecommendationResponse> e)
         {
             List <FriendRecommendation> friendRecommendations = e.Response.FriendRecommendations;
             if (friendRecommendations == null)
             {
                 failureCallback();
             }
             else
             {
                 foreach (FriendRecommendation item in friendRecommendations)
                 {
                     userDatabase.InsertUserDocument(new UserDocument
                     {
                         DisplayName = item.DisplayName,
                         FirstName   = item.FirstName,
                         Swid        = null,
                         HashedSwid  = null
                     });
                 }
                 IInternalUnidentifiedUser[] obj = friendRecommendations.Select((FriendRecommendation item) => RemoteUserFactory.CreateUnidentifiedUser(item.DisplayName, item.FirstName, userDatabase)).ToArray();
                 successCallback(obj);
             }
         };
         webCall.OnError += delegate
         {
             failureCallback();
         };
         webCall.Execute();
     }
     catch (Exception ex)
     {
         logger.Critical("Unhandled exception: " + ex);
         failureCallback();
     }
 }