Example #1
0
        // Commands

        public virtual async Task <ChatMessage> Post(
            Chat.PostCommand command, CancellationToken cancellationToken = default)
        {
            var(session, chatId, text) = command;
            var context = CommandContext.GetCurrent();

            if (Computed.IsInvalidating())
            {
                var invChatMessage = context.Operation().Items.Get <ChatMessage>();
                PseudoGetTail(chatId, default).Ignore();
                return(null !);
            }

            var user = await AuthService.GetUser(session, cancellationToken);

            user = user.MustBeAuthenticated();
            var userId = long.Parse(user.Id);
            var cp     = await GetPermissions(session, chatId, cancellationToken);

            if ((cp & ChatPermission.Write) != ChatPermission.Write)
            {
                throw new SecurityException("You can't post to this chat.");
            }
            var parsedMessage = await MessageParser.Parse(text, cancellationToken);

            await using var dbContext = await CreateCommandDbContext(cancellationToken);

            var now         = Clock.Now;
            var chatMessage = new ChatMessage(Ulid.NewUlid().ToString(), chatId)
            {
                UserId    = userId,
                CreatedAt = now,
                EditedAt  = now,
                Text      = parsedMessage.Format(),
            };
            var dbChatMessage = new DbChatMessage();

            dbChatMessage.UpdateFrom(chatMessage);
            dbContext.Add(dbChatMessage);
            await dbContext.SaveChangesAsync(cancellationToken);

            context.Operation().Items.Set(chatMessage);
            return(chatMessage);
        }
Example #2
0
 public Task <ChatMessage> Post([FromBody] Chat.PostCommand command, CancellationToken cancellationToken = default)
 {
     command.UseDefaultSession(SessionResolver);
     return(Chats.Post(command, cancellationToken));
 }