ActivityPub/entities/object/objectController.go

50 lines
1.3 KiB
Go
Raw Normal View History

2020-10-17 10:13:15 +00:00
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)
}
}