Unverified Commit b5d3bfdb authored by Hong Minhee's avatar Hong Minhee
Browse files

Many more vocabulary

parent ed53a629
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -65,6 +65,26 @@ const scalarTypes: Record<string, ScalarType> = {
      return `${v}["@value"]`;
    },
  },
  "http://www.w3.org/2001/XMLSchema#float": {
    name: "number",
    typeGuard(v) {
      return `typeof ${v} === "number" && !Number.isNaN(${v})`;
    },
    encoder(v) {
      return `{
        "@type": "http://www.w3.org/2001/XMLSchema#float",
        "@value": ${v},
      }`;
    },
    dataCheck(v) {
      return `typeof ${v} === "object" && "@type" in ${v}
        && ${v}["@type"] === "http://www.w3.org/2001/XMLSchema#float"
        && "@value" in ${v} && typeof ${v}["@value"] === "number"`;
    },
    decoder(v) {
      return `${v}["@value"]`;
    },
  },
  "http://www.w3.org/2001/XMLSchema#string": {
    name: "string",
    typeGuard(v) {
@@ -192,6 +212,25 @@ const scalarTypes: Record<string, ScalarType> = {
      })`;
    },
  },
  "fedify:units": {
    name: '"cm" | "feet" | "inches" | "km" | "m" | "miles"',
    typeGuard(v) {
      return `${v} == "cm" || ${v} == "feet" || ${v} == "inches" ` +
        `|| ${v} == "km" || ${v} == "m" || ${v} == "miles"`;
    },
    encoder(v) {
      return `{ "@value": ${v} }`;
    },
    dataCheck(v) {
      return `typeof ${v} === "object" && "@value" in ${v}
      && (${v}["@value"] == "cm" || ${v}["@value"] == "feet" ` +
        `|| ${v}["@value"] == "inches" || ${v}["@value"] == "km" ` +
        `|| ${v}["@value"] == "m" || ${v}["@value"] == "miles")`;
    },
    decoder(v) {
      return `${v}["@value"]`;
    },
  },
};

export function getTypeName(
+2 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import {
  removeFollower,
} from "../models/follower.ts";
import { openKv } from "../models/kv.ts";
import { countPosts, getPosts, toNote } from "../models/post.ts";
import { countPosts, getPosts, toArticle } from "../models/post.ts";

// The `Federation<TContextData>` object is a registry that registers
// federation-related callbacks:
@@ -97,7 +97,7 @@ federation.setOutboxDispatcher(
        id: new URL(`/posts/${post.uuid}#activity`, ctx.request.url),
        actor: ctx.getActorUri(handle),
        to: new URL("https://www.w3.org/ns/activitystreams#Public"),
        object: toNote(ctx, blog, post, comments),
        object: toArticle(ctx, blog, post, comments),
      });
      activities.push(activity);
    }
+5 −5
Original line number Diff line number Diff line
import { RequestContext } from "fedify/federation";
import { Collection, CollectionPage, Note } from "fedify/vocab";
import { Article, Collection, CollectionPage } from "fedify/vocab";
import markdownIt from "markdown-it";
import { uuidv7 } from "uuidv7";
import { Blog } from "./blog.ts";
@@ -70,15 +70,15 @@ export function getContentHtml(post: Post): string {
  return md.render(post.content);
}

// Represents a post as an ActivityStreams `Note`:
export function toNote(
// Represents a post as an ActivityStreams `Article`:
export function toArticle(
  context: RequestContext<void>,
  blog: Blog,
  post: Post,
  comments: Comment[],
): Note {
): Article {
  const url = new URL(`/posts/${post.uuid}`, context.url);
  return new Note({
  return new Article({
    id: url,
    attributedTo: context.getActorUri(blog.handle),
    to: new URL("https://www.w3.org/ns/activitystreams#Public"),
+6 −2
Original line number Diff line number Diff line
@@ -9,7 +9,11 @@ import {
  type Comment as CommentModel,
  getComments,
} from "../../models/comment.ts";
import { getPost, type Post as PostModel, toNote } from "../../models/post.ts";
import {
  getPost,
  type Post as PostModel,
  toArticle,
} from "../../models/post.ts";

export interface PostPageData {
  domain: string;
@@ -37,7 +41,7 @@ export const handler: Handler<PostPageData> = async (req, ctx) => {
    accept === "application/ld+json" || accept === "application/json"
  ) {
    const fedCtx = federation.createContext(req);
    const note = toNote(fedCtx, blog, post, comments);
    const note = toArticle(fedCtx, blog, post, comments);
    const jsonLd = await note.toJsonLd(fedCtx);
    return new Response(JSON.stringify(jsonLd), {
      headers: {
+2 −2
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ import {
  countPosts,
  getPosts,
  Post,
  toNote,
  toArticle,
} from "../../models/post.ts";

interface PostsData extends PostFormProps {
@@ -90,7 +90,7 @@ export const handler: Handlers<PostsData> = {
        id: new URL(`/posts/${post.uuid}#activity`, req.url),
        actor: fedCtx.getActorUri(blog.handle),
        to: new URL("https://www.w3.org/ns/activitystreams#Public"),
        object: toNote(fedCtx, blog, post, []),
        object: toArticle(fedCtx, blog, post, []),
      }),
    );

Loading