common

package
v0.0.0-...-97f72a7 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2019 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlagEmailVerified = 1 << iota
	FlagCountry2FA
)

These are the flags an user can have. Mostly settings or things like whether the user has verified their email address.

View Source
const (
	PrivilegeRead             = 1 << iota // used to be to fetch public data, such as user information etc. this is deprecated.
	PrivilegeReadConfidential             // (eventual) private messages, reports... of self
	PrivilegeWrite                        // change user information, write into confidential stuff...
	PrivilegeManageBadges                 // can change various users' badges.
	PrivilegeBetaKeys                     // can add, remove, upgrade/downgrade, make public beta keys.
	PrivilegeManageSettings               // maintainance, set registrations, global alerts, bancho settings
	PrivilegeViewUserAdvanced             // can see user email, and perhaps warnings in the future, basically.
	PrivilegeManageUser                   // can change user email, allowed status, userpage, rank, username...
	PrivilegeManageRoles                  // translates as admin, as they can basically assign roles to anyone, even themselves
	PrivilegeManageAPIKeys                // admin permission to manage user permission, not only self permissions. Only ever do this if you completely trust the application, because this essentially means to put the entire ripple database in the hands of a (potentially evil?) application.
	PrivilegeBlog                         // can do pretty much anything to the blog, and the documentation.
	PrivilegeAPIMeta                      // can do /meta API calls. basically means they can restart the API server.
	PrivilegeBeatmap                      // rank/unrank beatmaps. also BAT when implemented
	PrivilegeBancho                       // can log in to bancho and use the chat through the delta ws api
)

These are the various privileges a token can have.

Variables

View Source
var RavenClient *raven.Client

RavenClient is the raven client to which report errors happening. If nil, errors will just be fmt.Println'd

View Source
var Version string

Version is the git hash of the application. Do not edit. This is automatically set using -ldflags during build time.

Functions

func Err

func Err(c *fasthttp.RequestCtx, err error)

Err for peppy API calls

func GenericError

func GenericError(err error)

GenericError is just an error. Can't make a good description.

func In

func In(x, y, z int) int

In picks x if y < x, picks z if y > z, or if none of the previous conditions is satisfies, it simply picks y.

func InString

func InString(x int, y string, z, def int) int

InString takes y as a string, also allows for a default value should y be invalid as a number.

func Int

func Int(s string) int

Int converts s to an int. If s in an invalid int, it defaults to 0.

func Paginate

func Paginate(page, limit string, maxLimit int) string

Paginate creates an additional SQL LIMIT clause for paginating.

func RandomString

func RandomString(n int) string

RandomString generates a random string.

func SafeUsername

func SafeUsername(s string) string

SafeUsername makes a string lowercase and replaces all spaces with underscores.

func SanitiseString

func SanitiseString(s string) string

SanitiseString removes all control codes from a string.

func Sort

func Sort(md MethodData, config SortConfiguration) string

Sort allows the request to modify how the query is sorted.

func WSErr

func WSErr(err error)

WSErr is the error function for errors happening in the websockets.

Types

type CodeMessager

type CodeMessager interface {
	GetMessage() string
	GetCode() int
}

CodeMessager is something that has the Code() and Message() methods.

func SimpleResponse

func SimpleResponse(code int, message string) CodeMessager

SimpleResponse returns the most basic response.

type Conf

type Conf struct {
	DatabaseType           string `description:"At the moment, 'mysql' is the only supported database type."`
	DSN                    string `description:"The Data Source Name for the database. More: https://github.com/go-sql-driver/mysql#dsn-data-source-name"`
	ListenTo               string `description:"The IP/Port combination from which to take connections, e.g. :8080"`
	Unix                   bool   `description:"Bool indicating whether ListenTo is a UNIX socket or an address."`
	SentryDSN              string `description:"thing for sentry whatever"`
	HanayoKey              string
	BeatmapRequestsPerUser int
	RankQueueSize          int
	OsuAPIKey              string
	RedisAddr              string
	RedisPassword          string
	RedisDB                int
}

Conf is the configuration file data for the ripple API. Conf uses https://github.com/thehowl/conf

func GetConf

func GetConf() *Conf

GetConf returns the cachedConf.

func Load

func Load() (c Conf, halt bool)

Load creates a new Conf, using the data in the file "api.conf".

type MethodData

type MethodData struct {
	User  Token
	DB    *sqlx.DB
	Doggo *statsd.Client
	R     *redis.Client
	Ctx   *fasthttp.RequestCtx
}

MethodData is a struct containing the data passed over to an API method.

func (MethodData) ClientIP

func (md MethodData) ClientIP() string

ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.

func (MethodData) Err

func (md MethodData) Err(err error)

Err logs an error. If RavenClient is set, it will use the client to report the error to sentry, otherwise it will just write the error to stdout.

func (MethodData) HasQuery

func (md MethodData) HasQuery(q string) bool

HasQuery returns true if the parameter is encountered in the querystring. It returns true even if the parameter is "" (the case of ?param&etc=etc)

func (MethodData) ID

func (md MethodData) ID() int

ID retrieves the Token's owner user ID.

func (MethodData) IsBearer

func (md MethodData) IsBearer() bool

IsBearer tells whether the current token is a Bearer (oauth) token.

func (MethodData) Query

func (md MethodData) Query(q string) string

Query is shorthand for md.C.Query.

func (MethodData) Unmarshal

func (md MethodData) Unmarshal(into interface{}) error

Unmarshal unmarshals a request's JSON body into an interface.

type Privileges

type Privileges uint64

Privileges is a bitwise enum of the privileges of an user's API key.

func OAuthPrivileges

func OAuthPrivileges(scopes string) Privileges

OAuthPrivileges returns the equivalent in Privileges of a space-separated list of scopes.

func (Privileges) CanOnly

func (p Privileges) CanOnly(userPrivs UserPrivileges) Privileges

CanOnly removes any privilege that the user has requested to have, but cannot have due to their rank.

func (Privileges) String

func (p Privileges) String() string

type ResponseBase

type ResponseBase struct {
	Code    int    `json:"code"`
	Message string `json:"message,omitempty"`
}

ResponseBase is the data that is always returned with an API request.

func (ResponseBase) GetCode

func (r ResponseBase) GetCode() int

GetCode retrieves the response code.

func (ResponseBase) GetMessage

func (r ResponseBase) GetMessage() string

GetMessage retrieves the response message.

func (*ResponseBase) SetCode

func (r *ResponseBase) SetCode(i int)

SetCode changes the response code.

type SortConfiguration

type SortConfiguration struct {
	Allowed        []string // Allowed parameters
	Default        string
	DefaultSorting string // if empty, DESC
	Table          string
}

SortConfiguration is the configuration of Sort.

type Token

type Token struct {
	ID              int
	Value           string
	UserID          int
	TokenPrivileges Privileges
	UserPrivileges  UserPrivileges
}

Token is an API token.

func (Token) OnlyUserPublic

func (t Token) OnlyUserPublic(userManagerSeesEverything bool) string

OnlyUserPublic returns a string containing "(user.privileges & 1 = 1 OR users.id = <userID>)" if the user does not have the UserPrivilege AdminManageUsers, and returns "1" otherwise.

type UnixTimestamp

type UnixTimestamp time.Time

UnixTimestamp is simply a time.Time, but can be used to convert an unix timestamp in the database into a native time.Time.

func (UnixTimestamp) MarshalJSON

func (u UnixTimestamp) MarshalJSON() ([]byte, error)

MarshalJSON -> time.Time.MarshalJSON

func (*UnixTimestamp) Scan

func (u *UnixTimestamp) Scan(src interface{}) error

Scan decodes src into an unix timestamp.

func (*UnixTimestamp) UnmarshalJSON

func (u *UnixTimestamp) UnmarshalJSON(x []byte) error

UnmarshalJSON -> time.Time.UnmarshalJSON

type UpdateQuery

type UpdateQuery struct {
	Parameters []interface{}
	// contains filtered or unexported fields
}

UpdateQuery is simply an SQL update query, that can be built upon passed parameters.

func (*UpdateQuery) Add

func (u *UpdateQuery) Add(field string, value interface{}) *UpdateQuery

Add adds a new field with correspective value to UpdateQuery

func (*UpdateQuery) Fields

func (u *UpdateQuery) Fields() string

Fields retrieves the fields joined by a comma.

type UserPrivileges

type UserPrivileges uint64

UserPrivileges represents a bitwise enum of the privileges of an user.

const (
	UserPrivilegePublic UserPrivileges = 1 << iota
	UserPrivilegeNormal
	UserPrivilegeDonor
	AdminPrivilegeAccessRAP
	AdminPrivilegeManageUsers
	AdminPrivilegeBanUsers
	AdminPrivilegeSilenceUsers
	AdminPrivilegeWipeUsers
	AdminPrivilegeManageBeatmap
	AdminPrivilegeManageServer
	AdminPrivilegeManageSetting
	AdminPrivilegeManageBetaKey
	AdminPrivilegeManageReport
	AdminPrivilegeManageDocs
	AdminPrivilegeManageBadges
	AdminPrivilegeViewRAPLogs
	AdminPrivilegeManagePrivilege
	AdminPrivilegeSendAlerts
	AdminPrivilegeChatMod
	AdminPrivilegeKickUsers
	UserPrivilegePendingVerification
	UserPrivilegeTournamentStaff
	AdminPrivilegeCaker
)

user/admin privileges

func (UserPrivileges) String

func (p UserPrivileges) String() string

type WhereClause

type WhereClause struct {
	Clause string
	Params []interface{}
	// contains filtered or unexported fields
}

WhereClause is a struct representing a where clause. This is made to easily create WHERE clauses from parameters passed from a request.

func Where

func Where(clause, passedParam string, allowedValues ...string) *WhereClause

Where is the same as WhereClause.Where, but creates a new WhereClause.

func (*WhereClause) And

func (w *WhereClause) And() *WhereClause

And enables using AND instead of OR

func (*WhereClause) ClauseSafe

func (w *WhereClause) ClauseSafe() string

ClauseSafe returns the clause, always containing something. If w.Clause is empty, it returns "WHERE 1".

func (*WhereClause) In

func (w *WhereClause) In(initial string, fields ...[]byte) *WhereClause

In generates an IN clause. initial is the initial part, e.g. "users.id". Fields are the possible values. Sample output: users.id IN ('1', '2', '3')

func (*WhereClause) Or

func (w *WhereClause) Or() *WhereClause

Or enables using OR instead of AND

func (*WhereClause) Where

func (w *WhereClause) Where(clause, passedParam string, allowedValues ...string) *WhereClause

Where adds a new WHERE clause to the WhereClause.

Jump to

Keyboard shortcuts

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