helpers

package
v0.0.0-...-c76f5c3 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: GPL-2.0 Imports: 14 Imported by: 4

Documentation

Overview

Package helpers provides helper and utility functions that are shared by all parts of the Microcosm application

Index

Constants

View Source
const (
	ItemTypeActivity         string = "activity"
	ItemTypeAlbum            string = "album"
	ItemTypeArticle          string = "article"
	ItemTypeAttendee         string = "attendee"
	ItemTypeAttachment       string = "attachment"
	ItemTypeAttribute        string = "attribute"
	ItemTypeAuth             string = "auth"
	ItemTypeClassified       string = "classified"
	ItemTypeComment          string = "comment"
	ItemTypeConversation     string = "conversation"
	ItemTypeEvent            string = "event"
	ItemTypeFile             string = "file"
	ItemTypeHuddle           string = "huddle"
	ItemTypeMicrocosm        string = "microcosm"
	ItemTypePoll             string = "poll"
	ItemTypeProfile          string = "profile"
	ItemTypeQuestion         string = "question"
	ItemTypeRole             string = "role"
	ItemTypeSite             string = "site"
	ItemTypeUpdate           string = "update"
	ItemTypeUpdateOptionType string = "update_type"
	ItemTypeUser             string = "user"
	ItemTypeWatcher          string = "watcher"
	ItemTypeWhoAmI           string = "whoami"
)

Constants for all of the itemTypes

View Source
const (
	APITypeActivity         string = "/api/v1/activity"
	APITypeAlbum            string = "/api/v1/albums"
	APITypeArticle          string = "/api/v1/articles"
	APITypeAttendee         string = "/api/v1/events/%d/attendees"
	APITypeAttachment       string = "attachments"
	APITypeAttribute        string = "/api/v1/%s/%d/attributes"
	APITypeAuth             string = "/api/v1/auth"
	APITypeClassified       string = "/api/v1/classifieds"
	APITypeComment          string = "/api/v1/comments"
	APITypeConversation     string = "/api/v1/conversations"
	APITypeEvent            string = "/api/v1/events"
	APITypeFile             string = "/api/v1/files"
	APITypeHuddle           string = "/api/v1/huddles"
	APITypeMicrocosm        string = "/api/v1/microcosms"
	APITypeQuestion         string = "/api/v1/questions"
	APITypePoll             string = "/api/v1/polls"
	APITypeProfile          string = "/api/v1/profiles"
	APITypeRole             string = "/api/v1/roles"
	APITypeSite             string = "/api/v1/sites"
	APITypeUpdate           string = "/api/v1/updates"
	APITypeUpdateOptionType string = "/api/v1/updates/preferences/%d"
	APITypeUser             string = "/api/v1/users"
	APITypeWatcher          string = "/api/v1/watchers"
	APITypeWhoAmI           string = "/api/v1/whoami"
)

List of APITypes

View Source
const (
	UpdateTypeNewComment         string = "new_comment"
	UpdateTypeReplyToComment     string = "reply_to_comment"
	UpdateTypeMentioned          string = "mentioned"
	UpdateTypeNewCommentInHuddle string = "new_comment_in_huddle"
	UpdateTypeNewEventAttendee   string = "new_attendee"
	UpdateTypeNewPollVote        string = "new_vote"
	UpdateTypeEventReminder      string = "event_reminder"
	UpdateTypeNewItem            string = "new_item"
	UpdateTypeNewUser            string = "new_user"
)

List of update_types

View Source
const (
	UpdateTextNewComment         string = "There is a new comment in an item you are subscribed to"
	UpdateTextReplyToComment     string = "A user has replied to your post"
	UpdateTextMentioned          string = "You were mentioned in a comment"
	UpdateTextNewCommentInHuddle string = "There is a new comment in a huddle that you are part of"
	UpdateTextNewEventAttendee   string = "A user has RSVPed to an event you are subscribed to"
	UpdateTextNewPollVote        string = "A vote has been cast in a poll you are subscribed to"
	UpdateTextEventReminder      string = "You have an upcoming event"
	UpdateTextNewItem            string = "There is a new item in a microcosm you are subscribed to"
	UpdateTextNewUser            string = "There is a new user and you are watching all profiles"
)

List of texts for update_types

View Source
const (
	// DefaultQueryLimit defines the default number of items per page for APIs
	DefaultQueryLimit int64 = 25

	// DefaultQueryOffset defines the default offset for API responses
	DefaultQueryOffset int64 = 0
)
View Source
const JumpURL string = "http://microcosm.app/out/"

JumpURL is the URL to use for external URL redirects

View Source
const NoAuthMessage = "You do not have permission to do that"

NoAuthMessage is the unauthorised error message

Variables

View Source
var AllowedChars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

AllowedChars is the range of chars that can appear in a random string

ItemTypes is a list of all itemTypes

ItemTypesCommentable is a list of the itemTypes that can have comments on them

ItemTypesScoreable is a list of the itemTypes that will be scored to determine trending items

ItemTypesToAPIItem maps from the itemType to the APIType

UpdateTexts is a map of email titles for a given range of update_types

UpdateTypes is a map of update_types

Functions

func AttendanceStatus

func AttendanceStatus(query url.Values) (string, int, error)

AttendanceStatus returns status for the given request query

func Btoi

func Btoi(boolean bool) int

Btoi converts a boolean to an integer

func GetAttending

func GetAttending(query url.Values) (bool, int, error)

GetAttending returns isAttending for a given request query

func GetConnection

func GetConnection() (*sql.DB, error)

GetConnection returns a connection from the connection pool of the already instantiated db object

func GetCurrentPage

func GetCurrentPage(offset int64, limit int64) int64

GetCurrentPage returns the current page for a given offset and limit value

func GetItemAndItemType

func GetItemAndItemType(query url.Values) (int64, string, int, error)

GetItemAndItemType returns the item type and id for a given request query

func GetItemTypeFromInt

func GetItemTypeFromInt(value int64) (string, error)

GetItemTypeFromInt returns the string itemType for the integer itemTypeID

func GetLimitAndOffset

func GetLimitAndOffset(query url.Values) (int64, int64, int, error)

GetLimitAndOffset returns the Limit and Offset for a given request querystring

func GetLinkToThisPage

func GetLinkToThisPage(
	requestURL url.URL,
	offset int64,
	limit int64,
	total int64,
) url.URL

GetLinkToThisPage returns a link to the current page

func GetMapStringFromInt

func GetMapStringFromInt(theMap map[string]int64, value int64) (string, error)

GetMapStringFromInt for a given map[string]int64 return the value of the key where the value matches

func GetMaxOffset

func GetMaxOffset(total int64, limit int64) int64

GetMaxOffset returns the maximum possible offset for a given number of pages and limit per page

func GetPageCount

func GetPageCount(total int64, limit int64) int64

GetPageCount returns the number of pages for a given total and items per page

func GetTransaction

func GetTransaction() (*sql.Tx, error)

GetTransaction will begin and then return a transaction on the already instantiated db object

func InitDBConnection

func InitDBConnection(c DBConfig)

InitDBConnection will establish the connection to the database or die trying

func IsValidColor

func IsValidColor(color string) bool

IsValidColor returns true if the string color is a websafe recognised color

func MD5Sum

func MD5Sum(s string) string

MD5Sum calculates the MD5 of a string

func RandString

func RandString(length int) (string, error)

RandString returns a random string for a required length

func SHA1

func SHA1(bytes []byte) (string, error)

SHA1 calculates the SHA1 for a []byte

func TestPatch

func TestPatch(patches []PatchType) (int, error)

TestPatch partially implements http://tools.ietf.org/html/rfc6902

Patch examples: { "op": "test", "path": "/a/b/c", "value": "foo" }, { "op": "remove", "path": "/a/b/c" }, { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] }, { "op": "replace", "path": "/a/b/c", "value": 42 }, { "op": "move", "from": "/a/b/c", "path": "/a/b/d" }, { "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }

Types

type ArrayType

type ArrayType struct {
	Total     int64       `json:"total"`
	Limit     int64       `json:"limit"`
	Offset    int64       `json:"offset"`
	MaxOffset int64       `json:"maxOffset"`
	Pages     int64       `json:"totalPages"`
	Page      int64       `json:"page"`
	Links     []LinkType  `json:"links,omitempty"`
	Type      string      `json:"type"`
	Items     interface{} `json:"items"`
}

ArrayType describes an array in JSON and how to paginate the collection

func ConstructArray

func ConstructArray(
	resources interface{},
	contentType string,
	total int64,
	limit int64,
	offset int64,
	pages int64,
	requestURL *url.URL,
) ArrayType

ConstructArray returns an array

type AttachmentType

type AttachmentType struct {
	Href      string        `json:"href"`
	Created   int           `json:"created"`
	CreatedBy string        `json:"createdBy"`
	MimeType  string        `json:"mimetypes"`
	Bytes     int           `json:"bytes"`
	Width     int           `json:"width"`
	Height    int           `json:"height"`
	Views     int           `json:"views"`
	Thumbnail ThumbnailType `json:"thumbnail,omitempty"`
}

AttachmentType describes an attachment

type CoreMetaType

type CoreMetaType struct {
	Stats       []StatType  `json:"stats,omitempty"`
	Links       []LinkType  `json:"links,omitempty"`
	Permissions interface{} `json:"permissions,omitempty"`
}

CoreMetaType is used implicitly by all meta types, and explicitly by all collections

type CreatedMetaType

type CreatedMetaType struct {
	CreatedType
	ExtendedMetaType
}

CreatedMetaType for items (such as Alerts) that cannot be edited but have a creator

type CreatedType

type CreatedType struct {
	Created     time.Time   `json:"created"`
	CreatedByID int64       `json:"-"`
	CreatedBy   interface{} `json:"createdBy"`
}

CreatedType describes an author/creator

type DBConfig

type DBConfig struct {
	Host     string
	Port     int64
	Database string
	Username string
	Password string
}

DBConfig stores the connection information used by InitDBConnection to establish a connection to the database

type DefaultMetaType

type DefaultMetaType struct {
	CreatedType
	EditedType
	ExtendedMetaType
}

DefaultMetaType is used by single items

type DefaultNoFlagsMetaType

type DefaultNoFlagsMetaType struct {
	CreatedType
	EditedType
	CoreMetaType
}

DefaultNoFlagsMetaType is used by single items

type DefaultReducedFlagsMetaType

type DefaultReducedFlagsMetaType struct {
	CreatedType
	EditedType
	SimpleMetaType
}

DefaultReducedFlagsMetaType is used by single items

type EditedType

type EditedType struct {
	EditedNullable     pq.NullTime    `json:"-"`
	Edited             string         `json:"edited,omitempty"`
	EditedByNullable   sql.NullInt64  `json:"-"`
	EditedBy           interface{}    `json:"editedBy,omitempty"`
	EditReasonNullable sql.NullString `json:"-"`
	EditReason         string         `json:"editReason,omitempty"`
}

EditedType describes edited meta data

type Er

type Er interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

Er provides an interface that both sql.DB and sql.Tx fulfil, allowing for generic funcs that either can use.

type ExtendedMetaType

type ExtendedMetaType struct {
	Flags FlagsType `json:"flags,omitempty"`
	CoreMetaType
}

ExtendedMetaType is used implicitly by all meta types, and explicitly by all collections

type FlagsType

type FlagsType struct {
	Sticky    interface{} `json:"sticky,omitempty"`
	Open      interface{} `json:"open,omitempty"`
	Deleted   interface{} `json:"deleted,omitempty"`
	Moderated interface{} `json:"moderated,omitempty"`
	Visible   interface{} `json:"visible,omitempty"`
	Unread    interface{} `json:"unread,omitempty"`
	Watched   interface{} `json:"watched,omitempty"`
	Ignored   interface{} `json:"ignored,omitempty"`
	SendEmail interface{} `json:"sendEmail,omitempty"`
	SendSMS   interface{} `json:"sendSMS,omitempty"`
	Attending interface{} `json:"attending,omitempty"`
}

FlagsType describes the common flags

func (*FlagsType) SetVisible

func (f *FlagsType) SetVisible()

SetVisible determines whether the item should be visible and if so sets the visible flag

type LinkArrayType

type LinkArrayType struct {
	Links []LinkType `json:"links"`
}

LinkArrayType is a collection of links

type LinkType

type LinkType struct {
	Rel   string `json:"rel,omitempty"` // REST
	Href  string `json:"href"`
	Title string `json:"title,omitempty"`
	Text  string `json:"text,omitempty"` // HTML
}

LinkType is a link

func GetArrayLinks(
	requestURL url.URL,
	offset int64,
	limit int64,
	total int64,
) []LinkType

GetArrayLinks returns a collection of valid links for navigating a collection of items

func GetExtendedLink(
	rel string,
	title string,
	itemType string,
	firstID int64,
	secondID int64,
) LinkType

GetExtendedLink returns a link for child items

func GetLink(rel string, title string, itemType string, itemID int64) LinkType

GetLink returns a link to an item

type PatchType

type PatchType struct {
	Operation string         `json:"op"`
	Path      string         `json:"path"`
	From      string         `json:"from,omitempty"`
	RawValue  interface{}    `json:"value,omitempty"`
	Bool      sql.NullBool   `json:"-"`
	String    sql.NullString `json:"-"`
	Int64     sql.NullInt64  `json:"-"`
	Time      pq.NullTime    `json:"-"`
}

PatchType describes a JSON PATCH

func (*PatchType) ScanRawValue

func (p *PatchType) ScanRawValue() (int, error)

ScanRawValue scans a PATCH value

type SimpleFlagsType

type SimpleFlagsType struct {
	Deleted   bool `json:"deleted"`
	Moderated bool `json:"moderated"`
	Visible   bool `json:"visible"`
}

SimpleFlagsType describes simple flags

type SimpleMetaType

type SimpleMetaType struct {
	Flags SimpleFlagsType `json:"flags,omitempty"`
	CoreMetaType
}

SimpleMetaType is used explicitly by comments

type StatType

type StatType struct {
	Metric string `json:"metric"`
	Value  int64  `json:"value"`
}

StatType describes a statistic

type SummaryMetaType

type SummaryMetaType struct {
	CreatedType
	ExtendedMetaType
}

SummaryMetaType is used by summary of single items

type ThumbnailType

type ThumbnailType struct {
	Href     string `json:"href"`
	MimeType string `json:"mimetype"`
	Bytes    int    `json:"bytes"`
	Width    int    `json:"width"`
	Height   int    `json:"height"`
}

ThumbnailType describes a thumbnail of an image attachment

Jump to

Keyboard shortcuts

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