model

package
v0.0.0-...-bd8c8ec Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2019 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckSafe

func CheckSafe(directory string, base string) bool

CheckSafe checks if the directory is a child directory of base, to make sure that GetFileList won't read any folder other than the upload folder.

func CreateFilePath

func CreateFilePath(dir string, name string) string

CreateFilePath creates a filepath from the given directory and name, returning the name of the newly created filepath.

func DeleteComment

func DeleteComment(id int64) error

DeleteComment deletes the comment with the given ID from the DB.

func DeleteFileByID

func DeleteFileByID(id int64) error

DeleteFileByID deletes the given file id from the DB.

func DeleteOldTags

func DeleteOldTags() error

DeleteOldTags removes any unused tags from the DB.

func DeletePostById

func DeletePostById(id int64) error

DeletePostById deletes the given Post from the DB.

func DeletePostTagsByPostId

func DeletePostTagsByPostId(post_id int64) error

DeletePostTagsByPostId deletes removes tags associated with the given post from the DB.

func EncryptPassword

func EncryptPassword(password string) (string, error)

EncrypPassword hashes and salts the given password via bcrypt, returning the newly hashed and salted password.

func GenerateJWTKeys

func GenerateJWTKeys(bits int) ([]byte, []byte, error)

GenerateJWTKeys generates a new public/private key pair, to be used to create and validate JSON web tokens.

func GenerateSlug

func GenerateSlug(input string, table string) string

GenerateSlug generates a URL-friendly slug. The table is one of "posts", "tags", "navigation", or "users".

func GetNumberOfComments

func GetNumberOfComments() (int64, error)

GetNumberOfComments returns the total number of comments in the DB.

func GetNumberOfFileDbItem

func GetNumberOfFileDbItem() (int64, error)

GetNumberOfFileDbItem gets the total number of files in the DB.

func GetNumberOfPosts

func GetNumberOfPosts(isPage bool, published bool) (int64, error)

GetNumberOfPosts gets the total number of posts in the DB.

func GetNumberOfUsers

func GetNumberOfUsers() (int64, error)

GetNumberOfUsers returns the total number of users.

func GetSettingValue

func GetSettingValue(k string) string

GetSettingValue returns the Setting value associated with the given Setting key.

func Initialize

func Initialize(dbPath string, dbExists bool) error

Initialize sets up the DB by creaing a new connection, creating tables if they don't exist yet, and creates the welcome data.

func InitializeKey

func InitializeKey(privKeyPath, pubKeyPath string)

InitializeKey initializes the public and private keys used to create and validate JSON web tokens.

func InsertPostTag

func InsertPostTag(postID int64, tagID int64) error

InsertPostTag saves the Post ID to the given Tag ID in the DB.

func InsertRoleUser

func InsertRoleUser(role_id int, user_id int64) error

InsertRoleUser assigns a role to the given user based on the given Role ID.

func PostChangeSlug

func PostChangeSlug(slug string) bool

PostChangeSlug checks to see if there is a post associated with the given slug, and returns true if there isn't.

func SetMessageGenerator

func SetMessageGenerator(name string, fn func(v interface{}) string)

SetMessageGenerator maps a message generator's name to a function.

func SetNavigators

func SetNavigators(labels, urls []string) error

SetNavigators saves one or more label-url pairs in the site's Settings.

func SetSettingIfNotExists

func SetSettingIfNotExists(k, v, t string) error

SetSettingIfNotExists sets the setting created by the given key-value pair if the setting does not yet exist.

func ValidateJWT

func ValidateJWT(t string) (*jwt.Token, error)

ValidateJWT validates a JSON web token, returning the token if it is indeed valid.

Types

type Comment

type Comment struct {
	Id        int64      `meddler:"id,pk"`
	PostId    int64      `meddler:"post_id"`
	Author    string     `meddler:"author"`
	Email     string     `meddler:"author_email"`
	Avatar    string     `meddler:"author_avatar"`
	Website   string     `meddler:"author_url"`
	Ip        string     `meddler:"author_ip"`
	CreatedAt *time.Time `meddler:"created_at"`
	Content   string     `meddler:"content"`
	Approved  bool       `meddler:"approved"`
	UserAgent string     `meddler:"agent"`
	Type      string     `meddler:"type"`
	Parent    int64      `meddler:"parent"`
	UserId    int64      `meddler:"user_id"`
	Children  *Comments  `meddler:"-"`
}

A Comment defines comment item data.

func NewComment

func NewComment() *Comment

NewComment returns a new comment, with the CreatedAt field set to the current time.

func (*Comment) GetCommentById

func (c *Comment) GetCommentById() error

GetCommentById gets a comment by its ID, and populates that comment struct with the contents for that comment from the DB.

func (*Comment) ParentComment

func (c *Comment) ParentComment() (*Comment, error)

ParentComment returns the associated parent Comment, if one exists.

func (*Comment) ParentContent

func (c *Comment) ParentContent() string

ParentContent returns the parent of a given comment, if it exists. Used for threaded comments.

func (*Comment) Post

func (c *Comment) Post() *Post

Post returns the post associated with the commment.

func (*Comment) Save

func (c *Comment) Save() error

Save saves the comment in the DB.

func (*Comment) ToJson

func (c *Comment) ToJson() map[string]interface{}

ToJson returns a comment as a map, in order to be encoded as JSON.

func (*Comment) ValidateComment

func (c *Comment) ValidateComment() string

ValidateComment validates a comment to ensure that all required data exists and is valid. Returns an empty string on success.

type Comments

type Comments []*Comment

Comments are a slice of "Comment"s

func (Comments) Get

func (c Comments) Get(i int) *Comment

Get returns the Comment at the given index.

func (Comments) GetAll

func (c Comments) GetAll() []*Comment

GetAll returns a slice of all the "Comment"s.

func (*Comments) GetCommentList

func (c *Comments) GetCommentList(page, size int64, onlyApproved bool) (*utils.Pager, error)

GetCommentList returns a new pager based on the total number of comments.

func (*Comments) GetCommentsByPostId

func (comments *Comments) GetCommentsByPostId(id int64) error

GetCommentsByPostId gets all the comments for the given post ID.

func (Comments) Len

func (c Comments) Len() int

Len returns the number of "Comment"s in a "Comments".

type File

type File struct {
	os.FileInfo
	Url     string
	ModTime *time.Time
}

A File is a file, along with a URL and last modified time.

func GetFileList

func GetFileList(directory string) []*File

GetFileList returns a slice of all Files in the given directory.

type FileDb

type FileDb struct {
	ID              int64      `meddler:"id,pk"`
	Name            string     `meddler:"name"`
	URL             string     `meddler:"url"`
	CreatedAt       *time.Time `meddler:"created_at"`
	IsShowOnGallery bool       `meddler:"is_show_on_gallery"`
}

A FileDb defines comment item data.

func NewFileDb

func NewFileDb() *FileDb

NewFileDb returns a new FileDb, with the CreatedAt field set to the current time.

func (*FileDb) GetFileByID

func (f *FileDb) GetFileByID() error

func (*FileDb) Save

func (f *FileDb) Save() error

Save saves the comment in the DB.

type FileDbItem

type FileDbItem []*FileDb

FileDbItem are a slice of "FileDB"s

func GetGalleryList

func GetGalleryList(offset, limit int) (FileDbItem, error)

func (*FileDbItem) GetFileDbItem

func (fileItem *FileDbItem) GetFileDbItem(page, size int64, orderBy string) (*utils.Pager, error)

type JWT

type JWT struct {
	UserRole   int    `json:"user_role"`
	UserID     int64  `json:"user_id"`
	UserEmail  string `json:"user_email"`
	Expiration int64  `json:"expiration"`
	Token      string `json:"token"`
}

A JWT is a JSON web token, and contains all the values necessary to create and validate tokens.

func NewJWT

func NewJWT(user *User) (JWT, error)

NewJWT returns a JWT for the given User.

func NewJWTFromToken

func NewJWTFromToken(token *jwt.Token) JWT

NewJWTFromToken returns a JWT for the given token.

type Message

type Message struct {
	Id        int        `meddler:"id,pk"`
	Type      string     `meddler:"type"`
	Data      string     `meddler:"data"`
	IsRead    bool       `meddler:"is_read"`
	CreatedAt *time.Time `meddler:"created_at"`
}

A Message is a simple bit of info, used to alert the admin on the admin panel about things like new comments, etc.

func NewMessage

func NewMessage(tp string, data interface{}) *Message

NewMessage creates a new message.

func (*Message) Insert

func (m *Message) Insert() error

Insert saves a message to the DB.

type Messages

type Messages []*Message

Messages is a slice of "Message"s

func (Messages) Get

func (m Messages) Get(i int) *Message

Get returns the message at the given index inside Messages.

func (*Messages) GetUnreadMessages

func (m *Messages) GetUnreadMessages()

GetUnreadMessages gets all unread messages from the DB.

type Navigator struct {
	Label string `json:"label"`
	Url   string `json:"url"`
}

A Navigator represents a link in the site navigation menu.

func GetNavigators

func GetNavigators() []*Navigator

GetNavigators returns a slice of all Navigators.

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool // Valid is true if Time is not NULL
}

NullTime is an instant in time, with an extra flag to determine whether or not the time is valid. A time is considered "Valid" if it is "NOT NULL".

func (*NullTime) Scan

func (nt *NullTime) Scan(value interface{}) error

Scan implements the Scanner interface.

func (NullTime) Value

func (nt NullTime) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Post

type Post struct {
	Id              int64      `meddler:"id,pk",json:"id"`
	Title           string     `meddler:"title",json:"title"`
	Slug            string     `meddler:"slug",json:"slug"`
	Markdown        string     `meddler:"markdown",json:"markdown"`
	Html            string     `meddler:"html",json:"html"`
	Image           string     `meddler:"image",json:"image"`
	IsFeatured      bool       `meddler:"featured",json:"featured"`
	IsPage          bool       `meddler:"page",json:"is_page"` // Using "is_page" instead of "page" since nouns are generally non-bools
	AllowComment    bool       `meddler:"allow_comment",json:"allow_comment"`
	CommentNum      int64      `meddler:"comment_num",json:"comment_num"`
	IsPublished     bool       `meddler:"published",json:"published"`
	Language        string     `meddler:"language",json:"language"`
	MetaTitle       string     `meddler:"meta_title",json:"meta_title"`
	MetaDescription string     `meddler:"meta_description",json:"meta_description"`
	CreatedAt       *time.Time `meddler:"created_at",json:"created_at"`
	CreatedBy       int64      `meddler:"created_by",json:"created_by"`
	UpdatedAt       *time.Time `meddler:"updated_at",json:"updated_at"`
	UpdatedBy       int64      `meddler:"updated_by",json:"updated_by"`
	PublishedAt     *time.Time `meddler:"published_at",json:"published_at"`
	PublishedBy     int64      `meddler:"published_by",json:"published_by"`
	Hits            int64      `meddler:"-"`
	Category        string     `meddler:"-"`
}

A Post contains all the content required to populate a post or page on the blog. It also contains info to help sort and display the post.

func GetAllPosts

func GetAllPosts(offset, limit int) ([]*Post, error)

func NewPost

func NewPost() *Post

NewPost creates a new Post, with CreatedAt set to the current time.

func (*Post) Author

func (p *Post) Author() *User

Author returns the User who authored the post.

func (*Post) Comments

func (p *Post) Comments() []*Comment

Comments returns all the comments associated with the post.

func (*Post) Excerpt

func (p *Post) Excerpt() string

Excerpt returns the post execerpt, with a default length of 255 characters.

func (*Post) GetPostById

func (post *Post) GetPostById(id ...int64) error

GetPostById gets the post based on the Post ID.

func (*Post) GetPostBySlug

func (p *Post) GetPostBySlug(slug string) error

GetPostBySlug gets the post based on the Post Slug.

func (*Post) Insert

func (p *Post) Insert() error

Insert saves a post to the DB.

func (*Post) Publish

func (p *Post) Publish(by int64) error

func (*Post) Save

func (p *Post) Save(tags ...*Tag) error

Save saves a post to the DB, updating any given tags to include the Post ID.

func (*Post) Summary

func (p *Post) Summary() string

Summary returns the post summary.

func (*Post) TagString

func (p *Post) TagString() string

TagString returns all the tags associated with a post as a single string.

func (*Post) Tags

func (p *Post) Tags() []*Tag

Tags returns a slice of every tag associated with the post.

func (*Post) Update

func (p *Post) Update() error

Update updates an existing post in the DB.

func (*Post) UpdateFromJSON

func (p *Post) UpdateFromJSON(j []byte) error

func (*Post) UpdateFromRequest

func (p *Post) UpdateFromRequest(r *http.Request)

UpdateFromRequest updates an existing Post in the DB based on the data provided in the HTTP request.

func (*Post) Url

func (p *Post) Url() string

Url returns the URL of the post.

type Posts

type Posts []*Post

Posts is a slice of "Post"s

func GetPublishedPosts

func GetPublishedPosts(offset, limit int) (Posts, error)

func GetUnpublishedPosts

func GetUnpublishedPosts(offset, limit int) (Posts, error)

func (Posts) AppendPosts

func (p Posts) AppendPosts(posts Posts)

func (Posts) Get

func (p Posts) Get(i int) *Post

Get returns the Post at the given index.

func (*Posts) GetAllPostList

func (p *Posts) GetAllPostList(isPage bool, onlyPublished bool, orderBy string) error

GetAllPostList gets all the posts, with the options to get only pages, or only published posts. It is also possible to order the posts, with the order by string being one of six options:

"created_at"
"created_at DESC"
"updated_at"
"updated_at DESC"
"published_at"
"published_at DESC"

func (*Posts) GetAllPostsByTag

func (p *Posts) GetAllPostsByTag(tagId int64) error

GetAllPostsByTag gets all the Posts with the associated Tag.

func (*Posts) GetPostList

func (posts *Posts) GetPostList(page, size int64, isPage bool, onlyPublished bool, orderBy string) (*utils.Pager, error)

GetPostList returns a new pager based on all the posts in the DB.

func (*Posts) GetPostsByTag

func (p *Posts) GetPostsByTag(tagId, page, size int64, onlyPublished bool) (*utils.Pager, error)

GetPostsByTag returns a new pager based all the Posts associated with a Tag.

func (Posts) Len

func (p Posts) Len() int

Len returns the amount of "Post"s.

type Row

type Row interface {
	Scan(dest ...interface{}) error
}

A Row contains data that can be Scanned into a variable.

type Setting

type Setting struct {
	Id        int        `meddler:"id,pk"`
	Key       string     `meddler:"key"`
	Value     string     `meddler:"value"`
	Type      string     `meddler:"type"` // general, content, navigation, custom
	CreatedAt *time.Time `meddler:"created_at"`
	CreatedBy int64      `meddler:"created_by"`
	UpdatedAt *time.Time `meddler:"updated_at"`
	UpdatedBy int64      `meddler:"updated_by"`
}

A Setting is the data type that stores the blog's configuration options. It is essentially a key-value store for settings, along with a type to help specify the specific type of setting. A type can be either

general        site-wide general settings
content        related to showing content
navigation     site navigation settings
custom         custom settings

func NewSetting

func NewSetting(k, v, t string) *Setting

NewSetting returns a new setting from the given key-value pair.

func (*Setting) GetSetting

func (setting *Setting) GetSetting() error

GetSetting checks if a setting exists in the DB.

func (*Setting) Save

func (setting *Setting) Save() error

Save saves the setting to the DB.

type Settings

type Settings []*Setting

Settings a slice of all "Setting"s

func GetCustomSettings

func GetCustomSettings() *Settings

GetCustomSettings returns all custom settings.

func GetSettingsByType

func GetSettingsByType(t string) *Settings

GetSettingsByType returns all settings of the given type, where the setting key can be one of "general", "content", "navigation", or "custom".

type Statis

type Statis struct {
	Comments int64
	Posts    int64
	Pages    int64
	Files    int
	Version  int
	Sessions int
}

A Statis hold info about the site stats, including things like number of comments, posts, pages, etc.

func NewStatis

func NewStatis(app *golf.Application) *Statis

NewStatis returns a new Statis, pulling most info from the DB. The application argumen is required however to determine the number of active sessions.

type Tag

type Tag struct {
	Id        int64      `meddler:"id,pk"`
	Name      string     `meddler:"name"`
	Slug      string     `meddler:"slug"`
	Hidden    bool       `meddler:"hidden"`
	CreatedAt *time.Time `meddler:"created_at"`
	CreatedBy int64      `meddler:"created_by"`
	UpdatedAt *time.Time `meddler:"updated_at"`
	UpdatedBy int64      `meddler:"updated_by"`
}

A Tag is a keyword associated with a post.

func GenerateTagsFromCommaString

func GenerateTagsFromCommaString(input string) []*Tag

GenerateTagsFromCommaString returns a slice of "Tag"s from the given input. The input should be a comma-seperated list of tags, like

"news,tech,outdoors"

func NewTag

func NewTag(name, slug string) *Tag

NewTag creates a new Tag, with CreatedAt being set to the current time.

func (*Tag) GetTag

func (tag *Tag) GetTag() error

GetTag finds any data for the tag in the DB.

func (*Tag) GetTagBySlug

func (tag *Tag) GetTagBySlug() error

GetTagBySlug finds the tag based on the Tag's slug value.

func (*Tag) Insert

func (t *Tag) Insert() error

Insert inserts the tag into the DB.

func (*Tag) Save

func (t *Tag) Save() error

Save saves a Tag to the DB.

func (*Tag) Update

func (t *Tag) Update() error

Update updates an existing tag in the DB.

func (*Tag) Url

func (t *Tag) Url() string

Url returns the URL of the given slug.

type Tags

type Tags []*Tag

Tags are a slice of "Tag"s

func (Tags) Get

func (t Tags) Get(i int) *Tag

Get returns a Tag at the given index.

func (Tags) GetAll

func (t Tags) GetAll() []*Tag

GetAll returns a slice of every Tag.

func (*Tags) GetAllTags

func (tags *Tags) GetAllTags() error

GetAllTags gets all the tags in the DB.

func (*Tags) GetTagsByPostId

func (tags *Tags) GetTagsByPostId(postId int64) error

GetTagsByPostId finds all the tags with the give PostID

func (Tags) Len

func (t Tags) Len() int

Len returns the amount of "Tag"s in the Tags slice.

type Token

type Token struct {
	Id        int64      `meddler:"id,pk"`
	Value     string     `meddler:"value"`
	UserId    int64      `meddler:"user_id"`
	CreatedAt *time.Time `meddler:"created_at"`
	ExpiredAt *time.Time `meddler:"expired_at"`
}

A Token is used to associate a user with a session.

func NewToken

func NewToken(u *User, ctx *golf.Context, expire int64) *Token

NewToken creates a new token from the given user. Expire is the amount of time in seconds until expiry.

func (*Token) GetTokenByValue

func (t *Token) GetTokenByValue() error

GetTokenByValue gets a token from the DB based on it's value.

func (*Token) IsValid

func (t *Token) IsValid() bool

IsValid checks whether or not the token is valid.

func (*Token) Save

func (t *Token) Save() error

Save saves a token in the DB.

type User

type User struct {
	Id             int64      `meddler:"id,pk"`
	Name           string     `meddler:"name"`
	Slug           string     `meddler:"slug"`
	HashedPassword string     `meddler:"password"`
	Email          string     `meddler:"email"`
	Image          string     `meddler:"image"`    // NULL
	Cover          string     `meddler:"cover"`    // NULL
	Bio            string     `meddler:"bio"`      // NULL
	Website        string     `meddler:"website"`  // NULL
	Location       string     `meddler:"location"` // NULL
	Accessibility  string     `meddler:"accessibility"`
	Status         string     `meddler:"status"`
	Language       string     `meddler:"language"`
	Lastlogin      *time.Time `meddler:"last_login"`
	CreatedAt      *time.Time `meddler:"created_at"`
	CreatedBy      int        `meddler:"created_by"`
	UpdatedAt      *time.Time `meddler:"updated_at"`
	UpdatedBy      int        `meddler:"updated_by"`
	Role           int        `meddler:"-"` //1 = Administrator, 2 = Editor, 3 = Author, 4 = Owner
}

A User is a user on the site.

func NewUser

func NewUser(email, name string) *User

NewUser creates a new user from the given email and name, with the CreatedAt and UpdatedAt fields set to the current time.

func (*User) Avatar

func (u *User) Avatar() string

Avatar returns the Gravatar of the given user, with the Gravatar being 150px by 150px.

func (*User) ChangePassword

func (u *User) ChangePassword(password string) error

ChangePassword changes the password for the given user.

func (*User) CheckPassword

func (u *User) CheckPassword(password string) bool

CheckPassword checks to see if the given password matches the hashed password for the given user, returning true if it's a match.

func (*User) Create

func (u *User) Create(password string) error

Create saves a user in the DB with the given password, first hashing and salting that password via bcrypt.

func (*User) GetUserByEmail

func (u *User) GetUserByEmail() error

GetUserByEmail finds the user by email in the DB.

func (*User) GetUserById

func (u *User) GetUserById() error

GetUserById finds the user by ID in the DB.

func (*User) GetUserByName

func (u *User) GetUserByName() error

GetUserByName finds the user by name in the DB.

func (*User) GetUserBySlug

func (u *User) GetUserBySlug() error

GetUserBySlug finds the user by their slug in the DB.

func (*User) Insert

func (u *User) Insert() error

Insert inserts the user into the DB.

func (*User) Save

func (u *User) Save() error

Save saves a user to the DB.

func (*User) Update

func (u *User) Update() error

Update updates an existing user in the DB.

func (User) UserEmailExist

func (u User) UserEmailExist() bool

UserEmailExist checks to see if the given User's email exists.

Jump to

Keyboard shortcuts

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