示例#1
0
            public async Task <GroupResource> Handle(CreateGroupCommand request, CancellationToken cancellationToken = default)
            {
                int userId = _userProvider.GetCurrentUserId();

                Group group = new Group
                {
                    Name        = request.Name,
                    Description = request.Description,
                    Created     = _dateProvider.UtcNow(),
                };

                GroupMembership membership = new GroupMembership
                {
                    Group   = group,
                    UserId  = userId,
                    IsAdmin = true,
                    Created = _dateProvider.UtcNow()
                };

                Recipient recipient = new Recipient
                {
                    GroupMembership = membership
                };

                await _unitOfWork.Groups.Add(group, cancellationToken);

                await _unitOfWork.GroupMemberships.Add(membership, cancellationToken);

                await _unitOfWork.Recipients.Add(recipient, cancellationToken);

                await _unitOfWork.CommitAsync(cancellationToken);

                return(_mapper.Map <Group, GroupResource>(group));
            }
示例#2
0
    public string GenerateToken(User user)
    {
        byte[] key = Encoding.ASCII.GetBytes(_jwtOptions.Secret);

        JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

        SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor
        {
            Subject = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Email, user.Email),
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString()),
            }),
            Expires            = _dateProvider.UtcNow().AddHours(7),
            SigningCredentials = new SigningCredentials(
                key: new SymmetricSecurityKey(key),
                algorithm: SecurityAlgorithms.HmacSha256Signature
                )
        };

        SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);

        return(tokenHandler.WriteToken(token));
    }
        public async Task <FriendshipResource> Handle(RequestFriendshipCommand request, CancellationToken cancellationToken = default)
        {
            int userId = _userProvider.GetCurrentUserId();

            Friendship friendship = new Friendship
            {
                RequesterId = userId,
                AddresseeId = request.AddresseeId
            };

            FriendshipChange change = new FriendshipChange
            {
                Created = _dateProvider.UtcNow(),
                Status  = FriendshipStatus.Pending,
            };

            friendship.StatusChanges.Add(change);

            await _unitOfWork.Friendships.Add(friendship, cancellationToken);

            await _unitOfWork.CommitAsync(cancellationToken);

            FriendshipResource resource = _mapper.Map <Friendship, FriendshipResource>(friendship);

            return(resource);
        }
示例#4
0
        public Person(string name, IDateProvider dateProvider)
        {
            DateProvider = dateProvider;

            Name = name;
            CreatedDateUtc = DateProvider.UtcNow();
        }
            public async Task <GroupMembershipResource> Handle(CreateMembershipCommand request, CancellationToken cancellationToken = default)
            {
                User user = await _unitOfWork.Users
                            .GetById(request.UserId)
                            .AsTracking()
                            .SingleOrDefaultAsync(cancellationToken);

                GroupMembership membership = new GroupMembership
                {
                    User    = user,
                    UserId  = request.UserId,
                    GroupId = request.GroupId,
                    IsAdmin = request.IsAdmin,
                    Created = _dateProvider.UtcNow(),
                };

                Recipient recipient = new Recipient
                {
                    GroupMembership = membership,
                };

                await _unitOfWork.GroupMemberships.Add(membership, cancellationToken);

                await _unitOfWork.Recipients.Add(recipient, cancellationToken);

                await _unitOfWork.CommitAsync(cancellationToken);

                return(_mapper.Map <GroupMembership, GroupMembershipResource>(membership));
            }
示例#6
0
            public async Task <int> Handle(CreateAccountCommand request, CancellationToken cancellationToken = default)
            {
                User user = new User
                {
                    Email    = request.Email,
                    UserName = request.UserName,
                };

                Recipient recipient = new Recipient {
                    User = user
                };

                Availability availability = new Availability
                {
                    User             = user,
                    StatusId         = AvailabilityStatusId.Offline,
                    Modified         = _dateProvider.UtcNow(),
                    ModifiedManually = false,
                };

                // Generate salt + password hash
                byte[] salt = _cryptoService.GenerateSalt();
                byte[] hash = _cryptoService.HashPassword(request.Password, salt);

                user.PasswordSalt = salt;
                user.PasswordHash = hash;
                user.Created      = _dateProvider.UtcNow();

                // Add entites
                await _unitOfWork.Users.Add(user, cancellationToken);

                await _unitOfWork.Recipients.Add(recipient, cancellationToken);

                await _unitOfWork.Availabilities.Add(availability, cancellationToken);

                await _unitOfWork.CommitAsync(cancellationToken);

                return(user.UserId);
            }
示例#7
0
            public async Task <Unit> Handle(UpdateFriendshipStatusCommand request, CancellationToken cancellationToken = default)
            {
                FriendshipChange newChange = new FriendshipChange
                {
                    FriendshipId = request.FriendshipId,
                    StatusId     = request.FriendshipStatusId,
                    Created      = _dateProvider.UtcNow()
                };

                await _unitOfWork.FriendshipChanges.Add(newChange, cancellationToken);

                await _unitOfWork.CommitAsync(cancellationToken);

                return(Unit.Value);
            }
示例#8
0
        public async Task <Unit> Handle(UpdateAvailabilityCommand request, CancellationToken cancellationToken = default)
        {
            int userId = _userProvider.GetCurrentUserId();

            Availability availability = await _unitOfWork.Availabilities.GetByUser(userId);

            availability.Status           = request.AvailabilityStatus;
            availability.Modified         = _dateProvider.UtcNow();
            availability.ModifiedManually = true;

            _unitOfWork.Availabilities.Update(availability);
            await _unitOfWork.CommitAsync(cancellationToken);

            return(Unit.Value);
        }
示例#9
0
        public async Task <ChatMessageResource> Handle(SendMessageCommand request, CancellationToken cancellationToken = default)
        {
            int currentUserId = _userProvider.GetCurrentUserId();

            MessageRecipient ownMessageRecipient;

            Func <Task> notificationFactory;

            // Message to be stored
            Message message = new Message
            {
                AuthorId    = currentUserId,
                ParentId    = request.ParentId,
                HtmlContent = request.HtmlContent,
                Created     = _dateProvider.UtcNow(),
                IsEdited    = false,
            };

            await _unitOfWork.Messages.Add(message, cancellationToken);

            // Recipient to send the message to
            Recipient recipient = await _unitOfWork.Recipients.GetByIdIncludingMemberships(request.RecipientId, cancellationToken);

            // Send group chat message to all members of the group
            if (recipient.UserId == null)
            {
                IEnumerable <GroupMembership> members = recipient.GroupMembership.Group.Memberships;

                IEnumerable <MessageRecipient> messageRecipients = members.Select(member => new MessageRecipient
                {
                    Message     = message,
                    MessageId   = message.MessageId,
                    RecipientId = member.Recipient.RecipientId,
                    Recipient   = member.Recipient,
                    ReadDate    = null,
                    IsForwarded = false,
                    IsRead      = member.UserId == currentUserId,
                });

                // Get instance of relevant MessageRecipient for the current user
                ownMessageRecipient = messageRecipients
                                      .Single(mr => mr.Recipient.GroupMembership.UserId == currentUserId);

                await _unitOfWork.MessageRecipients.AddRange(messageRecipients, cancellationToken);

                // Pass task to perform when notifying users
                notificationFactory = async() => await NotifyGroupChatRecipients(members, messageRecipients);
            }

            // Send private chat message
            else
            {
                MessageRecipient messageRecipient = new()
                {
                    Message     = message,
                    MessageId   = message.MessageId,
                    RecipientId = recipient.RecipientId,
                    Recipient   = recipient,
                    ReadDate    = null,
                    IsForwarded = false,
                    IsRead      = false
                };

                ownMessageRecipient = messageRecipient;

                await _unitOfWork.MessageRecipients.Add(messageRecipient, cancellationToken);

                // Pass task to perform when notifying the recipient user
                notificationFactory = async() => await NotifyPrivateChatRecipient(messageRecipient);
            }

            // Commit changes to the database
            await _unitOfWork.CommitAsync(cancellationToken);

            // Notify recipient(s) of message
            await notificationFactory();

            return(new ChatMessageResource
            {
                MessageRecipientId = ownMessageRecipient.MessageRecipientId,
                MessageId = ownMessageRecipient.MessageId,
                HtmlContent = ownMessageRecipient.Message.HtmlContent,
                AuthorName = ownMessageRecipient.Message.HtmlContent,
                Created = ownMessageRecipient.Message.Created,
                IsOwnMessage = true,
                IsRead = true,
            });
        }