aboutsummaryrefslogtreecommitdiff
path: root/internal/integration/pinboard/pinboard.go
blob: adc2a30f2833a56a047a4fa2650f0efc7d60d0bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package pinboard // import "miniflux.app/v2/internal/integration/pinboard"

import (
	"encoding/xml"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"time"

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

var errPostNotFound = fmt.Errorf("pinboard: post not found")
var errMissingCredentials = fmt.Errorf("pinboard: missing auth token")

const defaultClientTimeout = 10 * time.Second

type Client struct {
	authToken string
}

func NewClient(authToken string) *Client {
	return &Client{authToken: authToken}
}

func (c *Client) CreateBookmark(entryURL, entryTitle, pinboardTags string, markAsUnread bool) error {
	if c.authToken == "" {
		return errMissingCredentials
	}

	// We check if the url is already bookmarked to avoid overriding existing data.
	post, err := c.getBookmark(entryURL)

	if err != nil && errors.Is(err, errPostNotFound) {
		post = NewPost(entryURL, entryTitle)
	} else if err != nil {
		// In case of any other error, we return immediately to avoid overriding existing data.
		return err
	}

	post.addTag(pinboardTags)
	if markAsUnread {
		post.SetToread()
	}

	values := url.Values{}
	values.Add("auth_token", c.authToken)
	post.AddValues(values)

	apiEndpoint := "https://api.pinboard.in/v1/posts/add?" + values.Encode()
	request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
	if err != nil {
		return fmt.Errorf("pinboard: unable to create request: %v", err)
	}

	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	request.Header.Set("User-Agent", "Miniflux/"+version.Version)

	httpClient := &http.Client{Timeout: defaultClientTimeout}
	response, err := httpClient.Do(request)
	if err != nil {
		return fmt.Errorf("pinboard: unable to send request: %v", err)
	}
	defer response.Body.Close()

	if response.StatusCode >= 400 {
		return fmt.Errorf("pinboard: unable to create a bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
	}

	return nil
}

// getBookmark fetches a bookmark from Pinboard. https://www.pinboard.in/api/#posts_get
func (c *Client) getBookmark(entryURL string) (*Post, error) {
	if c.authToken == "" {
		return nil, errMissingCredentials
	}

	values := url.Values{}
	values.Add("auth_token", c.authToken)
	values.Add("url", entryURL)

	apiEndpoint := "https://api.pinboard.in/v1/posts/get?" + values.Encode()
	request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
	if err != nil {
		return nil, fmt.Errorf("pinboard: unable to create request: %v", err)
	}

	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	request.Header.Set("User-Agent", "Miniflux/"+version.Version)

	httpClient := &http.Client{Timeout: defaultClientTimeout}
	response, err := httpClient.Do(request)
	if err != nil {
		return nil, fmt.Errorf("pinboard: unable fetch bookmark: %v", err)
	}
	defer response.Body.Close()

	if response.StatusCode >= 400 {
		return nil, fmt.Errorf("pinboard: unable to fetch bookmark, status=%d", response.StatusCode)
	}

	var results posts
	err = xml.NewDecoder(response.Body).Decode(&results)
	if err != nil {
		return nil, fmt.Errorf("pinboard: unable to decode XML: %v", err)
	}

	if len(results.Posts) == 0 {
		return nil, errPostNotFound
	}

	return &results.Posts[0], nil
}