Get PinnedTags from database, use them if "PinnedTags": true

This need `GRANT SELECT ON web_settings TO mastodon_read;`
Fixes #25
This commit is contained in:
Nico Berlee 2022-12-24 16:35:28 +01:00
parent db4023797a
commit 1b1a8d5d0b
No known key found for this signature in database
GPG Key ID: 539FFB087CD57940
3 changed files with 30 additions and 2 deletions

View File

@ -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<string> Tags { get; }
public ImmutableArray<SiteData> Sites { get; }
private Config(string importedPath, string fakeRelayUrl, string fakeRelayApiKey, string? mastodonPostgresConnectionString,
ImmutableArray<string> tags, ImmutableArray<SiteData> sites)
bool pinnedTags, ImmutableArray<string> tags, ImmutableArray<SiteData> 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; }

View File

@ -23,4 +23,25 @@ public static class MastodonConnectionHelper
return res;
}
public static async Task<List<string>> 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<string>();
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;
}
}

View File

@ -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)))