Finished Prototype

This commit is contained in:
Hoernschen 2020-10-17 12:13:15 +02:00
parent ea54a27796
commit 6de476260d
30 changed files with 2189 additions and 0 deletions

11
entities/object/object.go Normal file
View file

@ -0,0 +1,11 @@
package object
type Object struct {
Context string `json:"@context,omitempty"`
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
AttributedTo string `json:"attributedTo,omitempty"`
Content string `json:"content,omitempty"`
Published int64 `json:"published,omitempty"`
To string `json:"to,omitempty"`
}

View file

@ -0,0 +1,49 @@
package object
import (
"encoding/json"
"fmt"
"net/http"
"time"
"git.nutfactory.org/hoernschen/ActivityPub/config"
"git.nutfactory.org/hoernschen/ActivityPub/utils"
)
func New(objectType string, attributedTo string, content string, published int64, to string, userId string) (err error, newObject *Object) {
err, id := utils.CreateUUID()
url := fmt.Sprintf("%s://%s/%s/posts/%s", config.HttpString, config.Homeserver, userId, id)
if published == 0 {
published = time.Now().Unix()
}
if attributedTo == "" {
attributedTo = utils.GenerateProfileUrl(userId)
}
newObject = &Object{
Context: utils.GetDefaultContext(),
Id: url,
Type: objectType,
AttributedTo: attributedTo,
Content: content,
Published: published,
To: to,
}
return
}
func GetPostHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", utils.GetContentTypeString())
foundObject, err := ReadObject(r.URL.RequestURI())
if foundObject == nil || err != nil {
w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode(fmt.Sprintf("Post not found: %s", err)); err != nil {
panic(err)
}
return
}
foundObject.Context = utils.GetDefaultContext()
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(foundObject); err != nil {
panic(err)
}
}

View file

@ -0,0 +1,70 @@
package object
import (
"fmt"
"git.nutfactory.org/hoernschen/ActivityPub/utils/database"
)
func CreateObject(object *Object) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO object
(id, type, attributedTo, content, published, toActor)
VALUES
(?, ?, ?, ?, ?, ?)`)
tx, err := database.DB.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(
object.Id,
object.Type,
object.AttributedTo,
object.Content,
object.Published,
object.To,
)
if err != nil {
tx.Rollback()
return
}
tx.Commit()
return
}
func ReadObject(id string) (foundObject *Object, err error) {
queryStmt := fmt.Sprintf(`SELECT id, type, attributedTo, content, published, toActor
FROM object
WHERE id = '%s'`, id)
rows, err := database.DB.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
foundObject = &Object{}
err = rows.Scan(
&foundObject.Id,
&foundObject.Type,
&foundObject.AttributedTo,
&foundObject.Content,
&foundObject.Published,
&foundObject.To,
)
if err != nil {
return
}
}
return
}