-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from participating-online/15-add-createeditdel…
…ete-post-handling Add initial activity routes
- Loading branch information
Showing
11 changed files
with
143 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters