forgejo/services/federation/person_service.go
nachtjasmin 8ee4a7d658 chore: ensure consistent import aliasing for services and models (#10253)
To make sure that the code stays maintainable, I added the `importas` linter to ensure that the imports for models and services stay consistent.

I realised that this might be needed after finding some discrepancies between singular/plural naming, and, especially in the case of the `forgejo.org/services/context` package, multiple different aliases like `gitea_ctx`, `app_context` and `forgejo_context`. I decided for `app_context`, as that seems to be the most commonly used naming.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10253
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: nachtjasmin <nachtjasmin@posteo.de>
Co-committed-by: nachtjasmin <nachtjasmin@posteo.de>
2025-11-30 17:00:57 +01:00

60 lines
1.7 KiB
Go

// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package federation
import (
"context"
"net/http"
"forgejo.org/models/user"
"forgejo.org/modules/forgefed"
"forgejo.org/modules/log"
app_context "forgejo.org/services/context"
ap "github.com/go-ap/activitypub"
"github.com/go-ap/jsonld"
)
func ProcessPersonInbox(ctx context.Context, user *user.User, activity *ap.Activity) (ServiceResult, error) {
switch activity.Type {
case ap.CreateType:
return processPersonInboxCreate(ctx, user, activity)
case ap.FollowType:
return processPersonFollow(ctx, user, activity)
case ap.UndoType:
return processPersonInboxUndo(ctx, user, activity)
case ap.AcceptType:
return processPersonInboxAccept(activity)
}
log.Error("Unsupported PersonInbox activity: %v", activity.Type)
return ServiceResult{}, NewErrNotAcceptablef("unsupported activity: %v", activity.Type)
}
func FollowRemoteActor(ctx *app_context.APIContext, localUser *user.User, actorURI string) error {
_, federatedUser, federationHost, err := FindOrCreateFederatedUser(ctx.Base, actorURI)
if err != nil {
log.Error("Federated user not found (%s): %v", actorURI, err)
ctx.Error(http.StatusNotAcceptable, "Federated user not found", err)
return err
}
followReq, err := forgefed.NewForgeFollow(localUser.APActorID(), actorURI)
if err != nil {
return err
}
payload, err := jsonld.WithContext(jsonld.IRI(ap.ActivityBaseURI)).
Marshal(followReq)
if err != nil {
return err
}
hostURL := federationHost.AsURL()
return deliveryQueue.Push(deliveryQueueItem{
InboxURL: hostURL.JoinPath(federatedUser.InboxPath).String(),
Doer: localUser,
Payload: payload,
})
}