From ed0c28f24efbdcfe46e771b014e864f0334b360b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tao=20Bojl=C3=A9n?= <66130243+taobojlen@users.noreply.github.com> Date: Sat, 10 Jun 2023 20:41:38 +0100 Subject: [PATCH] fix nodeinfo for some pixelfed instances --- .../lib/backend/crawler/crawlers/nodeinfo.ex | 18 +++- .../crawler/crawlers/nodeinfo_test.exs | 35 ++++++++ .../support/data/json/nodeinfo/pixelfed.json | 89 +++++++++++++++++++ 3 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 backend/test/support/data/json/nodeinfo/pixelfed.json diff --git a/backend/lib/backend/crawler/crawlers/nodeinfo.ex b/backend/lib/backend/crawler/crawlers/nodeinfo.ex index d26cdec..e69f2f3 100644 --- a/backend/lib/backend/crawler/crawlers/nodeinfo.ex +++ b/backend/lib/backend/crawler/crawlers/nodeinfo.ex @@ -85,7 +85,9 @@ defmodule Backend.Crawler.Crawlers.Nodeinfo do description = [ get_in(nodeinfo, ["metadata", "description"]), - get_in(nodeinfo, ["metadata", "nodeDescription"]) + get_in(nodeinfo, ["metadata", "nodeDescription"]), + # pixelfed + get_in(nodeinfo, ["metadata", "config", "site", "description"]) ] |> Enum.filter(fn d -> d != nil end) |> Enum.at(0) @@ -96,8 +98,8 @@ defmodule Backend.Crawler.Crawlers.Nodeinfo do ApiCrawler.get_default(), %{ description: description, - user_count: user_count, - status_count: get_in(nodeinfo, ["usage", "localPosts"]), + user_count: handle_count(user_count), + status_count: nodeinfo |> get_in(["usage", "localPosts"]) |> handle_count(), instance_type: type, version: get_in(nodeinfo, ["software", "version"]), federation_restrictions: get_federation_restrictions(nodeinfo) @@ -152,4 +154,14 @@ defmodule Backend.Crawler.Crawlers.Nodeinfo do quarantined_domains end end + + # handle a count that may be formatted as a string or an integer + defp handle_count(count) do + if is_integer(count) do + count + else + {count, _rem} = Integer.parse(count) + count + end + end end diff --git a/backend/test/backend/crawler/crawlers/nodeinfo_test.exs b/backend/test/backend/crawler/crawlers/nodeinfo_test.exs index a6ca1f5..240bcfe 100644 --- a/backend/test/backend/crawler/crawlers/nodeinfo_test.exs +++ b/backend/test/backend/crawler/crawlers/nodeinfo_test.exs @@ -154,5 +154,40 @@ defmodule Backend.Crawler.Crawlers.NodeinfoTest do peers: [] } end + + # don't know why some pixelfed instances return numbers as strings + # but i've seen it in the wild, so we need to handle it + test "handles nodeinfo with some numbers stringified (pixelfed)" do + expect(HttpMock, :get_and_decode, fn "https://pixelfed.social/.well-known/nodeinfo" -> + {:ok, + %{ + "links" => [ + %{ + "rel" => "http://nodeinfo.diaspora.software/ns/schema/2.0", + "href" => "https://pixelfed.social/nodeinfo/2.0.json" + } + ] + }} + end) + + expect(HttpMock, :get_and_decode, fn "https://pixelfed.social/nodeinfo/2.0.json" -> + {:ok, TestHelpers.load_json("nodeinfo/pixelfed.json")} + end) + + result = Nodeinfo.crawl("pixelfed.social", %{}) + + assert result == %{ + description: + "Pixelfed is an image sharing platform, an ethical alternative to centralized platforms.", + user_count: 16, + status_count: 60, + statuses_seen: 0, + instance_type: :pixelfed, + version: "0.11.2", + federation_restrictions: [], + interactions: %{}, + peers: [] + } + end end end diff --git a/backend/test/support/data/json/nodeinfo/pixelfed.json b/backend/test/support/data/json/nodeinfo/pixelfed.json new file mode 100644 index 0000000..dc9ba68 --- /dev/null +++ b/backend/test/support/data/json/nodeinfo/pixelfed.json @@ -0,0 +1,89 @@ +{ + "metadata": { + "nodeName": "Pixelfed", + "software": { + "homepage": "https://pixelfed.org", + "repo": "https://github.com/pixelfed/pixelfed" + }, + "config": { + "open_registration": true, + "uploader": { + "max_photo_size": "15000", + "max_caption_length": "500", + "album_limit": "4", + "image_quality": 80, + "max_collection_length": 18, + "optimize_image": true, + "optimize_video": true, + "media_types": "image/jpeg,image/png,image/gif", + "enforce_account_limit": true + }, + "activitypub": { + "enabled": true, + "remote_follow": true + }, + "ab": { + "lc": false, + "rec": false, + "loops": false, + "top": false, + "polls": false, + "cached_public_timeline": false, + "gps": false, + "spa": true, + "emc": false + }, + "site": { + "name": "Pixelfe", + "domain": "pixelfed.example.com", + "url": "https://pixelfed.example.com", + "description": "Pixelfed is an image sharing platform, an ethical alternative to centralized platforms." + }, + "username": { + "remote": { + "formats": ["@", "from", "custom"], + "format": "@", + "custom": null + } + }, + "features": { + "mobile_apis": true, + "circles": false, + "stories": false, + "video": false, + "import": { + "instagram": false, + "mastodon": false, + "pixelfed": false + }, + "label": { + "covid": { + "enabled": true, + "org": "visit the WHO website", + "url": "https://www.who.int/emergencies/diseases/novel-coronavirus-2019/advice-for-public" + } + } + } + } + }, + "protocols": ["activitypub"], + "services": { + "inbound": [], + "outbound": [] + }, + "software": { + "name": "pixelfed", + "version": "0.11.2" + }, + "usage": { + "localPosts": "60", + "localComments": 0, + "users": { + "total": "16", + "activeHalfyear": 16, + "activeMonth": 2 + } + }, + "version": "2.0", + "openRegistrations": true +}