package activitypub import ( "context" "encoding/json" "fmt" "net/http" "strings" 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/storage-new" "git.mstar.dev/mstar/linstrom/storage-new/dbgen" "git.mstar.dev/mstar/linstrom/storage-new/models" ) type ActivityAcceptOut struct { Context any `json:"@context,omitempty"` Id string `json:"id"` Type string `json:"type"` Actor string `json:"actor"` Object any `json:"object"` } func activityAccept(w http.ResponseWriter, r *http.Request) { log := hlog.FromRequest(r) id := r.PathValue("id") activity, err := CreateFromStorage(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 AcceptFromStorage(ctx context.Context, id string) (*ActivityAcceptOut, error) { a := dbgen.Activity activity, err := a.Where(a.Id.Eq(id), a.Type.Eq(string(models.ActivityAccept))).First() if err != nil { return nil, err } // switch activity.ObjectType { // case models.ActivitystreamsActivityTargetFollow: // default: // return nil, errors.New("unknown activity target type") // } follow, err := FollowFromStorage(ctx, activity.ObjectId) if err != nil { return nil, err } var outId string if strings.HasPrefix(id, "http") { outId = id } else { outId = fmt.Sprintf("%s/api/activitypub/activity/accept/%s", config.GlobalConfig.General.GetFullPublicUrl(), id) } return &ActivityAcceptOut{ Id: outId, Actor: follow.Object.(string), Type: "Accept", Object: follow, }, nil }