Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,4 +1373,11 @@ var migrations = [...]func(tx *sql.Tx) error{
_, err = tx.Exec(sql)
return err
},
func(tx *sql.Tx) (err error) {
sql := `
ALTER TABLE integrations ADD COLUMN linkwarden_collection_id int;
`
_, err = tx.Exec(sql)
return err
},
}
3 changes: 3 additions & 0 deletions internal/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,20 @@ func SendEntry(entry *model.Entry, userIntegrations *model.Integration) {
slog.Int64("user_id", userIntegrations.UserID),
slog.Int64("entry_id", entry.ID),
slog.String("entry_url", entry.URL),
slog.Any("collection_id", userIntegrations.LinkwardenCollectionId),
)

client := linkwarden.NewClient(
userIntegrations.LinkwardenURL,
userIntegrations.LinkwardenAPIKey,
userIntegrations.LinkwardenCollectionId,
)
if err := client.CreateBookmark(entry.URL, entry.Title); err != nil {
slog.Error("Unable to send entry to Linkwarden",
slog.Int64("user_id", userIntegrations.UserID),
slog.Int64("entry_id", entry.ID),
slog.String("entry_url", entry.URL),
slog.Any("collection_id", userIntegrations.LinkwardenCollectionId),
slog.Any("error", err),
)
}
Expand Down
41 changes: 32 additions & 9 deletions internal/integration/linkwarden/linkwarden.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"

Expand All @@ -18,12 +19,23 @@ import (
const defaultClientTimeout = 10 * time.Second

type Client struct {
baseURL string
apiKey string
baseURL string
apiKey string
collectionId *int64
}

func NewClient(baseURL, apiKey string) *Client {
return &Client{baseURL: baseURL, apiKey: apiKey}
type linkwardenCollection struct {
Id *int64 `json:"id"`
}

type linkwardenRequest struct {
URL string `json:"url"`
Name string `json:"name"`
Collection *linkwardenCollection `json:"collection,omitempty"`
}

func NewClient(baseURL, apiKey string, collectionId *int64) *Client {
return &Client{baseURL: baseURL, apiKey: apiKey, collectionId: collectionId}
}

func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
Expand All @@ -36,10 +48,16 @@ func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
return fmt.Errorf(`linkwarden: invalid API endpoint: %v`, err)
}

requestBody, err := json.Marshal(map[string]string{
"url": entryURL,
"name": entryTitle,
})
payload := linkwardenRequest{
URL: entryURL,
Name: entryTitle,
}

if c.collectionId != nil {
payload.Collection = &linkwardenCollection{Id: c.collectionId}
}

requestBody, err := json.Marshal(payload)

if err != nil {
return fmt.Errorf("linkwarden: unable to encode request body: %v", err)
Expand All @@ -61,8 +79,13 @@ func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
}
defer response.Body.Close()

responseBody, err := io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("linkwarden: unable to read response body: %v", err)
}

if response.StatusCode >= 400 {
return fmt.Errorf("linkwarden: unable to create link: url=%s status=%d", apiEndpoint, response.StatusCode)
return fmt.Errorf("linkwarden: unable to create link: status=%d body=%s", response.StatusCode, string(responseBody))
}

return nil
Expand Down
Loading