public async Task <ActionResult <ConversationWritingActivityOutputModel> > UpdateWritingStatus(Guid conversationId, [FromBody] ConversationWritingActivityModel writingActivity)
        {
            var userId = Guid.Parse(User.Identity.GetSubjectId());
            var conversationWritingActivityOutputModel = new ConversationWritingActivityOutputModel
            {
                Activity       = writingActivity.Activity,
                ConversationId = conversationId,
                UserId         = userId
            };
            var conversationGroupName = _signalRGroupNameFactory.CreateConversationGroupName(conversationId);
            await _conversationHubContext.Clients.Group(conversationGroupName)
            .ConversationWritingActivity(conversationWritingActivityOutputModel);

            return(conversationWritingActivityOutputModel);
        }
Exemple #2
0
        public async Task <ActionResult <InvitationModel> > AcceptInvitation(string token, [FromBody] InvitationAcceptModel model)
        {
            Guid?userId;

            if (model.ShouldCreateAnonymousUser)
            {
                if (User.IsAuthenticated())
                {
                    return(ValidationProblem("Anonymous account cannot be created when user is already signed in"));
                }
                var user = await CreateAnonymousUser();

                if (user == null)
                {
                    return(Problem("Could not create anonymous user"));
                }
                await _signInManager.SignInAsync(user, false);

                userId = user.Id;
            }
            else
            {
                userId = Guid.Parse(User.Identity.GetSubjectId());
            }

            var dbInvitation = await _context.Invitations.Where(x => x.Token == token).SingleOrDefaultAsync();

            if (dbInvitation == null || !dbInvitation.IsActive)
            {
                return(ValidationProblem("The invitation either doesn't exist or is not active anymore"));
            }

            if (await _context.ConversationUsers.AnyAsync(x =>
                                                          x.ConversationId == dbInvitation.ConversationId && x.UserId == userId.Value))
            {
                return(ValidationProblem("The user is already a member of the conversation"));
            }

            if (!dbInvitation.IsPermanent)
            {
                dbInvitation.IsActive = false;
            }

            _context.ConversationUsers.Add(new ConversationUser
            {
                UserId         = userId.Value,
                ConversationId = dbInvitation.ConversationId,
                Id             = Guid.NewGuid(),
                UserNickName   = model.Nickname
            });
            await _context.SaveChangesAsync();

            var conversationUserModel = _context.ConversationUsers
                                        .Include(x => x.User)
                                        .Where(x => x.ConversationId == dbInvitation.ConversationId && x.UserId == userId.Value)
                                        .Select(x =>
                                                new ApiConversationUser(
                                                    x.Id,
                                                    x.UserId,
                                                    x.ConversationId,
                                                    new ApiUser(x.UserId, x.User.UserName, ConversationHub.Connections.GetConnections(x.UserId).Any() ? UserStatus.Online : UserStatus.Offline) /*TODO: make it nicer*/)
            {
                Nickname = x.UserNickName
            })
                                        .Single();
            await _conversationHubContext.Clients
            .Group(_signalRGroupNameFactory.CreateConversationGroupName(dbInvitation.ConversationId))
            .ConversationUserAdded(conversationUserModel);

            return(await RetrieveInvitationModel(dbInvitation.Id));
        }
        public async Task <ActionResult> StartListeningConversation(Guid conversationId, [FromBody] string connectionId)
        {
            await _conversationHubContext.Groups.AddToGroupAsync(connectionId, _signalRGroupNameFactory.CreateConversationGroupName(conversationId));

            return(Ok(string.Empty)); // TODO: in javascript it's crashing when the response cannot parse as JSON
        }