8000 [pull] main from miniflux:main by pull[bot] · Pull Request #187 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ services:
- ADMIN_USERNAME=admin
- ADMIN_PASSWORD=test123
db:
image: postgres:15
image: postgres:latest
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data
- postgres-data:/var/lib/postgresql
hostname: postgres
environment:
POSTGRES_DB: miniflux2
Expand Down
2 changes: 1 addition & 1 deletion contrib/docker-compose/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=miniflux
volumes:
- miniflux-db:/var/lib/postgresql/data
- miniflux-db:/var/lib/postgresql
healthcheck:
test: ["CMD", "pg_isready", "-U", "miniflux"]
interval: 10s
Expand Down
2 changes: 1 addition & 1 deletion contrib/docker-compose/caddy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ services:
- POSTGRES_USER=miniflux
- POSTGRES_PASSWORD=secret
volumes:
- miniflux-db:/var/lib/postgresql/data
- miniflux-db:/var/lib/postgresql
healthcheck:
test: ["CMD", "pg_isready", "-U", "miniflux"]
interval: 10s
Expand Down
2 changes: 1 addition & 1 deletion contrib/docker-compose/traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ services:
- POSTGRES_USER=miniflux
- POSTGRES_PASSWORD=secret
volumes:
- miniflux-db:/var/lib/postgresql/data
- miniflux-db:/var/lib/postgresql
healthcheck:
test: ["CMD", "pg_isready", "-U", "miniflux"]
interval: 10s
Expand Down
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
},
}
19 changes: 11 additions & 8 deletions internal/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,23 +271,26 @@ func SendEntry(entry *model.Entry, userIntegrations *model.Integration) {
}

if userIntegrations.LinkwardenEnabled {
slog.Debug("Sending entry to linkwarden",
attrs := []any{
slog.Int64("user_id", userIntegrations.UserID),
slog.Int64("entry_id", entry.ID),
slog.String("entry_url", entry.URL),
)
}

if userIntegrations.LinkwardenCollectionID != nil {
attrs = append(attrs, slog.Int64("collection_id", *userIntegrations.LinkwardenCollectionID))
}

slog.Debug("Sending entry to linkwarden", attrs...)

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("error", err),
)
attrs = append(attrs, slog.Any("error", err))
slog.Error("Unable to send entry to Linkwarden", attrs...)
}
}

Expand Down
63 changes: 63 additions & 0 deletions internal/integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package integration

import (
"bytes"
"log/slog"
"strings"
"testing"

"miniflux.app/v2/internal/model"
)

func TestSendEntryLogsLinkwardenCollectionID(t *testing.T) {
var buf bytes.Buffer
handler := slog.NewJSONHandler(&buf, nil)
logger := slog.New(handler)
prev := slog.Default()
slog.SetDefault(logger)
defer slog.SetDefault(prev)

entry := &model.Entry{ID: 52, URL: "https://example.org/test.html", Title: "Test"}
coll := int64(12345)
userIntegrations := &model.Integration{
UserID: 1,
LinkwardenEnabled: true,
LinkwardenCollectionID: &coll,
LinkwardenURL: "",
LinkwardenAPIKey: "",
}

SendEntry(entry, userIntegrations)

out := buf.String()
if !strings.Contains(out, `"collection_id":12345`) {
t.Fatalf("expected collection_id in logs; got: %s", out)
}
}

func TestSendEntryLogsLinkwardenWithoutCollectionID(t *testing.T) {
var buf bytes.Buffer
handler := slog.NewJSONHandler(&buf, nil)
logger := slog.New(handler)
prev := slog.Default()
slog.SetDefault(logger)
defer slog.SetDefault(prev)

entry := &model.Entry{ID: 52, URL: "https://example.org/test.html", Title: "Test"}
userIntegrations := &model.Integration{
UserID: 1,
LinkwardenEnabled: true,
LinkwardenURL: "",
LinkwardenAPIKey: "",
}

SendEntry(entry, userIntegrations)

out := buf.String()
if strings.Contains(out, "collection_id") {
t.Fatalf("did not expect collection_id in logs; got: %s", out)
}
}
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
0