145 lines
4.5 KiB
C#
145 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Jellyfin.Plugin.Herald.Notices;
|
|
|
|
/// <summary>
|
|
/// The decisions around who sees a notice and which responses are allowed.
|
|
/// </summary>
|
|
public static class NoticeRules
|
|
{
|
|
/// <summary>
|
|
/// Maximum title length.
|
|
/// </summary>
|
|
public const int MaxTitleLength = 200;
|
|
|
|
/// <summary>
|
|
/// Maximum body length.
|
|
/// </summary>
|
|
public const int MaxBodyLength = 20000;
|
|
|
|
/// <summary>
|
|
/// Longest wait before a notice can be accepted, in seconds.
|
|
/// </summary>
|
|
public const int MaxAcceptDelaySeconds = 600;
|
|
|
|
/// <summary>
|
|
/// Checks whether a notice is addressed to a user.
|
|
/// </summary>
|
|
/// <param name="notice">The notice.</param>
|
|
/// <param name="userId">The user id.</param>
|
|
/// <returns><c>true</c> if the user is addressed.</returns>
|
|
public static bool IsTargeted(Notice notice, Guid userId)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(notice);
|
|
return notice.Target.AllUsers || notice.Target.UserIds.Contains(userId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a notice still has to be shown to a user.
|
|
/// </summary>
|
|
/// <param name="notice">The notice.</param>
|
|
/// <param name="userId">The user id.</param>
|
|
/// <param name="userAcks">The acknowledgements of that user.</param>
|
|
/// <returns><c>true</c> if the notice is pending.</returns>
|
|
public static bool IsPending(Notice notice, Guid userId, IEnumerable<NoticeAck> userAcks)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(notice);
|
|
ArgumentNullException.ThrowIfNull(userAcks);
|
|
|
|
if (!notice.Enabled || !IsTargeted(notice, userId))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return !userAcks.Any(a => a.UserId == userId && a.NoticeId == notice.Id && a.Revision == notice.Revision);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Selects the notices a user still has to see, oldest first.
|
|
/// </summary>
|
|
/// <param name="notices">All notices.</param>
|
|
/// <param name="userId">The user id.</param>
|
|
/// <param name="userAcks">The acknowledgements of that user.</param>
|
|
/// <returns>The pending notices.</returns>
|
|
public static IReadOnlyList<Notice> SelectPending(IEnumerable<Notice> notices, Guid userId, IReadOnlyCollection<NoticeAck> userAcks)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(notices);
|
|
|
|
return notices
|
|
.Where(n => IsPending(n, userId, userAcks))
|
|
.OrderBy(n => n.CreatedAt)
|
|
.ThenBy(n => n.Id)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates a response to a notice.
|
|
/// </summary>
|
|
/// <param name="notice">The notice, or <c>null</c> if it does not exist.</param>
|
|
/// <param name="userId">The responding user.</param>
|
|
/// <param name="revision">The revision the user saw.</param>
|
|
/// <param name="action">The response.</param>
|
|
/// <returns>The validation outcome.</returns>
|
|
public static AckResult ValidateAck(Notice? notice, Guid userId, int revision, AckAction action)
|
|
{
|
|
if (notice is null || !notice.Enabled || !IsTargeted(notice, userId))
|
|
{
|
|
return AckResult.NotFound;
|
|
}
|
|
|
|
if (revision != notice.Revision)
|
|
{
|
|
return AckResult.RevisionMismatch;
|
|
}
|
|
|
|
if (notice.RequireAccept && action != AckAction.Accepted)
|
|
{
|
|
return AckResult.AcceptRequired;
|
|
}
|
|
|
|
return AckResult.Ok;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates the editable fields of a notice.
|
|
/// </summary>
|
|
/// <param name="draft">The draft.</param>
|
|
/// <returns>An error message, or <c>null</c> if the draft is valid.</returns>
|
|
public static string? ValidateDraft(NoticeDraft? draft)
|
|
{
|
|
if (draft is null)
|
|
{
|
|
return "A notice is required.";
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(draft.Title))
|
|
{
|
|
return "Title is required.";
|
|
}
|
|
|
|
if (draft.Title.Trim().Length > MaxTitleLength)
|
|
{
|
|
return "Title is too long.";
|
|
}
|
|
|
|
if ((draft.Body?.Length ?? 0) > MaxBodyLength)
|
|
{
|
|
return "Body is too long.";
|
|
}
|
|
|
|
if (draft.AcceptDelaySeconds < 0 || draft.AcceptDelaySeconds > MaxAcceptDelaySeconds)
|
|
{
|
|
return "The wait before accepting has to be between 0 and 600 seconds.";
|
|
}
|
|
|
|
if (draft.Target is { AllUsers: false } target && (target.UserIds is null || target.UserIds.Count == 0))
|
|
{
|
|
return "Select at least one user or address all users.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|