202 lines
7.7 KiB
C#
202 lines
7.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using Jellyfin.Plugin.Herald.Notices;
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
namespace Jellyfin.Plugin.Herald.Storage;
|
|
|
|
/// <summary>
|
|
/// SQLite backed <see cref="INoticeStore"/>.
|
|
/// </summary>
|
|
public class SqliteNoticeStore : INoticeStore
|
|
{
|
|
private const string NoticeColumns = "id, title, body, require_accept, enabled, target_all, target_users, revision, created_at, updated_at, accept_delay";
|
|
private const string AckColumns = "notice_id, revision, user_id, action, timestamp";
|
|
|
|
private readonly HeraldDatabase _database;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SqliteNoticeStore"/> class.
|
|
/// </summary>
|
|
/// <param name="database">The database.</param>
|
|
public SqliteNoticeStore(HeraldDatabase database)
|
|
{
|
|
_database = database;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IReadOnlyList<Notice> GetNotices()
|
|
{
|
|
return _database.Read(connection =>
|
|
{
|
|
using var command = Db.Command(connection, null, "SELECT " + NoticeColumns + " FROM notices ORDER BY created_at, id;");
|
|
using var reader = command.ExecuteReader();
|
|
var notices = new List<Notice>();
|
|
while (reader.Read())
|
|
{
|
|
notices.Add(ReadNotice(reader));
|
|
}
|
|
|
|
return notices;
|
|
});
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Notice? GetNotice(Guid id)
|
|
{
|
|
return _database.Read(connection =>
|
|
{
|
|
using var command = Db.Command(connection, null, "SELECT " + NoticeColumns + " FROM notices WHERE id = $id;");
|
|
command.Parameters.AddWithValue("$id", Db.Id(id));
|
|
using var reader = command.ExecuteReader();
|
|
return reader.Read() ? ReadNotice(reader) : null;
|
|
});
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void SaveNotice(Notice notice)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(notice);
|
|
|
|
_database.Write((connection, transaction) =>
|
|
{
|
|
using var command = Db.Command(
|
|
connection,
|
|
transaction,
|
|
"""
|
|
INSERT INTO notices (id, title, body, require_accept, enabled, target_all, target_users, revision, created_at, updated_at, accept_delay)
|
|
VALUES ($id, $title, $body, $requireAccept, $enabled, $targetAll, $targetUsers, $revision, $createdAt, $updatedAt, $acceptDelay)
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
title = excluded.title,
|
|
body = excluded.body,
|
|
require_accept = excluded.require_accept,
|
|
accept_delay = excluded.accept_delay,
|
|
enabled = excluded.enabled,
|
|
target_all = excluded.target_all,
|
|
target_users = excluded.target_users,
|
|
revision = excluded.revision,
|
|
updated_at = excluded.updated_at;
|
|
""");
|
|
command.Parameters.AddWithValue("$id", Db.Id(notice.Id));
|
|
command.Parameters.AddWithValue("$title", notice.Title);
|
|
command.Parameters.AddWithValue("$body", notice.Body);
|
|
command.Parameters.AddWithValue("$requireAccept", notice.RequireAccept);
|
|
command.Parameters.AddWithValue("$enabled", notice.Enabled);
|
|
command.Parameters.AddWithValue("$targetAll", notice.Target.AllUsers);
|
|
command.Parameters.AddWithValue("$targetUsers", JsonSerializer.Serialize(notice.Target.UserIds.Select(Db.Id)));
|
|
command.Parameters.AddWithValue("$revision", notice.Revision);
|
|
command.Parameters.AddWithValue("$createdAt", Db.Time(notice.CreatedAt));
|
|
command.Parameters.AddWithValue("$updatedAt", Db.Time(notice.UpdatedAt));
|
|
command.Parameters.AddWithValue("$acceptDelay", notice.AcceptDelaySeconds);
|
|
return command.ExecuteNonQuery();
|
|
});
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public bool DeleteNotice(Guid id)
|
|
{
|
|
return _database.Write((connection, transaction) =>
|
|
{
|
|
using var acks = Db.Command(connection, transaction, "DELETE FROM notice_acks WHERE notice_id = $id;");
|
|
acks.Parameters.AddWithValue("$id", Db.Id(id));
|
|
acks.ExecuteNonQuery();
|
|
|
|
using var notice = Db.Command(connection, transaction, "DELETE FROM notices WHERE id = $id;");
|
|
notice.Parameters.AddWithValue("$id", Db.Id(id));
|
|
return notice.ExecuteNonQuery() > 0;
|
|
});
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IReadOnlyList<NoticeAck> GetAcksByUser(Guid userId)
|
|
{
|
|
return _database.Read(connection =>
|
|
{
|
|
using var command = Db.Command(connection, null, "SELECT " + AckColumns + " FROM notice_acks WHERE user_id = $userId;");
|
|
command.Parameters.AddWithValue("$userId", Db.Id(userId));
|
|
return ReadAcks(command);
|
|
});
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IReadOnlyList<NoticeAck> GetAcks(Guid noticeId, int revision)
|
|
{
|
|
return _database.Read(connection =>
|
|
{
|
|
using var command = Db.Command(
|
|
connection,
|
|
null,
|
|
"SELECT " + AckColumns + " FROM notice_acks WHERE notice_id = $noticeId AND revision = $revision ORDER BY timestamp;");
|
|
command.Parameters.AddWithValue("$noticeId", Db.Id(noticeId));
|
|
command.Parameters.AddWithValue("$revision", revision);
|
|
return ReadAcks(command);
|
|
});
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void AddAck(NoticeAck ack)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(ack);
|
|
|
|
_database.Write((connection, transaction) =>
|
|
{
|
|
using var command = Db.Command(
|
|
connection,
|
|
transaction,
|
|
"""
|
|
INSERT OR IGNORE INTO notice_acks (notice_id, revision, user_id, action, timestamp)
|
|
VALUES ($noticeId, $revision, $userId, $action, $timestamp);
|
|
""");
|
|
command.Parameters.AddWithValue("$noticeId", Db.Id(ack.NoticeId));
|
|
command.Parameters.AddWithValue("$revision", ack.Revision);
|
|
command.Parameters.AddWithValue("$userId", Db.Id(ack.UserId));
|
|
command.Parameters.AddWithValue("$action", (int)ack.Action);
|
|
command.Parameters.AddWithValue("$timestamp", Db.Time(ack.Timestamp));
|
|
return command.ExecuteNonQuery();
|
|
});
|
|
}
|
|
|
|
private static List<NoticeAck> ReadAcks(SqliteCommand command)
|
|
{
|
|
using var reader = command.ExecuteReader();
|
|
var acks = new List<NoticeAck>();
|
|
while (reader.Read())
|
|
{
|
|
acks.Add(new NoticeAck
|
|
{
|
|
NoticeId = Db.Id(reader.GetString(0)),
|
|
Revision = reader.GetInt32(1),
|
|
UserId = Db.Id(reader.GetString(2)),
|
|
Action = (AckAction)reader.GetInt32(3),
|
|
Timestamp = Db.Time(reader.GetString(4))
|
|
});
|
|
}
|
|
|
|
return acks;
|
|
}
|
|
|
|
private static Notice ReadNotice(SqliteDataReader reader)
|
|
{
|
|
var userIds = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)) ?? [];
|
|
return new Notice
|
|
{
|
|
Id = Db.Id(reader.GetString(0)),
|
|
Title = reader.GetString(1),
|
|
Body = reader.GetString(2),
|
|
RequireAccept = reader.GetBoolean(3),
|
|
AcceptDelaySeconds = reader.GetInt32(10),
|
|
Enabled = reader.GetBoolean(4),
|
|
Target = new NoticeTarget
|
|
{
|
|
AllUsers = reader.GetBoolean(5),
|
|
UserIds = userIds.Select(Db.Id).ToList()
|
|
},
|
|
Revision = reader.GetInt32(7),
|
|
CreatedAt = Db.Time(reader.GetString(8)),
|
|
UpdatedAt = Db.Time(reader.GetString(9))
|
|
};
|
|
}
|
|
}
|