diff --git a/src/db/blog.ts b/src/db/blog.ts index 64bc8ba..c23ccc6 100644 --- a/src/db/blog.ts +++ b/src/db/blog.ts @@ -5,11 +5,11 @@ import { Blog } from "~entities/blog.js"; import { generateKeyPair } from "./util/rsa.js"; import MercuryDataSource from "./index.js"; -export const notLocalAccount = new Error("account is not local"); +export const incorrectAccountType = new Error("wrong account type for function"); /** Create a local blog. Throws an error if the given account is not a local account. */ export async function createLocalBlog(account: Account, name: string) { - if (account.host) throw notLocalAccount; + if (account.host) throw incorrectAccountType; const keyPair = await generateKeyPair(); @@ -25,3 +25,19 @@ export async function createLocalBlog(account: Account, name: string) { return blog; } + +export async function createRemoteBlog(account: Account, name: string, apId: string, publicKey: string) { + if (!account.host) throw incorrectAccountType; + + const blog = new Blog(); + blog.id = ulid(); + blog.apId = apId; + blog.username = name; + blog.host = account.host; + blog.account = account; + blog.publicKey = publicKey; + + await MercuryDataSource.getRepository(Blog).save(blog); + + return blog; +} diff --git a/src/db/entities/blog.ts b/src/db/entities/blog.ts index e92f03f..daef6c0 100644 --- a/src/db/entities/blog.ts +++ b/src/db/entities/blog.ts @@ -14,6 +14,8 @@ import { Post } from "./post.js"; export class Blog { @PrimaryColumn("text") id: string; + @Column("text", { nullable: true, unique: true, comment: "ActivityPub ID" }) + apId: string | null; @Column("text", { nullable: false }) username: string; @Column("text", { nullable: true }) diff --git a/src/db/entities/post.ts b/src/db/entities/post.ts index 4cc665b..10a1e38 100644 --- a/src/db/entities/post.ts +++ b/src/db/entities/post.ts @@ -5,6 +5,8 @@ import { Blog } from "./blog.js"; export class Post { @PrimaryColumn("text") id: string; + @Column("text", { nullable: true, unique: true, comment: "ActivityPub ID" }) + apId: string | null; @Column("text", { nullable: true }) content: string | null; @Column("text", { nullable: true }) diff --git a/src/db/index.ts b/src/db/index.ts index 2e22177..5c202e3 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -15,6 +15,8 @@ const MercuryDataSource = new DataSource({ database: config.DATABASE_NAME, entities: [Account, Blog, Post], migrations: ["src/db/migrations/*.js"], + logging: + process.env.NODE_ENV === "production" ? ["error"] : ["query", "error"], }); export default MercuryDataSource; diff --git a/src/db/migrations/1689900517279-addApIds.js b/src/db/migrations/1689900517279-addApIds.js new file mode 100644 index 0000000..493a336 --- /dev/null +++ b/src/db/migrations/1689900517279-addApIds.js @@ -0,0 +1,50 @@ +export class AddApIds1689900517279 { + name = 'AddApIds1689900517279' + + async up(queryRunner) { + await queryRunner.query(` + ALTER TABLE "post" + ADD "apId" text + `); + await queryRunner.query(` + ALTER TABLE "post" + ADD CONSTRAINT "UQ_e16e967a725a0f3f681bf99bd6e" UNIQUE ("apId") + `); + await queryRunner.query(` + COMMENT ON COLUMN "post"."apId" IS 'ActivityPub ID' + `); + await queryRunner.query(` + ALTER TABLE "blog" + ADD "apId" text + `); + await queryRunner.query(` + ALTER TABLE "blog" + ADD CONSTRAINT "UQ_624066bd60ecf91ee390637d171" UNIQUE ("apId") + `); + await queryRunner.query(` + COMMENT ON COLUMN "blog"."apId" IS 'ActivityPub ID' + `); + } + + async down(queryRunner) { + await queryRunner.query(` + COMMENT ON COLUMN "blog"."apId" IS 'ActivityPub ID' + `); + await queryRunner.query(` + ALTER TABLE "blog" DROP CONSTRAINT "UQ_624066bd60ecf91ee390637d171" + `); + await queryRunner.query(` + ALTER TABLE "blog" DROP COLUMN "apId" + `); + await queryRunner.query(` + COMMENT ON COLUMN "post"."apId" IS 'ActivityPub ID' + `); + await queryRunner.query(` + ALTER TABLE "post" DROP CONSTRAINT "UQ_e16e967a725a0f3f681bf99bd6e" + `); + await queryRunner.query(` + ALTER TABLE "post" DROP COLUMN "apId" + `); + } + +} diff --git a/src/start.ts b/src/start.ts index c20d5b5..d3da7fe 100644 --- a/src/start.ts +++ b/src/start.ts @@ -21,6 +21,11 @@ export default async function start() { log.debug("Setting up routes"); const app = Fastify(); + app.setNotFoundHandler((req, res) => { + log.debug("Route %s not found", req.url); + res.status(404).send({ message: "Not found" }); + }); + const routes = await getRoutes(); mountRoutes(app, routes);