1
0
Fork 0
miniflux/storage/integration.go

211 lines
5.3 KiB
Go
Raw Normal View History

2017-12-02 22:32:14 -05:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 00:51:50 -04:00
package storage // import "miniflux.app/storage"
2017-12-02 22:32:14 -05:00
import (
"database/sql"
"fmt"
2018-08-25 00:51:50 -04:00
"miniflux.app/model"
2017-12-02 22:32:14 -05:00
)
// HasDuplicateFeverUsername checks if another user have the same fever username.
func (s *Storage) HasDuplicateFeverUsername(userID int64, feverUsername string) bool {
2019-10-30 01:48:07 -04:00
query := `SELECT true FROM integrations WHERE user_id != $1 AND fever_username=$2`
var result bool
s.db.QueryRow(query, userID, feverUsername).Scan(&result)
2019-10-30 01:48:07 -04:00
return result
}
2017-12-03 20:44:27 -05:00
// UserByFeverToken returns a user by using the Fever API token.
func (s *Storage) UserByFeverToken(token string) (*model.User, error) {
query := `
SELECT
users.id, users.is_admin, users.timezone
2017-12-03 20:44:27 -05:00
FROM users
LEFT JOIN integrations ON integrations.user_id=users.id
2019-10-30 01:48:07 -04:00
WHERE
integrations.fever_enabled='t' AND lower(integrations.fever_token)=lower($1)
2017-12-03 20:44:27 -05:00
`
var user model.User
err := s.db.QueryRow(query, token).Scan(&user.ID, &user.IsAdmin, &user.Timezone)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, fmt.Errorf("unable to fetch user: %v", err)
2019-10-30 01:48:07 -04:00
default:
return &user, nil
2017-12-03 20:44:27 -05:00
}
}
2017-12-02 22:32:14 -05:00
// Integration returns user integration settings.
func (s *Storage) Integration(userID int64) (*model.Integration, error) {
2019-10-30 01:48:07 -04:00
query := `
SELECT
2017-12-02 22:32:14 -05:00
user_id,
pinboard_enabled,
pinboard_token,
pinboard_tags,
2017-12-03 00:12:03 -05:00
pinboard_mark_as_unread,
instapaper_enabled,
instapaper_username,
2017-12-03 20:44:27 -05:00
instapaper_password,
fever_enabled,
fever_username,
fever_password,
2017-12-18 23:52:46 -05:00
fever_token,
wallabag_enabled,
wallabag_url,
wallabag_client_id,
wallabag_client_secret,
wallabag_username,
2018-02-25 14:49:08 -05:00
wallabag_password,
nunux_keeper_enabled,
nunux_keeper_url,
2018-05-20 16:31:56 -04:00
nunux_keeper_api_key,
pocket_enabled,
pocket_access_token,
pocket_consumer_key
2019-10-30 01:48:07 -04:00
FROM
integrations
WHERE
user_id=$1
2017-12-02 22:32:14 -05:00
`
var integration model.Integration
err := s.db.QueryRow(query, userID).Scan(
&integration.UserID,
&integration.PinboardEnabled,
&integration.PinboardToken,
&integration.PinboardTags,
&integration.PinboardMarkAsUnread,
2017-12-03 00:12:03 -05:00
&integration.InstapaperEnabled,
&integration.InstapaperUsername,
&integration.InstapaperPassword,
2017-12-03 20:44:27 -05:00
&integration.FeverEnabled,
&integration.FeverUsername,
&integration.FeverPassword,
&integration.FeverToken,
2017-12-18 23:52:46 -05:00
&integration.WallabagEnabled,
&integration.WallabagURL,
&integration.WallabagClientID,
&integration.WallabagClientSecret,
&integration.WallabagUsername,
&integration.WallabagPassword,
2018-02-25 14:49:08 -05:00
&integration.NunuxKeeperEnabled,
&integration.NunuxKeeperURL,
&integration.NunuxKeeperAPIKey,
2018-05-20 16:31:56 -04:00
&integration.PocketEnabled,
&integration.PocketAccessToken,
&integration.PocketConsumerKey,
2017-12-02 22:32:14 -05:00
)
switch {
case err == sql.ErrNoRows:
2017-12-15 21:55:57 -05:00
return &integration, nil
2017-12-02 22:32:14 -05:00
case err != nil:
2019-10-30 01:48:07 -04:00
return &integration, fmt.Errorf(`store: unable to fetch integration row: %v`, err)
default:
return &integration, nil
2017-12-02 22:32:14 -05:00
}
}
// UpdateIntegration saves user integration settings.
func (s *Storage) UpdateIntegration(integration *model.Integration) error {
query := `
2019-10-30 01:48:07 -04:00
UPDATE
integrations
SET
2017-12-02 22:32:14 -05:00
pinboard_enabled=$1,
pinboard_token=$2,
pinboard_tags=$3,
2017-12-03 00:12:03 -05:00
pinboard_mark_as_unread=$4,
instapaper_enabled=$5,
instapaper_username=$6,
2017-12-03 20:44:27 -05:00
instapaper_password=$7,
fever_enabled=$8,
fever_username=$9,
fever_password=$10,
2017-12-18 23:52:46 -05:00
fever_token=$11,
wallabag_enabled=$12,
wallabag_url=$13,
wallabag_client_id=$14,
wallabag_client_secret=$15,
wallabag_username=$16,
2018-02-25 14:49:08 -05:00
wallabag_password=$17,
nunux_keeper_enabled=$18,
nunux_keeper_url=$19,
2018-05-20 16:31:56 -04:00
nunux_keeper_api_key=$20,
pocket_enabled=$21,
pocket_access_token=$22,
pocket_consumer_key=$23
2019-10-30 01:48:07 -04:00
WHERE
user_id=$24
2017-12-02 22:32:14 -05:00
`
_, err := s.db.Exec(
query,
integration.PinboardEnabled,
integration.PinboardToken,
integration.PinboardTags,
integration.PinboardMarkAsUnread,
2017-12-03 00:12:03 -05:00
integration.InstapaperEnabled,
integration.InstapaperUsername,
integration.InstapaperPassword,
2017-12-03 20:44:27 -05:00
integration.FeverEnabled,
integration.FeverUsername,
integration.FeverPassword,
integration.FeverToken,
2017-12-18 23:52:46 -05:00
integration.WallabagEnabled,
integration.WallabagURL,
integration.WallabagClientID,
integration.WallabagClientSecret,
integration.WallabagUsername,
integration.WallabagPassword,
2018-02-25 14:49:08 -05:00
integration.NunuxKeeperEnabled,
integration.NunuxKeeperURL,
integration.NunuxKeeperAPIKey,
2018-05-20 16:31:56 -04:00
integration.PocketEnabled,
integration.PocketAccessToken,
integration.PocketConsumerKey,
2017-12-02 22:32:14 -05:00
integration.UserID,
)
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to update integration row: %v`, err)
2017-12-02 22:32:14 -05:00
}
return nil
}
// CreateIntegration creates initial user integration settings.
func (s *Storage) CreateIntegration(userID int64) error {
query := `INSERT INTO integrations (user_id) VALUES ($1)`
_, err := s.db.Exec(query, userID)
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to create integration row: %v`, err)
2017-12-02 22:32:14 -05:00
}
return nil
}
// HasSaveEntry returns true if the given user can save articles to third-parties.
func (s *Storage) HasSaveEntry(userID int64) (result bool) {
query := `
2019-10-30 01:48:07 -04:00
SELECT
true
FROM
integrations
WHERE
user_id=$1
AND
(pinboard_enabled='t' OR instapaper_enabled='t' OR wallabag_enabled='t' OR nunux_keeper_enabled='t' OR pocket_enabled='t')
`
if err := s.db.QueryRow(query, userID).Scan(&result); err != nil {
result = false
}
return result
}