plugin

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SearchContentStatusAvailable = 1
	SearchContentStatusDeleted   = 10
)

Variables

View Source
var (
	CallAgent, _ = MakePlugin[Agent](true)
)
View Source
var (
	// CallBase is a function that calls all registered base plugins
	CallBase, _ = MakePlugin[Base](true)
)
View Source
var (
	// CallCache is a function that calls all registered cache
	CallCache, _ = MakePlugin[Cache](false)
)
View Source
var (
	// CallConfig is a function that calls all registered config plugins
	CallConfig, _ = MakePlugin[Config](true)
)
View Source
var (
	// CallConnector is a function that calls all registered connectors
	CallConnector, _ = MakePlugin[Connector](false)
)
View Source
var (
	// CallFilter is a function that calls all registered parsers
	CallFilter, _ = MakePlugin[Filter](false)
)
View Source
var (
	// CallParser is a function that calls all registered parsers
	CallParser, _ = MakePlugin[Parser](false)
)
View Source
var (
	// CallUserCenter is a function that calls all registered parsers
	CallSearch, _ = MakePlugin[Search](false)
)
View Source
var (
	// CallStorage is a function that calls all registered storage
	CallStorage, _ = MakePlugin[Storage](false)
)
View Source
var (
	// CallUserCenter is a function that calls all registered parsers
	CallUserCenter, _ = MakePlugin[UserCenter](false)
)
View Source
var (
	DefaultFileTypeCheckMapping = map[UploadSource]map[string]bool{
		UserAvatar: {
			".jpg":  true,
			".jpeg": true,
			".png":  true,
			".webp": true,
		},
		UserPost: {
			".jpg":  true,
			".jpeg": true,
			".png":  true,
			".gif":  true,
			".webp": true,
		},
		AdminBranding: {
			".jpg":  true,
			".jpeg": true,
			".png":  true,
			".ico":  true,
		},
	}
)
View Source
var StatusManager = statusManager{
	// contains filtered or unexported fields
}

StatusManager is a manager that manages the status of plugins Init Plugins: json.Unmarshal([]byte(`{"plugin1": true, "plugin2": false}`), &plugin.StatusManager) Dump Status: json.Marshal(plugin.StatusManager)

Functions

func MakePlugin

func MakePlugin[T Base](super bool) (CallFn[T], RegisterFn[T])

MakePlugin creates a plugin caller and register stack manager The parameter super presents if the plugin can be disabled. It returns a register function and a caller function The register function is used to register a plugin, it will be called in the plugin's init function The caller function is used to call all registered plugins

func RankAgentEnabled

func RankAgentEnabled() (enabled bool)

func Register

func Register(p Base)

Register registers a plugin

func RegisterGetSiteURLFunc

func RegisterGetSiteURLFunc(fn func() string)

RegisterGetSiteURLFunc Register a function to get the site url.

func SiteURL

func SiteURL() string

SiteURL The site url is the domain address of the current site. e.g. http://localhost:8080 When some Agent plugins want to redirect to the origin site, it can use this function to get the site url.

func Translate

func Translate(ctx *GinContext, key string) string

Translate translates the key to the current language of the context

func UserCenterEnabled

func UserCenterEnabled() (enabled bool)

Types

type Agent

type Agent interface {
	Base
	RegisterUnAuthRouter(r *gin.RouterGroup)
	RegisterAuthUserRouter(r *gin.RouterGroup)
	RegisterAuthAdminRouter(r *gin.RouterGroup)
}

type Base

type Base interface {
	// Info returns the plugin information
	Info() Info
}

Base is the base plugin

type Cache

type Cache interface {
	Base

	GetString(ctx context.Context, key string) (data string, exist bool, err error)
	SetString(ctx context.Context, key, value string, ttl time.Duration) (err error)
	GetInt64(ctx context.Context, key string) (data int64, exist bool, err error)
	SetInt64(ctx context.Context, key string, value int64, ttl time.Duration) (err error)
	Increase(ctx context.Context, key string, value int64) (data int64, err error)
	Decrease(ctx context.Context, key string, value int64) (data int64, err error)
	Del(ctx context.Context, key string) (err error)
	Flush(ctx context.Context) (err error)
}

type CallFn

type CallFn[T Base] func(fn Caller[T]) error

type Caller

type Caller[T Base] func(p T) error

type Config

type Config interface {
	Base

	// ConfigFields returns the list of config fields
	ConfigFields() []ConfigField

	// ConfigReceiver receives the config data, it calls when the config is saved or initialized.
	// We recommend to unmarshal the data to a struct, and then use the struct to do something.
	// The config is encoded in JSON format.
	// It depends on the definition of ConfigFields.
	ConfigReceiver(config []byte) error
}

type ConfigField

type ConfigField struct {
	Name        string               `json:"name"`
	Type        ConfigType           `json:"type"`
	Title       Translator           `json:"title"`
	Description Translator           `json:"description"`
	Required    bool                 `json:"required"`
	Value       any                  `json:"value"`
	UIOptions   ConfigFieldUIOptions `json:"ui_options"`
	Options     []ConfigFieldOption  `json:"options,omitempty"`
}

type ConfigFieldOption

type ConfigFieldOption struct {
	Label Translator `json:"label"`
	Value string     `json:"value"`
}

type ConfigFieldUIOptions

type ConfigFieldUIOptions struct {
	Placeholder Translator      `json:"placeholder,omitempty"`
	Rows        string          `json:"rows,omitempty"`
	InputType   InputType       `json:"input_type,omitempty"`
	Label       Translator      `json:"label,omitempty"`
	Action      *UIOptionAction `json:"action,omitempty"`
	Variant     string          `json:"variant,omitempty"`
	Text        Translator      `json:"text,omitempty"`
}

type ConfigType

type ConfigType string
const (
	ConfigTypeInput    ConfigType = "input"
	ConfigTypeTextarea ConfigType = "textarea"
	ConfigTypeCheckbox ConfigType = "checkbox"
	ConfigTypeRadio    ConfigType = "radio"
	ConfigTypeSelect   ConfigType = "select"
	ConfigTypeUpload   ConfigType = "upload"
	ConfigTypeTimezone ConfigType = "timezone"
	ConfigTypeSwitch   ConfigType = "switch"
	ConfigTypeButton   ConfigType = "button"
)

type Connector

type Connector interface {
	Base

	// ConnectorLogoSVG presents the logo in svg format
	ConnectorLogoSVG() string

	// ConnectorName presents the name of the connector
	// e.g. Facebook, Twitter, Instagram
	ConnectorName() Translator

	// ConnectorSlugName presents the slug name of the connector
	// Please use lowercase and hyphen as the separator
	// e.g. facebook, twitter, instagram
	ConnectorSlugName() string

	// ConnectorSender presents the sender of the connector
	// It handles the start endpoint of the connector
	// receiverURL is the whole URL of the receiver
	ConnectorSender(ctx *GinContext, receiverURL string) (redirectURL string)

	// ConnectorReceiver presents the receiver of the connector
	// It handles the callback endpoint of the connector, and returns the
	ConnectorReceiver(ctx *GinContext, receiverURL string) (userInfo ExternalLoginUserInfo, err error)
}

type ControlCenter

type ControlCenter struct {
	Name  string `json:"name"`
	Label string `json:"label"`
	Url   string `json:"url"`
}

type ExternalLoginUserInfo

type ExternalLoginUserInfo struct {
	// required. The unique user ID provided by the third-party login
	ExternalID string
	// optional. This name is used preferentially during registration
	DisplayName string
	// optional. This username is used preferentially during registration
	Username string
	// optional. If email exist will bind the existing user
	// IMPORTANT: The email must have been verified. If the plugin can't guarantee the email is verified, please leave it empty.
	Email string
	// optional. The avatar URL provided by the third-party login platform
	Avatar string
	// optional. The original user information provided by the third-party login platform
	MetaInfo string
}

ExternalLoginUserInfo external login user info

type Filter

type Filter interface {
	Base
	FilterText(text string) (err error)
}

type GinContext

type GinContext = gin.Context

GinContext is a wrapper of gin.Context We export it to make it easy to use in plugins

type Info

type Info struct {
	Name        Translator
	SlugName    string
	Description Translator
	Author      string
	Version     string
	Link        string
}

Info presents the plugin information

type InputType

type InputType string
const (
	InputTypeText     InputType = "text"
	InputTypeColor    InputType = "color"
	InputTypeDate     InputType = "date"
	InputTypeDatetime InputType = "datetime-local"
	InputTypeEmail    InputType = "email"
	InputTypeMonth    InputType = "month"
	InputTypeNumber   InputType = "number"
	InputTypePassword InputType = "password"
	InputTypeRange    InputType = "range"
	InputTypeSearch   InputType = "search"
	InputTypeTel      InputType = "tel"
	InputTypeTime     InputType = "time"
	InputTypeUrl      InputType = "url"
	InputTypeWeek     InputType = "week"
)

type LoadingAction

type LoadingAction struct {
	Text  Translator        `json:"text"`
	State LoadingActionType `json:"state"`
}

type LoadingActionType

type LoadingActionType string
const (
	LoadingActionStateNone     LoadingActionType = "none"
	LoadingActionStatePending  LoadingActionType = "pending"
	LoadingActionStateComplete LoadingActionType = "completed"
)

type OnCompleteAction

type OnCompleteAction struct {
	ToastReturnMessage bool `json:"toast_return_message"`
	RefreshFormConfig  bool `json:"refresh_form_config"`
}

type Parser

type Parser interface {
	Base
	Parse(text string) (string, error)
}

type PersonalBranding

type PersonalBranding struct {
	Icon  string `json:"icon"`
	Name  string `json:"name"`
	Label string `json:"label"`
	Url   string `json:"url"`
}

type RegisterFn

type RegisterFn[T Base] func(p T)
type Search interface {
	Base
	Description() SearchDesc
	RegisterSyncer(ctx context.Context, syncer SearchSyncer)
	SearchContents(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
	SearchQuestions(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
	SearchAnswers(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
	UpdateContent(ctx context.Context, content *SearchContent) (err error)
	DeleteContent(ctx context.Context, objectID string) (err error)
}

type SearchAcceptedCond added in v1.1.1

type SearchAcceptedCond int
const (
	AcceptedCondAll SearchAcceptedCond = iota
	AcceptedCondTrue
	AcceptedCondFalse
)

type SearchBasicCond added in v1.1.1

type SearchBasicCond struct {
	// From zero-based page number
	Page int
	// Page size
	PageSize int

	// The keywords for search.
	Words []string
	// TagIDs is a list of tag IDs.
	TagIDs []string
	// The object's owner user ID.
	UserID string
	// The order of the search result.
	Order SearchOrderCond

	// Weathers the question is accepted or not. Only support search question.
	QuestionAccepted SearchAcceptedCond
	// Weathers the answer is accepted or not. Only support search answer.
	AnswerAccepted SearchAcceptedCond

	// Only support search answer.
	QuestionID string

	// greater than or equal to the number of votes.
	VoteAmount int
	// greater than or equal to the number of views.
	ViewAmount int
	// greater than or equal to the number of answers. Only support search question.
	AnswerAmount int
}

type SearchContent added in v1.1.1

type SearchContent struct {
	ObjectID    string              `json:"objectID"`
	Title       string              `json:"title"`
	Type        string              `json:"type"`
	Content     string              `json:"content"`
	Answers     int64               `json:"answers"`
	Status      SearchContentStatus `json:"status"`
	Tags        []string            `json:"tags"`
	QuestionID  string              `json:"questionID"`
	UserID      string              `json:"userID"`
	Views       int64               `json:"views"`
	Created     int64               `json:"created"`
	Active      int64               `json:"active"`
	Score       int64               `json:"score"`
	HasAccepted bool                `json:"hasAccepted"`
}

type SearchContentStatus added in v1.1.1

type SearchContentStatus int

type SearchDesc added in v1.1.2

type SearchDesc struct {
	// A svg icon it wil be display in search result page. optional
	Icon string `json:"icon"`
	// The link address of the search engine. optional
	Link string `json:"link"`
}

type SearchOrderCond added in v1.1.1

type SearchOrderCond string
const (
	SearchNewestOrder    SearchOrderCond = "newest"
	SearchActiveOrder    SearchOrderCond = "active"
	SearchScoreOrder     SearchOrderCond = "score"
	SearchRelevanceOrder SearchOrderCond = "relevance"
)

type SearchResult added in v1.1.1

type SearchResult struct {
	// ID content ID
	ID string
	// Type content type, example: "answer", "question"
	Type string
}

type SearchSyncer added in v1.1.2

type SearchSyncer interface {
	GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*SearchContent, err error)
	GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*SearchContent, err error)
}

type SettingInfo

type SettingInfo struct {
	ProfileSettingRedirectURL string `json:"profile_setting_redirect_url"`
	AccountSettingRedirectURL string `json:"account_setting_redirect_url"`
}

type Stack

type Stack[T Base] struct {
	// contains filtered or unexported fields
}

type Storage

type Storage interface {
	Base

	// UploadFile uploads a file to storage.
	// The file is in the Form of the ctx and the key is "file"
	UploadFile(ctx *GinContext, source UploadSource) UploadFileResponse
}

type TranslateFn

type TranslateFn func(ctx *GinContext) string

TranslateFn presents a generator of translated string. We use it to delegate the translation work outside the plugin.

type Translator

type Translator struct {
	Fn TranslateFn
}

Translator contains a function that translates the key to the current language of the context

func MakeTranslator

func MakeTranslator(key string) Translator

MakeTranslator generates a translator from the key

func (Translator) Translate

func (t Translator) Translate(ctx *GinContext) string

Translate translates the key to the current language of the context

type UIOptionAction

type UIOptionAction struct {
	Url        string            `json:"url"`
	Method     string            `json:"method,omitempty"`
	Loading    *LoadingAction    `json:"loading,omitempty"`
	OnComplete *OnCompleteAction `json:"on_complete,omitempty"`
}

type UploadFileResponse

type UploadFileResponse struct {
	// FullURL is the URL that can be used to access the file
	FullURL string
	// OriginalError is the error returned by the storage plugin. It is used for debugging.
	OriginalError error
	// DisplayErrorMsg is the error message that will be displayed to the user.
	DisplayErrorMsg Translator
}

type UploadSource

type UploadSource string
const (
	UserAvatar    UploadSource = "user_avatar"
	UserPost      UploadSource = "user_post"
	AdminBranding UploadSource = "admin_branding"
)

type UserCenter

type UserCenter interface {
	Base
	// Description returns the description of the user center, including the name, icon, url, etc.
	Description() UserCenterDesc
	// ControlCenterItems returns the items that will be displayed in the control center
	ControlCenterItems() []ControlCenter
	// LoginCallback is called when the user center login callback is called
	LoginCallback(ctx *GinContext) (userInfo *UserCenterBasicUserInfo, err error)
	// SignUpCallback is called when the user center sign up callback is called
	SignUpCallback(ctx *GinContext) (userInfo *UserCenterBasicUserInfo, err error)
	// UserInfo returns the user information
	UserInfo(externalID string) (userInfo *UserCenterBasicUserInfo, err error)
	// UserStatus returns the latest user status
	UserStatus(externalID string) (userStatus UserStatus)
	// UserList returns the user list information
	UserList(externalIDs []string) (userInfo []*UserCenterBasicUserInfo, err error)
	// UserSettings returns the user settings
	UserSettings(externalID string) (userSettings *SettingInfo, err error)
	// PersonalBranding returns the personal branding information
	PersonalBranding(externalID string) (branding []*PersonalBranding)
	// AfterLogin is called after the user logs in
	AfterLogin(externalID, accessToken string)
}

func GetUserCenter

func GetUserCenter() (uc UserCenter, ok bool)

type UserCenterBasicUserInfo

type UserCenterBasicUserInfo struct {
	ExternalID  string     `json:"external_id"`
	Username    string     `json:"username"`
	DisplayName string     `json:"display_name"`
	Email       string     `json:"email"`
	Rank        int        `json:"rank"`
	Avatar      string     `json:"avatar"`
	Mobile      string     `json:"mobile"`
	Bio         string     `json:"bio"`
	Status      UserStatus `json:"status"`
}

type UserCenterDesc

type UserCenterDesc struct {
	Name                      string     `json:"name"`
	DisplayName               Translator `json:"display_name"`
	Icon                      string     `json:"icon"`
	Url                       string     `json:"url"`
	LoginRedirectURL          string     `json:"login_redirect_url"`
	SignUpRedirectURL         string     `json:"sign_up_redirect_url"`
	RankAgentEnabled          bool       `json:"rank_agent_enabled"`
	UserStatusAgentEnabled    bool       `json:"user_status_agent_enabled"`
	UserRoleAgentEnabled      bool       `json:"user_role_agent_enabled"`
	MustAuthEmailEnabled      bool       `json:"must_auth_email_enabled"`
	EnabledOriginalUserSystem bool       `json:"enabled_original_user_system"`
}

type UserStatus

type UserStatus int
const (
	UserStatusAvailable UserStatus = 1
	UserStatusSuspended UserStatus = 9
	UserStatusDeleted   UserStatus = 10
)

Jump to

Keyboard shortcuts

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