[PLUGIN][Pinboard] For tag list request, respond with the most common variant and the corresponding count for each canon tag

This commit is contained in:
Hugo Sales 2022-04-01 02:10:12 +01:00
parent 539104ec33
commit 556ac85061
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -393,21 +393,26 @@ class APIv1 extends Controller
$user = $check;
}
$tags_freq = [];
// Convert the list of all tags to a map from canon to
// variations (i.e. tags that canonicalize to the same)
$canon_to_variations = [];
foreach ($user->getActor()->getNoteTags(Pin::note_type) as $tag) {
if (!isset($tags_freq[$tag->getCanonical()])) {
$tags_freq[$tag->getCanonical()] = [];
if (!isset($canon_to_variations[$tag->getCanonical()])) {
$canon_to_variations[$tag->getCanonical()] = [];
}
$tags_freq[$tag->getCanonical()][] = $tag->getTag();
$canon_to_variations[$tag->getCanonical()][] = $tag->getTag();
}
foreach ($tags_freq as $canon => $variations) {
// Pick the most common variation and assign to it the count
// of all the variations of each canon tag
$most_common_variant_frequencies = [];
foreach ($canon_to_variations as $variations) {
$freqs = array_count_values($variations);
arsort($freqs);
$tags_freq[$canon] = $freqs[array_key_first($freqs)];
$most_common_variant_frequencies[array_key_first($freqs)] = \count($variations);
}
return self::respond($tags_freq);
return self::respond($most_common_variant_frequencies);
}
public function unimplmented(Request $request)