add activitypub ID fields to blog and post
This commit is contained in:
parent
40ad12c924
commit
ca724dab9a
6 changed files with 79 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 })
|
||||
|
|
|
@ -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 })
|
||||
|
|
|
@ -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;
|
||||
|
|
50
src/db/migrations/1689900517279-addApIds.js
Normal file
50
src/db/migrations/1689900517279-addApIds.js
Normal 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"
|
||||
`);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue