code cleanup

This commit is contained in:
Nazar Kanaev
2020-07-24 16:39:12 +01:00
parent c04f54619b
commit b223233318
9 changed files with 112 additions and 184 deletions

View File

@@ -1,13 +1,13 @@
package storage
type Feed struct {
Id int64 `json:"id"`
FolderId *int64 `json:"folder_id"`
Title string `json:"title"`
Id int64 `json:"id"`
FolderId *int64 `json:"folder_id"`
Title string `json:"title"`
Description string `json:"description"`
Link string `json:"link"`
FeedLink string `json:"feed_link"`
Icon string `json:"icon"`
Link string `json:"link"`
FeedLink string `json:"feed_link"`
Icon string `json:"icon"`
}
func (s *Storage) CreateFeed(title, description, link, feedLink, icon string, folderId *int64) *Feed {
@@ -26,13 +26,13 @@ func (s *Storage) CreateFeed(title, description, link, feedLink, icon string, fo
return nil
}
return &Feed{
Id: id,
Title: title,
Id: id,
Title: title,
Description: description,
Link: link,
FeedLink: feedLink,
Icon: icon,
FolderId: folderId,
Link: link,
FeedLink: feedLink,
Icon: icon,
FolderId: folderId,
}
}

View File

@@ -5,9 +5,9 @@ import (
)
type Folder struct {
Id int64 `json:"id"`
Title string `json:"title"`
IsExpanded bool `json:"is_expanded"`
Id int64 `json:"id"`
Title string `json:"title"`
IsExpanded bool `json:"is_expanded"`
}
func (s *Storage) CreateFolder(title string) *Folder {

View File

@@ -1,11 +1,11 @@
package storage
import (
"fmt"
"time"
"strings"
"encoding/json"
"fmt"
"golang.org/x/net/html"
"strings"
"time"
)
type ItemStatus int
@@ -16,15 +16,15 @@ const (
STARRED ItemStatus = 2
)
var StatusRepresentations = map[ItemStatus]string {
UNREAD: "unread",
READ: "read",
var StatusRepresentations = map[ItemStatus]string{
UNREAD: "unread",
READ: "read",
STARRED: "starred",
}
var StatusValues = map[string]ItemStatus {
"unread": UNREAD,
"read": READ,
var StatusValues = map[string]ItemStatus{
"unread": UNREAD,
"read": READ,
"starred": STARRED,
}
@@ -42,25 +42,25 @@ func (s *ItemStatus) UnmarshalJSON(b []byte) error {
}
type Item struct {
Id int64 `json:"id"`
GUID string `json:"guid"`
FeedId int64 `json:"feed_id"`
Title string `json:"title"`
Link string `json:"link"`
Description string `json:"description"`
Content string `json:"content"`
Author string `json:"author"`
Date *time.Time `json:"date"`
Id int64 `json:"id"`
GUID string `json:"guid"`
FeedId int64 `json:"feed_id"`
Title string `json:"title"`
Link string `json:"link"`
Description string `json:"description"`
Content string `json:"content"`
Author string `json:"author"`
Date *time.Time `json:"date"`
DateUpdated *time.Time `json:"date_updated"`
Status ItemStatus `json:"status"`
Image string `json:"image"`
Status ItemStatus `json:"status"`
Image string `json:"image"`
}
type ItemFilter struct {
FolderID *int64
FeedID *int64
Status *ItemStatus
Search *string
FeedID *int64
Status *ItemStatus
Search *string
}
func (s *Storage) CreateItems(items []Item) bool {
@@ -227,8 +227,8 @@ func (s *Storage) MarkItemsRead(filter ItemFilter) bool {
}
type FeedStat struct {
FeedId int64 `json:"feed_id"`
UnreadCount int64 `json:"unread"`
FeedId int64 `json:"feed_id"`
UnreadCount int64 `json:"unread"`
StarredCount int64 `json:"starred"`
}
@@ -255,20 +255,20 @@ func (s *Storage) FeedStats() []FeedStat {
}
func HTMLText(s string) string {
tokenizer := html.NewTokenizer(strings.NewReader(s))
tokenizer := html.NewTokenizer(strings.NewReader(s))
contents := make([]string, 0)
for {
for {
token := tokenizer.Next()
if token == html.ErrorToken {
break
}
if token == html.TextToken {
content := strings.TrimSpace(html.UnescapeString(string(tokenizer.Text())))
if len(content) > 0 {
content := strings.TrimSpace(html.UnescapeString(string(tokenizer.Text())))
if len(content) > 0 {
contents = append(contents, content)
}
}
}
}
}
return strings.Join(contents, " ")
}
@@ -314,7 +314,7 @@ func (s *Storage) SyncSearch() {
func (s *Storage) DeleteOldItems() {
result, err := s.db.Exec(
`delete from items where status = ? and date < ?`,
READ, time.Now().Add(-time.Hour * 24 * 90) /* 90 days */)
READ, time.Now().Add(-time.Hour*24*90) /* 90 days */)
if err != nil {
s.log.Print(err)
return

View File

@@ -4,8 +4,8 @@ import "encoding/json"
func settingsDefaults() map[string]interface{} {
return map[string]interface{}{
"filter": "",
"feed": "",
"filter": "",
"feed": "",
"feed_list_width": 300,
"item_list_width": 300,
}
@@ -27,7 +27,7 @@ func (s *Storage) GetSettingsValue(key string) interface{} {
}
func (s *Storage) GetSettings() map[string]interface{} {
result := settingsDefaults()
result := settingsDefaults()
rows, err := s.db.Query(`select key, val from settings;`)
if err != nil {
s.log.Print(err)
@@ -66,7 +66,7 @@ func (s *Storage) UpdateSettings(kv map[string]interface{}) bool {
)
if err != nil {
s.log.Print(err)
return false
return false
}
}
return true

View File

@@ -1,10 +1,10 @@
package storage
import (
"os"
"log"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)
var initQuery string = `
@@ -63,7 +63,7 @@ end;
`
type Storage struct {
db *sql.DB
db *sql.DB
log *log.Logger
}
@@ -77,7 +77,7 @@ func New() (*Storage, error) {
if err != nil {
return nil, err
}
logger := log.New(os.Stdout, "storage: ", log.Ldate | log.Ltime | log.Lshortfile)
logger := log.New(os.Stdout, "storage: ", log.Ldate|log.Ltime|log.Lshortfile)
return &Storage{db: db, log: logger}, nil
}