Skip to content

Commit

Permalink
Add Mastodon extensions to Actor types
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Mar 4, 2024
1 parent e151872 commit aac953a
Show file tree
Hide file tree
Showing 10 changed files with 1,036 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
"cSpell.words": [
"bccs",
"btos",
"cfworker",
"codegen",
"Deno",
"discoverability",
"docloader",
"fedify",
"fediverse",
Expand Down
760 changes: 745 additions & 15 deletions codegen/__snapshots__/class.test.ts.snap

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions codegen/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { join } from "jsr:@std/path@^0.218.2";
import * as url from "jsr:@std/url@^0.218.2";
import { parse } from "jsr:@std/yaml@^0.218.2";
import { Schema as JsonSchema } from "https://deno.land/x/[email protected]/schema.js";
import {
Schema as JsonSchema,
Validator,
} from "npm:@cfworker/[email protected]";
import { readDirRecursive } from "./fs.ts";

/**
Expand Down Expand Up @@ -172,27 +175,28 @@ export class SchemaError extends Error {
}
}

async function loadSchemaSchema(): Promise<JsonSchema> {
async function loadSchemaValidator(): Promise<Validator> {
const thisFile = new URL(import.meta.url);
const response = await fetch(url.join(url.dirname(thisFile), "schema.yaml"));
const content = await response.text();
const schemaObject = parse(content);
const schema = new JsonSchema(schemaObject as object);
await schema.deref();
return schema;
return new Validator(schemaObject as JsonSchema);
}

const schemaSchema: JsonSchema = await loadSchemaSchema();
const schemaValidator: Validator = await loadSchemaValidator();

async function loadSchema(path: string): Promise<TypeSchema> {
const content = await Deno.readTextFile(path);
const schema = parse(content);
const result = schemaValidator.validate(schema);
const errors: SchemaError[] = [];
for (const e of schemaSchema.errors(schema)) {
errors.push(new SchemaError(path, `${path}: ${e.message}`));
if (result.valid) return schema as TypeSchema;
for (const e of result.errors) {
errors.push(
new SchemaError(path, `${path}:${e.instanceLocation}: ${e.error}`),
);
}
if (errors.length > 0) throw new AggregateError(errors);
return schema as TypeSchema;
throw new AggregateError(errors);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion examples/blog/federation/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ federation.setActorDispatcher("/users/{handle}", async (ctx, handle, key) => {
name: blog.title,
summary: blog.description,
preferredUsername: handle,
url: new URL("/", ctx.request.url),
url: new URL("/", ctx.url),
published: blog.published,
discoverable: true,
suspended: false,
indexable: true,
memorial: false,
// A `Context<TContextData>` object has several purposes, and one of
// them is to provide a way to generate URIs for the dispatchers and
// the collections:
Expand Down
90 changes: 75 additions & 15 deletions vocab/__snapshots__/vocab.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,11 @@ snapshot[`Deno.inspect(Application) [auto] 1`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -789,7 +793,11 @@ snapshot[`Deno.inspect(Application) [auto] 2`] = `
followers: URL "https://example.com/",
linked: URL "https://example.com/",
streams: [ URL "https://example.com/" ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -857,7 +865,11 @@ snapshot[`Deno.inspect(Application) [auto] 3`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {}, Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -3051,7 +3063,11 @@ snapshot[`Deno.inspect(Group) [auto] 1`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -3107,7 +3123,11 @@ snapshot[`Deno.inspect(Group) [auto] 2`] = `
followers: URL "https://example.com/",
linked: URL "https://example.com/",
streams: [ URL "https://example.com/" ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -3175,7 +3195,11 @@ snapshot[`Deno.inspect(Group) [auto] 3`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {}, Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -4688,7 +4712,11 @@ snapshot[`Deno.inspect(Organization) [auto] 1`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -4744,7 +4772,11 @@ snapshot[`Deno.inspect(Organization) [auto] 2`] = `
followers: URL "https://example.com/",
linked: URL "https://example.com/",
streams: [ URL "https://example.com/" ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -4812,7 +4844,11 @@ snapshot[`Deno.inspect(Organization) [auto] 3`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {}, Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -5020,7 +5056,11 @@ snapshot[`Deno.inspect(Person) [auto] 1`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -5076,7 +5116,11 @@ snapshot[`Deno.inspect(Person) [auto] 2`] = `
followers: URL "https://example.com/",
linked: URL "https://example.com/",
streams: [ URL "https://example.com/" ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -5144,7 +5188,11 @@ snapshot[`Deno.inspect(Person) [auto] 3`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {}, Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -5998,7 +6046,11 @@ snapshot[`Deno.inspect(Service) [auto] 1`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -6054,7 +6106,11 @@ snapshot[`Deno.inspect(Service) [auto] 2`] = `
followers: URL "https://example.com/",
linked: URL "https://example.com/",
streams: [ URL "https://example.com/" ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down Expand Up @@ -6122,7 +6178,11 @@ snapshot[`Deno.inspect(Service) [auto] 3`] = `
followers: Collection {},
linked: Collection {},
streams: [ Collection {}, Collection {} ],
endpoints: Endpoints {}
endpoints: Endpoints {},
discoverable: true,
suspended: true,
memorial: true,
indexable: true
}'
`;

Expand Down
39 changes: 39 additions & 0 deletions vocab/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ description: Describes a software application.
defaultContext:
- "https://www.w3.org/ns/activitystreams"
- "https://w3id.org/security/v1"
- toot: "http://joinmastodon.org/ns#"
discoverable: "toot:discoverable"
suspended: "toot:suspended"
memorial: "toot:memorial"
indexable: "toot:indexable"

properties:
- pluralName: preferredUsernames
Expand Down Expand Up @@ -138,3 +143,37 @@ properties:
a link to a JSON-LD document with these properties.
range:
- "https://www.w3.org/ns/activitystreams#Endpoints"

- singularName: discoverable
functional: true
uri: "http://joinmastodon.org/ns#discoverable"
description: |
Allows users to opt-in or opt-out of discoverability features like
the profile directory. This flag may also be used as an indicator of
the user's preferences toward being included in external discovery services,
such as search engines or other indexing tools.
range:
- "http://www.w3.org/2001/XMLSchema#boolean"

- singularName: suspended
functional: true
uri: "http://joinmastodon.org/ns#suspended"
description: |
Reports whether a user was locally suspended, for better handling of
these accounts.
range:
- "http://www.w3.org/2001/XMLSchema#boolean"

- singularName: memorial
functional: true
uri: "http://joinmastodon.org/ns#memorial"
description: Whether the actor is in-memorial state.
range:
- "http://www.w3.org/2001/XMLSchema#boolean"

- singularName: indexable
functional: true
uri: "http://joinmastodon.org/ns#indexable"
description: Whether the actor allows to be indexed.
range:
- "http://www.w3.org/2001/XMLSchema#boolean"
39 changes: 39 additions & 0 deletions vocab/group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ description: Represents a formal or informal collective of Actors.
defaultContext:
- "https://www.w3.org/ns/activitystreams"
- "https://w3id.org/security/v1"
- toot: "http://joinmastodon.org/ns#"
discoverable: "toot:discoverable"
suspended: "toot:suspended"
memorial: "toot:memorial"
indexable: "toot:indexable"

properties:
- pluralName: preferredUsernames
Expand Down Expand Up @@ -138,3 +143,37 @@ properties:
a link to a JSON-LD document with these properties.
range:
- "https://www.w3.org/ns/activitystreams#Endpoints"

- singularName: discoverable
functional: true
uri: "http://joinmastodon.org/ns#discoverable"
description: |
Allows users to opt-in or opt-out of discoverability features like
the profile directory. This flag may also be used as an indicator of
the user's preferences toward being included in external discovery services,
such as search engines or other indexing tools.
range:
- "http://www.w3.org/2001/XMLSchema#boolean"

- singularName: suspended
functional: true
uri: "http://joinmastodon.org/ns#suspended"
description: |
Reports whether a user was locally suspended, for better handling of
these accounts.
range:
- "http://www.w3.org/2001/XMLSchema#boolean"

- singularName: memorial
functional: true
uri: "http://joinmastodon.org/ns#memorial"
description: Whether the actor is in-memorial state.
range:
- "http://www.w3.org/2001/XMLSchema#boolean"

- singularName: indexable
functional: true
uri: "http://joinmastodon.org/ns#indexable"
description: Whether the actor allows to be indexed.
range:
- "http://www.w3.org/2001/XMLSchema#boolean"
Loading

0 comments on commit aac953a

Please sign in to comment.