2019-07-17 05:28:37 +00:00
|
|
|
package browsers
|
|
|
|
|
|
|
|
import (
|
2022-01-17 03:42:11 +00:00
|
|
|
"context"
|
2019-07-17 05:28:37 +00:00
|
|
|
"database/sql"
|
2020-11-16 18:31:34 +00:00
|
|
|
|
|
|
|
"github.com/mat/besticon/besticon"
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-16 18:31:34 +00:00
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2019-07-17 05:28:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Database sql wrapper for operations with browser objects.
|
|
|
|
type Database struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes database.
|
|
|
|
func (db Database) Close() error {
|
|
|
|
return db.db.Close()
|
|
|
|
}
|
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
func NewDB(db *sql.DB) *Database {
|
|
|
|
return &Database{db: db}
|
2019-07-17 05:28:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 18:31:34 +00:00
|
|
|
type BookmarksType string
|
|
|
|
|
|
|
|
type Bookmark struct {
|
2022-06-08 18:39:53 +00:00
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ImageURL string `json:"imageUrl"`
|
|
|
|
Removed bool `json:"removed"`
|
|
|
|
Clock uint64 `json:"-"` //used to sync
|
2022-06-09 15:21:57 +00:00
|
|
|
DeletedAt uint64 `json:"deletedAt,omitempty"`
|
2020-11-16 18:31:34 +00:00
|
|
|
}
|
2022-08-24 14:05:35 +00:00
|
|
|
type Browser struct {
|
|
|
|
ID string `json:"browser-id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Timestamp uint64 `json:"timestamp"`
|
|
|
|
Dapp bool `json:"dapp?"`
|
|
|
|
HistoryIndex int `json:"history-index"`
|
|
|
|
History []string `json:"history,omitempty"`
|
|
|
|
}
|
2020-11-16 18:31:34 +00:00
|
|
|
|
|
|
|
func (db *Database) GetBookmarks() ([]*Bookmark, error) {
|
2022-06-08 18:39:53 +00:00
|
|
|
rows, err := db.db.Query(`SELECT url, name, image_url, removed, deleted_at FROM bookmarks`)
|
2020-11-16 18:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var rst []*Bookmark
|
|
|
|
for rows.Next() {
|
|
|
|
bookmark := &Bookmark{}
|
2022-06-08 18:39:53 +00:00
|
|
|
err := rows.Scan(&bookmark.URL, &bookmark.Name, &bookmark.ImageURL, &bookmark.Removed, &bookmark.DeletedAt)
|
2020-11-16 18:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rst = append(rst, bookmark)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) StoreBookmark(bookmark Bookmark) (Bookmark, error) {
|
2022-06-09 15:21:57 +00:00
|
|
|
insert, err := db.db.Prepare("INSERT OR REPLACE INTO bookmarks (url, name, image_url, removed, clock, deleted_at) VALUES (?, ?, ?, ?, ?, ?)")
|
2020-11-16 18:31:34 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return bookmark, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the right icon
|
|
|
|
finder := besticon.IconFinder{}
|
|
|
|
icons, iconError := finder.FetchIcons(bookmark.URL)
|
|
|
|
|
|
|
|
if iconError == nil && len(icons) > 0 {
|
2023-01-13 17:12:46 +00:00
|
|
|
icon := finder.IconInSizeRange(besticon.SizeRange{Min: 48, Perfect: 48, Max: 100})
|
2020-11-16 18:31:34 +00:00
|
|
|
if icon != nil {
|
|
|
|
bookmark.ImageURL = icon.URL
|
|
|
|
} else {
|
|
|
|
bookmark.ImageURL = icons[0].URL
|
|
|
|
}
|
|
|
|
} else {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("error getting the bookmark icon", zap.Error(iconError))
|
2020-11-16 18:31:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 15:21:57 +00:00
|
|
|
_, err = insert.Exec(bookmark.URL, bookmark.Name, bookmark.ImageURL, bookmark.Removed, bookmark.Clock, bookmark.DeletedAt)
|
2020-11-16 18:31:34 +00:00
|
|
|
return bookmark, err
|
|
|
|
}
|
|
|
|
|
2022-01-17 03:42:11 +00:00
|
|
|
func (db *Database) StoreBookmarkWithoutFetchIcon(bookmark *Bookmark, tx *sql.Tx) (err error) {
|
|
|
|
if tx == nil {
|
|
|
|
tx, err = db.db.BeginTx(context.Background(), &sql.TxOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// don't shadow original error
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:21:57 +00:00
|
|
|
insert, err := tx.Prepare("INSERT OR REPLACE INTO bookmarks (url, name, image_url, removed, clock, deleted_at) VALUES (?, ?, ?, ?, ?, ?)")
|
2022-01-17 03:42:11 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer insert.Close()
|
|
|
|
|
2022-06-09 15:21:57 +00:00
|
|
|
_, err = insert.Exec(bookmark.URL, bookmark.Name, bookmark.ImageURL, bookmark.Removed, bookmark.Clock, bookmark.DeletedAt)
|
2022-01-17 03:42:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) StoreSyncBookmarks(bookmarks []*Bookmark) ([]*Bookmark, error) {
|
|
|
|
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// don't shadow original error
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var storedBookmarks []*Bookmark
|
|
|
|
for _, bookmark := range bookmarks {
|
|
|
|
shouldSync, err := db.shouldSyncBookmark(bookmark, tx)
|
|
|
|
if err != nil {
|
|
|
|
return storedBookmarks, err
|
|
|
|
}
|
|
|
|
if shouldSync {
|
|
|
|
err := db.StoreBookmarkWithoutFetchIcon(bookmark, tx)
|
|
|
|
if err != nil {
|
|
|
|
return storedBookmarks, err
|
|
|
|
}
|
|
|
|
storedBookmarks = append(storedBookmarks, bookmark)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return storedBookmarks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) shouldSyncBookmark(bookmark *Bookmark, tx *sql.Tx) (shouldSync bool, err error) {
|
|
|
|
if tx == nil {
|
|
|
|
tx, err = db.db.BeginTx(context.Background(), &sql.TxOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// don't shadow original error
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
qr := tx.QueryRow(`SELECT 1 FROM bookmarks WHERE url = ? AND clock > ?`, bookmark.URL, bookmark.Clock)
|
|
|
|
var result int
|
|
|
|
err = qr.Scan(&result)
|
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows:
|
|
|
|
// Query does not match, therefore synced_at value is not older than the new clock value or id was not found
|
|
|
|
return true, nil
|
|
|
|
case nil:
|
|
|
|
// Error is nil, therefore query matched and synced_at is older than the new clock
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
// Error is not nil and is not sql.ErrNoRows, therefore pass out the error
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:31:34 +00:00
|
|
|
func (db *Database) UpdateBookmark(originalURL string, bookmark Bookmark) error {
|
2022-06-08 18:39:53 +00:00
|
|
|
insert, err := db.db.Prepare("UPDATE bookmarks SET url = ?, name = ?, image_url = ?, removed = ?, clock = ?, deleted_at = ? WHERE url = ?")
|
2020-11-16 18:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-08 18:39:53 +00:00
|
|
|
_, err = insert.Exec(bookmark.URL, bookmark.Name, bookmark.ImageURL, bookmark.Removed, bookmark.Clock, bookmark.DeletedAt, originalURL)
|
2020-11-16 18:31:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) DeleteBookmark(url string) error {
|
|
|
|
_, err := db.db.Exec(`DELETE FROM bookmarks WHERE url = ?`, url)
|
|
|
|
return err
|
|
|
|
}
|
2022-01-17 03:42:11 +00:00
|
|
|
|
|
|
|
func (db *Database) RemoveBookmark(url string) error {
|
|
|
|
_, err := db.db.Exec(`UPDATE bookmarks SET removed = 1 WHERE url = ?`, url)
|
|
|
|
return err
|
|
|
|
}
|