Documentation ¶
Index ¶
- Constants
- Variables
- type Comment
- type LoginOutput
- type Notification
- type Post
- type Service
- func (s *Service) AuthUser(ctx context.Context) (User, error)
- func (s *Service) AuthUserID(token string) (int64, error)
- func (s *Service) Comments(ctx context.Context, postID int64, last int, before int64) ([]Comment, error)
- func (s *Service) CreateComment(ctx context.Context, postID int64, content string) (Comment, error)
- func (s *Service) CreatePost(ctx context.Context, content string, spoilerOf *string, nsfw bool) (TimelineItem, error)
- func (s *Service) CreateUser(ctx context.Context, email, username string) error
- func (s *Service) Followees(ctx context.Context, username string, first int, after string) ([]UserProfile, error)
- func (s *Service) Followers(ctx context.Context, username string, first int, after string) ([]UserProfile, error)
- func (s *Service) Login(ctx context.Context, email string) (LoginOutput, error)
- func (s *Service) MarkNotificationAsRead(ctx context.Context, notificationID int64) error
- func (s *Service) MarkNotificationsAsRead(ctx context.Context) error
- func (s *Service) Notifications(ctx context.Context, last int, before int64) ([]Notification, error)
- func (s *Service) Post(ctx context.Context, postID int64) (Post, error)
- func (s *Service) Posts(ctx context.Context, username string, last int, before int64) ([]Post, error)
- func (s *Service) Timeline(ctx context.Context, last int, before int64) ([]TimelineItem, error)
- func (s *Service) ToggleCommentLike(ctx context.Context, commentID int64) (ToggleLikeOutput, error)
- func (s *Service) ToggleFollow(ctx context.Context, username string) (ToggleFollowOutput, error)
- func (s *Service) TogglePostLike(ctx context.Context, postID int64) (ToggleLikeOutput, error)
- func (s *Service) UpdateAvatar(ctx context.Context, r io.Reader) (string, error)
- func (s *Service) User(ctx context.Context, username string) (UserProfile, error)
- func (s *Service) Users(ctx context.Context, search string, first int, after string) ([]UserProfile, error)
- type TimelineItem
- type ToggleFollowOutput
- type ToggleLikeOutput
- type User
- type UserProfile
Constants ¶
const ( // TokenLifeSpan until tokens are valid TokenLifeSpan = time.Hour * 24 * 14 // KeyAuthUserID to use in context KeyAuthUserID key = "auth_user_id" )
const MaxAvatarBytes = 5 << 20 // 5MB
MaxAvatarBytes maximum file upload size
Variables ¶
var ( // ErrInvalidContent used when the content is invalid. ErrInvalidContent = errors.New("invalid content") //ErrInvalidSpoiler used for invalid spoiler title. ErrInvalidSpoiler = errors.New("invalid spoiler") // ErrPostNotFound used for post not found. ErrPostNotFound = errors.New("post not found") )
var ( // ErrUserNotFound used when the user wasn't found on the db. ErrUserNotFound = errors.New("user not found") // ErrInvalidEmail used when the email is invalid. ErrInvalidEmail = errors.New("invalid email") // ErrInvalidUsername used when the username is invalid. ErrInvalidUsername = errors.New("invalid username") // ErrEmailTaken used when there is already an user registered with that email ErrEmailTaken = errors.New("email already exists") // ErrUsernameTaken used when there is already an user registered with that username ErrUsernameTaken = errors.New("username already exists") // ErrForbiddenFollow used when you try to follow yourself ErrForbiddenFollow = errors.New("cannot follow yourself") // ErrUnsupportedAvatarFormat used when the avatar file extension is invalid ErrUnsupportedAvatarFormat = errors.New("only png and jpeg allowed as avatar") )
var ErrCommentNotFound = errors.New("comment not found")
ErrCommentNotFound denotes that the comment was not found
var ( // ErrUnauthenticated used when there is no user authenticated in the context. ErrUnauthenticated = errors.New("unauthenticated") )
Functions ¶
This section is empty.
Types ¶
type Comment ¶
type Comment struct { ID int64 `json:"id"` UserID int64 `json:"-"` PostID int64 `json:""` Content string `json:"content"` LikesCount int `json:"likes_count"` CreatedAt time.Time `json:"created_at"` User *User `json:"user,omitempty"` Mine bool `json:"mine"` Liked bool `json:"liked"` }
Comment represents the comment on database
type LoginOutput ¶
type LoginOutput struct { Token string `json:"token,omitempty"` ExpiresAt time.Time `json:"expires_at,omitempty"` AuthUser User `json:"auth_user,omitempty"` }
LoginOutput response
type Notification ¶
type Notification struct { ID int64 `json:"id"` UserID int64 `json:"-"` Actors []string `json:"actors"` Type string `json:"type"` PostID *int64 `json:post_id, ommitempty` Read bool `json:"read"` IssuedAt time.Time `json:"issued_at"` }
Notification model
type Post ¶
type Post struct { ID int64 `json:"id"` UserID int64 `json:"-"` Content string `json:"content"` SpoilerOf *string `json:"spoiler_of"` NSFW bool `json:"nsfw"` LikesCount int `json:"likes_count"` CommentsCount int `json:"comments_count"` CreatedAt time.Time `json:"created_at"` User *User `json:"user,omitempty"` Mine bool `json:"mine"` Liked bool `json:"liked"` Subscribed bool `json:"subscribed"` }
Post Model
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service contains the core logic. You can use it to back REST, GraphQL or RPC API
func (*Service) AuthUserID ¶
AuthUserID retrieves the user ID from the token
func (*Service) Comments ¶
func (s *Service) Comments(ctx context.Context, postID int64, last int, before int64) ([]Comment, error)
Comments from a post in descending order with backward pagination
func (*Service) CreateComment ¶
CreateComment creates a comment
func (*Service) CreatePost ¶
func (s *Service) CreatePost( ctx context.Context, content string, spoilerOf *string, nsfw bool, ) (TimelineItem, error)
CreatePost publishes a post to the user timeline and fan-ous it to his followers
func (*Service) CreateUser ¶
CreateUser inserts an user in the database
func (*Service) Followees ¶
func (s *Service) Followees(ctx context.Context, username string, first int, after string) ([]UserProfile, error)
Followees in ascending order with foward pagination and filtered by username.
func (*Service) Followers ¶
func (s *Service) Followers(ctx context.Context, username string, first int, after string) ([]UserProfile, error)
Followers in ascending order with foward pagination and filtered by username.
func (*Service) MarkNotificationAsRead ¶
MarkNotificationAsRead sets a notification from the authenticated user as read
func (*Service) MarkNotificationsAsRead ¶
MarkNotificationsAsRead sets all notifications from the authenticated user as read
func (*Service) Notifications ¶
func (s *Service) Notifications(ctx context.Context, last int, before int64) ([]Notification, error)
Notifications from the authenticated user in descending order with backward pagination
func (*Service) Posts ¶
func (s *Service) Posts( ctx context.Context, username string, last int, before int64, ) ([]Post, error)
Posts from a user in descending order and with backward pagination
func (*Service) ToggleCommentLike ¶
ToggleCommentLike -
func (*Service) ToggleFollow ¶
ToggleFollow between two users
func (*Service) TogglePostLike ¶
TogglePostLike toggles the post like
func (*Service) UpdateAvatar ¶
UpdateAvatar updates the avatar of an authenticated user
type TimelineItem ¶
type TimelineItem struct { ID int64 `json:"id"` UserID int64 `json:"-"` PostID int64 `json:"-"` Post Post `json:"post"` }
TimelineItem represents an item on user's timeline
type ToggleFollowOutput ¶
type ToggleFollowOutput struct { Following bool `json:"following"` FollowersCount int `json:"followers_count"` }
ToggleFollowOutput response
type ToggleLikeOutput ¶
ToggleLikeOutput represents the output for toggle like