AP: move types to their own directory

This commit is contained in:
2024-02-23 13:33:37 -03:00
parent b27e6cb2ec
commit 6f7e223b3d
6 changed files with 165 additions and 141 deletions

View File

@@ -1,8 +1,8 @@
module ap.actor; module ap.types.actor;
import std.json; import std.json;
import ap.activity_stream; import ap.types.object;
import util; import util;
/++ /++

View File

@@ -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"));
}
}

View File

@@ -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"));
}
}

105
source/ap/types/link.d Normal file
View File

@@ -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");
}
}

View File

@@ -1,4 +1,4 @@
module ap.activity_stream; module ap.types.object;
import std.algorithm; import std.algorithm;
import std.array; import std.array;
@@ -6,6 +6,8 @@ import std.datetime;
import std.format; import std.format;
import std.json; import std.json;
import ap.types.link;
import ap.types.collection;
import util; import util;
union Store { union Store {
@@ -197,137 +199,3 @@ class ASObject {
return output.length == 2 ? "" : output.join("\n"); 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"));
}
}

View File

@@ -10,8 +10,7 @@ import singletons;
import net.request_pool; import net.request_pool;
import webfinger; import webfinger;
import ap.errors; import ap.errors;
import ap.actor; import ap.types.object;
import ap.activity_stream;
enum { enum {
ActivityJson = "application/activity+json", ActivityJson = "application/activity+json",
@@ -29,7 +28,7 @@ JSONValue apFetchRemoteUser(JSONValue wf) {
rq.url = wf["href"].str; rq.url = wf["href"].str;
rq.headers["Accept"] = wf["type"].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); return parseJSON(cast(string) rs.responseBody);
} }
@@ -44,7 +43,7 @@ ASObject apFetchASObject(string id, string contentType = ActivityJson) {
rq.url = id; rq.url = id;
rq.headers["Accept"] = contentType; rq.headers["Accept"] = contentType;
Response rs = Rp.request(rq, true); Response rs = RP.request(rq, true);
return new ASObject(parseJSON(cast(string) rs.responseBody)); return new ASObject(parseJSON(cast(string) rs.responseBody));
} }