-
-
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 #92 from devanbenz/feat-77-comment
feat/add-comment
- Loading branch information
Showing
10 changed files
with
256 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"$id": "https://sublinks.org/comment.schema.json", | ||
"title": "Comment", | ||
"description": "A comment in a Sublinks Post attributable to an author.", | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"description": "The unique identifier for the comment.", | ||
"type": "string" | ||
}, | ||
"post_id": { | ||
"description": "The unique identifier of Post comment is for.", | ||
"type": "string" | ||
}, | ||
"content": { | ||
"description": "The content of the comment.", | ||
"type": "string" | ||
}, | ||
"author_id": { | ||
"description": "The unique identifier for the actor that is the author of the comment. I.E. discuss.online/u/lazyguru", | ||
"type": "string" | ||
}, | ||
"published": { | ||
"description": "The date and time the comment was published.", | ||
"type": "string" | ||
}, | ||
"nsfw": { | ||
"description": "Whether the comment has sensitive (NSFW) content or not.", | ||
"type": "boolean" | ||
} | ||
}, | ||
"required": ["id", "post", "content", "author_id", "published"] | ||
} |
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,60 @@ | ||
package activitypub | ||
|
||
import ( | ||
"fmt" | ||
"sublinks/sublinks-federation/internal/model" | ||
"time" | ||
) | ||
|
||
type Note struct { | ||
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"` | ||
Audience string `json:"audience"` | ||
InReplyTo string `json:"inReplyTo"` | ||
Content string `json:"content"` | ||
MediaType string `json:"mediaType"` | ||
Source Source `json:"source,omitempty"` | ||
Tag []Tag `json:"tag,omitempty"` | ||
Distinguished bool `json:"distinguished,omitempty"` | ||
Language Language `json:"language,omitempty"` | ||
Published time.Time `json:"published"` | ||
Updated time.Time `json:"updated"` | ||
} | ||
|
||
type Tag struct { | ||
Href string `json:"href"` | ||
Type string `json:"type"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func NewNote(commentUrl string, fromUser string, postUrl string, commentBody string, published time.Time) *Note { | ||
return &Note{ | ||
Id: commentUrl, | ||
Type: "Note", | ||
AttributedTo: fromUser, | ||
To: []string{"https://www.w3.org/ns/activitystreams#Public"}, | ||
Cc: []string{fromUser, commentUrl}, | ||
Audience: commentUrl, | ||
InReplyTo: postUrl, | ||
Content: commentBody, | ||
MediaType: "text/html", | ||
Source: Source{ | ||
Content: fmt.Sprintf("This is a comment on %s post", postUrl), | ||
MediaType: "text/markdown", | ||
}, | ||
Language: Language{ | ||
Identifier: "en", | ||
Name: "English", | ||
}, | ||
Distinguished: false, | ||
Published: published, | ||
} | ||
} | ||
|
||
func ConvertCommentToNote(c *model.Comment) *Note { | ||
return NewNote(c.UrlStub, c.Author, c.Post, c.Content, c.Published) | ||
} |
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,34 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"sublinks/sublinks-federation/internal/activitypub" | ||
"sublinks/sublinks-federation/internal/model" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func (server *Server) SetupCommentRoutes() { | ||
server.Router.HandleFunc("/comment/{commentId}", server.getCommentHandler).Methods("GET") | ||
} | ||
|
||
func (server *Server) getCommentHandler(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
comment := model.Comment{UrlStub: vars["commentId"]} | ||
err := server.Database.Find(&comment) | ||
if err != nil { | ||
server.Logger.Error(fmt.Sprintf("Error reading comment: %+v %s", comment, err), err) | ||
return | ||
} | ||
commentLd := activitypub.ConvertCommentToNote(&comment) | ||
commentLd.Context = activitypub.GetContext() | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Add("content-type", "application/activity+json") | ||
content, _ := json.MarshalIndent(commentLd, "", " ") | ||
_, err = w.Write(content) | ||
if err != nil { | ||
server.Logger.Error("Error writing response", err) | ||
} | ||
} |
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,13 @@ | ||
package model | ||
|
||
import "time" | ||
|
||
type Comment struct { | ||
Id string `json:"id" gorm:"primary_key"` | ||
UrlStub string `json:"url_stub"` | ||
Post string `json:"post_id"` | ||
Author string `json:"author_id"` | ||
Nsfw bool `json:"nsfw"` | ||
Published time.Time `json:"published"` | ||
Content string `json:"content"` | ||
} |
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,7 @@ | ||
package queue | ||
|
||
const ( | ||
ActorRoutingKey = "actor.create" | ||
PostRoutingKey = "post.create" | ||
CommentRoutingKey = "comment.create" | ||
) |
Oops, something went wrong.