Ejemplo n.º 1
0
    /// <inheritdoc />
    public async Task <Message> ReplyToAsync(string userId, Message message, MessageToEdit messageParam, CancellationToken token = default(CancellationToken))
    {
        ThrowIfDisposed();
        token.ThrowIfCancellationRequested();
        if (message.SenderId != userId && message.RecipientId != userId)
        {
            throw new UnauthorizedAccessException("The sender must be a part of the conversation.");
        }

        Thread thread;

        if (message.ThreadId == null)
        {
            thread = new Thread
            {
                Id       = Guid.NewGuid().ToString(),
                Subject  = message.Subject,
                SenderId = message.SenderId
            };
        }
        else
        {
            thread = await Context.Threads.FirstOrDefaultAsync(e => e.Id == message.ThreadId, token);

            if (thread == null)
            {
                return(null);
            }
            thread.IsArchived = false;
            Context.Update(thread);
        }

        message.IsArchived = false;
        Context.Update(message);

        Message newMessage = new Message
        {
            Id          = Guid.NewGuid(),
            ThreadId    = thread.Id,
            Thread      = thread,
            SenderId    = userId,
            RecipientId = message.SenderId,
            Subject     = message.Subject,
            Content     = messageParam.Content,
            MessageSent = DateTime.UtcNow
        };

        await DbSet.AddAsync(newMessage, token);

        thread.Modified = DateTime.UtcNow;
        await Context.SaveChangesAsync(token);

        return(newMessage);
    }
Ejemplo n.º 2
0
    /// <inheritdoc />
    public Message ReplyTo(string userId, Message message, MessageToEdit messageParam)
    {
        ThrowIfDisposed();
        if (message.SenderId != userId && message.RecipientId != userId)
        {
            throw new UnauthorizedAccessException("The sender must be a part of the conversation.");
        }

        Thread thread;

        if (message.ThreadId == null)
        {
            thread = new Thread
            {
                Id       = Guid.NewGuid().ToString(),
                Subject  = message.Subject,
                SenderId = message.SenderId
            };
        }
        else
        {
            thread = Context.Threads.FirstOrDefault(e => e.Id == message.ThreadId);
            if (thread == null)
            {
                return(null);
            }
            thread.IsArchived = false;
            Context.Update(thread);
        }

        message.IsArchived = false;
        Context.Update(message);

        Message newMessage = new Message
        {
            Id          = Guid.NewGuid(),
            ThreadId    = thread.Id,
            SenderId    = userId,
            RecipientId = message.SenderId,
            Subject     = message.Subject,
            Content     = messageParam.Content,
            MessageSent = DateTime.UtcNow
        };

        DbSet.Add(newMessage);
        thread.Modified = DateTime.UtcNow;
        Context.SaveChanges();
        return(newMessage);
    }
Ejemplo n.º 3
0
    /// <inheritdoc />
    public void ArchiveThread(Thread thread)
    {
        ThrowIfDisposed();
        if (thread.IsArchived)
        {
            return;
        }
        thread.IsArchived = true;
        Context.Update(thread);

        foreach (Message message in Context.Messages.Where(e => e.ThreadId == thread.Id))
        {
            message.IsArchived = true;
            Context.Update(message);
        }

        Context.SaveChanges();
    }
Ejemplo n.º 4
0
    /// <inheritdoc />
    public async Task ArchiveThreadAsync(Thread thread, CancellationToken token = default(CancellationToken))
    {
        ThrowIfDisposed();
        token.ThrowIfCancellationRequested();
        if (thread.IsArchived)
        {
            return;
        }
        thread.IsArchived = true;
        Context.Update(thread);

        foreach (Message message in Context.Messages.Where(e => e.ThreadId == thread.Id))
        {
            message.IsArchived = true;
            Context.Update(message);
        }

        await Context.SaveChangesAsync(token);
    }