Start work on note importer
This commit is contained in:
parent
66fbf9afbb
commit
5cc6314360
1 changed files with 64 additions and 1 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue