Follow accept works and messags are pushed as expected
Some checks failed
/ docker (push) Failing after 2m50s
Some checks failed
/ docker (push) Failing after 2m50s
This commit is contained in:
parent
9a3a330b1d
commit
ff6a730e58
10 changed files with 482 additions and 19 deletions
|
@ -1 +1,95 @@
|
|||
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/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)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusNotFound)
|
||||
case nil:
|
||||
activity.Context = activitypub.BaseLdContext
|
||||
data, err := json.Marshal(activity)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Any("activity", activity).Msg("Failed to marshal create activity")
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/activity+json")
|
||||
fmt.Fprint(w, string(data))
|
||||
default:
|
||||
if storage.HandleReconnectError(err) {
|
||||
log.Error().Err(err).Msg("Connection failed, restart attempt started")
|
||||
} else {
|
||||
log.Error().Err(err).Msg("Failed to get create activity from db")
|
||||
}
|
||||
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: id,
|
||||
Type: "Follow",
|
||||
}
|
||||
if follower.RemoteInfo != nil {
|
||||
out.Actor = follower.RemoteInfo.ApLink
|
||||
} else {
|
||||
out.Actor = userIdToApUrl(follower.ID)
|
||||
}
|
||||
if followed.RemoteInfo != nil {
|
||||
out.Object = followed.RemoteInfo.ApLink
|
||||
} else {
|
||||
out.Object = userIdToApUrl(followed.ID)
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue