linstrom/activitypub/translators/follow.go

61 lines
1.6 KiB
Go

package translators
import (
"context"
"strconv"
"git.mstar.dev/mstar/linstrom/activitypub"
"git.mstar.dev/mstar/linstrom/config"
"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 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
}