Move translators db->ap to separate module
This commit is contained in:
parent
cfe5047433
commit
d86ad370df
16 changed files with 456 additions and 423 deletions
|
@ -1,35 +1,23 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
"github.com/rs/zerolog/hlog"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/activitypub"
|
||||
"git.mstar.dev/mstar/linstrom/config"
|
||||
"git.mstar.dev/mstar/linstrom/activitypub/translators"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/models"
|
||||
)
|
||||
|
||||
type ActivityFollowOut struct {
|
||||
Context any `json:"@context,omitempty"`
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Actor string `json:"actor"`
|
||||
Object any `json:"object"`
|
||||
}
|
||||
|
||||
func activityFollow(w http.ResponseWriter, r *http.Request) {
|
||||
log := hlog.FromRequest(r)
|
||||
id := r.PathValue("id")
|
||||
activity, err := FollowFromStorage(r.Context(), id)
|
||||
activity, err := translators.FollowFromStorage(r.Context(), id)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusNotFound)
|
||||
|
@ -52,45 +40,3 @@ func activityFollow(w http.ResponseWriter, r *http.Request) {
|
|||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func FollowFromStorage(ctx context.Context, id string) (*ActivityFollowOut, error) {
|
||||
ac := dbgen.Activity
|
||||
u2u := dbgen.UserToUserRelation
|
||||
u := dbgen.User
|
||||
// log := log.Ctx(ctx)
|
||||
activity, err := ac.Where(ac.Id.Eq(id), ac.Type.Eq(string(models.ActivityFollow))).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
followId, err := strconv.ParseUint(activity.ObjectId, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
relation, err := u2u.Where(u2u.ID.Eq(followId)).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
follower, err := u.Where(u.ID.Eq(relation.UserId)).Preload(u.RemoteInfo).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
followed, err := u.Where(u.ID.Eq(relation.TargetUserId)).Preload(u.RemoteInfo).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := ActivityFollowOut{
|
||||
Id: config.GlobalConfig.General.GetFullPublicUrl() + "/api/activitypub/activity/follow/" + id,
|
||||
Type: "Follow",
|
||||
}
|
||||
if follower.RemoteInfo != nil {
|
||||
out.Actor = follower.RemoteInfo.ApLink
|
||||
} else {
|
||||
out.Actor = activitypub.UserIdToApUrl(follower.ID)
|
||||
}
|
||||
if followed.RemoteInfo != nil {
|
||||
out.Object = followed.RemoteInfo.ApLink
|
||||
} else {
|
||||
out.Object = activitypub.UserIdToApUrl(followed.ID)
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue