Skip to content

Commit

Permalink
{Article,ChatMessage,Note,Question}.quoteUrl props
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Sep 8, 2024
1 parent 1d5990d commit 22e07c3
Show file tree
Hide file tree
Showing 15 changed files with 6,926 additions and 5,202 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"docloader",
"eddsa",
"fedi",
"fedibird",
"fedify",
"fediverse",
"followable",
Expand Down
16 changes: 16 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ To be released.
- Added `ActorCallbackSetters.mapHandle()` method.
- Added `ActorHandleMapper` type.

- Added `quoteUrl` property to `Article`, `ChatMessage`, `Note`, and
`Question` classes in Activity Vocabulary API.

- Added `Article.quoteUrl` property.
- `new Article()` constructor now accepts `quoteUrl` option.
- `Article.clone()` method now accepts `quoteUrl` option.
- Added `ChatMessage.quoteUrl` property.
- `new ChatMessage()` constructor now accepts `quoteUrl` option.
- `ChatMessage.clone()` method now accepts `quoteUrl` option.
- Added `Note.quoteUrl` property.
- `new Note()` constructor now accepts `quoteUrl` option.
- `Note.clone()` method now accepts `quoteUrl` option.
- Added `Question.quoteUrl` property.
- `new Question()` constructor now accepts `quoteUrl` option.
- `Question.clone()` method now accepts `quoteUrl` option.

- Removed `expand` option of `Object.toJsonLd()` method, which was deprecated
in version 0.14.0. Use `format: "expand"` option instead.

Expand Down
11,758 changes: 6,583 additions & 5,175 deletions src/codegen/__snapshots__/class.test.ts.snap

Large diffs are not rendered by default.

52 changes: 42 additions & 10 deletions src/codegen/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,33 @@ export async function* generateEncoder(
);
compactItems.push(item);
}
if (compactItems.length > 0) {
`;
if (property.functional || property.container !== "list") {
yield `
if (compactItems.length > 1) {
result[${JSON.stringify(property.compactName)}] = compactItems;
} else if (compactItems.length === 1) {
result[${JSON.stringify(property.compactName)}] = compactItems[0];
}
result[${JSON.stringify(property.compactName)}]
= compactItems.length > 1
? compactItems
: compactItems[0];
`;
if (property.functional && property.redundantProperties != null) {
for (const prop of property.redundantProperties) {
yield `
result[${JSON.stringify(prop.compactName)}]
= compactItems.length > 1
? compactItems
: compactItems[0];
`;
}
}
} else {
yield `
if (compactItems.length > 0) {
result[${JSON.stringify(property.compactName)}] = compactItems;
}
result[${JSON.stringify(property.compactName)}] = compactItems;
`;
}
yield `
}
`;
}
yield `
result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};
Expand Down Expand Up @@ -171,7 +182,7 @@ export async function* generateEncoder(
yield `;
}
if (array.length > 0) {
values[${JSON.stringify(property.uri)}] = (
const propValue = (
`;
if (!property.functional && property.container === "list") {
yield `{ "@list": array }`;
Expand All @@ -180,6 +191,16 @@ export async function* generateEncoder(
}
yield `
);
values[${JSON.stringify(property.uri)}] = propValue;
`;
if (property.functional && property.redundantProperties != null) {
for (const prop of property.redundantProperties) {
yield `
values[${JSON.stringify(prop.uri)}] = propValue;
`;
}
}
yield `
}
`;
}
Expand Down Expand Up @@ -317,7 +338,18 @@ export async function* generateDecoder(
yield await generateField(property, types, "const ");
const arrayVariable = `${variable}__array`;
yield `
const ${arrayVariable} = values[${JSON.stringify(property.uri)}];
let ${arrayVariable} = values[${JSON.stringify(property.uri)}];
`;
if (property.functional && property.redundantProperties != null) {
for (const prop of property.redundantProperties) {
yield `
if (${arrayVariable} == null || ${arrayVariable}.length < 1) {
${arrayVariable} = values[${JSON.stringify(prop.uri)}];
}
`;
}
}
yield `
for (
const v of ${arrayVariable} == null
? []
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export async function getFieldName(
"SHA-1",
new TextEncoder().encode(propertyUri),
);
return `${prefix}_${encodeBase58(hashedUri)}`;
const match = propertyUri.match(/#([A-Za-z0-9_]+)$/);
const suffix = match == null ? "" : `_${match[1]}`;
return `${prefix}_${encodeBase58(hashedUri)}${suffix}`;
}

export async function generateField(
Expand Down
21 changes: 20 additions & 1 deletion src/codegen/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface PropertySchemaBase {
* The property name used in the compacted JSON-LD document. It is used as
* the key of the property.
*/
compactName: string;
compactName?: string;

/**
* The qualified URI of the superproperty of the property (if any).
Expand Down Expand Up @@ -174,6 +174,25 @@ export type PropertySchema =
* and `singularAccessor` should not be specified.
*/
functional: true;

/**
* If it's present, those redundant properties are also filled with
* the same value altogether when the object is serialized into
* JSON-LD. When it's deserialized from JSON-LD, it tries to
* parse the values of the specified properties in order.
*/
redundantProperties?: {
/**
* The qualified URI of the property.
*/
uri: string;

/**
* The property name used in the compacted JSON-LD document. It is used
* as the key of the property.
*/
compactName?: string;
}[];
};

/**
Expand Down
21 changes: 21 additions & 0 deletions src/codegen/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ $defs:
functional:
description: *functional-description
const: true
redundantProperties::
description: >-
If it's present, those redundant properties are also filled with
the same value altogether when the object is serialized into
JSON-LD. When it's deserialized from JSON-LD, it tries to
parse the values of the specified properties in order.
type: array
items:
type: object
properties:
uri:
description: The qualified URI of the property.
type: string
format: uri
compactName:
description: >-
The property name used in the compacted JSON-LD document.
It is used as the key of the property.
type: string
required:
- uri
required:
- functional

Expand Down
20 changes: 20 additions & 0 deletions src/codegen/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,26 @@ const scalarTypes: Record<string, ScalarType> = {
return `parseLanguageTag(${v}["@value"])`;
},
},
"fedify:url": {
name: "URL",
typeGuard(v) {
return `${v} instanceof URL`;
},
encoder(v) {
return `{ "@value": ${v}.href }`;
},
compactEncoder(v) {
return `${v}.href`;
},
dataCheck(v) {
return `typeof ${v} === "object" && "@value" in ${v}
&& typeof ${v}["@value"] === "string"
&& ${v}["@value"] !== "" && ${v}["@value"] !== "/"`;
},
decoder(v) {
return `new URL(${v}["@value"])`;
},
},
"fedify:publicKey": {
name: "CryptoKey",
typeGuard(v) {
Expand Down
20 changes: 20 additions & 0 deletions src/federation/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ test("handleObject()", async () => {
Hashtag: "as:Hashtag",
sensitive: "as:sensitive",
toot: "http://joinmastodon.org/ns#",
_misskey_quote: "misskey:_misskey_quote",
fedibird: "http://fedibird.com/ns#",
misskey: "https://misskey-hub.net/ns#",
quoteUri: "fedibird:quoteUri",
quoteUrl: "as:quoteUrl",
},
],
id: "https://example.com/users/someone/notes/123",
Expand Down Expand Up @@ -534,6 +539,11 @@ test("handleObject()", async () => {
Hashtag: "as:Hashtag",
sensitive: "as:sensitive",
toot: "http://joinmastodon.org/ns#",
_misskey_quote: "misskey:_misskey_quote",
fedibird: "http://fedibird.com/ns#",
misskey: "https://misskey-hub.net/ns#",
quoteUri: "fedibird:quoteUri",
quoteUrl: "as:quoteUrl",
},
],
id: "https://example.com/users/someone/notes/123",
Expand Down Expand Up @@ -1161,6 +1171,11 @@ test("respondWithObject()", async () => {
Hashtag: "as:Hashtag",
sensitive: "as:sensitive",
toot: "http://joinmastodon.org/ns#",
_misskey_quote: "misskey:_misskey_quote",
fedibird: "http://fedibird.com/ns#",
misskey: "https://misskey-hub.net/ns#",
quoteUri: "fedibird:quoteUri",
quoteUrl: "as:quoteUrl",
},
],
id: "https://example.com/notes/1",
Expand Down Expand Up @@ -1196,6 +1211,11 @@ test("respondWithObjectIfAcceptable", async () => {
Hashtag: "as:Hashtag",
sensitive: "as:sensitive",
toot: "http://joinmastodon.org/ns#",
_misskey_quote: "misskey:_misskey_quote",
fedibird: "http://fedibird.com/ns#",
misskey: "https://misskey-hub.net/ns#",
quoteUri: "fedibird:quoteUri",
quoteUrl: "as:quoteUrl",
},
],
id: "https://example.com/notes/1",
Expand Down
36 changes: 24 additions & 12 deletions src/vocab/__snapshots__/vocab.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ snapshot[`Deno.inspect(ChatMessage) [auto] 1`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proof: DataIntegrityProof {}
proof: DataIntegrityProof {},
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -171,7 +172,8 @@ snapshot[`Deno.inspect(ChatMessage) [auto] 2`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proof: URL "https://example.com/"
proof: URL "https://example.com/",
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -206,7 +208,8 @@ snapshot[`Deno.inspect(ChatMessage) [auto] 3`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proofs: [ DataIntegrityProof {}, DataIntegrityProof {} ]
proofs: [ DataIntegrityProof {}, DataIntegrityProof {} ],
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -1168,7 +1171,8 @@ snapshot[`Deno.inspect(Article) [auto] 1`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proof: DataIntegrityProof {}
proof: DataIntegrityProof {},
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -1203,7 +1207,8 @@ snapshot[`Deno.inspect(Article) [auto] 2`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proof: URL "https://example.com/"
proof: URL "https://example.com/",
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -1238,7 +1243,8 @@ snapshot[`Deno.inspect(Article) [auto] 3`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proofs: [ DataIntegrityProof {}, DataIntegrityProof {} ]
proofs: [ DataIntegrityProof {}, DataIntegrityProof {} ],
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -4057,7 +4063,8 @@ snapshot[`Deno.inspect(Note) [auto] 1`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proof: DataIntegrityProof {}
proof: DataIntegrityProof {},
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -4092,7 +4099,8 @@ snapshot[`Deno.inspect(Note) [auto] 2`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proof: URL "https://example.com/"
proof: URL "https://example.com/",
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -4127,7 +4135,8 @@ snapshot[`Deno.inspect(Note) [auto] 3`] = `
duration: PT1H,
sensitive: true,
source: Source {},
proofs: [ DataIntegrityProof {}, DataIntegrityProof {} ]
proofs: [ DataIntegrityProof {}, DataIntegrityProof {} ],
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -5312,7 +5321,8 @@ snapshot[`Deno.inspect(Question) [auto] 1`] = `
exclusiveOptions: [ Object {} ],
inclusiveOptions: [ Object {} ],
closed: 2024-03-03T08:30:06.796196096Z,
voters: 123
voters: 123,
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -5357,7 +5367,8 @@ snapshot[`Deno.inspect(Question) [auto] 2`] = `
exclusiveOptions: [ URL "https://example.com/" ],
inclusiveOptions: [ URL "https://example.com/" ],
closed: 2024-03-03T08:30:06.796196096Z,
voters: 123
voters: 123,
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down Expand Up @@ -5402,7 +5413,8 @@ snapshot[`Deno.inspect(Question) [auto] 3`] = `
exclusiveOptions: [ Object {}, Object {} ],
inclusiveOptions: [ Object {}, Object {} ],
closed: 2024-03-03T08:30:06.796196096Z,
voters: 123
voters: 123,
quoteUrl: URL "https://fedify.dev/"
}'
`;

Expand Down
Loading

0 comments on commit 22e07c3

Please sign in to comment.