karma

package
v0.0.0-...-0197395 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Commands = map[string]struct{}{
	// contains filtered or unexported fields
}

Commands a set of the support commands by this processor

View Source
var ResponseHelp = slack.Response{
	ResponseType: slack.ResponseType.Ephemeral,
	Text:         "",
	Attachments: []slack.Attachments{
		{
			Fallback: "*Help* Helpful information on how to manage karma.",
			Title:    "Helpful information on how to manage karma.",
			Text:     "Below are the sub-commands:",
			Fields: []slack.Field{
				{
					Title: cmdMe,
					Value: "Return your karma and daily usage limits.",
					Short: true,
				},
				{
					Title: cmdStatus,
					Value: "Provide a @user and return their karma.",
					Short: true,
				},
				{
					Title: cmdAdd,
					Value: "Provide a @user and increase their karma. Optionally, pass a quantity of karma to give.",
					Short: true,
				},
				{
					Title: cmdSub,
					Value: "Provide a @user and decrease their karma. Optionally, pass a quantity of karma to take.",
					Short: true,
				},
				{
					Title: cmdTop,
					Value: "Return the top 3 users by karma. Optionally, pass a quantity for the top n users",
					Short: true,
				},
				{
					Title: cmdHelp,
					Value: "This helpful dialogue. You're welcome!",
					Short: true,
				},
			},
		},
	},
}

ResponseHelp the slack response for the HELP command

Functions

func Abs

func Abs(x int) int

Abs absolute value of an int

func BindRoutes

func BindRoutes(karmaGroup *gin.RouterGroup, knaveGroup *gin.RouterGroup, karmaHandler Handler)

BindRoutes bind handlers to router

func InitDB

func InitDB(dataSourceName string) (*sql.DB, error)

InitDB initializes the db and tables that we require

func IsoDate

func IsoDate(t time.Time) string

IsoDate converts a time object to 2006-01-02 format

func MsgGiveKarma

func MsgGiveKarma(callee, target string, delta int) string

MsgGiveKarma announces who gave how much karma to whom

func MsgOverDailyLimit

func MsgOverDailyLimit(limit, usage, remainder int) string

MsgOverDailyLimit generates daily limit error message (string)

func MsgTakeKarma

func MsgTakeKarma(callee, target string, delta int) string

MsgTakeKarma announces who took how much karma from whom

func MsgTopKarma

func MsgTopKarma(topUsers []UserKarma) string

MsgTopKarma table for viewing top users by karma

func MsgUserDailyLimit

func MsgUserDailyLimit(usage, remaining int) string

MsgUserDailyLimit the remaining daily limits

func MsgUserStatus

func MsgUserStatus(userID string, k int) string

MsgUserStatus the User's Karma status

func MsgUserStatusTarget

func MsgUserStatusTarget(callee, target string) string

MsgUserStatusTarget lets all users know who requested karma totals

Types

type Command

type Command string

Command my own string type for commands (think of it as an enum)

type DAO

type DAO interface {
	GetKarma(team, user string) (int, error)
	UpdateKarma(team, user string, delta int) (int, error)
	DeleteKarma(team, user string) (int, error)
	Top(team string, n int) ([]UserKarma, error)
	Usage(slack.CommandData, slack.Response) error
	GetDaily(team, user string, date time.Time) (int, error)
	UpdateDaily(team, user string, date time.Time, karma int) (int, error)
	UpdateKarmaDaily(team, callee, target string, delta int, date time.Time) (int, error)
}

DAO Data Access Object for the Karma database

type Handler

type Handler interface {
	GetKarma(c *gin.Context)
	AddKarma(c *gin.Context)
	DelKarma(c *gin.Context)
	SlashKarma(c *gin.Context)
	TopKarma(c *gin.Context)
}

Handler interface for Karma handler

type MockDAO

type MockDAO struct {
	GetKarmaMock         func(team, user string) (int, error)
	UpdateKarmaMock      func(team, user string, delta int) (int, error)
	DeleteKarmaMock      func(team, user string) (int, error)
	UsageMock            func(slack.CommandData, slack.Response) error
	TopMock              func(team string, n int) ([]UserKarma, error)
	GetDailyMock         func(team, user string, date time.Time) (int, error)
	UpdateDailyMock      func(team, user string, date time.Time, karma int) (int, error)
	UpdateKarmaDailyMock func(team, callee, target string, delta int, date time.Time) (int, error)
}

MockDAO a mock dao for karma whose mock functions can be monkeypatched

func FullDailyDao

func FullDailyDao() MockDAO

FullDailyDao factory method for a mock dao were all users are full on their daily limit

func HappyDao

func HappyDao() MockDAO

HappyDao factory method for a mock dao that will always succeed

func NewMockDao

func NewMockDao(usage int) MockDAO

NewMockDao constructor func for making mock dao

func SadDao

func SadDao() MockDAO

SadDao factory method for a mock dao that will always fail with an error

func (MockDAO) DeleteKarma

func (m MockDAO) DeleteKarma(team, user string) (int, error)

DeleteKarma .

func (MockDAO) GetDaily

func (m MockDAO) GetDaily(team, user string, date time.Time) (int, error)

GetDaily .

func (MockDAO) GetKarma

func (m MockDAO) GetKarma(team, user string) (int, error)

GetKarma .

func (MockDAO) Top

func (m MockDAO) Top(team string, n int) ([]UserKarma, error)

Top .

func (MockDAO) UpdateDaily

func (m MockDAO) UpdateDaily(team, user string, date time.Time, karma int) (int, error)

UpdateDaily .

func (MockDAO) UpdateKarma

func (m MockDAO) UpdateKarma(team, user string, delta int) (int, error)

UpdateKarma .

func (MockDAO) UpdateKarmaDaily

func (m MockDAO) UpdateKarmaDaily(team, callee, target string, delta int, date time.Time) (int, error)

UpdateKarmaDaily .

func (MockDAO) Usage

func (m MockDAO) Usage(d slack.CommandData, r slack.Response) error

Usage .

type ProcConfig

type ProcConfig struct {
	SingleLimit    int
	DailyLimit     int
	TopUserDefault int
	TopUserMax     int
}

ProcConfig processor config object to contain all of these customizations SingleLimit one time karma swings are capped at 5 (default) DailyLimit this is the default daily limit for giving/ taking karma used by top function as guard rails used by top function as guard rails

var DefaultConfig ProcConfig = ProcConfig{
	SingleLimit:    5,
	DailyLimit:     25,
	TopUserDefault: 3,
	TopUserMax:     10,
}

DefaultConfig default settings for the Processor

type Processor

type Processor interface {
	Process(cd slack.CommandData) (slack.Response, error)
}

Processor processes slash-commands into slack responses

type SQLiteDAO

type SQLiteDAO struct {
	// contains filtered or unexported fields
}

SQLiteDAO a SQLite imlpementation of the Karma database

func NewDao

func NewDao(db *sql.DB) SQLiteDAO

NewDao factory method

func (SQLiteDAO) DeleteKarma

func (dao SQLiteDAO) DeleteKarma(team, user string) (int, error)

DeleteKarma resets all karma for a given user in a given team to zer0

func (SQLiteDAO) GetDaily

func (dao SQLiteDAO) GetDaily(team, user string, date time.Time) (int, error)

GetDaily return the amount of karma the team/user has gives/taken for a day

func (SQLiteDAO) GetKarma

func (dao SQLiteDAO) GetKarma(team, user string) (int, error)

GetKarma returns the karma value for the user in a given team

func (SQLiteDAO) Top

func (dao SQLiteDAO) Top(team string, n int) ([]UserKarma, error)

Top returns the top n users (ordered by karma) from a given team

func (SQLiteDAO) UpdateDaily

func (dao SQLiteDAO) UpdateDaily(team, user string, date time.Time, karma int) (int, error)

UpdateDaily adds karma to team/user's daily usage count

func (SQLiteDAO) UpdateKarma

func (dao SQLiteDAO) UpdateKarma(workspace, user string, delta int) (int, error)

UpdateKarma adds (or removes) karma from a user in a given team (workspace)

func (SQLiteDAO) UpdateKarmaDaily

func (dao SQLiteDAO) UpdateKarmaDaily(team, callee, target string, delta int, date time.Time) (int, error)

UpdateKarmaDaily updates the karma total and daily usage at the same time, returns new karma the target receives karma the callee has their daily usage incremented

func (SQLiteDAO) Usage

func (dao SQLiteDAO) Usage(data slack.CommandData, res slack.Response) error

Usage tracks the usage of karma by pairing the request with the response

type SQLiteHandler

type SQLiteHandler struct {
	// contains filtered or unexported fields
}

SQLiteHandler Karma Handler implementation using sqlite

func NewHandler

func NewHandler(proc Processor, dao DAO) SQLiteHandler

NewHandler factory method

func (SQLiteHandler) AddKarma

func (h SQLiteHandler) AddKarma(c *gin.Context)

AddKarma handler method to add (or subtract) karma from an individual

func (SQLiteHandler) DelKarma

func (h SQLiteHandler) DelKarma(c *gin.Context)

DelKarma handler method to delete (reset) karma to zer0

func (SQLiteHandler) GetKarma

func (h SQLiteHandler) GetKarma(c *gin.Context)

GetKarma handler method to read the current karma for an individual

func (SQLiteHandler) SlashKarma

func (h SQLiteHandler) SlashKarma(c *gin.Context)

SlashKarma handler method for the `/karma` slash-command

func (SQLiteHandler) TopKarma

func (h SQLiteHandler) TopKarma(c *gin.Context)

TopKarma returns the top n users for a given team

type SlackProcessor

type SlackProcessor struct {
	// contains filtered or unexported fields
}

SlackProcessor an implementation of KarmaProcessor that uses SQLite

func NewProcessor

func NewProcessor(config ProcConfig, dao DAO, insult, compliment shakespeare.Generator) SlackProcessor

NewProcessor factory method

func (SlackProcessor) Process

Process handles Karma processing from slack API

func (SlackProcessor) Salutation

func (p SlackProcessor) Salutation(k int) string

Salutation appends a Salutation (insult or compliment)

type UserKarma

type UserKarma struct {
	User  string
	Karma int
}

UserKarma slack users and their karma totals

Jump to

Keyboard shortcuts

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