tavern/web/handler_actor.go

172 lines
4.3 KiB
Go

package web
import (
"net/http"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"github.com/ngerakines/tavern/storage"
)
func (h handler) actorInfo(c *gin.Context) {
name := c.Param("name")
user, err := h.storage.GetUserByName(c.Request.Context(), name)
if err != nil {
h.logger.Error("unable to get user by name", zap.Error(err), zap.String("name", name))
c.AbortWithStatus(http.StatusNotFound)
return
}
actorID := storage.NewActorID(user.Name, h.domain)
h.logger.Debug("User info request received", zap.String("user", user.Name))
j := storage.EmptyPayload()
j["@context"] = "https://www.w3.org/ns/activitystreams"
j["id"] = actorID
j["inbox"] = actorID.Inbox()
j["outbox"] = actorID.Outbox()
j["name"] = user.DisplayName
j["preferredUsername"] = user.Name
j["summary"] = ""
j["type"] = "Person"
j["url"] = actorID
j["followers"] = actorID.Followers()
j["following"] = actorID.Following()
k := storage.EmptyPayload()
k["id"] = actorID.MainKey()
k["owner"] = actorID
k["publicKeyPem"] = user.PublicKey
j["publicKey"] = k
h.writeActivityJsonWithProfile(c, j)
}
func (h handler) actorFollowers(c *gin.Context) {
name := c.Param("name")
user, err := h.storage.GetUserByName(c.Request.Context(), name)
if err != nil {
h.logger.Error("unable to get user by name", zap.Error(err), zap.String("name", name))
c.AbortWithStatus(http.StatusNotFound)
return
}
total, err := h.storage.RowCount(c.Request.Context(), `SELECT COUNT(*) FROM followers WHERE user_id = $1`, user.ID)
if err != nil {
h.logger.Error("unable to count followers", zap.Error(err), zap.String("name", name))
c.AbortWithStatus(http.StatusNotFound)
return
}
j := storage.EmptyPayload()
j["@context"] = "https://www.w3.org/ns/activitystreams"
actorID := storage.NewActorID(user.Name, h.domain)
page := intParam(c, "page", 0)
if page == 0 {
j["id"] = actorID.Followers()
j["type"] = "OrderedCollection"
j["totalItems"] = total
j["first"] = actorID.FollowersPage(1)
h.writeJSONLD(c, j)
return
}
offset := (page - 1) * 20
actors, err := h.storage.GetUserFollowers(c.Request.Context(), user.ID, 20, offset)
if err != nil {
h.logger.Error("unable to get followers", zap.Error(err), zap.String("name", name))
c.AbortWithStatus(http.StatusNotFound)
return
}
actorIDs := make([]string, len(actors))
for i, actor := range actors {
actorIDs[i] = actor.Actor
}
j["id"] = actorID.FollowersPage(1)
j["type"] = "OrderedCollectionPage"
j["totalItems"] = total
j["partOf"] = actorID.Followers()
if offset < total {
j["next"] = actorID.FollowersPage(page + 1)
}
if page > 1 {
j["prev"] = actorID.FollowersPage(page - 1)
}
j["orderedItems"] = actorIDs
h.writeJSONLD(c, j)
}
func (h handler) actorFollowing(c *gin.Context) {
name := c.Param("name")
user, err := h.storage.GetUserByName(c.Request.Context(), name)
if err != nil {
h.logger.Error("unable to get user by name", zap.Error(err), zap.String("name", name))
c.AbortWithStatus(http.StatusNotFound)
return
}
total, err := h.storage.RowCount(c.Request.Context(), `SELECT COUNT(*) FROM following WHERE user_id = $1`, user.ID)
if err != nil {
h.logger.Error("unable to count following", zap.Error(err), zap.String("name", name))
c.AbortWithStatus(http.StatusNotFound)
return
}
j := storage.EmptyPayload()
j["@context"] = "https://www.w3.org/ns/activitystreams"
actorID := storage.NewActorID(user.Name, h.domain)
page := intParam(c, "page", 0)
if page == 0 {
j["id"] = actorID.Following()
j["type"] = "OrderedCollection"
j["totalItems"] = total
j["first"] = actorID.FollowingPage(1)
h.writeJSONLD(c, j)
return
}
offset := (page - 1) * 20
actors, err := h.storage.GetUserFollowing(c.Request.Context(), user.ID, 20, offset)
if err != nil {
h.logger.Error("unable to get following", zap.Error(err), zap.String("name", name))
c.AbortWithStatus(http.StatusNotFound)
return
}
actorIDs := make([]string, len(actors))
for i, actor := range actors {
actorIDs[i] = actor.Actor
}
j["id"] = actorID.FollowersPage(1)
j["type"] = "OrderedCollectionPage"
j["totalItems"] = total
j["partOf"] = actorID.Following()
if offset < total {
j["next"] = actorID.FollowingPage(page + 1)
}
if page > 1 {
j["prev"] = actorID.FollowingPage(page - 1)
}
j["orderedItems"] = actorIDs
h.writeJSONLD(c, j)
}