Add Notion integration

This commit is contained in:
Jean Khawand 2023-07-08 00:20:14 +02:00 committed by GitHub
parent 06c37a132f
commit bfb4fc1c36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 229 additions and 6 deletions

View file

@ -707,4 +707,13 @@ 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 notion_enabled bool default 'f';
ALTER TABLE integrations ADD COLUMN notion_token text default '';
ALTER TABLE integrations ADD COLUMN notion_page_id text default '';
`
_, err = tx.Exec(sql)
return err
},
}

View file

@ -44,9 +44,9 @@ type Client struct {
requestPassword string
requestUserAgent string
requestCookie string
useProxy bool
doNotFollowRedirects bool
customHeaders map[string]string
useProxy bool
doNotFollowRedirects bool
ClientTimeout int
ClientMaxBodySize int64
@ -111,6 +111,12 @@ func (c *Client) WithAuthorization(authorization string) *Client {
return c
}
// WithCustomHeaders defines custom HTTP headers.
func (c *Client) WithCustomHeaders(customHeaders map[string]string) *Client {
c.customHeaders = customHeaders
return c
}
// WithCacheHeaders defines caching headers.
func (c *Client) WithCacheHeaders(etagHeader, lastModifiedHeader string) *Client {
c.requestEtagHeader = etagHeader
@ -183,6 +189,22 @@ func (c *Client) PostJSON(data interface{}) (*Response, error) {
return c.executeRequest(request)
}
// PatchJSON performs a Patch HTTP request with a JSON payload.
func (c *Client) PatchJSON(data interface{}) (*Response, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
request, err := c.buildRequest(http.MethodPatch, bytes.NewReader(b))
if err != nil {
return nil, err
}
request.Header.Add("Content-Type", "application/json")
return c.executeRequest(request)
}
func (c *Client) executeRequest(request *http.Request) (*Response, error) {
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[HttpClient] inputURL=%s", c.inputURL))
@ -337,6 +359,10 @@ func (c *Client) buildHeaders() http.Header {
headers.Add("Cookie", c.requestCookie)
}
for key, value := range c.customHeaders {
headers.Add(key, value)
}
headers.Add("Connection", "close")
return headers
}

View file

@ -9,6 +9,7 @@ import (
"miniflux.app/integration/instapaper"
"miniflux.app/integration/linkding"
"miniflux.app/integration/matrixbot"
"miniflux.app/integration/notion"
"miniflux.app/integration/nunuxkeeper"
"miniflux.app/integration/pinboard"
"miniflux.app/integration/pocket"
@ -62,6 +63,18 @@ func SendEntry(entry *model.Entry, integration *model.Integration) {
}
}
if integration.NotionEnabled {
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Notion", entry.ID, entry.URL, integration.UserID)
client := notion.NewClient(
integration.NotionToken,
integration.NotionPageID,
)
if err := client.AddEntry(entry.URL, entry.Title); err != nil {
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
}
}
if integration.NunuxKeeperEnabled {
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to NunuxKeeper", entry.ID, entry.URL, integration.UserID)

View file

@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package notion
import (
"fmt"
"miniflux.app/http/client"
)
// Client represents a Notion client.
type Client struct {
token string
pageID string
}
// NewClient returns a new Notion client.
func NewClient(token, pageID string) *Client {
return &Client{token, pageID}
}
func (c *Client) AddEntry(entryURL string, entryTitle string) error {
if c.token == "" || c.pageID == "" {
return fmt.Errorf("notion: missing credentials")
}
clt := client.New("https://api.notion.com/v1/blocks/" + c.pageID + "/children")
block := &Data{
Children: []Block{
{
Object: "block",
Type: "bookmark",
Bookmark: Bookmark{
Caption: []interface{}{},
URL: entryURL,
},
},
},
}
clt.WithAuthorization("Bearer " + c.token)
customHeaders := map[string]string{
"Notion-Version": "2022-06-28",
}
clt.WithCustomHeaders(customHeaders)
response, error := clt.PatchJSON(block)
if error != nil {
return fmt.Errorf("notion: unable to patch entry: %v", error)
}
if response.HasServerFailure() {
return fmt.Errorf("notion: request failed, status=%d", response.StatusCode)
}
return nil
}

View file

@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package notion
type Data struct {
Children []Block `json:"children"`
}
type Block struct {
Object string `json:"object"`
Type string `json:"type"`
Bookmark Bookmark `json:"bookmark"`
}
type Bookmark struct {
Caption []interface{} `json:"caption"` // Assuming the "caption" field can have different types
URL string `json:"url"`
}

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Wallabag Client-Secret",
"form.integration.wallabag_username": "Wallabag Benutzername",
"form.integration.wallabag_password": "Wallabag Passwort",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Artikel in Nunux Keeper speichern",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper API-Endpunkt",
"form.integration.nunux_keeper_api_key": "Nunux Keeper API-Schlüssel",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Wallabag Μυστικό Πελάτη",
"form.integration.wallabag_username": "Όνομα Χρήστη Wallabag",
"form.integration.wallabag_password": "Wallabag Κωδικός Πρόσβασης",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Αποθήκευση άρθρων στο Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Τελικό σημείο Nunux Keeper API",
"form.integration.nunux_keeper_api_key": "Κλειδί API Nunux Keeper",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Wallabag Client Secret",
"form.integration.wallabag_username": "Wallabag Username",
"form.integration.wallabag_password": "Wallabag Password",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Save entries to Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper API Endpoint",
"form.integration.nunux_keeper_api_key": "Nunux Keeper API key",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Secreto de cliente de Wallabag",
"form.integration.wallabag_username": "Nombre de usuario de Wallabag",
"form.integration.wallabag_password": "Contraseña de Wallabag",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Enviar artículos a Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Acceso API de Nunux Keeper",
"form.integration.nunux_keeper_api_key": "Clave de API de Nunux Keeper",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Wallabag Client Secret",
"form.integration.wallabag_username": "Wallabag-käyttäjätunnus",
"form.integration.wallabag_password": "Wallabag-salasana",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Tallenna artikkelit Nunux Keeperiin",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper API-päätepiste",
"form.integration.nunux_keeper_api_key": "Nunux Keeper API-avain",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Clé secrète du client Wallabag",
"form.integration.wallabag_username": "Nom d'utilisateur de Wallabag",
"form.integration.wallabag_password": "Mot de passe de Wallabag",
"form.integration.notion_activate": "Sauvegarder les articles vers Notion",
"form.integration.notion_page_id": "l'identifiant de la page Notion",
"form.integration.notion_token": "Jeton d'accès de l'API de Notion",
"form.integration.nunux_keeper_activate": "Sauvegarder les articles vers Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "URL de l'API de Nunux Keeper",
"form.integration.nunux_keeper_api_key": "Clé d'API de Nunux Keeper",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "वालाबैग क्लाइंट सीक्रेट",
"form.integration.wallabag_username": "वालाबैग उपयोगकर्ता नाम",
"form.integration.wallabag_password": "वालाबैग पासवर्ड",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "विषय-वस्तु को ननक्स कीपर में सहेजें",
"form.integration.nunux_keeper_endpoint": "ननक्स कीपर एपीआई समापन बिंदु",
"form.integration.nunux_keeper_api_key": "ननक्स कीपर एपीआई कुंजी",

View file

@ -348,6 +348,9 @@
"form.integration.wallabag_client_secret": "Rahasia Klien Wallabag",
"form.integration.wallabag_username": "Nama Pengguna Wallabag",
"form.integration.wallabag_password": "Kata Sandi Wallabag",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Simpan artikel ke Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Titik URL API Nunux Keeper",
"form.integration.nunux_keeper_api_key": "Kunci API Nunux Keeper",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Client secret dell'account Wallabag",
"form.integration.wallabag_username": "Nome utente dell'account Wallabag",
"form.integration.wallabag_password": "Password dell'account Wallabag",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Salva gli articoli su Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Endpoint dell'API di Nunux Keeper",
"form.integration.nunux_keeper_api_key": "API key dell'account Nunux Keeper",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Wallabag の Client Secret",
"form.integration.wallabag_username": "Wallabag のユーザー名",
"form.integration.wallabag_password": "Wallabag のパスワード",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Nunux Keeper に記事を保存する",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper の API Endpoint",
"form.integration.nunux_keeper_api_key": "Nunux Keeper の API key",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Wallabag Client-Secret",
"form.integration.wallabag_username": "Wallabag gebruikersnaam",
"form.integration.wallabag_password": "Wallabag wachtwoord",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Opslaan naar Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper URL",
"form.integration.nunux_keeper_api_key": "Nunux Keeper API-sleutel",

View file

@ -353,6 +353,9 @@
"form.integration.wallabag_client_secret": "Wallabag Client Secret",
"form.integration.wallabag_username": "Login do Wallabag",
"form.integration.wallabag_password": "Hasło do Wallabag",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Zapisz artykuly do Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper URL",
"form.integration.nunux_keeper_api_key": "Nunux Keeper API key",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Segredo do cliente (Client Secret) do Wallabag",
"form.integration.wallabag_username": "Nome de usuário do Wallabag",
"form.integration.wallabag_password": "Senha do Wallabag",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Salvar itens no Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Endpoint de API do Nunux Keeper",
"form.integration.nunux_keeper_api_key": "Chave de API do Nunux Keeper",

View file

@ -353,6 +353,9 @@
"form.integration.wallabag_client_secret": "Wallabag Client Secret",
"form.integration.wallabag_username": "Имя пользователя Wallabag",
"form.integration.wallabag_password": "Пароль Wallabag",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Сохранять статьи в Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Конечная точка Nunux Keeper API",
"form.integration.nunux_keeper_api_key": "API-ключ Nunux Keeper",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Wallabag Client Secret",
"form.integration.wallabag_username": "Wallabag Kullanıcı Adı",
"form.integration.wallabag_password": "Wallabag Parolası",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Makaleleri Nunux Keeper'a kaydet",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper API Uç Noktası",
"form.integration.nunux_keeper_api_key": "Nunux Keeper API anahtarı",

View file

@ -350,6 +350,9 @@
"form.integration.wallabag_client_secret": "Wallabag Client Secret",
"form.integration.wallabag_username": "Ім’я користувача Wallabag",
"form.integration.wallabag_password": "Пароль Wallabag",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "Зберігати статті до Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper API Endpoint",
"form.integration.nunux_keeper_api_key": "Ключ API Nunux Keeper",

View file

@ -349,6 +349,9 @@
"form.integration.wallabag_client_secret": "Wallabag 客户端 Secret",
"form.integration.wallabag_username": "Wallabag 用户名",
"form.integration.wallabag_password": "Wallabag 密码",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "保存文章到 Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper API 端点",
"form.integration.nunux_keeper_api_key": "Nunux Keeper API 密钥",

View file

@ -351,6 +351,9 @@
"form.integration.wallabag_client_secret": "Wallabag 客戶端 Secret",
"form.integration.wallabag_username": "Wallabag 使用者名稱",
"form.integration.wallabag_password": "Wallabag 密碼",
"form.integration.notion_activate": "Save entries to Notion",
"form.integration.notion_page_id": "Notion Page ID",
"form.integration.notion_token": "Notion Secret Token",
"form.integration.nunux_keeper_activate": "儲存文章到 Nunux Keeper",
"form.integration.nunux_keeper_endpoint": "Nunux Keeper API 端點",
"form.integration.nunux_keeper_api_key": "Nunux Keeper API 金鑰",

View file

@ -29,6 +29,9 @@ type Integration struct {
NunuxKeeperEnabled bool
NunuxKeeperURL string
NunuxKeeperAPIKey string
NotionEnabled bool
NotionToken string
NotionPageID string
EspialEnabled bool
EspialURL string
EspialAPIKey string

View file

@ -130,6 +130,9 @@ func (s *Storage) Integration(userID int64) (*model.Integration, error) {
wallabag_client_secret,
wallabag_username,
wallabag_password,
notion_enabled,
notion_token,
notion_page_id,
nunux_keeper_enabled,
nunux_keeper_url,
nunux_keeper_api_key,
@ -181,6 +184,9 @@ func (s *Storage) Integration(userID int64) (*model.Integration, error) {
&integration.WallabagClientSecret,
&integration.WallabagUsername,
&integration.WallabagPassword,
&integration.NotionEnabled,
&integration.NotionToken,
&integration.NotionPageID,
&integration.NunuxKeeperEnabled,
&integration.NunuxKeeperURL,
&integration.NunuxKeeperAPIKey,
@ -269,7 +275,10 @@ func (s *Storage) UpdateIntegration(integration *model.Integration) error {
matrix_bot_user=$40,
matrix_bot_password=$41,
matrix_bot_url=$42,
matrix_bot_chat_id=$43
matrix_bot_chat_id=$43,
notion_enabled=$45,
notion_token=$46,
notion_page_id=$47
WHERE
user_id=$44
`
@ -318,6 +327,9 @@ func (s *Storage) UpdateIntegration(integration *model.Integration) error {
integration.MatrixBotPassword,
integration.MatrixBotURL,
integration.MatrixBotChatID,
integration.NotionEnabled,
integration.NotionToken,
integration.NotionPageID,
integration.UserID,
)
} else {
@ -367,7 +379,10 @@ func (s *Storage) UpdateIntegration(integration *model.Integration) error {
matrix_bot_user=$40,
matrix_bot_password=$41,
matrix_bot_url=$42,
matrix_bot_chat_id=$43
matrix_bot_chat_id=$43,
notion_enabled=$45,
notion_token=$46,
notion_page_id=$47
WHERE
user_id=$44
`
@ -417,6 +432,9 @@ func (s *Storage) UpdateIntegration(integration *model.Integration) error {
integration.MatrixBotURL,
integration.MatrixBotChatID,
integration.UserID,
integration.NotionEnabled,
integration.NotionToken,
integration.NotionPageID,
)
}
@ -437,7 +455,7 @@ func (s *Storage) HasSaveEntry(userID int64) (result bool) {
WHERE
user_id=$1
AND
(pinboard_enabled='t' OR instapaper_enabled='t' OR wallabag_enabled='t' OR nunux_keeper_enabled='t' OR espial_enabled='t' OR pocket_enabled='t' OR linkding_enabled='t')
(pinboard_enabled='t' OR instapaper_enabled='t' OR wallabag_enabled='t' OR notion_enabled='t' OR nunux_keeper_enabled='t' OR espial_enabled='t' OR pocket_enabled='t' OR linkding_enabled='t')
`
if err := s.db.QueryRow(query, userID).Scan(&result); err != nil {
result = false

View file

@ -142,7 +142,22 @@
<button type="submit" class="button button-primary" data-label-loading="{{ t "form.submit.saving" }}">{{ t "action.update" }}</button>
</div>
</div>
<h3>Notion</h3>
<div class="form-section">
<label>
<input type="checkbox" name="notion_enabled" value="1" {{ if .form.NotionEnabled }}checked{{ end }}> {{ t "form.integration.notion_activate" }}
</label>
<label for="form-notion-token">{{ t "form.integration.notion_token" }}</label>
<input type="password" name="notion_token" id="form-notion-token" value="{{ .form.NotionToken }}" spellcheck="false">
<label for="form-notion-page-id">{{ t "form.integration.notion_page_id" }}</label>
<input type="text" name="notion_page_id" id="form-notion-page-id" value="{{ .form.NotionPageID }}" spellcheck="false">
<div class="buttons">
<button type="submit" class="button button-primary" data-label-loading="{{ t "form.submit.saving" }}">{{ t "action.update" }}</button>
</div>
</div>
<h3>Nunux Keeper</h3>
<div class="form-section">
<label>

View file

@ -31,6 +31,9 @@ type IntegrationForm struct {
WallabagClientSecret string
WallabagUsername string
WallabagPassword string
NotionEnabled bool
NotionPageID string
NotionToken string
NunuxKeeperEnabled bool
NunuxKeeperURL string
NunuxKeeperAPIKey string
@ -76,6 +79,9 @@ func (i IntegrationForm) Merge(integration *model.Integration) {
integration.WallabagClientSecret = i.WallabagClientSecret
integration.WallabagUsername = i.WallabagUsername
integration.WallabagPassword = i.WallabagPassword
integration.NotionEnabled = i.NotionEnabled
integration.NotionPageID = i.NotionPageID
integration.NotionToken = i.NotionToken
integration.NunuxKeeperEnabled = i.NunuxKeeperEnabled
integration.NunuxKeeperURL = i.NunuxKeeperURL
integration.NunuxKeeperAPIKey = i.NunuxKeeperAPIKey
@ -124,6 +130,9 @@ func NewIntegrationForm(r *http.Request) *IntegrationForm {
WallabagClientSecret: r.FormValue("wallabag_client_secret"),
WallabagUsername: r.FormValue("wallabag_username"),
WallabagPassword: r.FormValue("wallabag_password"),
NotionEnabled: r.FormValue("notion_enabled") == "1",
NotionPageID: r.FormValue("notion_page_id"),
NotionToken: r.FormValue("notion_token"),
NunuxKeeperEnabled: r.FormValue("nunux_keeper_enabled") == "1",
NunuxKeeperURL: r.FormValue("nunux_keeper_url"),
NunuxKeeperAPIKey: r.FormValue("nunux_keeper_api_key"),

View file

@ -46,6 +46,9 @@ func (h *handler) showIntegrationPage(w http.ResponseWriter, r *http.Request) {
WallabagClientSecret: integration.WallabagClientSecret,
WallabagUsername: integration.WallabagUsername,
WallabagPassword: integration.WallabagPassword,
NotionEnabled: integration.NotionEnabled,
NotionPageID: integration.NotionPageID,
NotionToken: integration.NotionToken,
NunuxKeeperEnabled: integration.NunuxKeeperEnabled,
NunuxKeeperURL: integration.NunuxKeeperURL,
NunuxKeeperAPIKey: integration.NunuxKeeperAPIKey,