model

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package model provides database models and data access layer for Omnom.

This package defines all database entities and their relationships, including:

  • Users: User accounts with authentication tokens
  • Bookmarks: Saved web pages with metadata, tags, and collections
  • Snapshots: Archived copies of bookmarked web pages
  • Tags: User-defined labels for organizing bookmarks
  • Collections: Hierarchical bookmark organization
  • Feeds: RSS/Atom and ActivityPub feed subscriptions
  • FeedItems: Individual posts from subscribed feeds
  • Resources: Embedded media and assets from web pages
  • ActivityPub: Federation followers and interactions

The package uses GORM as the ORM layer and supports both SQLite and PostgreSQL databases. All models embed CommonFields which provide ID, timestamps, and soft-delete functionality.

Database migrations are handled automatically through the Init function which sets up the database connection and runs necessary schema updates.

Example usage:

err := model.Init(cfg)
if err != nil {
    log.Fatal(err)
}
user := model.GetUser("username")
bookmarks, _, err := model.SearchBookmarks(user.ID, 50, "golang")

Index

Constants

View Source
const ErrDBType = utils.StringError("Unknown database type")

ErrDBType is returned when an unknown database type is encountered.

Variables

View Source
var DB *gorm.DB

DB is the global database instance.

View Source
var DBType = Sqlite

DBType holds the type of the database being used.

Functions

func AddFeedItem added in v0.6.0

func AddFeedItem(i *FeedItem) int64

AddFeedItem adds a new feed item and notifies subscribed users. Returns the number of users notified about the new item.

func CreateAPFollower added in v0.4.0

func CreateAPFollower(uid uint, follower string) error

CreateAPFollower creates a new ActivityPub follower record.

func CreateGlob added in v0.7.0

func CreateGlob(s string) string

CreateGlob converts a search string into a SQL LIKE pattern.

func CreateUser

func CreateUser(username, email string) error

CreateUser creates a new user with the specified username and email.

func DeleteUserFeed added in v0.6.0

func DeleteUserFeed(f *UserFeed) error

DeleteUserFeed deletes a user's feed subscription and associated items. If this is the last subscription to the feed, the feed itself is also deleted.

func GenerateToken

func GenerateToken() string

GenerateToken generates a new random token string.

func GetUnreadBookmarkCount added in v0.6.0

func GetUnreadBookmarkCount(uid uint) int64

GetUnreadBookmarkCount returns the number of unread bookmarks for a user.

func GetUnreadFeedItemCount added in v0.6.0

func GetUnreadFeedItemCount(uid uint) int64

GetUnreadFeedItemCount returns the number of unread feed items for a user.

func Init

func Init(c *config.Config) error

Init initializes the database connection and runs migrations.

Types

type APFollower added in v0.4.0

type APFollower struct {
	CommonFields
	UserID   uint   `gorm:"uniqueIndex:apuidx" json:"uid"`
	Follower string `gorm:"uniqueIndex:apuidx" json:"follower"`
}

APFollower represents an ActivityPub follower.

type Bookmark

type Bookmark struct {
	CommonFields
	URL          string      `json:"url"`
	Title        string      `json:"title"`
	Notes        string      `json:"notes"`
	Domain       string      `json:"domain"`
	Favicon      string      `json:"favicon"`
	Tags         []Tag       `gorm:"many2many:bookmark_tags;" json:"tags"`
	Snapshots    []Snapshot  `json:"snapshots"`
	CollectionID uint        `json:"-"`
	Collection   *Collection `json:"collection"`
	Public       bool        `json:"public"`
	Unread       bool        `json:"unread"`
	UserID       uint        `json:"user_id"`
	User         User        `json:"-"`
}

Bookmark represents a saved webpage bookmark.

func GetOrCreateBookmark added in v0.4.0

func GetOrCreateBookmark(u *User, urlString, title, tags, notes, public, favicon, collection, unread string) (*Bookmark, bool, error)

GetOrCreateBookmark retrieves an existing bookmark or creates a new one. TODO use Bookmark as parameter instead of strings

func GetUnreadBookmarkItems added in v0.6.0

func GetUnreadBookmarkItems(uid, limit uint) []*Bookmark

GetUnreadBookmarkItems retrieves unread bookmarks for a user.

func SearchBookmarks added in v0.7.0

func SearchBookmarks(uid, limit uint, query string) ([]*Bookmark, int64, error)

SearchBookmarks searches bookmarks by query string.

type Collection added in v0.5.0

type Collection struct {
	CommonFields
	Name      string `gorm:"uniqueIndex:cuid" json:"name"`
	UserID    uint   `gorm:"uniqueIndex:cuid" json:"user_id"`
	ParentID  uint
	Children  []*Collection `gorm:"foreignKey:parent_id"`
	User      User          `json:"-"`
	Bookmarks []Bookmark    `json:"bookmarks"`
}

Collection represents a bookmark collection.

func GetCollection added in v0.5.0

func GetCollection(uid uint, cid string) *Collection

GetCollection retrieves a collection by its ID.

func GetCollectionByName added in v0.5.0

func GetCollectionByName(uid uint, cname string) *Collection

GetCollectionByName retrieves a collection by its name.

func GetCollectionTree added in v0.5.0

func GetCollectionTree(uid uint) []*Collection

GetCollectionTree retrieves collections organized as a tree structure.

func GetCollections added in v0.5.0

func GetCollections(uid uint) []*Collection

GetCollections retrieves all collections for a user.

type CommonFields added in v0.2.0

type CommonFields struct {
	ID        uint       `gorm:"primary_key" json:"id"`
	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"deleted_at"`
}

CommonFields contains fields common to all models.

type DBTypedef added in v0.7.0

type DBTypedef int

DBTypedef represents the type of database being used.

const (
	// Sqlite represents SQLite database type.
	Sqlite DBTypedef = iota
	// Psql represents PostgreSQL database type.
	Psql
)

type Database added in v0.2.0

type Database struct {
	ID      uint `gorm:"primaryKey"`
	Version uint
}

Database represents the database version tracking table.

type Feed added in v0.6.0

type Feed struct {
	CommonFields
	Name    string      `json:"name"`
	URL     string      `json:"gorm:"unique" url"`
	Author  string      `json:"author"`
	Type    string      `json:"type"`
	Favicon string      `json:"favicon"`
	Items   []*FeedItem `json:"items"`
	Users   []*User     `gorm:"many2many:user_feeds;" json:"-"`
}

Feed represents an RSS or ActivityPub feed.

func GetFeedByID added in v0.6.0

func GetFeedByID(id uint) (*Feed, error)

GetFeedByID retrieves a feed by its ID.

func GetFeedByURL added in v0.6.0

func GetFeedByURL(u string) (*Feed, error)

GetFeedByURL retrieves a feed by its URL.

func GetFeeds added in v0.6.0

func GetFeeds() ([]*Feed, error)

GetFeeds retrieves all feeds from the database.

type FeedItem added in v0.6.0

type FeedItem struct {
	CommonFields
	URL                string  `gorm:"uniqueIndex:feeditemuidx" json:"url"`
	Title              string  `json:"title"`
	Content            string  `json:"content"`
	OriginalAuthorID   string  `json:"original_author_id"`
	OriginalAuthorName string  `json:"original_author_name"`
	InReplyTo          string  `json:"in_reply_to"`
	Context            string  `json:"context"`
	Favicon            string  `json:"favicon"`
	FeedID             uint    `gorm:"uniqueIndex:feeditemuidx" json:"feed_id"`
	Feed               *Feed   `json:"feed"`
	Users              []*User `gorm:"many2many:user_feed_items;" json:"-"`
}

FeedItem represents an item in a feed.

func GetFeedItem added in v0.7.0

func GetFeedItem(fid uint, u string) (*FeedItem, error)

GetFeedItem retrieves a specific feed item by feed ID and URL.

type FeedType added in v0.7.0

type FeedType string

FeedType represents the type of feed.

const (
	// RSSFeed represents an RSS/Atom feed.
	RSSFeed FeedType = "rss"
	// ActivityPubFeed represents an ActivityPub feed.
	ActivityPubFeed FeedType = "ap"
)

type Resource added in v0.2.0

type Resource struct {
	CommonFields
	Key              string     `gorm:"unique" json:"key"`
	MimeType         string     `json:"mimeType"`
	OriginalFilename string     `json:"originalFilename"`
	Size             uint       `json:"size"`
	Snapshots        []Snapshot `gorm:"many2many:snapshot_resources;" json:"snapshots"`
}

Resource represents a webpage resource like images or stylesheets.

func GetOrCreateResource added in v0.2.0

func GetOrCreateResource(key string, mimeType string, fname string, size uint) *Resource

GetOrCreateResource retrieves an existing resource or creates a new one.

type Snapshot

type Snapshot struct {
	CommonFields
	Title      string      `json:"title"`
	Key        string      `json:"key"`
	Text       string      `json:"text"`
	BookmarkID uint        `json:"bookmark_id"`
	Bookmark   Bookmark    `json:"bookmark"`
	Size       uint        `json:"size"`
	Resources  []*Resource `gorm:"many2many:snapshot_resources;" json:"resources"`
}

Snapshot represents a saved webpage snapshot.

func GetSnapshotWithResources added in v0.5.0

func GetSnapshotWithResources(key string) (*Snapshot, error)

GetSnapshotWithResources retrieves a snapshot with its associated resources.

type Tag

type Tag struct {
	CommonFields
	Text      string     `gorm:"unique" json:"text"`
	Bookmarks []Bookmark `gorm:"many2many:bookmark_tags;" json:"bookmarks"`
}

Tag represents a bookmark tag.

func GetOrCreateTag added in v0.2.0

func GetOrCreateTag(tag string) Tag

GetOrCreateTag retrieves an existing tag or creates a new one.

func GetUserTagsFromText added in v0.7.0

func GetUserTagsFromText(s string, uid uint) ([]*Tag, error)

GetUserTagsFromText retrieves user tags that match the given text.

type TagCount added in v0.4.0

type TagCount struct {
	Tag   string
	Count int64
}

TagCount represents a tag with its usage count.

func GetFrequentPublicTags added in v0.4.0

func GetFrequentPublicTags(count int) []*TagCount

GetFrequentPublicTags retrieves the most frequently used public tags.

type Token

type Token struct {
	CommonFields
	UserID uint   `json:"user_id"`
	Text   string `json:"text"`
}

Token represents an API or submission token.

func CreateAddonToken added in v0.6.0

func CreateAddonToken(uid uint) (*Token, error)

CreateAddonToken creates a new addon token for a user.

type UnreadFeedItem added in v0.6.0

type UnreadFeedItem struct {
	FeedItem
	FeedID         uint   `json:"feed_id"`
	FeedName       string `json:"feed_name"`
	FeedAuthor     string `json:"feed_author"`
	FeedURL        string `json:"feed_url"`
	FeedType       string `json:"feed_type"`
	FeedFavicon    string `json:"feed_favicon"`
	UserFeedItemID uint
	Unread         bool
}

UnreadFeedItem represents a feed item with unread status and feed metadata.

func GetUnreadFeedItems added in v0.6.0

func GetUnreadFeedItems(uid, limit uint) []*UnreadFeedItem

GetUnreadFeedItems retrieves unread feed items for a user.

func SearchFeedItems added in v0.7.0

func SearchFeedItems(uid, limit uint, query string, feedID uint, includeRead bool) ([]*UnreadFeedItem, int64, error)

SearchFeedItems searches feed items by query string with optional filters. Returns matching items and total count.

type User

type User struct {
	CommonFields
	Username         string      `gorm:"unique" json:"username"`
	Email            *string     `gorm:"unique" json:"email"`
	OAuthID          *string     `gorm:"unique" json:"-"`
	LoginToken       string      `json:"-"`
	SubmissionTokens []Token     `json:"-"`
	Bookmarks        []Bookmark  `json:"bookmarks"`
	Feeds            []*Feed     `gorm:"many2many:user_feeds;" json:"feeds"`
	FeedsItems       []*FeedItem `gorm:"many2many:user_feed_items;" json:"feed_items"`
}

User represents a user account.

func GetUser

func GetUser(name string) *User

GetUser retrieves a user by username or email.

func GetUserByLoginToken

func GetUserByLoginToken(tok string) *User

GetUserByLoginToken retrieves a user by their login token.

func GetUserByOAuthID added in v0.3.0

func GetUserByOAuthID(id string) *User

GetUserByOAuthID retrieves a user by their OAuth ID.

func GetUserBySubmissionToken

func GetUserBySubmissionToken(tok string) *User

GetUserBySubmissionToken retrieves a user by their submission token.

type UserFeed added in v0.6.0

type UserFeed struct {
	CommonFields
	Name   string `json:"name"`
	Public bool   `json:"public"`
	FeedID uint   `json:"feed_id"`
	Feed   *Feed  `json:"feed"`
	UserID uint   `json:"user_id"`
	User   *User  `json:"-"`
}

UserFeed represents a user's subscription to a feed.

func GetUserFeed added in v0.6.0

func GetUserFeed(uid uint, fid string) (*UserFeed, error)

GetUserFeed retrieves a specific user feed by user ID and feed ID.

type UserFeedItem added in v0.6.0

type UserFeedItem struct {
	CommonFields
	Unread     bool      `json:"unread"`
	FeedItemID uint      `gorm:"uniqueIndex:userfeeditemuidx" json:"feed_item_id"`
	FeedItem   *FeedItem `json:"feed_item"`
	UserID     uint      `gorm:"uniqueIndex:userfeeditemuidx" json:"user_id"`
	User       *User     `json:"-"`
}

UserFeedItem represents a user's relationship with a feed item.

type UserFeedSummary added in v0.6.0

type UserFeedSummary struct {
	UserFeed
	Count uint
}

UserFeedSummary represents a user feed with item count.

func GetUserFeeds added in v0.6.0

func GetUserFeeds(uid uint, unread bool) ([]*UserFeedSummary, error)

GetUserFeeds retrieves all feeds for a user with item counts. If unread is true item count contains only unread items.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL