//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ 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, version 3.
// │
// │ 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/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
namespace slskd.Search.API
{
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
public static class SearchHubMethods
{
public static readonly string List = "LIST";
public static readonly string Create = "CREATE";
public static readonly string Response = "RESPONSE";
public static readonly string Update = "UPDATE";
public static readonly string Delete = "DELETE";
}
///
/// Extension methods for the search SignalR hub.
///
public static class SearchHubExtensions
{
///
/// Broadcast an update for a search.
///
///
/// Responses are removed prior to sending.
///
/// The hub.
/// The search to broadcast.
/// The operation context.
public static Task BroadcastUpdateAsync(this IHubContext hub, Search search)
{
return hub.Clients.All.SendAsync(SearchHubMethods.Update, search with { Responses = null });
}
///
/// Broadcast a newly received search response.
///
/// The hub.
/// The ID of the search associated with the response.
/// The response to broadcast.
/// The operation context.
public static Task BroadcastResponseAsync(this IHubContext hub, Guid searchId, Soulseek.SearchResponse response)
{
return hub.Clients.All.SendAsync(SearchHubMethods.Response, new { searchId, response });
}
///
/// Broadcast the creation of a new search.
///
///
/// Responses are removed prior to sending.
///
/// The hub.
/// The search to broadcast.
/// The operation context.
public static Task BroadcastCreateAsync(this IHubContext hub, Search search)
{
return hub.Clients.All.SendAsync(SearchHubMethods.Create, search with { Responses = null });
}
///
/// Broadcast the deletion of a search.
///
///
/// Responses are removed prior to sending.
///
/// The hub.
/// The search to broadcast.
/// The operation context.
public static Task BroadcastDeleteAsync(this IHubContext hub, Search search)
{
return hub.Clients.All.SendAsync(SearchHubMethods.Delete, search with { Responses = null });
}
}
///
/// The search SignalR hub.
///
[Authorize(Policy = AuthPolicy.Any)]
public class SearchHub : Hub
{
public SearchHub(
ISearchService searchService)
{
Searches = searchService;
}
private ISearchService Searches { get; }
public override async Task OnConnectedAsync()
{
var searches = await Searches.ListAsync();
await Clients.Caller.SendAsync(SearchHubMethods.List, searches);
}
}
}