private void HandleFriendUpdate(ParseData pd)
        {
            DataReader dr    = new DataReader(pd.Data);
            byte       entry = dr.ReadByte();

            if (m_friendsList.Count <= entry)
            {
                return;
            }
            FriendUser friend = m_friendsList[entry];

            friend.Status       = (FriendStatus)dr.ReadByte();
            friend.LocationType = (FriendLocation)dr.ReadByte();
            string prodID = dr.ReadDwordString(0);

            friend.Location = dr.ReadCString();

            if (friend.LocationType != FriendLocation.Offline)
            {
                friend.Product = Product.GetByProductCode(prodID);
            }
            else
            {
                friend.Product = null;
            }

            FriendUpdatedEventArgs args = new FriendUpdatedEventArgs(friend)
            {
                EventData = pd
            };

            OnFriendUpdated(args);
        }
        public ProfileViewModel Handle()
        {
            if (_CurrentUser is null)
            {
                throw new DomainException(ErrorMessages.NotSignedIn);
            }

            var existingAccount = _UserStore.GetById(AccountId);

            if (existingAccount is null || existingAccount.AccountStatus.Equals(Status.Suspended) ||
                existingAccount.AccountStatus.Equals(Status.Inactive))
            {
                throw new DomainException(ErrorMessages.UserDoesNotExist);
            }

            FriendList FriendList        = _FriendListStore.GetFriendListOfUser(existingAccount.UserId);
            FriendList CurrentFriendList = _FriendListStore.GetFriendListOfUser(_CurrentUser.Id);
            var        FriendsOfUser     = _FriendListStore.GetFriendsOfUser(FriendList.Id);
            var        FriendsOfCurrent  = _FriendListStore.GetFriendsOfUser(CurrentFriendList.Id);

            List <Group>   GroupsWithUser      = _GroupStore.GetGroupsWithUser(existingAccount.UserId);
            List <Request> FriendRequests      = _FriendListStore.GetIncomingFriendRequests(FriendList.Id);
            List <Request> CurrentUserRequests = _FriendListStore.GetIncomingFriendRequests(CurrentFriendList.Id);
            FriendUser     existingFriend      = FriendsOfCurrent.FirstOrDefault(x => x.UserId.Equals(existingAccount.UserId));

            var ProfileViewModel = CreateViewModel(existingAccount, GroupsWithUser, existingFriend, FriendList, FriendRequests,
                                                   CurrentFriendList, CurrentUserRequests);

            ProfileViewModel.Friends = GetFriendDTOs(FriendsOfUser);
            return(ProfileViewModel);
        }
        private void HandleFriendMoved(ParseData pd)
        {
            DataReader dr       = new DataReader(pd.Data);
            byte       index    = dr.ReadByte();
            byte       newIndex = dr.ReadByte();

            FriendUser friend = m_friendsList[index];

            friend.Index = newIndex;
            m_friendsList.Insert(newIndex, friend);

            if (newIndex < index)
            {
                for (int i = newIndex + 1; i <= index; i++)
                {
                    m_friendsList[i].Index += 1;
                }
            }
            else if (newIndex > index)
            {
                for (int i = index; i < newIndex; i++)
                {
                    m_friendsList[i].Index -= 1;
                }
            }

            FriendMovedEventArgs args = new FriendMovedEventArgs(friend, newIndex)
            {
                EventData = pd
            };

            OnFriendMoved(args);
        }
        private void HandleFriendAdded(ParseData pd)
        {
            DataReader dr        = new DataReader(pd.Data);
            int        nextIndex = m_friendsList.Count;
            FriendUser newFriend = __ParseNewFriend(dr, nextIndex);

            m_friendsList.Add(newFriend);

            FriendAddedEventArgs args = new FriendAddedEventArgs(newFriend)
            {
                EventData = pd
            };

            OnFriendAdded(args);
        }
        private void HandleFriendRemoved(ParseData pd)
        {
            DataReader dr    = new DataReader(pd.Data);
            byte       index = dr.ReadByte();

            FriendUser removed = m_friendsList[index];

            m_friendsList.RemoveAt(index);

            for (int i = index; i < m_friendsList.Count; i++)
            {
                m_friendsList[i].Index -= 1;
            }

            FriendRemovedEventArgs args = new FriendRemovedEventArgs(removed)
            {
                EventData = pd
            };

            OnFriendRemoved(args);
        }
Exemple #6
0
        public void DrawItem(CustomItemDrawData e)
        {
            e.DrawBackground();
            if ((e.State & DrawItemState.Selected) == System.Windows.Forms.DrawItemState.Selected)
            {
                e.DrawFocusRectangle();
            }
            FriendUser friend = e.Item as FriendUser;

            Color textColor = e.ForeColor;

            if (friend.LocationType == FriendLocation.Offline)
            {
                if ((e.State & DrawItemState.Selected) == System.Windows.Forms.DrawItemState.Selected)
                {
                    textColor = Color.Black;
                }
                else
                {
                    textColor = Color.SlateGray;
                }
            }

            using (SolidBrush textBrush = new SolidBrush(textColor))
                using (StringFormat nameFormat = new StringFormat()
                {
                    Trimming = StringTrimming.EllipsisCharacter
                })
                {
                    PointF iconPosition = new PointF((float)e.Bounds.Location.X + 1.0f, (float)e.Bounds.Location.Y + 1.0f);
                    e.Graphics.DrawImage(m_provider.GetImageFor(friend.Product), (PointF)iconPosition);

                    SizeF      nameSize = e.Graphics.MeasureString(friend.AccountName, e.Font);
                    RectangleF nameArea = new RectangleF((float)e.Bounds.X + (float)m_provider.IconSize.Width + 1.0f + 4.0f,
                                                         (float)e.Bounds.Y + (((float)e.Bounds.Height - nameSize.Height) / 2.0f),
                                                         (float)e.Bounds.Width - (float)m_provider.IconSize.Width - 2.0f - 4.0f,
                                                         (float)nameSize.Height);
                    e.Graphics.DrawString(friend.AccountName, e.Font, textBrush, nameArea, nameFormat);
                }
        }
Exemple #7
0
        public override async Task ExecuteAsync(AddFriendCommand command)
        {
            Logger.ExecuteAddFriend(command.UserId, command.FriendEmail);

            FriendUser?userToBecomeFriendWith = await UnitOfWork.SingleOrDefaultAsync <User, FriendUser>(
                u => u.Email == command.FriendEmail.ToLowerInvariant() && u.Roles.Any(r => r == Roles.User),
                u => new FriendUser(u.Id, u.Friends));

            if (userToBecomeFriendWith == null)
            {
                throw new ValidationException("No user with this e-mail address found.");
            }

            if (command.UserId == userToBecomeFriendWith.Id)
            {
                throw new ValidationException("You must not be your own friend.");
            }

            FriendUser currentUser = await UnitOfWork.GetByIdOrThrowAsync <User, FriendUser>(command.UserId, u => new FriendUser(u.Id, u.Friends));

            if (currentUser.Friends.Contains(userToBecomeFriendWith.Id) || userToBecomeFriendWith.Friends.Contains(command.UserId))
            {
                throw new ValidationException("This user is already your friend.");
            }

            // Its necessary to update friends on both users.
            // Actually a transaction is necessary here, currently the naive approach is taken.
            // In the future, e.g. consider to use a appropriate data structure  (own collection) or introduce mongodb transactions.
            await UnitOfWork.UpdateAsync <User>(command.UserId, new { Friends = new List <Guid> {
                                                                          userToBecomeFriendWith.Id
                                                                      } });

            await UnitOfWork.UpdateAsync <User>(userToBecomeFriendWith.Id, new { Friends = new List <Guid> {
                                                                                     command.UserId
                                                                                 } });

            Logger.ExecuteAddFriendSuccessful(command.UserId, command.FriendEmail, userToBecomeFriendWith.Id);
        }
        private static FriendUser __ParseNewFriend(DataReader dr, int i)
        {
            string         acct         = dr.ReadCString();
            FriendStatus   status       = (FriendStatus)dr.ReadByte();
            FriendLocation location     = (FriendLocation)dr.ReadByte();
            string         productID    = dr.ReadDwordString(0);
            Product        prod         = null;
            string         locationName = string.Empty;

            if (location == FriendLocation.Offline)
            {
                dr.Seek(1);
            }
            else
            {
                prod         = Product.GetByProductCode(productID);
                locationName = dr.ReadCString();
            }

            FriendUser friend = new FriendUser(i, acct, status, location, prod, locationName);

            return(friend);
        }
        private void HandleFriendsList(ParseData pd)
        {
            DataReader dr         = new DataReader(pd.Data);
            int        numEntries = dr.ReadByte();

            FriendUser[] list = new FriendUser[numEntries];
            for (int i = 0; i < numEntries; i++)
            {
                FriendUser friend = __ParseNewFriend(dr, i);
                list[i] = friend;
            }

            m_friendsList.AddRange(list);

            Debug.WriteLine("Received friends list; " + list.Length + " user on it.");

            FriendListReceivedEventArgs args = new FriendListReceivedEventArgs(list)
            {
                EventData = pd
            };

            OnFriendListReceived(args);
        }
        public ProfileViewModel CreateViewModel(UserAccount existingAccount, List <Group> GroupsWithUser, FriendUser existingFriend,
                                                FriendList FriendList, List <Request> FriendRequests, FriendList CurrentFriendList, List <Request> CurrentUserRequests)
        {
            var ProfileViewModel = new ProfileViewModel
            {
                FirstName        = existingAccount.FirstName,
                LastName         = existingAccount.LastName,
                Location         = existingAccount.Location,
                Email            = existingAccount.Email,
                Username         = existingAccount.Username,
                Occupation       = existingAccount.Occupation,
                UserType         = existingAccount.UserType,
                LastActive       = existingAccount.LastActive,
                JoinDate         = existingAccount.DateJoined,
                Groups           = GroupsWithUser,
                PendingRequest   = FriendRequests.FirstOrDefault(x => x.Username.Equals(_CurrentUser.UserName)),
                FriendListID     = FriendList.Id,
                ExistingFriend   = existingFriend,
                RequestToCurrent = CurrentUserRequests.FirstOrDefault(x => x.Username.Equals(existingAccount.Username))
            };

            return(ProfileViewModel);
        }
Exemple #11
0
 public FriendUser AddFriend(FriendUser Friend)
 {
     _Context.FriendUser.Add(Friend);
     _Context.SaveChanges();
     return(Friend);
 }