service

package
v0.0.0-...-5554f4f Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2020 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TokenLifeSpan until tokens are valid
	TokenLifeSpan = time.Hour * 24 * 14
	// KeyAuthUserID to use in context
	KeyAuthUserID key = "auth_user_id"
)
View Source
const MaxAvatarBytes = 5 << 20 // 5MB

MaxAvatarBytes maximum file upload size

Variables

View Source
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")
)
View Source
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")
)
View Source
var ErrCommentNotFound = errors.New("comment not found")

ErrCommentNotFound denotes that the comment was not found

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

func New(db *sql.DB, codec *branca.Branca, origin string) *Service

New Service implementation.

func (*Service) AuthUser

func (s *Service) AuthUser(ctx context.Context) (User, error)

AuthUser retrieves user from the context

func (*Service) AuthUserID

func (s *Service) AuthUserID(token string) (int64, error)

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

func (s *Service) CreateComment(ctx context.Context, postID int64, content string) (Comment, error)

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

func (s *Service) CreateUser(ctx context.Context, email, username string) error

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) Login

func (s *Service) Login(ctx context.Context, email string) (LoginOutput, error)

Login login the user

func (*Service) MarkNotificationAsRead

func (s *Service) MarkNotificationAsRead(ctx context.Context, notificationID int64) error

MarkNotificationAsRead sets a notification from the authenticated user as read

func (*Service) MarkNotificationsAsRead

func (s *Service) MarkNotificationsAsRead(ctx context.Context) error

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) Post

func (s *Service) Post(ctx context.Context, postID int64) (Post, error)

Post retrieves the post from a postID

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) Timeline

func (s *Service) Timeline(
	ctx context.Context,
	last int,
	before int64,
) ([]TimelineItem, error)

Timeline retrieves the user's timeline

func (*Service) ToggleCommentLike

func (s *Service) ToggleCommentLike(ctx context.Context, commentID int64) (ToggleLikeOutput, error)

ToggleCommentLike -

func (*Service) ToggleFollow

func (s *Service) ToggleFollow(ctx context.Context, username string) (ToggleFollowOutput, error)

ToggleFollow between two users

func (*Service) TogglePostLike

func (s *Service) TogglePostLike(ctx context.Context, postID int64) (ToggleLikeOutput, error)

TogglePostLike toggles the post like

func (*Service) UpdateAvatar

func (s *Service) UpdateAvatar(ctx context.Context, r io.Reader) (string, error)

UpdateAvatar updates the avatar of an authenticated user

func (*Service) User

func (s *Service) User(ctx context.Context, username string) (UserProfile, error)

User selects the user from the database with the given username

func (*Service) Users

func (s *Service) Users(ctx context.Context, search string, first int, after string) ([]UserProfile, error)

Users in ascending order with foward pagination and filtered by username.

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

type ToggleLikeOutput struct {
	Liked      bool `json:"liked"`
	LikesCount int  `json:"likes_count"`
}

ToggleLikeOutput represents the output for toggle like

type User

type User struct {
	ID        int64   `json:"id,omitempty"`
	Username  string  `json:"username"`
	AvatarURL *string `json:"avatar_url"`
}

User model

type UserProfile

type UserProfile struct {
	User
	Email          string `json:"email,omitempty"`
	FollowersCount int    `json:"followers_count"`
	FolloweesCount int    `json:"followees_count"`
	Me             bool   `json:"me"`
	Following      bool   `json:"following"`
	Followeed      bool   `json:"followed"`
}

UserProfile model

Jump to

Keyboard shortcuts

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