76 lines
2 KiB
Go
76 lines
2 KiB
Go
|
package actor
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"time"
|
||
|
|
||
|
"git.nutfactory.org/hoernschen/ActivityPub/config"
|
||
|
"git.nutfactory.org/hoernschen/ActivityPub/utils"
|
||
|
)
|
||
|
|
||
|
func New(userId string) (newActor *Actor) {
|
||
|
id := utils.GenerateProfileUrl(userId)
|
||
|
newActor = GenerateProfile(id, userId)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func GenerateProfile(id string, userId string) (profile *Actor) {
|
||
|
profile = &Actor{
|
||
|
Context: utils.GetDefaultContext(),
|
||
|
Id: id,
|
||
|
Name: userId,
|
||
|
PreferredUsername: userId,
|
||
|
Type: "Person",
|
||
|
Inbox: utils.GenerateInboxUrl(userId),
|
||
|
Outbox: utils.GenerateOutboxUrl(userId),
|
||
|
Followers: utils.GenerateFollowersUrl(userId),
|
||
|
Following: utils.GenerateFollowingUrl(userId),
|
||
|
Liked: utils.GenerateLikedUrl(userId),
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func GetProfileHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
w.Header().Set("Content-Type", utils.GetContentTypeString())
|
||
|
foundActor, err := ReadActor(fmt.Sprintf("%s://%s%s", config.HttpString, config.Homeserver, r.URL.RequestURI()))
|
||
|
if foundActor == nil || err != nil {
|
||
|
w.WriteHeader(http.StatusBadRequest)
|
||
|
if err := json.NewEncoder(w).Encode(fmt.Sprintf("Actor not found: %s", err)); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
profile := GenerateProfile(foundActor.Id, foundActor.Name)
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
if err := json.NewEncoder(w).Encode(profile); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetProfile(actorId string) (err error, profile *Actor) {
|
||
|
client := &http.Client{Timeout: 2 * time.Second}
|
||
|
req, err := http.NewRequest(http.MethodGet, actorId, bytes.NewBuffer(nil))
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
req.Header["Content-Type"] = []string{utils.GetContentTypeString()}
|
||
|
r, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if r.StatusCode != http.StatusOK {
|
||
|
err = utils.HandleHTTPError(r)
|
||
|
return
|
||
|
} else {
|
||
|
decoder := json.NewDecoder(r.Body)
|
||
|
err = decoder.Decode(&profile)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|