add activitypub ID fields to blog and post

This commit is contained in:
sam 2023-07-21 02:55:04 +02:00
parent 40ad12c924
commit ca724dab9a
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
6 changed files with 79 additions and 2 deletions

View file

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

View file

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

View file

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

View file

@ -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;

View file

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

View file

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