From 19f3da7f5aeebde1548dcb5dbe0d4502e053c144 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Thu, 31 Oct 2024 18:53:16 -0600 Subject: [PATCH] Adding tags support to pinboard import --- core/import.go | 41 ++++++++++++++++++++++++++++++++++++++++- models/org_link.go | 18 ++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/core/import.go b/core/import.go index 79bf55a..760d428 100644 --- a/core/import.go +++ b/core/import.go @@ -40,6 +40,7 @@ type importObj interface { GetHash() string GetTitle() string GetDescription() string + GetTags() []string IsPublic() bool IsUnread() bool IsNote() bool @@ -105,6 +106,10 @@ func (p pinBoardObj) IsUnread() bool { return p.ToRead == "yes" } +func (p pinBoardObj) GetTags() []string { + return strings.Split(strings.TrimSpace(p.Tags), " ") +} + // Especific html object representation, used by Safari and Chrome type htmlObj struct { Title string @@ -154,6 +159,10 @@ func (h htmlObj) IsUnread() bool { return false } +func (h htmlObj) GetTags() []string { + return []string{} +} + // This adapter struct is used to wrap a slice [n:m] // of pinBoardObj or htmlObj and pass it // as parameter to the importer functions @@ -264,9 +273,13 @@ func processOrgLinks(obj importObj, baseURLMap map[string]int, var linkType int if obj.IsNote() { + // Hash is set in processBaseURLs linkType = models.NoteType } else { linkType = models.OrgLinkType + if obj.GetHash() == "" { + obj.SetHash(ksuid.New().String()) + } } return &models.OrgLink{ @@ -286,6 +299,7 @@ func processOrgLinks(obj importObj, baseURLMap map[string]int, func importOrgLinks(ctx context.Context, objAdapter *importAdapter, baseURLMap map[string]int, org *models.Organization, user *models.User, billEnabled bool) error { orgLinks := make([]*models.OrgLink, 0) + hashTags := make(map[string][]string) switch objAdapter.GetType() { case pinBoardType: @@ -293,6 +307,7 @@ func importOrgLinks(ctx context.Context, objAdapter *importAdapter, baseURLMap m link := processOrgLinks(obj, baseURLMap, org, user, billEnabled) if link != nil { orgLinks = append(orgLinks, link) + hashTags[link.Hash] = obj.GetTags() } } case htmlType: @@ -300,13 +315,37 @@ func importOrgLinks(ctx context.Context, objAdapter *importAdapter, baseURLMap m link := processOrgLinks(obj, baseURLMap, org, user, billEnabled) if link != nil { orgLinks = append(orgLinks, link) + hashTags[link.Hash] = obj.GetTags() } } } if len(orgLinks) == 0 { return nil } - return models.OrgLinkStoreBatch(ctx, orgLinks) + err := models.OrgLinkStoreBatch(ctx, orgLinks) + if err != nil { + return err + } + + // Process tags + for _, link := range orgLinks { + tags, ok := hashTags[link.Hash] + if !ok { + tags = []string{} + } + if len(tags) > 0 { + tagIDs, err := links.ProcessTags(ctx, tags) + if err != nil { + return err + } + err = models.CreateBatchTagLinks(ctx, link.ID, tagIDs) + if err != nil { + return err + } + } + + } + return nil } func ImportFromPinBoard(c echo.Context, src multipart.File, diff --git a/models/org_link.go b/models/org_link.go index ddb737c..0f21cc6 100644 --- a/models/org_link.go +++ b/models/org_link.go @@ -274,10 +274,24 @@ func OrgLinkStoreBatch(ctx context.Context, links []*OrgLink) error { batch = batch.Values(link.Title, link.URL, link.Description, link.BaseURLID, link.OrgID, link.UserID, link.Visibility, link.Hash, link.Type, link.Unread) } - _, err := batch. + rows, err := batch. + Suffix(`RETURNING id`). PlaceholderFormat(sq.Dollar). RunWith(tx). - ExecContext(ctx) + QueryContext(ctx) + if err != nil { + return err + } + + defer rows.Close() + + // Add ID's to new entries + for _, link := range links { + rows.Next() + if err = rows.Scan(&link.ID); err != nil { + return err + } + } return err }) return err -- 2.45.2