Documentation ¶
Overview ¶
Package posts contains functionality to create, find, and update posts, comments and likes.
Index ¶
- Constants
- Variables
- type Action
- type CreatorBinding
- type ParentType
- type Parenter
- type Post
- func (p Post) CanDelete(userID string, db *gorm.DB) bool
- func (p Post) CanUpdate(userID string) bool
- func (p Post) FormatCreated() string
- func (p Post) GetAction() string
- func (p Post) GetChildren(currentUserID string, db *gorm.DB) (c Posts, err error)
- func (p *Post) GetCommentCount(db *gorm.DB) error
- func (p *Post) GetComments(userID string, db *gorm.DB) (cs Posts, err error)
- func (p Post) GetContent() string
- func (p Post) GetID() string
- func (p *Post) GetLikeByUserID(userID string, db *gorm.DB) (l Post, err error)
- func (p *Post) GetLikeCount(userID string, db *gorm.DB) error
- func (p Post) GetNotifType() string
- func (p *Post) GetParent(currentUserID string, db *gorm.DB) (err error)
- func (p Post) GetType() string
- func (p *Post) GetUser(db *gorm.DB) error
- func (p Post) GetUserID() string
- func (p *Post) PostParentType() ParentType
- func (p *Post) Update(userID string, b UpdaterBinding) models.ValidationErrors
- func (p Post) Validate() models.ValidationErrors
- type Posts
- func GetFeedByUserIDs(currentUserID string, userIDs []string, offset int, db *gorm.DB) (ps Posts, err error)
- func GetFeedByUserIDsAfter(currentUserID string, userIDs []string, after time.Time, db *gorm.DB) (ps Posts, err error)
- func ListByIDs(currentUserID string, ids []string, offset int, db *gorm.DB) (ps Posts, err error)
- func ListByParent(currentUserID string, parentType ParentType, parentID string, offset int, ...) (ps Posts, err error)
- func (ps *Posts) CollectParents(currentUserID string, db *gorm.DB) (err error)
- func (ps *Posts) GetChildCountList(userID string, db *gorm.DB) error
- func (ps *Posts) GetRelations(userID string, db *gorm.DB)
- func (ps Posts) GetUserIDs() (userIDs []string)
- func (ps *Posts) GetUsers(db *gorm.DB) error
- func (ps *Posts) Unique()
- type UpdaterBinding
Constants ¶
const ( APost = "post" AComment = "comment" ALike = "like" AUserTag = "usertag" )
const ( // PTNoType is a post to a feed PTNoType ParentType = "" // PTPost is a comment or like to a post PTPost = "post" // PTComment is a like of a comment PTComment = "comment" // PTEvent is a post to an event PTEvent = "event" // PTGroup is a post to a group PTGroup = "group" )
Variables ¶
var ( ErrNoUpdate = errors.New("Cannot update this post") ErrAction = errors.New("Unknown Post.Action") ErrParentType = errors.New("Post cannot be a child to this type") ErrLikeParent = errors.New("Invalid parent type for like post") ErrLikeBody = errors.New("Post with Like action cannot have a body") ErrMustHaveParent = errors.New("Posts with Comment and Like actions must have a parent") ErrCommentParent = errors.New("Posts with Comment action can only be children of Post types") ErrScanAction = errors.New("Could not scan action") ErrScanParentType = errors.New("Could not scan parent type") ErrParentQuery = errors.New("Cannot get this post's parent in this way. This only works for comments and likes") ErrCannotUpdate = errors.New("You do not have the permissions to update this record") )
Functions ¶
This section is empty.
Types ¶
type CreatorBinding ¶
type CreatorBinding struct { ParentType string `form:"parent_type" json:"parent_type"` ParentID string `form:"parent_id" json:"parent_id"` Body string `form:"body" json:"body"` Action string `form:"action" json:"action" binding:"required"` }
CreatorBinding is a struct to use for binding JSON requests to a new Post.
type ParentType ¶
type ParentType string
func (*ParentType) Scan ¶
func (p *ParentType) Scan(src interface{}) error
type Parenter ¶
type Parenter interface {
PostParentType() ParentType
}
Parenter is an interface for objects that can be parents of posts
type Post ¶
type Post struct { ID string `json:"id" gorm:"primary_key" sql:"type:uuid;default:uuid_generate_v4()"` UserID string `json:"user_id" sql:"type:uuid"` ParentType ParentType `json:"parent_type"` ParentID sql.NullString `json:"parent_id" sql:"type:uuid;default:null"` Body string `json:"body"` Action Action `json:"action"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` User users.User `json:"user" sql:"-"` Parent interface{} `json:"parent" sql:"-"` LikeCount int `json:"like_count" sql:"-"` Liked bool `json:"liked" sql:"-"` CommentCount int `json:"child_count" sql:"-"` Attachments []attachments.Attachment `json:"attachments" sql:"-"` }
Post is the representation of posts, comments and likes. See ./types.go for more information About what kind of type a post is.
func New ¶
func New(userID string, b CreatorBinding) (p Post, errs models.ValidationErrors)
New uses a CreatorBinding to initialize a new Post and validate it. It does not save the Post to the database. This should always be used rather than creating a Post manually from user input.
func (Post) CanDelete ¶
CanDelete is a helper function to determine wheter a user should be able to delete a Post
func (Post) CanUpdate ¶
CanUpdate is a helper function to determine wheter a user should be able to update a Post
func (Post) FormatCreated ¶
func (Post) GetChildren ¶
func (*Post) GetComments ¶
func (*Post) GetLikeByUserID ¶
func (Post) GetNotifType ¶
func (*Post) PostParentType ¶
func (p *Post) PostParentType() ParentType
PostParentType satisfies the Parenter interface
func (*Post) Update ¶
func (p *Post) Update(userID string, b UpdaterBinding) models.ValidationErrors
Update uses am UpdaterBinding to update an existing Post and validate it. It does not save the Post to the databse. This should always be used rather than updating a Post manually from user input.
func (Post) Validate ¶
func (p Post) Validate() models.ValidationErrors
Validate validates a Post based on its properties
type Posts ¶
type Posts []Post
Posts is a list type of post
func GetFeedByUserIDs ¶
func GetFeedByUserIDsAfter ¶
func ListByParent ¶
func (*Posts) CollectParents ¶
CollectParents queries the database for the parent of a group of posts
func (*Posts) GetChildCountList ¶
func (Posts) GetUserIDs ¶
GetUserIDs satisfies UserIDerSlice
type UpdaterBinding ¶
type UpdaterBinding struct {
Body string `json:"body" binding:"required"`
}
UpdaterBinding is a struct to use for binding JSON requests to update a Post.