From 1b1a8d5d0b50720659a276278dbcf90d95a6fb88 Mon Sep 17 00:00:00 2001 From: Nico Berlee Date: Sat, 24 Dec 2022 16:35:28 +0100 Subject: [PATCH] Get PinnedTags from database, use them if "PinnedTags": true This need `GRANT SELECT ON web_settings TO mastodon_read;` Fixes #25 --- src/Config.cs | 7 +++++-- src/MastodonConnectionHelper.cs | 21 +++++++++++++++++++++ src/Program.cs | 4 ++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Config.cs b/src/Config.cs index 2e160d2..8df9ebe 100644 --- a/src/Config.cs +++ b/src/Config.cs @@ -11,17 +11,19 @@ public class Config public string FakeRelayUrl { get; } public string FakeRelayApiKey { get; } public string? MastodonPostgresConnectionString { get; } + public bool PinnedTags { get; } public ImmutableArray Tags { get; } public ImmutableArray Sites { get; } private Config(string importedPath, string fakeRelayUrl, string fakeRelayApiKey, string? mastodonPostgresConnectionString, - ImmutableArray tags, ImmutableArray sites) + bool pinnedTags, ImmutableArray tags, ImmutableArray sites) { ImportedPath = importedPath; FakeRelayUrl = fakeRelayUrl; FakeRelayApiKey = fakeRelayApiKey; MastodonPostgresConnectionString = mastodonPostgresConnectionString; + PinnedTags = pinnedTags; Tags = tags; Sites = sites; } @@ -68,7 +70,7 @@ public class Config } Instance = new Config(importedPath, data.FakeRelayUrl, apiKey, data.MastodonPostgresConnectionString, - data.Tags.ToImmutableArray(), data.GetImmutableSites()); + data.PinnedTags, data.Tags.ToImmutableArray(), data.GetImmutableSites()); } public class ConfigData @@ -76,6 +78,7 @@ public class Config public string? FakeRelayUrl { get; set; } public string? FakeRelayApiKey { get; set; } public string? MastodonPostgresConnectionString { get; set; } + public bool PinnedTags { get; set; } public string[]? Instances { get; set; } public string[]? Tags { get; set; } public InternalSiteData[]? Sites { get; set; } diff --git a/src/MastodonConnectionHelper.cs b/src/MastodonConnectionHelper.cs index 968eaa1..dbb2b86 100644 --- a/src/MastodonConnectionHelper.cs +++ b/src/MastodonConnectionHelper.cs @@ -23,4 +23,25 @@ public static class MastodonConnectionHelper return res; } + + public static async Task> GetPinnedTagsAsync() + { + if (Config.Instance == null) throw new Exception("Config object is not initialized"); + if (Config.Instance.MastodonPostgresConnectionString.IsNullOrEmpty()) + { + throw new Exception("Missing mastodon postgres connection string"); + } + + await using var conn = new NpgsqlConnection(Config.Instance.MastodonPostgresConnectionString); + await conn.OpenAsync(); + + var res = new List(); + await using var cmd = new NpgsqlCommand("SELECT DISTINCT col->'params'->>'id' FROM web_settings, json_array_elements(data->'columns') col WHERE col->>'id' = 'HASHTAG' AND col->'params'->>'id' IS NOT NULL ORDER BY col->'params'->>'id' ASC", conn); + await using var reader = await cmd.ExecuteReaderAsync(); + while (await reader.ReadAsync()) + res.Add(reader.GetString(0)); + + return res; + } + } diff --git a/src/Program.cs b/src/Program.cs index d9f4bf9..879710d 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -50,6 +50,10 @@ int numberOfTags; if (Config.Instance.MastodonPostgresConnectionString.HasValue()) { var tags = await MastodonConnectionHelper.GetFollowedTagsAsync(); + if (Config.Instance.PinnedTags) + { + tags = tags.Concat(await MastodonConnectionHelper.GetPinnedTagsAsync()).Distinct().ToList(); + } numberOfTags = tags.Count; sitesTags = Config.Instance.Sites .SelectMany(s => tags.Select(t => (s.Host, t)))