posts

package
v0.0.0-...-6dfde0d Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2017 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package posts contains functionality to create, find, and update posts, comments and likes.

Index

Constants

View Source
const (
	APost    = "post"
	AComment = "comment"
	ALike    = "like"
	AUserTag = "usertag"
)
View Source
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

View Source
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 Action

type Action string

func (*Action) Scan

func (a *Action) Scan(src interface{}) error

func (Action) Value

func (a Action) Value() (driver.Value, error)

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

func (ParentType) Value

func (p ParentType) Value() (driver.Value, 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 GetByID

func GetByID(id string, userID string, db *gorm.DB) (p Post, err error)

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

func (p Post) CanDelete(userID string, db *gorm.DB) bool

CanDelete is a helper function to determine wheter a user should be able to delete a Post

func (Post) CanUpdate

func (p Post) CanUpdate(userID string) bool

CanUpdate is a helper function to determine wheter a user should be able to update a Post

func (Post) FormatCreated

func (p Post) FormatCreated() string

func (Post) GetAction

func (p Post) GetAction() string

func (Post) GetChildren

func (p Post) GetChildren(currentUserID string, db *gorm.DB) (c Posts, err error)

func (*Post) GetCommentCount

func (p *Post) GetCommentCount(db *gorm.DB) error

func (*Post) GetComments

func (p *Post) GetComments(userID string, db *gorm.DB) (cs Posts, err error)

func (Post) GetContent

func (p Post) GetContent() string

GetContent satisfies hashtags/Hashtagger

func (Post) GetID

func (p Post) GetID() string

GetID satisfies hashtags/Hashtagger

func (*Post) GetLikeByUserID

func (p *Post) GetLikeByUserID(userID string, db *gorm.DB) (l Post, err error)

func (*Post) GetLikeCount

func (p *Post) GetLikeCount(userID string, db *gorm.DB) error

func (Post) GetNotifType

func (p Post) GetNotifType() string

func (*Post) GetParent

func (p *Post) GetParent(currentUserID string, db *gorm.DB) (err error)

GetParent queries the database for the parent of a post

func (Post) GetType

func (p Post) GetType() string

GetType satisfies hashtags/Hashtagger

func (*Post) GetUser

func (p *Post) GetUser(db *gorm.DB) error

func (Post) GetUserID

func (p Post) GetUserID() string

GetUserID satisfies UserTagger interface

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 GetFeedByUserIDs(currentUserID string, userIDs []string, offset int, db *gorm.DB) (ps Posts, err error)

func GetFeedByUserIDsAfter

func GetFeedByUserIDsAfter(currentUserID string, userIDs []string, after time.Time, db *gorm.DB) (ps Posts, err error)

func ListByIDs

func ListByIDs(currentUserID string, ids []string, offset int, db *gorm.DB) (ps Posts, err error)

func ListByParent

func ListByParent(currentUserID string, parentType ParentType, parentID string, offset int, db *gorm.DB) (ps Posts, err error)

func (*Posts) CollectParents

func (ps *Posts) CollectParents(currentUserID string, db *gorm.DB) (err error)

CollectParents queries the database for the parent of a group of posts

func (*Posts) GetChildCountList

func (ps *Posts) GetChildCountList(userID string, db *gorm.DB) error

func (*Posts) GetRelations

func (ps *Posts) GetRelations(userID string, db *gorm.DB)

func (Posts) GetUserIDs

func (ps Posts) GetUserIDs() (userIDs []string)

GetUserIDs satisfies UserIDerSlice

func (*Posts) GetUsers

func (ps *Posts) GetUsers(db *gorm.DB) error

func (*Posts) Unique

func (ps *Posts) Unique()

Unique removes any post from the list that has its parent already in the list

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.

Jump to

Keyboard shortcuts

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