public MessageViewModel(Message message)
        {
            Id = message.Id;
            From = message.From ?? string.Empty;
            Content = message.Content ?? string.Empty;
            Subject = message.Subject ?? string.Empty;

            if (message.User != null)
                User = new UserViewModel(message.User);

            When = message.When;
        }
 private void OnUpdateActivity(MessangerUser user, MessangerGroup group)
 {
     var userViewModel = new UserViewModel(user);
     Clients.Group(group.Name).updateActivity(userViewModel, group.Name);
 }
        private void DisconnectClient(string clientId)
        {
            string userId = _service.DisconnectClient(clientId);

            if (string.IsNullOrEmpty(userId))
                return;

            // Query for the user to get the updated status
            MessangerUser user = _repository.GetUserById(userId);

            // There's no associated user for this client id
            if (user == null)
                return;

            // The user will be marked as offline if all clients leave
            if (user.Status == (int)UserStatus.Offline)
            {
                foreach (var group in user.Groups)
                {
                    var userViewModel = new UserViewModel(user);
                    //Clients.Group(group.Name).leave(userViewModel, group.Name).Wait();
                    Groups.Remove(clientId, group.Name);
                }
            }
        }
        public override Task OnReconnected()
        {
            if (!Context.User.Identity.IsAuthenticated)
                return null;

            Clients.Caller.onStatus("Reconnecting...").Wait();

            string id = GetUserId();

            if (string.IsNullOrEmpty(id))
                return null;

            MessangerUser user = _repository.VerifyUserId(id);

            // Make sure this client is being tracked
            _service.AddClient(user, Context.ConnectionId, UserAgent);

            var currentStatus = (UserStatus)user.Status;
            if (currentStatus == UserStatus.Offline)
            {
                // Mark the user as inactive
                user.Status = (int)UserStatus.Inactive;
                _repository.CommitChanges();

                // If the user was offline that means they are not in the user list so we need to tell everyone the user is really in the group
                var userViewModel = new UserViewModel(user);

                foreach (var group in user.Groups)
                {
                    var isOwner = user.OwnedGroups.Contains(group);

                    // Tell the people in this group that you've joined
                    //Clients.Group(group.Name).addUser(userViewModel, group.Name, isOwner).Wait();

                    // Add the caller to the group so they receive messages
                    if (Groups != null)
                        Groups.Add(Context.ConnectionId, group.Name).Wait();
                }
            }

            Clients.Caller.onStatus("User has been successfully reconnected.").Wait();
            return base.OnReconnected();
        }