//
// 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/.
//
namespace slskd.Messaging
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Serilog;
using Soulseek;
///
/// Manages private messages.
///
public interface IConversationService
{
///
/// Acknowledges all unacknowledged records from the specified .
///
/// The username associated with the conversation.
/// The operation context.
Task AcknowledgeAsync(string username);
///
/// Acknowledges the record associated with the specified and .
///
/// The username associated with the conversation.
/// The ID of the message.
/// The operation context.
Task AcknowledgeMessageAsync(string username, int id);
///
/// Creates a new, or activates an existing, conversation with the specified .
///
/// The username associated with the conversation.
/// The operation context.
Task CreateAsync(string username);
///
/// Returns the matching the specified .
///
/// An expression used to locate the conversation.
/// The operation context, including the located conversation, if one was found.
Task FindAsync(Expression> expression);
///
/// Returns the list of all records matching the specified .
///
/// An optional expression used to locate conversations.
/// The operation context, including the list of found conversations.
Task> ListAsync(Expression> expression = null);
///
/// Handles the receipt of an inbound .
///
/// The username associated with the message.
/// The message.
/// The operation context.
Task HandleMessageAsync(string username, PrivateMessage message);
///
/// Removes (marks inactive) the conversation with the specified .
///
/// The username associated with the conversation.
/// The operation context.
Task RemoveAsync(string username);
///
/// Sends the specified to the specified .
///
/// The username of the recipient.
/// The message.
/// The operation context.
Task SendMessageAsync(string username, string message);
}
public class ConversationService : IConversationService
{
public ConversationService(
ISoulseekClient soulseekClient,
IDbContextFactory contextFactory)
{
SoulseekClient = soulseekClient;
ContextFactory = contextFactory;
}
private IDbContextFactory ContextFactory { get; }
private ILogger Log { get; } = Serilog.Log.ForContext();
private ISoulseekClient SoulseekClient { get; }
///
/// Acknowledges all unacknowledged records from the specified .
///
/// The username associated with the conversation.
/// The operation context.
public async Task AcknowledgeAsync(string username)
{
using var context = ContextFactory.CreateDbContext();
var unacked = context.PrivateMessages
.Where(m => m.Username == username && !m.Acknowledged)
.Select(m => AcknowledgeMessageAsync(username, m.Id));
await Task.WhenAll(unacked);
}
///
/// Acknowledges the record associated with the specified and .
///
/// The username associated with the conversation.
/// The ID of the message.
/// The operation context.
public async Task AcknowledgeMessageAsync(string username, int id)
{
using var context = ContextFactory.CreateDbContext();
var message = context.PrivateMessages.FirstOrDefault(m => m.Username == username && m.Id == id);
if (message != default)
{
await SoulseekClient.AcknowledgePrivateMessageAsync(id);
message.Acknowledged = true;
context.SaveChanges();
}
else
{
Log.Warning("Attempted to acknowledge an unknown private message from {Username} with ID {Id}", username, id);
}
}
///
/// Creates a new, or activates an existing, conversation with the specified .
///
/// The username associated with the conversation.
/// The operation context.
public Task CreateAsync(string username)
{
ActivateConversation(username);
return Task.CompletedTask;
}
///
/// Returns the matching the specified .
///
/// An expression used to locate the conversation.
/// The operation context, including the located conversation, if one was found.
public Task FindAsync(Expression> expression)
{
using var context = ContextFactory.CreateDbContext();
var conversation = context.Conversations
.AsNoTracking()
.Where(expression)
.FirstOrDefault();
if (conversation != default)
{
// add an option to limit this, or figure out pagination.
conversation.Messages = context.PrivateMessages.Where(m => m.Username == conversation.Username);
}
return Task.FromResult(conversation);
}
///
/// Returns the list of all records matching the specified .
///
/// An optional expression used to locate conversations.
/// The operation context, including the list of found conversations.
public Task> ListAsync(Expression> expression = null)
{
using var context = ContextFactory.CreateDbContext();
return Task.FromResult(context.Conversations.Where(expression).AsEnumerable());
}
///
/// Handles the receipt of an inbound .
///
/// The username associated with the message.
/// The message.
/// The operation context.
public Task HandleMessageAsync(string username, PrivateMessage message)
{
ActivateConversation(username);
using var context = ContextFactory.CreateDbContext();
// the server replays unacked messages when we log in. figure out if we've seen this message before, and if so sync it.
var existing = context.PrivateMessages.FirstOrDefault(m =>
m.Username == message.Username &&
m.Message == message.Message &&
m.Id == message.Id);
if (existing != null)
{
// the message was replayed. i'm not sure what is updated, so just update anything that might have updated
existing.Timestamp = message.Timestamp;
existing.Replayed = message.Replayed;
existing.Acknowledged = message.Acknowledged;
}
else
{
// this is a new message, so append it
context.PrivateMessages.Add(message);
}
context.SaveChanges();
return Task.CompletedTask;
}
///
/// Removes (marks inactive) the conversation with the specified .
///
/// The username associated with the conversation.
/// The operation context.
public async Task RemoveAsync(string username)
{
await AcknowledgeAsync(username);
using var context = ContextFactory.CreateDbContext();
var conversation = context.Conversations.FirstOrDefault(c => c.Username == username);
if (conversation != default)
{
conversation.Active = false;
context.SaveChanges();
}
else
{
Log.Warning("Attempted to remove an unknown conversation with {Username}", username);
}
}
///
/// Sends the specified to the specified .
///
/// The username of the recipient.
/// The message.
/// The operation context.
public async Task SendMessageAsync(string username, string message)
{
ActivateConversation(username);
// send the message over the network, then persist this should *probably* use a transaction but i'm scared of locking
// with SQLite so i won't
await SoulseekClient.SendPrivateMessageAsync(username, message);
using var context = ContextFactory.CreateDbContext();
context.PrivateMessages.Add(new PrivateMessage
{
Acknowledged = true,
Id = 0,
Message = message,
Replayed = false,
Timestamp = DateTime.UtcNow,
Username = username,
Direction = MessageDirection.Out,
});
context.SaveChanges();
}
private void ActivateConversation(string username)
{
using var context = ContextFactory.CreateDbContext();
var conversation = context.Conversations.FirstOrDefault(c => c.Username == username);
if (conversation != default)
{
conversation.Active = true;
}
else
{
context.Conversations.Add(new Conversation { Username = username, Active = true });
}
context.SaveChanges();
}
}
}