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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package dto

import (
	"encoding/json"
	"errors"
	"fmt"
)

type UserInfo struct {
	Context           any           `json:"@context"`
	Id                string        `json:"id"`
	Type              string        `json:"type"`
	PreferredUserName string        `json:"preferredUsername"`
	Name              string        `json:"name"`
	Summary           string        `json:"summary"`
	ManuallyApproves  bool          `json:"manuallyApprovesFollowers"`
	Published         string        `json:"published"`
	Inbox             string        `json:"inbox"`
	Outbox            string        `json:"outbox"`
	Followers         string        `json:"followers"`
	Following         string        `json:"following"`
	Endpoints         UserEndpoints `json:"endpoints"`
	PublicKey         PublicKey     `json:"publicKey"`
	Attachments       []Attachment  `json:"attachment"`
	Icon              Image         `json:"icon"`
	Image             Image         `json:"image"`
}

type Attachment struct {
	Type  string `json:"type"`
	Name  string `json:"name"`
	Value string `json:"value"`
}

type Image struct {
	Type string `json:"type"`
	Url  string `json:"url"`
}

type UserEndpoints struct {
	SharedInbox string `json:"sharedInbox"`
}

type PublicKey struct {
	Id           string `json:"id"`
	Owner        string `json:"owner"`
	PublicKeyPem string `json:"publicKeyPem"`
}

type OrderedListSummary struct {
	Context    any     `json:"@context"`
	Id         string  `json:"id"`
	Type       string  `json:"type"`
	TotalItems uint    `json:"totalItems"`
	First      *string `json:"first,omitempty"`
	Last       *string `json:"last,omitempty"`
}

func getRecipient(raw any) ([]string, error) {
	var res []string
	if raw == nil {
		return res, nil
	}
	if slice, ok := raw.([]interface{}); ok {
		for _, s := range slice {
			if str, ok := s.(string); ok {
				res = append(res, str)
			} else {
				return res, fmt.Errorf("list of recipients must only contain strings")
			}
		}
	} else if str, ok := raw.(string); ok {
		res = []string{str}
	} else {
		return res, fmt.Errorf("to and cc must be single string or array of strings")
	}
	return res, nil
}

type ActivityInBase struct {
	Id     string   `json:"id"`
	Type   string   `json:"type"`
	Actor  string   `json:"actor"`
	To     []string `json:"-"`
	RawTo  any      `json:"to"`
	Cc     []string `json:"-"`
	RawCc  any      `json:"cc"`
	Object any      `json:"object"`
}

func (x *ActivityInBase) UnmarshalJSON(data []byte) error {
	var err error
	type Y ActivityInBase
	var y = (*Y)(x)
	if err = json.Unmarshal(data, y); err != nil {
		return err
	}
	if y.To, err = getRecipient(y.RawTo); err != nil {
		return err
	}
	if y.Cc, err = getRecipient(y.RawCc); err != nil {
		return err
	}
	return nil
}

type ActivityIn[T any] struct {
	Id     string   `json:"id"`
	Type   string   `json:"type"`
	Actor  string   `json:"actor"`
	To     []string `json:"-"`
	RawTo  any      `json:"to"`
	Cc     []string `json:"-"`
	RawCc  any      `json:"cc"`
	Object T        `json:"object"`
}

func (x *ActivityIn[T]) UnmarshalJSON(data []byte) error {
	var err error
	type Y ActivityIn[T]
	var y = (*Y)(x)
	if err = json.Unmarshal(data, y); err != nil {
		return err
	}
	if y.To, err = getRecipient(y.RawTo); err != nil {
		return err
	}
	if y.Cc, err = getRecipient(y.RawCc); err != nil {
		return err
	}
	return nil
}

type ActivityOut struct {
	Context any       `json:"@context"`
	Id      string    `json:"id"`
	Type    string    `json:"type"`
	Actor   string    `json:"actor"`
	To      *[]string `json:"to,omitempty"`
	Cc      *[]string `json:"cc,omitempty"`
	Object  any       `json:"object,omitempty"`
}

type Note struct {
	Context      string   `json:"@context,omitempty"`
	Id           string   `json:"id"`
	Type         string   `json:"type"`
	Published    string   `json:"published"`
	Summary      *string  `json:"summary"`
	AttributedTo string   `json:"attributedTo"`
	InReplyTo    *string  `json:"inReplyTo"`
	To           []string `json:"-"`
	RawTo        any      `json:"to"`
	Cc           []string `json:"-"`
	RawCc        any      `json:"cc"`
	Content      string   `json:"content"`
	Tag          *[]Tag   `json:"-"`
	RawTag       any      `json:"tag,omitempty"`
}

func (x *Note) UnmarshalJSON(data []byte) error {
	var err error
	type Y Note
	var y = (*Y)(x)
	if err = json.Unmarshal(data, y); err != nil {
		return err
	}
	if y.To, err = getRecipient(y.RawTo); err != nil {
		return err
	}
	if y.Cc, err = getRecipient(y.RawCc); err != nil {
		return err
	}
	if y.Tag, err = getTag(y.RawTag); err != nil {
		return err
	}
	return nil
}

func (x *Note) MarshalJSON() ([]byte, error) {
	type Y Note
	var y = (*Y)(x)
	y.RawTo = y.To
	y.RawCc = y.Cc
	y.RawTag = y.Tag
	return json.Marshal(y)
}

type Tag struct {
	Type string `json:"type"`
	Href string `json:"href"`
	Name string `json:"name"`
}

func getTag(raw any) (*[]Tag, error) {
	// No value is legit
	if raw == nil {
		return nil, nil
	}

	retrieve := func(obj *map[string]interface{}) (*Tag, error) {
		var tag Tag
		var ok bool
		if tag.Href, ok = (*obj)["href"].(string); !ok {
			return nil, errors.New("invalid data in tag's 'href' property; string expected")
		}
		if tag.Name, ok = (*obj)["name"].(string); !ok {
			return nil, errors.New("invalid data in tag's 'name' property; string expected")
		}
		if tag.Type, ok = (*obj)["type"].(string); !ok {
			return nil, errors.New("invalid data in tag's 'type' property; string expected")
		}
		return &tag, nil
	}

	// Single Tag object
	if obj, ok := raw.(map[string]interface{}); ok {
		if tag, err := retrieve(&obj); err != nil {
			return nil, err
		} else {
			return &[]Tag{*tag}, nil
		}
	}
	// Array
	if slice, ok := raw.([]interface{}); ok {
		var res []Tag
		for _, s := range slice {
			if obj, ok := s.(map[string]interface{}); ok {
				if tag, err := retrieve(&obj); err != nil {
					return nil, err
				} else {
					res = append(res, *tag)
				}
			} else {
				return nil, errors.New("unexpected item in 'tag' array; must only contain tag objects")
			}
		}
		return &res, nil
	}
	return nil, errors.New("invalid data in 'tag' property")
}