-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c701950
commit 6d9641d
Showing
6 changed files
with
368 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package note_tasks | ||
|
||
import ( | ||
"context" | ||
"github.com/versia-pub/versia-go/internal/repository" | ||
|
||
"github.com/google/uuid" | ||
"github.com/versia-pub/versia-go/internal/entity" | ||
) | ||
|
||
type FederateNoteData struct { | ||
NoteID uuid.UUID `json:"noteID"` | ||
} | ||
|
||
func (t *Handler) FederateNote(ctx context.Context, data FederateNoteData) error { | ||
s := t.telemetry.StartSpan(ctx, "function", "tasks/Handler.FederateNote") | ||
defer s.End() | ||
ctx = s.Context() | ||
|
||
var n *entity.Note | ||
if err := t.repositories.Atomic(ctx, func(ctx context.Context, tx repository.Manager) error { | ||
var err error | ||
n, err = tx.Notes().GetByID(ctx, data.NoteID) | ||
if err != nil { | ||
return err | ||
} | ||
if n == nil { | ||
t.log.V(-1).Info("Could not find note", "id", data.NoteID) | ||
return nil | ||
} | ||
|
||
for _, uu := range n.Mentions { | ||
if !uu.IsRemote { | ||
t.log.V(2).Info("User is not remote", "user", uu.ID) | ||
continue | ||
} | ||
|
||
res, err := t.federationService.SendToInbox(ctx, n.Author, &uu, n.ToVersia()) | ||
if err != nil { | ||
t.log.Error(err, "Failed to send note to remote user", "res", res, "user", uu.ID) | ||
} else { | ||
t.log.V(2).Info("Sent note to remote user", "res", res, "user", uu.ID) | ||
} | ||
} | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package note_tasks | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/versia-pub/versia-go/internal/repository" | ||
"github.com/versia-pub/versia-go/internal/service" | ||
|
||
"git.devminer.xyz/devminer/unitel" | ||
"github.com/go-logr/logr" | ||
"github.com/versia-pub/versia-go/pkg/taskqueue" | ||
) | ||
|
||
const ( | ||
FederateNote = "federate_note" | ||
) | ||
|
||
type Handler struct { | ||
federationService service.FederationService | ||
|
||
repositories repository.Manager | ||
|
||
telemetry *unitel.Telemetry | ||
log logr.Logger | ||
} | ||
|
||
func NewHandler(federationService service.FederationService, repositories repository.Manager, telemetry *unitel.Telemetry, log logr.Logger) *Handler { | ||
return &Handler{ | ||
federationService: federationService, | ||
|
||
repositories: repositories, | ||
|
||
telemetry: telemetry, | ||
log: log, | ||
} | ||
} | ||
|
||
func (t *Handler) Register(tq *taskqueue.Client) { | ||
tq.RegisterHandler(FederateNote, parse(t.FederateNote)) | ||
} | ||
|
||
func parse[T any](handler func(context.Context, T) error) func(context.Context, taskqueue.Task) error { | ||
return func(ctx context.Context, task taskqueue.Task) error { | ||
var data T | ||
if err := json.Unmarshal(task.Payload, &data); err != nil { | ||
return err | ||
} | ||
|
||
return handler(ctx, data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.