From 04af58dde1febfb7142573adec31090a8836d7b4 Mon Sep 17 00:00:00 2001 From: Nico Berlee Date: Sat, 24 Dec 2022 21:33:38 +0100 Subject: [PATCH] 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. --- src/MastodonConnectionHelper.cs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/MastodonConnectionHelper.cs b/src/MastodonConnectionHelper.cs index dbb2b86..1babebe 100644 --- a/src/MastodonConnectionHelper.cs +++ b/src/MastodonConnectionHelper.cs @@ -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(); - 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; }