page.outbox.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"slices"
	"strconv"
	"strings"
	"time"
)

type OutboxMeta struct {
	Context      string                 `json:"@context"`
	Summmary     string                 `json:"summary"`
	Type         string                 `json:"type"`
	TotalItems   int                    `json:"inbox"`
	OrderedItems []*PostMetaWithContext `json:"orderedItems"`
}

type NoteMeta struct {
	ID           string            `json:"id"`
	Type         string            `json:"type"`
	Summary      *string           `json:"summary"`
	InReplyTo    string            `json:"inReplyTo"`
	Published    string            `json:"published"`
	URL          string            `json:"url"`
	AttributedTo string            `json:"attributedTo"`
	To           []string          `json:"to"`
	CC           []string          `json:"cc"`
	Sensitive    bool              `json:"sensitive"`
	Content      string            `json:"content"`
	ContentMap   map[string]string `json:"contentMap"`
	Conversation string            `json:"conversation"`
	Tag          []*Tag            `json:"tag"`
	Likes        CollectionMeta    `json:"likes"`
	Replies      CollectionMeta    `json:"replies"`
	Shares       CollectionMeta    `json:"shares"`
	created      time.Time
	actor        string
}

func (nm *NoteMeta) LocalID() string {
	if nm.created.Unix() < 0 {
		nm.created, _ = time.Parse("2006-01-02T15:04:05Z", nm.Published)
	}
	return strconv.Itoa(int(nm.created.Unix()))
}

type CollectionMeta struct {
	ID         string `json:"id"`
	Type       string `json:"type"`
	TotalItems int    `json:"totalItems"`
}

type CollectionMetaWithContext struct {
	Context string `json:"@context"`
	CollectionMeta
}

func (u *User) processOutbox(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Content-Type", "application/activity+json")
	response := OutboxMeta{}
	response.Context = "https://www.w3.org/ns/activitystreams"
	response.Summmary = fmt.Sprintf("%s's Posts", u.Name)
	response.Type = "OrderedCollection"

	response.TotalItems = len(u.posts)

	WalkCache(u, "outbox", "create", func(id string, data []byte) {
		activity := &PostMetaWithContext{}
		if err := json.Unmarshal(data, activity); err == nil {
			response.OrderedItems = append(response.OrderedItems, activity)
		}
	})

	slices.SortFunc(response.OrderedItems, func(a *PostMetaWithContext, b *PostMetaWithContext) int {
		return strings.Compare(a.Object.ID, b.Object.ID)
	})
	slices.Reverse(response.OrderedItems)
	data, _ := json.Marshal(response)
	w.Write(data)

}