Skip to content

Commit

Permalink
Merge pull request #18 from participating-online/15-add-createeditdel…
Browse files Browse the repository at this point in the history
…ete-post-handling

Add initial activity routes
  • Loading branch information
lazyguru authored Dec 6, 2023
2 parents c28fdda + 9db5bdd commit 3ec8695
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .codesandbox/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
"branch": true,
"resume": true
}
},
"go test": {
"name": "Test",
"command": "go test -v ./...",
"runAtStart": true,
"restartOn": {
"files": ["*"],
"branch": true,
"resume": true
}
}
}
}
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Contributing
Aka. Developer Guidelines

## Intro

We're currently _very_ early in the development stages of the Sublinks federation service. Therefore there aren't too many guidelines yet. However, as you can see below there are some that we would appreciate you to consider before and while you contribute to this project.

As with most things you'll read about in this document, even these guidelines are up for discussion. If you believe anything should be changed please feel free to create a pull request and let us know what and why. Same thing applies if you want to add or stricten/loosen a guideline.

## Tests

Support for tests has been set up. Please submit unit and/or end-to-end tests with all PRs. 100% coverage (at least with unit tests) is a falacy. Ideally we want to strive for "good enough" coverage instead.

## Pull Requests

The pull request flow in this project isn't anything special. We require a pull request to be created before anything is merged into `main`. At least one person must approve the pull request.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module participating-online/sublinks-federation

go 1.21.4

require github.com/gorilla/mux v1.8.1
require (
github.com/gorilla/mux v1.8.1
golang.org/x/text v0.14.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
33 changes: 33 additions & 0 deletions internal/activitypub/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package activitypub

type Activity struct {
Context Context `json:"@context"`
Actor string `json:"actor"`
To []string `json:"to"`
Object interface{} `json:"object"`
Cc []string `json:"cc"`
Audience string `json:"audience"`
Type string `json:"type"`
Id string `json:"id"`
}

func NewActivity(
id string,
actType string,
actor string,
to []string,
cc []string,
audience string,
obj interface{},
) Activity {
return Activity{
Context: *GetContext(),
Id: id,
Type: actType,
Actor: actor,
To: to,
Cc: cc,
Audience: audience,
Object: obj,
}
}
4 changes: 2 additions & 2 deletions internal/activitypub/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type moderator struct {
Id string `json:"@id"`
}

func GetContext() Context {
return Context{
func GetContext() *Context {
return &Context{
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
lemmyContextData{
Expand Down
16 changes: 7 additions & 9 deletions internal/activitypub/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,27 @@ type Language struct {
}

type Post struct {
Context Context `json:"@context"`
Context *Context `json:"@context,omitempty"`
Id string `json:"id"`
Type string `json:"type"`
AttributedTo string `json:"attributedTo"`
To []string `json:"to"`
Cc []string `json:"cc"`
Cc []string `json:"cc,omitempty"`
Audience string `json:"audience"`
Name string `json:"name"`
Content string `json:"content"`
MediaType string `json:"mediaType"`
Source Source `json:"source"`
Attachment []Link `json:"attachment"`
Image []Link `json:"image"`
Attachment []Link `json:"attachment,omitempty"`
Image []Link `json:"image,omitempty"`
Sensitive bool `json:"sensitive"`
CommentsEnabled bool `json:"commentsEnabled"`
Language Language `json:"language"`
Published time.Time `json:"published"`
}

func NewPost(postUrl string, fromUser string, communityUrl string, postTitle string, postBody string, nsfw bool, published time.Time) Post {
post := Post{
Context: GetContext(),
func NewPost(postUrl string, fromUser string, communityUrl string, postTitle string, postBody string, nsfw bool, published time.Time) *Post {
return &Post{
Id: postUrl,
Type: "Page",
AttributedTo: fromUser,
Expand All @@ -57,10 +56,9 @@ func NewPost(postUrl string, fromUser string, communityUrl string, postTitle str
},
Published: published,
}
return post
}

func ConvertPostToApub(p *lemmy.PostResponse) Post {
func ConvertPostToApub(p *lemmy.PostResponse) *Post {
return NewPost(
p.PostView.Post.ApId,
fmt.Sprintf("https://demo.sublinks.org/u/%s", p.PostView.Creator.Name),
Expand Down
2 changes: 1 addition & 1 deletion internal/activitypub/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Endpoints struct {
}

type User struct {
Context Context `json:"@context"`
Context *Context `json:"@context"`
Id string `json:"id"`
PreferredUsername string `json:"preferredUsername"`
Inbox string `json:"inbox"`
Expand Down
66 changes: 66 additions & 0 deletions internal/http/routes/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package routes

import (
"context"
"encoding/json"
"log"
"net/http"
"participating-online/sublinks-federation/internal/activitypub"
"participating-online/sublinks-federation/internal/lemmy"

"fmt"

"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/gorilla/mux"
)

func SetupActivityRoutes(r *mux.Router) {
r.HandleFunc("/activities/{action}/{id}", getActivityHandler).Methods("GET")
}

func getActivityHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var content []byte
switch vars["action"] {
case "create":
obj, err := GetPostActivityObject(vars["id"])
if err != nil {
log.Println("Error reading object", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
content, _ = json.MarshalIndent(
activitypub.NewActivity(
r.RequestURI,
cases.Title(language.English).String(vars["action"]),
obj.AttributedTo,
obj.To,
obj.Cc,
obj.Audience,
obj,
), "", " ")

break
default:
error.Error(fmt.Errorf("action %s not found", vars["action"]))
w.WriteHeader(http.StatusNotFound)
return
}

w.WriteHeader(http.StatusOK)
w.Header().Add("content-type", "application/activity+json")
w.Write(content)
}

func GetPostActivityObject(id string) (*activitypub.Post, error) {
ctx := context.Background()
c := lemmy.GetLemmyClient(ctx)
post, err := c.GetPost(ctx, id)
if err != nil {
log.Println("Error reading post", err)
return nil, err
}
return activitypub.ConvertPostToApub(post), nil
}
1 change: 1 addition & 0 deletions internal/http/routes/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func getPostHandler(w http.ResponseWriter, r *http.Request) {
return
}
postLd := activitypub.ConvertPostToApub(post)
postLd.Context = activitypub.GetContext()
w.WriteHeader(http.StatusOK)
w.Header().Add("content-type", "application/activity+json")
content, _ := json.MarshalIndent(postLd, "", " ")
Expand Down
1 change: 1 addition & 0 deletions internal/http/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ func SetupRoutes() *mux.Router {
SetupUserRoutes(r)
SetupPostRoutes(r)
SetupApubRoutes(r)
SetupActivityRoutes(r)
return r
}

0 comments on commit 3ec8695

Please sign in to comment.