New Sign in

fierj

Public

Tiny personal git forge

← fierj / ap.go
package main

import "log/slog"

const apContext = "https://www.w3.org/ns/activitystreams"

type APObject struct {
	Context      string   `json:"@context"`
	ID           string   `json:"id"`
	Type         string   `json:"type"`
	Published    string   `json:"published,omitempty"`
	AttributedTo string   `json:"attributedTo,omitempty"`
	Content      string   `json:"content,omitempty"`
	URL          string   `json:"url,omitempty"`
	To           []string `json:"to,omitempty"`
	CC           []string `json:"cc,omitempty"`
}

func actorURL(host, repoName string) string {
	return "https://" + host + "/" + repoName + "/ap/actor"
}

func deliverThreadCreate(cfg Config, repoName string, t *Thread) {
	if cfg.Host == "" {
		return
	}
	actor := actorURL(cfg.Host, repoName)
	threadURL := "https://" + cfg.Host + "/" + repoName + "/threads/" + t.ID
	note := APObject{
		Context:      apContext,
		ID:           threadURL,
		Type:         "Note",
		Published:    t.Created,
		AttributedTo: actor,
		Content:      t.Title + "\n\n" + t.Body,
		URL:          threadURL,
		To:           []string{"https://www.w3.org/ns/activitystreams#Public"},
		CC:           []string{"https://" + cfg.Host + "/" + repoName + "/ap/followers"},
	}
	activity := map[string]any{
		"@context":  apContext,
		"id":        threadURL + "/activity",
		"type":      "Create",
		"actor":     actor,
		"published": t.Created,
		"to":        []string{"https://www.w3.org/ns/activitystreams#Public"},
		"cc":        []string{"https://" + cfg.Host + "/" + repoName + "/ap/followers"},
		"object":    note,
	}
	// TODO: implement actual delivery to inboxes of followers.
	slog.Info("ap: would deliver Create activity", "actor", actor, "thread", threadURL)
	_ = activity
}