Finished Prototype
This commit is contained in:
parent
ea54a27796
commit
6de476260d
30 changed files with 2189 additions and 0 deletions
15
entities/actor/actor.go
Normal file
15
entities/actor/actor.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package actor
|
||||
|
||||
type Actor struct {
|
||||
Context string `json:"@context,omitempty"`
|
||||
Id string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
PreferredUsername string `json:"preferredUsername,omitempty"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
Inbox string `json:"inbox,omitempty"`
|
||||
Outbox string `json:"outbox,omitempty"`
|
||||
Followers string `json:"followers,omitempty"`
|
||||
Following string `json:"following,omitempty"`
|
||||
Liked string `json:"liked,omitempty"`
|
||||
}
|
75
entities/actor/actorController.go
Normal file
75
entities/actor/actorController.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
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
|
||||
}
|
76
entities/actor/actorDatabaseConnector.go
Normal file
76
entities/actor/actorDatabaseConnector.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
package actor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.nutfactory.org/hoernschen/ActivityPub/utils/database"
|
||||
)
|
||||
|
||||
func CreateActor(actor *Actor) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO actor
|
||||
(id, type, name, preferredUsername, summary, inbox, outbox, followers, following, liked)
|
||||
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(
|
||||
actor.Id,
|
||||
actor.Type,
|
||||
actor.Name,
|
||||
actor.PreferredUsername,
|
||||
actor.Summary,
|
||||
actor.Inbox,
|
||||
actor.Outbox,
|
||||
actor.Followers,
|
||||
actor.Following,
|
||||
actor.Liked,
|
||||
)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadActor(id string) (foundActor *Actor, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, type, name, preferredUsername, summary, inbox, outbox, followers, following, liked
|
||||
FROM actor
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundActor = &Actor{}
|
||||
err = rows.Scan(
|
||||
&foundActor.Id,
|
||||
&foundActor.Type,
|
||||
&foundActor.Name,
|
||||
&foundActor.PreferredUsername,
|
||||
&foundActor.Summary,
|
||||
&foundActor.Inbox,
|
||||
&foundActor.Outbox,
|
||||
&foundActor.Followers,
|
||||
&foundActor.Following,
|
||||
&foundActor.Liked,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue