//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
using Microsoft.Extensions.Options;
namespace slskd.Messaging.API
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
///
/// Conversations.
///
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("0")]
[ApiController]
[Produces("application/json")]
[Consumes("application/json")]
public class ConversationsController : ControllerBase
{
///
/// Initializes a new instance of the class.
///
///
///
///
public ConversationsController(
IStateMonitor applicationStateMonotor,
IMessagingService messagingService,
IOptionsSnapshot optionsSnapshot)
{
ApplicationStateMonitor = applicationStateMonotor;
Messages = messagingService;
OptionsSnapshot = optionsSnapshot;
}
private IStateMonitor ApplicationStateMonitor { get; }
private IMessagingService Messages { get; }
private IOptionsSnapshot OptionsSnapshot { get; }
///
/// Acknowledges the given message id for the given username.
///
///
///
///
/// The request completed successfully.
///
/// A conversation with the specified username, or a message matching the specified id could not be found.
///
[HttpPut("{username}/{id}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task Acknowledge([FromRoute]string username, [FromRoute]int id)
{
if (Program.IsRelayAgent)
{
return Forbid();
}
var message = Messages.Conversations.FindMessageAsync(username, id);
if (message == default)
{
return NotFound();
}
await Messages.Conversations.AcknowledgeMessageAsync(username, id);
return Ok();
}
///
/// Acknowledges all messages from the given username.
///
///
///
/// The request completed successfully.
/// A conversation with the specified username could not be found.
[HttpPut("{username}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task AcknowledgeAll([FromRoute]string username)
{
if (Program.IsRelayAgent)
{
return Forbid();
}
var conversation = Messages.Conversations.FindAsync(username);
if (conversation == default)
{
return NotFound();
}
await Messages.Conversations.AcknowledgeAsync(username);
return Ok();
}
///
/// Closes the conversation associated with the given username.
///
///
/// The request completed successfully.
/// A conversation with the specified username could not be found.
[HttpDelete("{username}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(404)]
[ProducesResponseType(204)]
public async Task Close([FromRoute]string username)
{
if (Program.IsRelayAgent)
{
return Forbid();
}
var conversation = Messages.Conversations.FindAsync(username, includeInactive: false);
if (conversation == default)
{
return NotFound();
}
await Messages.Conversations.RemoveAsync(username);
return StatusCode(204);
}
///
/// Gets all active conversations.
///
///
/// The request completed successfully.
[HttpGet("")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(List), 200)]
public async Task GetAll([FromQuery] bool includeInactive = false, [FromQuery]bool unAcknowledgedOnly = false)
{
if (Program.IsRelayAgent)
{
return Forbid();
}
var conversations = await Messages.Conversations.ListAsync(c => includeInactive || c.IsActive);
if (unAcknowledgedOnly)
{
conversations = conversations.Where(c => c.HasUnAcknowledgedMessages);
}
return Ok(conversations);
}
///
/// Gets the conversation associated with the specified username.
///
/// The username associated with the desired conversation.
///
///
/// The request completed successfully.
/// A matching search was not found.
[HttpGet("{username}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(Conversation), 200)]
[ProducesResponseType(404)]
public async Task GetByUsername([FromRoute]string username, [FromQuery]bool includeMessages = true)
{
if (Program.IsRelayAgent)
{
return Forbid();
}
var conversation = await Messages.Conversations.FindAsync(username, includeMessages: includeMessages);
if (conversation == default)
{
return NotFound();
}
return Ok(conversation);
}
[HttpGet("{username}/messages")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(List), 200)]
[ProducesResponseType(404)]
public async Task GetMessagesByUsername([FromRoute]string username, [FromQuery]bool unAcknowledgedOnly = false)
{
if (Program.IsRelayAgent)
{
return Forbid();
}
var conversation = await Messages.Conversations.FindAsync(username, includeMessages: true);
if (conversation == default)
{
return NotFound();
}
var messages = conversation.Messages;
if (unAcknowledgedOnly)
{
messages = messages.Where(m => !m.IsAcknowledged);
}
return Ok(messages);
}
///
/// Sends a private message to the specified username.
///
///
///
///
/// The request completed successfully.
/// The specified message is null or empty.
[HttpPost("{username}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
public async Task Send([FromRoute]string username, [FromBody]string message)
{
if (Program.IsRelayAgent)
{
return Forbid();
}
if (string.IsNullOrEmpty(message))
{
return BadRequest();
}
await Messages.Conversations.SendMessageAsync(username, message);
return StatusCode(201);
}
}
}