🐸 Frogue(Fed)

~ololduck/Frogue

Raw

use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Remote ActivityPub actors we've fetched and cached.
        // actor_url is the canonical ActivityPub ID.
        manager
            .get_connection()
            .execute_unprepared(
                "CREATE TABLE IF NOT EXISTS ap_remote_actors (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    actor_url TEXT NOT NULL UNIQUE,
                    inbox_url TEXT NOT NULL,
                    shared_inbox_url TEXT,
                    public_key_pem TEXT NOT NULL,
                    public_key_id TEXT NOT NULL,
                    name TEXT,
                    preferred_username TEXT,
                    fetched_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
                )",
            )
            .await?;

        // Follower relationships: which remote actors follow which local actors.
        // follow_activity_id is the ActivityPub Follow activity's id, for Undo matching.
        manager
            .get_connection()
            .execute_unprepared(
                "CREATE TABLE IF NOT EXISTS ap_followers (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    local_actor_id INTEGER NOT NULL,
                    remote_actor_id INTEGER NOT NULL,
                    follow_activity_id TEXT NOT NULL,
                    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
                    FOREIGN KEY (local_actor_id) REFERENCES ap_actors(id) ON DELETE CASCADE,
                    FOREIGN KEY (remote_actor_id) REFERENCES ap_remote_actors(id) ON DELETE CASCADE,
                    UNIQUE (local_actor_id, remote_actor_id)
                )",
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .get_connection()
            .execute_unprepared("DROP TABLE IF EXISTS ap_followers")
            .await?;

        manager
            .get_connection()
            .execute_unprepared("DROP TABLE IF EXISTS ap_remote_actors")
            .await?;

        Ok(())
    }
}