-
Notifications
You must be signed in to change notification settings - Fork 1
/
actor.go
49 lines (41 loc) · 1.58 KB
/
actor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package services
import (
"context"
"fmt"
"github.com/shigde/sfu/internal/activitypub/instance"
"github.com/shigde/sfu/internal/activitypub/models"
"github.com/shigde/sfu/internal/activitypub/outbox"
"github.com/shigde/sfu/internal/activitypub/remote"
)
type ActorService struct {
config *instance.FederationConfig
actorRep *models.ActorRepository
sender *outbox.Sender
}
func NewActorService(config *instance.FederationConfig, actorRep *models.ActorRepository, sender *outbox.Sender) *ActorService {
return &ActorService{
config: config, actorRep: actorRep, sender: sender}
}
func (a *ActorService) GetLocalInstanceActor(ctx context.Context) (*models.Actor, error) {
actorIri := instance.BuildAccountIri(a.config.InstanceUrl, a.config.InstanceUsername)
actor, err := a.actorRep.GetActorByActorIRI(ctx, actorIri)
if err != nil {
return nil, fmt.Errorf("reading local instance actor from db: %w", err)
}
return actor, nil
}
func (a *ActorService) CreateActorFromRemoteAccount(ctx context.Context, accountIri string, localInstanceActor *models.Actor) (*models.Actor, error) {
req, err := a.sender.GetSignedRequest(localInstanceActor.GetActorIri(), accountIri)
if err != nil {
return nil, fmt.Errorf("building signed actor request to fetch remote account: %w", err)
}
actor, err := remote.FetchAccountAsActor(ctx, req)
if err != nil {
return nil, fmt.Errorf("fetching account from remote instance to build an actor: %w", err)
}
actor, err = a.actorRep.Upsert(ctx, actor)
if err != nil {
return nil, fmt.Errorf("insert actor in dab if not exists: %w", err)
}
return actor, nil
}