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
- Variables
- func AddFeedItem(i *FeedItem) int64
- func CreateAPFollower(uid uint, follower string) error
- func CreateGlob(s string) string
- func CreateUser(username, email string) error
- func DeleteUserFeed(f *UserFeed) error
- func GenerateToken() string
- func GetUnreadBookmarkCount(uid uint) int64
- func GetUnreadFeedItemCount(uid uint) int64
- func Init(c *config.Config) error
- type APFollower
- type Bookmark
- type Collection
- type CommonFields
- type DBTypedef
- type Database
- type Feed
- type FeedItem
- type FeedType
- type Resource
- type Snapshot
- type Tag
- type TagCount
- type Token
- type UnreadFeedItem
- type User
- type UserFeed
- type UserFeedItem
- type UserFeedSummary
Constants ¶
const ErrDBType = utils.StringError("Unknown database type")
ErrDBType is returned when an unknown database type is encountered.
Variables ¶
var DB *gorm.DB
DB is the global database instance.
var DBType = Sqlite
DBType holds the type of the database being used.
Functions ¶
func AddFeedItem ¶ added in v0.6.0
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
CreateAPFollower creates a new ActivityPub follower record.
func CreateGlob ¶ added in v0.7.0
CreateGlob converts a search string into a SQL LIKE pattern.
func CreateUser ¶
CreateUser creates a new user with the specified username and email.
func DeleteUserFeed ¶ added in v0.6.0
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 GetUnreadBookmarkCount ¶ added in v0.6.0
GetUnreadBookmarkCount returns the number of unread bookmarks for a user.
func GetUnreadFeedItemCount ¶ added in v0.6.0
GetUnreadFeedItemCount returns the number of unread feed items for a user.
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
GetUnreadBookmarkItems retrieves unread bookmarks for a user.
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.
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
GetFeedByID retrieves a feed by its ID.
func GetFeedByURL ¶ added in v0.6.0
GetFeedByURL retrieves a feed by its URL.
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.
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.
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
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
GetOrCreateTag retrieves an existing tag or creates a new one.
type TagCount ¶ added in v0.4.0
TagCount represents a tag with its usage count.
func GetFrequentPublicTags ¶ added in v0.4.0
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
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 GetUserByLoginToken ¶
GetUserByLoginToken retrieves a user by their login token.
func GetUserByOAuthID ¶ added in v0.3.0
GetUserByOAuthID retrieves a user by their OAuth ID.
func GetUserBySubmissionToken ¶
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.
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
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.