mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-10-17 06:28:22 +00:00
Sent user activities to distant federated server (#8792)
This PR is part of #4767. It contains * a refactoring of validation error messages * adds the ability to send user-activities to distant federated servers Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8792 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de> Co-committed-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
This commit is contained in:
parent
6b6fa21b25
commit
c081f20776
22 changed files with 885 additions and 130 deletions
83
services/federation/user_activity.go
Normal file
83
services/federation/user_activity.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package federation
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
activities_model "forgejo.org/models/activities"
|
||||
"forgejo.org/models/forgefed"
|
||||
"forgejo.org/models/user"
|
||||
"forgejo.org/modules/setting"
|
||||
"forgejo.org/modules/structs"
|
||||
"forgejo.org/services/convert"
|
||||
|
||||
ap "github.com/go-ap/activitypub"
|
||||
"github.com/go-ap/jsonld"
|
||||
)
|
||||
|
||||
func SendUserActivity(ctx context.Context, doer *user.User, activity *activities_model.Action) error {
|
||||
followers, err := user.GetFollowersForUser(ctx, doer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
userActivity, err := convert.ActionToForgeUserActivity(ctx, activity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
payload, err := jsonld.WithContext(
|
||||
jsonld.IRI(ap.ActivityBaseURI),
|
||||
).Marshal(userActivity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, follower := range followers {
|
||||
_, federatedUserFollower, err := user.GetFederatedUserByUserID(ctx, follower.FollowingUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
federationHost, err := forgefed.GetFederationHost(ctx, federatedUserFollower.FederationHostID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hostURL := federationHost.AsURL()
|
||||
if err := deliveryQueue.Push(deliveryQueueItem{
|
||||
InboxURL: hostURL.JoinPath(federatedUserFollower.InboxPath).String(),
|
||||
Doer: doer,
|
||||
Payload: payload,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NotifyActivityPubFollowers(ctx context.Context, actions []activities_model.Action) error {
|
||||
if !setting.Federation.Enabled {
|
||||
return nil
|
||||
}
|
||||
for _, act := range actions {
|
||||
if act.Repo != nil {
|
||||
if act.Repo.IsPrivate {
|
||||
continue
|
||||
}
|
||||
if act.Repo.Owner.KeepActivityPrivate || act.Repo.Owner.Visibility != structs.VisibleTypePublic {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if act.ActUser.KeepActivityPrivate || act.ActUser.Visibility != structs.VisibleTypePublic {
|
||||
continue
|
||||
}
|
||||
if err := SendUserActivity(ctx, act.ActUser, &act); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue