From 6f7e223b3d69e5b13cc124112b46f0f485374827 Mon Sep 17 00:00:00 2001 From: marisa Date: Fri, 23 Feb 2024 13:33:37 -0300 Subject: [PATCH] AP: move types to their own directory --- source/ap/{ => types}/actor.d | 4 +- source/ap/types/collection.d | 28 ++++ source/ap/types/collection_page.d | 24 +++ source/ap/types/link.d | 105 +++++++++++++ .../ap/{activity_stream.d => types/object.d} | 138 +----------------- source/ap/util.d | 7 +- 6 files changed, 165 insertions(+), 141 deletions(-) rename source/ap/{ => types}/actor.d (97%) create mode 100644 source/ap/types/collection.d create mode 100644 source/ap/types/collection_page.d create mode 100644 source/ap/types/link.d rename source/ap/{activity_stream.d => types/object.d} (61%) diff --git a/source/ap/actor.d b/source/ap/types/actor.d similarity index 97% rename from source/ap/actor.d rename to source/ap/types/actor.d index b072350..80af734 100644 --- a/source/ap/actor.d +++ b/source/ap/types/actor.d @@ -1,8 +1,8 @@ -module ap.actor; +module ap.types.actor; import std.json; -import ap.activity_stream; +import ap.types.object; import util; /++ diff --git a/source/ap/types/collection.d b/source/ap/types/collection.d new file mode 100644 index 0000000..59bc11a --- /dev/null +++ b/source/ap/types/collection.d @@ -0,0 +1,28 @@ +module ap.types.collection; + +import std.json; + +import ap.types.object; +import ap.types.collection_page; +import util; + +class Collection : ASObject { + int totalItems; + CollectionPage current; + CollectionPage first; + CollectionPage last; + ASObject items; + + this(JSONValue json) { + super(json); + + if (this.m_objType != ObjectType.Object) + throw new Exception("Wrong Collection format?"); + + optional(json, "totalItems", totalItems); + current = new CollectionPage(json.optional!JSONValue("current")); + first = new CollectionPage(json.optional!JSONValue("first")); + last = new CollectionPage(json.optional!JSONValue("last")); + items = new ASObject(json.optional!JSONValue("items")); + } +} diff --git a/source/ap/types/collection_page.d b/source/ap/types/collection_page.d new file mode 100644 index 0000000..2d56462 --- /dev/null +++ b/source/ap/types/collection_page.d @@ -0,0 +1,24 @@ +module ap.types.collection_page; + +import std.json; + +import ap.types.collection; +import ap.types.object; +import util; + +class CollectionPage : Collection { + CollectionPage partOf; + CollectionPage next; + CollectionPage prev; + + this(JSONValue json) { + super(json); + + if (this.m_objType != ObjectType.Object) + throw new Exception("Wrong CollectionPage format?"); + + partOf = new CollectionPage(json.optional!JSONValue("partOf")); + next = new CollectionPage(json.optional!JSONValue("next")); + prev = new CollectionPage(json.optional!JSONValue("prev")); + } +} diff --git a/source/ap/types/link.d b/source/ap/types/link.d new file mode 100644 index 0000000..6c14291 --- /dev/null +++ b/source/ap/types/link.d @@ -0,0 +1,105 @@ +module ap.types.link; + +import std.algorithm; +import std.array; +import std.format; +import std.json; + +import ap.types.object; +import util; + +class Link { + string type; + string href; + string[] rel; + string mediaType; + string name; + int height, width; + ASObject preview; + + protected Store m_store; + protected ObjectType m_objType; + + JSONValue raw; + + this() { + } + + this(JSONValue json) { + switch (json.type) { + case JSONType.ARRAY: + m_objType = ObjectType.Array; + m_store.links = json.array.map!(item => new Link(item)).array; + break; + + case JSONType.OBJECT: + m_objType = ObjectType.Object; + + optional(json, "type", type); + optional(json, "href", href); + optional(json, "mediaType", mediaType); + optional(json, "name", name); + optional(json, "height", height); + optional(json, "width", width); + + const(JSONValue)* j; + if ((j = "rel" in json) != null) + rel = (*j).array.map!(item => item.str).array; + + preview = new ASObject(json.optional!JSONValue("preview")); + break; + + case JSONType.STRING: + m_objType = ObjectType.String; + m_store.str = json.str; + break; + + case JSONType.NULL: + break; + + default: + throw new Exception("Unrecognized ActivityStream Link JSON format"); + } + + this.raw = json; + } + + string stringRep(int indentation = 0, string indentStr = " ") const { + string[] output; + string indent = ""; + string j; + + if (this.m_objType == ObjectType.String) + return format("%s%s", indent, this.m_store.str); + + for (int i = 0; i < indentation; i++) + indent ~= indentStr; + + output ~= format("%s {", this.type); + + if (href) + output ~= format("%s%shref = %s", indent, indentStr, href); + + if (mediaType) + output ~= format("%s%smediaType = %s", indent, indentStr, mediaType); + + if (name) + output ~= format("%s%sname = %s", indent, indentStr, name); + + if (height) + output ~= format("%s%sheight = %d", indent, indentStr, height); + + if (width) + output ~= format("%s%swidth = %d", indent, indentStr, width); + + if (preview && (j = preview.stringRep(indentation + 1)) != string.init) + output ~= format("%s%spreview = %s", indent, indentStr, j); + + output ~= indent ~ "}"; + + if (output.length == 2) + return ""; + + return output.join("\n"); + } +} diff --git a/source/ap/activity_stream.d b/source/ap/types/object.d similarity index 61% rename from source/ap/activity_stream.d rename to source/ap/types/object.d index 9e3e5e0..403052b 100644 --- a/source/ap/activity_stream.d +++ b/source/ap/types/object.d @@ -1,4 +1,4 @@ -module ap.activity_stream; +module ap.types.object; import std.algorithm; import std.array; @@ -6,6 +6,8 @@ import std.datetime; import std.format; import std.json; +import ap.types.link; +import ap.types.collection; import util; union Store { @@ -197,137 +199,3 @@ class ASObject { return output.length == 2 ? "" : output.join("\n"); } } - -class Link { - string type; - string href; - string[] rel; - string mediaType; - string name; - int height, width; - ASObject preview; - - protected Store m_store; - protected ObjectType m_objType; - - JSONValue raw; - - this() { - } - - this(JSONValue json) { - switch (json.type) { - case JSONType.ARRAY: - m_objType = ObjectType.Array; - m_store.links = json.array.map!(item => new Link(item)).array; - break; - - case JSONType.OBJECT: - m_objType = ObjectType.Object; - - optional(json, "type", type); - optional(json, "href", href); - optional(json, "mediaType", mediaType); - optional(json, "name", name); - optional(json, "height", height); - optional(json, "width", width); - - const(JSONValue)* j; - if ((j = "rel" in json) != null) - rel = (*j).array.map!(item => item.str).array; - - preview = new ASObject(json.optional!JSONValue("preview")); - break; - - case JSONType.STRING: - m_objType = ObjectType.String; - m_store.str = json.str; - break; - - case JSONType.NULL: - break; - - default: - throw new Exception("Unrecognized ActivityStream Link JSON format"); - } - - this.raw = json; - } - - string stringRep(int indentation = 0, string indentStr = " ") const { - string[] output; - string indent = ""; - string j; - - if (this.m_objType == ObjectType.String) - return format("%s%s", indent, this.m_store.str); - - for (int i = 0; i < indentation; i++) - indent ~= indentStr; - - output ~= format("%s {", this.type); - - if (href) - output ~= format("%s%shref = %s", indent, indentStr, href); - - if (mediaType) - output ~= format("%s%smediaType = %s", indent, indentStr, mediaType); - - if (name) - output ~= format("%s%sname = %s", indent, indentStr, name); - - if (height) - output ~= format("%s%sheight = %d", indent, indentStr, height); - - if (width) - output ~= format("%s%swidth = %d", indent, indentStr, width); - - if (preview && (j = preview.stringRep(indentation + 1)) != string.init) - output ~= format("%s%spreview = %s", indent, indentStr, j); - - output ~= indent ~ "}"; - - if (output.length == 2) - return ""; - - return output.join("\n"); - } -} - -class Collection : ASObject { - int totalItems; - CollectionPage current; - CollectionPage first; - CollectionPage last; - ASObject items; - - this(JSONValue json) { - super(json); - - if (this.m_objType != ObjectType.Object) - throw new Exception("Wrong Collection format?"); - - optional(json, "totalItems", totalItems); - current = new CollectionPage(json.optional!JSONValue("current")); - first = new CollectionPage(json.optional!JSONValue("first")); - last = new CollectionPage(json.optional!JSONValue("last")); - items = new ASObject(json.optional!JSONValue("items")); - } -} - -class CollectionPage : Collection { - CollectionPage partOf; - CollectionPage next; - CollectionPage prev; - - this(JSONValue json) { - super(json); - - if (this.m_objType != ObjectType.Object) - throw new Exception("Wrong CollectionPage format?"); - - partOf = new CollectionPage(json.optional!JSONValue("partOf")); - next = new CollectionPage(json.optional!JSONValue("next")); - prev = new CollectionPage(json.optional!JSONValue("prev")); - } -} diff --git a/source/ap/util.d b/source/ap/util.d index 9b35686..02c5b09 100644 --- a/source/ap/util.d +++ b/source/ap/util.d @@ -10,8 +10,7 @@ import singletons; import net.request_pool; import webfinger; import ap.errors; -import ap.actor; -import ap.activity_stream; +import ap.types.object; enum { ActivityJson = "application/activity+json", @@ -29,7 +28,7 @@ JSONValue apFetchRemoteUser(JSONValue wf) { rq.url = wf["href"].str; rq.headers["Accept"] = wf["type"].str; - Response rs = Rp.request(rq, true); + Response rs = RP.request(rq, true); return parseJSON(cast(string) rs.responseBody); } @@ -44,7 +43,7 @@ ASObject apFetchASObject(string id, string contentType = ActivityJson) { rq.url = id; rq.headers["Accept"] = contentType; - Response rs = Rp.request(rq, true); + Response rs = RP.request(rq, true); return new ASObject(parseJSON(cast(string) rs.responseBody)); }