refactor(workspace): 🎨 improve code structure and format

master
Xeronith 2022-08-29 22:52:58 +04:30
rodzic 6932fb7428
commit 7064fdff2d
10 zmienionych plików z 23 dodań i 54 usunięć

Wyświetl plik

@ -2,8 +2,6 @@ package activitypub
import "time"
const Public = "https://www.w3.org/ns/activitystreams#Public"
type Activity struct {
Context interface{} `json:"@context"`
ID string `json:"id,omitempty"`

Wyświetl plik

@ -12,9 +12,9 @@ type OrderedCollection struct {
func NewOrderedCollection(id string, items interface{}, length int) *OrderedCollection {
return &OrderedCollection{
Context: "https://www.w3.org/ns/activitystreams",
Context: ActivityStreams,
ID: id,
Type: "OrderedCollection",
Type: TypeOrderedCollection,
TotalItems: length,
OrderedItems: items,
}

Wyświetl plik

@ -21,16 +21,16 @@ type Note struct {
func NewNote(from, to, content string) *Note {
return &Note{
Context: "https://www.w3.org/ns/activitystreams",
Context: ActivityStreams,
To: []string{to},
Content: content,
Type: "Note",
Type: TypeNote,
AttributedTo: from,
}
}
func NewPublicNote(from, content string) *Note {
return NewNote(from, "https://www.w3.org/ns/activitystreams#Public", content)
return NewNote(from, Public, content)
}
func (note *Note) Wrap(username string) *Activity {

Wyświetl plik

@ -2,8 +2,6 @@ package activitypub
import "encoding/json"
const ActivityStreams = "https://www.w3.org/ns/activitystreams"
type Object struct {
Context interface{} `json:"@context"`
Type string `json:"type"`

Wyświetl plik

@ -1,8 +1,13 @@
package activitypub
const (
TypeCreate = "Create"
TypeFollow = "Follow"
TypeAccept = "Accept"
TypeNote = "Note"
ActivityStreams = "https://www.w3.org/ns/activitystreams"
TypeCreate = "Create"
TypeFollow = "Follow"
TypeAccept = "Accept"
TypeNote = "Note"
TypeOrderedCollection = "OrderedCollection"
Public = ActivityStreams + "#Public"
)

Wyświetl plik

@ -31,9 +31,9 @@ var Followers = route.New(HttpGet, "/u/:username/followers", func(x IContext) er
}
result := &activitypub.Followers{
Context: "https://www.w3.org/ns/activitystreams",
Context: activitypub.ActivityStreams,
ID: id,
Type: "OrderedCollection",
Type: activitypub.TypeOrderedCollection,
TotalItems: len(items),
OrderedItems: items,
}
@ -55,7 +55,7 @@ var AcceptFollowRequest = route.New(HttpPut, "/u/:username/followers/:id/accept"
}
data, _ := json.Marshal(&activitypub.Activity{
Context: "https://www.w3.org/ns/activitystreams",
Context: activitypub.ActivityStreams,
ID: x.StringUtil().Format("%s://%s/%s", config.PROTOCOL, config.DOMAIN, x.GUID()),
Type: activitypub.TypeAccept,
Actor: x.StringUtil().Format("%s://%s/u/%s", config.PROTOCOL, config.DOMAIN, username),

Wyświetl plik

@ -26,7 +26,7 @@ func createActor(user *repos.User) *activitypub.Actor {
return &activitypub.Actor{
Context: []interface{}{
"https://www.w3.org/ns/activitystreams",
activitypub.ActivityStreams,
"https://w3id.org/security/v1",
},
Followers: id + "/followers",

Wyświetl plik

@ -70,7 +70,7 @@ var InboxPost = route.New(HttpPost, "/u/:username/inbox", func(x IContext) error
if user.Access == repos.ACCESS_PUBLIC {
data, _ := json.Marshal(&activitypub.Activity{
Context: "https://www.w3.org/ns/activitystreams",
Context: activitypub.ActivityStreams,
ID: x.StringUtil().Format("%s://%s/%s", config.PROTOCOL, config.DOMAIN, x.GUID()),
Type: activitypub.TypeAccept,
Actor: x.StringUtil().Format("%s://%s/u/%s", config.PROTOCOL, config.DOMAIN, username),
@ -146,9 +146,9 @@ var InboxGet = route.New(HttpGet, "/u/:username/inbox", func(x IContext) error {
}
outbox := &activitypub.Outbox{
Context: "https://www.w3.org/ns/activitystreams",
Context: activitypub.ActivityStreams,
ID: id,
Type: "OrderedCollection",
Type: activitypub.TypeOrderedCollection,
TotalItems: len(items),
OrderedItems: items,
}

Wyświetl plik

@ -35,36 +35,3 @@ var User = route.New(HttpGet, "/u/:username", func(x IContext) error {
})
}
})
var _ = route.New(HttpPost, "/u/:username/:followers", func(x IContext) error {
username := x.Request().Params("username")
if username == "" {
return x.BadRequest("Bad request")
}
storage := x.Storage()
domain := x.Config().Get("domain")
result := storage.Prepare("select followers from accounts where name = ?").Param(fmt.Sprintf("%s@%s", username, domain))
if result.Get("followers") == nil {
result.Set("followers", "[]")
}
followers := x.ParseJson(result.Get("followers"))
followersCollection := fmt.Sprintf(`
{
"type":"OrderedCollection",
"totalItems":followers.length,
"id": "https://%[1]s/u/%[2]s/followers",
"first": {
"type":"OrderedCollectionPage",
"totalItems":%[3]d,
"partOf": "https://%[1]s/u/%[2]s/followers",
"orderedItems": followers,
"id": "https://%[1]s/u/%[2]s/followers?page=1"
},
"@context":["https://www.w3.org/ns/activitystreams"]
}
`, domain, username, followers.Length())
return x.Json(followersCollection)
})

Wyświetl plik

@ -1,6 +1,7 @@
package utility
import (
"activitypub"
"errors"
"fmt"
"regexp"
@ -50,5 +51,5 @@ var _ = validate.RegisterValidation("password", func(fl validator.FieldLevel) bo
})
var _ = validate.RegisterValidation("activitystream", func(fl validator.FieldLevel) bool {
return fl.Field().String() == "https://www.w3.org/ns/activitystreams"
return fl.Field().String() == activitypub.ActivityStreams
})