From 5cc6314360dd5741f1d4b9d596878b51e51852f5 Mon Sep 17 00:00:00 2001 From: mstar Date: Thu, 22 May 2025 17:02:28 +0200 Subject: [PATCH] Start work on note importer --- activitypub/importNote.go | 65 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/activitypub/importNote.go b/activitypub/importNote.go index 8f54e4e..fc1a690 100644 --- a/activitypub/importNote.go +++ b/activitypub/importNote.go @@ -1,3 +1,66 @@ package activitypub -func ImportRemoteNote(noteId string) {} +import ( + "encoding/json" + "fmt" + "io" + "time" + + "git.mstar.dev/mstar/goutils/other" + "gorm.io/gorm" + + "git.mstar.dev/mstar/linstrom/storage-new/dbgen" + "git.mstar.dev/mstar/linstrom/storage-new/models" + webshared "git.mstar.dev/mstar/linstrom/web/shared" +) + +func ImportRemoteNote(noteId string, requester *models.User) (string, error) { + type Note struct { + Type string + Id string + Summary *string + Content string + MkContent *string `json:"_misskey_content"` + Published time.Time + To []string + Cc []string + InReplyTo *string + Sensitive bool + AttributedTo string + } + res, _, err := webshared.RequestSigned("GET", noteId, nil, requester) + if err != nil { + return "", other.Error("activitypub", "failed to request object", err) + } + if res.StatusCode != 200 { + return "", fmt.Errorf("activitypub: invalid status code: %v", res.StatusCode) + } + body, _ := io.ReadAll(res.Body) + res.Body.Close() + data := Note{} + err = json.Unmarshal(body, &data) + if err != nil { + return "", other.Error("activitypub", "json unmarshalling failed", err) + } + if data.Type != "Note" { + return "", fmt.Errorf("activitypub: invalid object type: %q", data.Type) + } + // (Re-)Import account this note belongs to + _, err = ImportRemoteAccountByAPUrl(data.AttributedTo) + if err != nil { + return "", other.Error("activitypub", "failed to import note author", err) + } + dbNote, err := dbgen.Note.Where(dbgen.Note.ID.Eq(data.Id)).First() + switch err { + case nil: + case gorm.ErrRecordNotFound: + dbNote = &models.Note{ + ID: data.Id, + CreatedAt: data.Published, + CreatorId: data.AttributedTo, + } + default: + return "", other.Error("activitypub", "failed to check db for note", err) + } + return dbNote.ID, nil +}