1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Make sure golint pass on the code base

This commit is contained in:
Frédéric Guillot 2017-11-27 21:30:04 -08:00
parent 8781648af9
commit bb8e61c7c5
59 changed files with 322 additions and 171 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/miniflux/miniflux2/model"
)
// AnotherCategoryExists checks if another category exists with the same title.
func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:AnotherCategoryExists] userID=%d, categoryID=%d, title=%s", userID, categoryID, title))
@ -23,6 +24,7 @@ func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string)
return result >= 1
}
// CategoryExists checks if the given category exists into the database.
func (s *Storage) CategoryExists(userID, categoryID int64) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CategoryExists] userID=%d, categoryID=%d", userID, categoryID))
@ -32,8 +34,9 @@ func (s *Storage) CategoryExists(userID, categoryID int64) bool {
return result >= 1
}
func (s *Storage) GetCategory(userID, categoryID int64) (*model.Category, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetCategory] userID=%d, getCategory=%d", userID, categoryID))
// Category returns a category from the database.
func (s *Storage) Category(userID, categoryID int64) (*model.Category, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:Category] userID=%d, getCategory=%d", userID, categoryID))
var category model.Category
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 AND id=$2`
@ -41,29 +44,31 @@ func (s *Storage) GetCategory(userID, categoryID int64) (*model.Category, error)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("Unable to fetch category: %v", err)
return nil, fmt.Errorf("unable to fetch category: %v", err)
}
return &category, nil
}
func (s *Storage) GetFirstCategory(userID int64) (*model.Category, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetFirstCategory] userID=%d", userID))
// FirstCategory returns the first category for the given user.
func (s *Storage) FirstCategory(userID int64) (*model.Category, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FirstCategory] userID=%d", userID))
var category model.Category
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC`
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("Unable to fetch category: %v", err)
return nil, fmt.Errorf("unable to fetch category: %v", err)
}
return &category, nil
}
func (s *Storage) GetCategoryByTitle(userID int64, title string) (*model.Category, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetCategoryByTitle] userID=%d, title=%s", userID, title))
// CategoryByTitle finds a category by the title.
func (s *Storage) CategoryByTitle(userID int64, title string) (*model.Category, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CategoryByTitle] userID=%d, title=%s", userID, title))
var category model.Category
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 AND title=$2`
@ -77,10 +82,11 @@ func (s *Storage) GetCategoryByTitle(userID int64, title string) (*model.Categor
return &category, nil
}
func (s *Storage) GetCategories(userID int64) (model.Categories, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetCategories] userID=%d", userID))
// Categories returns all categories that belongs to the given user.
func (s *Storage) Categories(userID int64) (model.Categories, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:Categories] userID=%d", userID))
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1`
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC`
rows, err := s.db.Query(query, userID)
if err != nil {
return nil, fmt.Errorf("Unable to fetch categories: %v", err)
@ -100,8 +106,9 @@ func (s *Storage) GetCategories(userID int64) (model.Categories, error) {
return categories, nil
}
func (s *Storage) GetCategoriesWithFeedCount(userID int64) (model.Categories, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetCategoriesWithFeedCount] userID=%d", userID))
// CategoriesWithFeedCount returns all categories with the number of feeds.
func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CategoriesWithFeedCount] userID=%d", userID))
query := `SELECT
c.id, c.user_id, c.title,
(SELECT count(*) FROM feeds WHERE feeds.category_id=c.id) AS count
@ -126,6 +133,7 @@ func (s *Storage) GetCategoriesWithFeedCount(userID int64) (model.Categories, er
return categories, nil
}
// CreateCategory creates a new category.
func (s *Storage) CreateCategory(category *model.Category) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CreateCategory] title=%s", category.Title))
@ -149,6 +157,7 @@ func (s *Storage) CreateCategory(category *model.Category) error {
return nil
}
// UpdateCategory updates an existing category.
func (s *Storage) UpdateCategory(category *model.Category) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UpdateCategory] categoryID=%d", category.ID))
@ -167,6 +176,7 @@ func (s *Storage) UpdateCategory(category *model.Category) error {
return nil
}
// RemoveCategory deletes a category.
func (s *Storage) RemoveCategory(userID, categoryID int64) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:RemoveCategory] userID=%d, categoryID=%d", userID, categoryID))

View file

@ -14,6 +14,7 @@ import (
"github.com/miniflux/miniflux2/model"
)
// FeedExists checks if the given feed exists.
func (s *Storage) FeedExists(userID, feedID int64) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FeedExists] userID=%d, feedID=%d", userID, feedID))
@ -23,6 +24,7 @@ func (s *Storage) FeedExists(userID, feedID int64) bool {
return result >= 1
}
// FeedURLExists checks if feed URL already exists.
func (s *Storage) FeedURLExists(userID int64, feedURL string) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FeedURLExists] userID=%d, feedURL=%s", userID, feedURL))
@ -43,8 +45,9 @@ func (s *Storage) CountFeeds(userID int64) int {
return result
}
func (s *Storage) GetFeeds(userID int64) (model.Feeds, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetFeeds] userID=%d", userID))
// Feeds returns all feeds of the given user.
func (s *Storage) Feeds(userID int64) (model.Feeds, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:Feeds] userID=%d", userID))
feeds := make(model.Feeds, 0)
query := `SELECT
@ -109,8 +112,9 @@ func (s *Storage) GetFeeds(userID int64) (model.Feeds, error) {
return feeds, nil
}
func (s *Storage) GetFeedById(userID, feedID int64) (*model.Feed, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetFeedById] feedID=%d", feedID))
// FeedByID returns a feed by the ID.
func (s *Storage) FeedByID(userID, feedID int64) (*model.Feed, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FeedByID] feedID=%d", feedID))
var feed model.Feed
feed.Category = &model.Category{UserID: userID}
@ -149,6 +153,7 @@ func (s *Storage) GetFeedById(userID, feedID int64) (*model.Feed, error) {
return &feed, nil
}
// CreateFeed creates a new feed.
func (s *Storage) CreateFeed(feed *model.Feed) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CreateFeed] feedURL=%s", feed.FeedURL))
sql := `
@ -184,6 +189,7 @@ func (s *Storage) CreateFeed(feed *model.Feed) error {
return nil
}
// UpdateFeed updates an existing feed.
func (s *Storage) UpdateFeed(feed *model.Feed) (err error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UpdateFeed] feedURL=%s", feed.FeedURL))
@ -213,6 +219,7 @@ func (s *Storage) UpdateFeed(feed *model.Feed) (err error) {
return nil
}
// RemoveFeed removes a feed.
func (s *Storage) RemoveFeed(userID, feedID int64) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:RemoveFeed] userID=%d, feedID=%d", userID, feedID))

View file

@ -14,6 +14,7 @@ import (
"github.com/miniflux/miniflux2/model"
)
// HasIcon checks if the given feed has an icon.
func (s *Storage) HasIcon(feedID int64) bool {
var result int
query := `SELECT count(*) as c FROM feed_icons WHERE feed_id=$1`
@ -21,8 +22,9 @@ func (s *Storage) HasIcon(feedID int64) bool {
return result == 1
}
func (s *Storage) GetIconByID(iconID int64) (*model.Icon, error) {
defer helper.ExecutionTime(time.Now(), "[Storage:GetIconByID]")
// IconByID returns an icon by the ID.
func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
defer helper.ExecutionTime(time.Now(), "[Storage:IconByID]")
var icon model.Icon
query := `SELECT id, hash, mime_type, content FROM icons WHERE id=$1`
@ -36,8 +38,9 @@ func (s *Storage) GetIconByID(iconID int64) (*model.Icon, error) {
return &icon, nil
}
func (s *Storage) GetIconByHash(icon *model.Icon) error {
defer helper.ExecutionTime(time.Now(), "[Storage:GetIconByHash]")
// IconByHash returns an icon by the hash (checksum).
func (s *Storage) IconByHash(icon *model.Icon) error {
defer helper.ExecutionTime(time.Now(), "[Storage:IconByHash]")
err := s.db.QueryRow(`SELECT id FROM icons WHERE hash=$1`, icon.Hash).Scan(&icon.ID)
if err == sql.ErrNoRows {
@ -49,6 +52,7 @@ func (s *Storage) GetIconByHash(icon *model.Icon) error {
return nil
}
// CreateIcon creates a new icon.
func (s *Storage) CreateIcon(icon *model.Icon) error {
defer helper.ExecutionTime(time.Now(), "[Storage:CreateIcon]")
@ -73,10 +77,11 @@ func (s *Storage) CreateIcon(icon *model.Icon) error {
return nil
}
// CreateFeedIcon creates an icon and associate the icon to the given feed.
func (s *Storage) CreateFeedIcon(feed *model.Feed, icon *model.Icon) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CreateFeedIcon] feedID=%d", feed.ID))
err := s.GetIconByHash(icon)
err := s.IconByHash(icon)
if err != nil {
return err
}

View file

@ -7,11 +7,13 @@ package storage
import (
"database/sql"
"fmt"
"github.com/miniflux/miniflux2/helper"
"github.com/miniflux/miniflux2/model"
)
func (s *Storage) GetSessions(userID int64) (model.Sessions, error) {
// Sessions returns the list of sessions for the given user.
func (s *Storage) Sessions(userID int64) (model.Sessions, error) {
query := `SELECT id, user_id, token, created_at, user_agent, ip FROM sessions WHERE user_id=$1 ORDER BY id DESC`
rows, err := s.db.Query(query, userID)
if err != nil {
@ -41,6 +43,7 @@ func (s *Storage) GetSessions(userID int64) (model.Sessions, error) {
return sessions, nil
}
// CreateSession creates a new sessions.
func (s *Storage) CreateSession(username, userAgent, ip string) (sessionID string, err error) {
var userID int64
@ -61,7 +64,8 @@ func (s *Storage) CreateSession(username, userAgent, ip string) (sessionID strin
return token, nil
}
func (s *Storage) GetSessionByToken(token string) (*model.Session, error) {
// SessionByToken finds a session by the token.
func (s *Storage) SessionByToken(token string) (*model.Session, error) {
var session model.Session
query := "SELECT id, user_id, token, created_at, user_agent, ip FROM sessions WHERE token = $1"
@ -83,6 +87,7 @@ func (s *Storage) GetSessionByToken(token string) (*model.Session, error) {
return &session, nil
}
// RemoveSessionByToken remove a session by using the token.
func (s *Storage) RemoveSessionByToken(userID int64, token string) error {
result, err := s.db.Exec(`DELETE FROM sessions WHERE user_id=$1 AND token=$2`, userID, token)
if err != nil {
@ -101,6 +106,7 @@ func (s *Storage) RemoveSessionByToken(userID int64, token string) error {
return nil
}
// RemoveSessionByID remove a session by using the ID.
func (s *Storage) RemoveSessionByID(userID, sessionID int64) error {
result, err := s.db.Exec(`DELETE FROM sessions WHERE user_id=$1 AND id=$2`, userID, sessionID)
if err != nil {
@ -119,7 +125,8 @@ func (s *Storage) RemoveSessionByID(userID, sessionID int64) error {
return nil
}
// FlushAllSessions removes all sessions from the database.
func (s *Storage) FlushAllSessions() (err error) {
_, err = s.db.Exec(`delete from sessions`)
_, err = s.db.Exec(`DELETE FROM sessions`)
return
}

View file

@ -8,19 +8,23 @@ import (
"database/sql"
"log"
// Postgresql driver import
_ "github.com/lib/pq"
)
// Storage handles all operations related to the database.
type Storage struct {
db *sql.DB
}
// Close closes all database connections.
func (s *Storage) Close() {
s.db.Close()
}
func NewStorage(databaseUrl string, maxOpenConns int) *Storage {
db, err := sql.Open("postgres", databaseUrl)
// NewStorage returns a new Storage.
func NewStorage(databaseURL string, maxOpenConns int) *Storage {
db, err := sql.Open("postgres", databaseURL)
if err != nil {
log.Fatalf("Unable to connect to the database: %v", err)
}

View file

@ -6,12 +6,14 @@ package storage
import (
"fmt"
"github.com/miniflux/miniflux2/helper"
"time"
"github.com/miniflux/miniflux2/helper"
)
func (s *Storage) GetTimezones() (map[string]string, error) {
defer helper.ExecutionTime(time.Now(), "[Storage:GetTimezones]")
// Timezones returns all timezones supported by the database.
func (s *Storage) Timezones() (map[string]string, error) {
defer helper.ExecutionTime(time.Now(), "[Storage:Timezones]")
timezones := make(map[string]string)
query := `select name from pg_timezone_names() order by name asc`

View file

@ -19,6 +19,7 @@ import (
"golang.org/x/crypto/bcrypt"
)
// SetLastLogin updates the last login date of a user.
func (s *Storage) SetLastLogin(userID int64) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:SetLastLogin] userID=%d", userID))
query := "UPDATE users SET last_login_at=now() WHERE id=$1"
@ -30,6 +31,7 @@ func (s *Storage) SetLastLogin(userID int64) error {
return nil
}
// UserExists checks if a user exists by using the given username.
func (s *Storage) UserExists(username string) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UserExists] username=%s", username))
@ -38,6 +40,7 @@ func (s *Storage) UserExists(username string) bool {
return result >= 1
}
// AnotherUserExists checks if another user exists with the given username.
func (s *Storage) AnotherUserExists(userID int64, username string) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:AnotherUserExists] userID=%d, username=%s", userID, username))
@ -46,6 +49,7 @@ func (s *Storage) AnotherUserExists(userID int64, username string) bool {
return result >= 1
}
// CreateUser creates a new user.
func (s *Storage) CreateUser(user *model.User) (err error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CreateUser] username=%s", user.Username))
password := ""
@ -84,6 +88,7 @@ func (s *Storage) CreateUser(user *model.User) (err error) {
return nil
}
// UpdateExtraField updates an extra field of the given user.
func (s *Storage) UpdateExtraField(userID int64, field, value string) error {
query := fmt.Sprintf(`UPDATE users SET extra = hstore('%s', $1) WHERE id=$2`, field)
_, err := s.db.Exec(query, value, userID)
@ -93,6 +98,7 @@ func (s *Storage) UpdateExtraField(userID int64, field, value string) error {
return nil
}
// RemoveExtraField deletes an extra field for the given user.
func (s *Storage) RemoveExtraField(userID int64, field string) error {
query := `UPDATE users SET extra = delete(extra, $1) WHERE id=$2`
_, err := s.db.Exec(query, field, userID)
@ -102,6 +108,7 @@ func (s *Storage) RemoveExtraField(userID int64, field string) error {
return nil
}
// UpdateUser updates a user.
func (s *Storage) UpdateUser(user *model.User) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UpdateUser] username=%s", user.Username))
user.Username = strings.ToLower(user.Username)
@ -128,8 +135,9 @@ func (s *Storage) UpdateUser(user *model.User) error {
return nil
}
func (s *Storage) GetUserById(userID int64) (*model.User, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetUserById] userID=%d", userID))
// UserByID finds a user by the ID.
func (s *Storage) UserByID(userID int64) (*model.User, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UserByID] userID=%d", userID))
var user model.User
var extra hstore.Hstore
@ -151,8 +159,9 @@ func (s *Storage) GetUserById(userID int64) (*model.User, error) {
return &user, nil
}
func (s *Storage) GetUserByUsername(username string) (*model.User, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetUserByUsername] username=%s", username))
// UserByUsername finds a user by the username.
func (s *Storage) UserByUsername(username string) (*model.User, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UserByUsername] username=%s", username))
var user model.User
row := s.db.QueryRow("SELECT id, username, is_admin, theme, language, timezone FROM users WHERE username=$1", username)
@ -166,8 +175,9 @@ func (s *Storage) GetUserByUsername(username string) (*model.User, error) {
return &user, nil
}
func (s *Storage) GetUserByExtraField(field, value string) (*model.User, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetUserByExtraField] field=%s", field))
// UserByExtraField finds a user by an extra field value.
func (s *Storage) UserByExtraField(field, value string) (*model.User, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UserByExtraField] field=%s", field))
var user model.User
query := `SELECT id, username, is_admin, theme, language, timezone FROM users WHERE extra->$1=$2`
row := s.db.QueryRow(query, field, value)
@ -181,6 +191,7 @@ func (s *Storage) GetUserByExtraField(field, value string) (*model.User, error)
return &user, nil
}
// RemoveUser deletes a user.
func (s *Storage) RemoveUser(userID int64) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:RemoveUser] userID=%d", userID))
@ -195,14 +206,15 @@ func (s *Storage) RemoveUser(userID int64) error {
}
if count == 0 {
return errors.New("nothing has been removed.")
return errors.New("nothing has been removed")
}
return nil
}
func (s *Storage) GetUsers() (model.Users, error) {
defer helper.ExecutionTime(time.Now(), "[Storage:GetUsers]")
// Users returns all users.
func (s *Storage) Users() (model.Users, error) {
defer helper.ExecutionTime(time.Now(), "[Storage:Users]")
var users model.Users
rows, err := s.db.Query("SELECT id, username, is_admin, theme, language, timezone, last_login_at FROM users ORDER BY username ASC")
@ -233,6 +245,7 @@ func (s *Storage) GetUsers() (model.Users, error) {
return users, nil
}
// CheckPassword validate the hashed password.
func (s *Storage) CheckPassword(username, password string) error {
defer helper.ExecutionTime(time.Now(), "[Storage:CheckPassword]")
@ -241,13 +254,13 @@ func (s *Storage) CheckPassword(username, password string) error {
err := s.db.QueryRow("SELECT password FROM users WHERE username=$1", username).Scan(&hash)
if err == sql.ErrNoRows {
return fmt.Errorf("Unable to find this user: %s\n", username)
return fmt.Errorf("unable to find this user: %s", username)
} else if err != nil {
return fmt.Errorf("Unable to fetch user: %v\n", err)
return fmt.Errorf("unable to fetch user: %v", err)
}
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
return fmt.Errorf("Invalid password for %s\n", username)
return fmt.Errorf("invalid password for %s", username)
}
return nil