Columns of PinnedTags can have Include additional tags for this column enabled

This adds not only filter capabilities, which are not relevent to this project
like `not` and `all` but also has the `any` field, which is an array where
you may add more tags.

Unfortunatly, I was unable to create a precise select query for this, as the
database field `data` is not jsonb. I think parsing json  is really the most
optimal way under these circumstances.
This commit is contained in:
Nico Berlee 2022-12-24 21:33:38 +01:00
parent 1b1a8d5d0b
commit 04af58dde1
No known key found for this signature in database
GPG Key ID: 539FFB087CD57940

View File

@ -1,4 +1,5 @@
using Npgsql;
using System.Text.Json;
namespace GetMoarFediverse;
@ -36,11 +37,33 @@ public static class MastodonConnectionHelper
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);
// Column 0: the 'original' tag with was pinned
// Column 1: Config of 'Include additional tags for this column' this includes the the tags in 'any' array.
await using var cmd = new NpgsqlCommand(@"
SELECT DISTINCT col->'params'->>'id', col->'params'->>'tags'
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));
if (reader.IsDBNull(1)) continue;
var doc = JsonDocument.Parse(reader.GetString(1));
var anyArray = doc.RootElement.GetProperty("any");
foreach (var item in anyArray.EnumerateArray())
{
var value = item.GetProperty("value");
if (value.ValueKind != JsonValueKind.Null)
{
var valuestring = value.GetString();
if (valuestring.HasValue())
res.Add(valuestring);
}
}
}
return res;
}