kb

package
v0.0.0-...-48663ca Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

This package implements common federated wiki types

Index

Constants

View Source
const (
	Blocked   Rights = "blocked"
	Reader           = "reader"
	Editor           = "editor"
	Moderator        = "moderator"
)
View Source
const DefaultBucketName = "rt-knowledge-base-dev"

AWS S3 related

View Source
const DefaultRegion = "us-east-1"

Variables

View Source
var (
	// S3 requests related errors
	ErrUnableToDelete          = errors.New("Unable to delete given object.")
	ErrUnableToCreateS3Session = errors.New("Unable to create S3 session.")
	ErrDoesNotExist            = errors.New("404 Not Found")
	ErrBadRequest              = errors.New("Bad request, likely due to invalid input.")
)
View Source
var (
	ErrUserExists    = errors.New("User already exists.")
	ErrUserNotExist  = errors.New("User does not exist.")
	ErrGroupExists   = errors.New("Group already exists.")
	ErrGroupNotExist = errors.New("Group does not exist.")
	ErrPageExists    = errors.New("Page already exists.")
	ErrPageNotExist  = errors.New("Page does not exist.")

	ErrConcurrentEdit = errors.New("Concurrent modification of page.")

	ErrInvalidSlug = errors.New("Invalid slug.")
)
View Source
var (
	ErrUnknownAction = errors.New("unknown action")
)

Functions

func AddCSPHeader

func AddCSPHeader(w http.ResponseWriter) string

func AddCommonHeaders

func AddCommonHeaders(w http.ResponseWriter)

func ExtractSynopsis

func ExtractSynopsis(page *Page) string

func ExtractTags

func ExtractTags(page *Page) []string

func NewID

func NewID() string

func SlugToTitle

func SlugToTitle(slug Slug) string

func SlugifyTags

func SlugifyTags(tags []string) []string

func SortGroupsByPriority

func SortGroupsByPriority(user User, groups []Group)

func SortPageEntries

func SortPageEntries(xs []PageEntry, fn func(a, b *PageEntry) bool)

func SortPageEntriesByDate

func SortPageEntriesByDate(xs []PageEntry)

func SortPageEntriesByRank

func SortPageEntriesByRank(xs []PageEntry, ranking []Slug)

func SortPageEntriesBySlug

func SortPageEntriesBySlug(xs []PageEntry)

func SortTagEntriesByName

func SortTagEntriesByName(xs []TagEntry)

func ValidateSlug

func ValidateSlug(slug Slug) error

ValidateSlug verifies whether a `slug` is valid

func WriteResult

func WriteResult(w http.ResponseWriter, err error)

Types

type Access

type Access interface {
	VerifyUser(user User) error

	IsAdmin(user Slug) bool
	SetAdmin(user Slug, isAdmin bool) error

	Rights(group, user Slug) Rights

	// member is either a User or a Group
	AddUser(group, user Slug) error
	RemoveUser(group, user Slug) error

	CommunityAdd(group, member Slug, rights Rights) error
	CommunityRemove(group, member Slug) error

	List(group Slug) ([]Member, error)
}

type Action

type Action map[string]interface{}

Action represents a operation that can be applied to a fedwiki.Page

func ReadJSONAction

func ReadJSONAction(r io.Reader) (Action, error)

func (Action) Item

func (action Action) Item() (Item, bool)

Item returns the item attribute

func (Action) Str

func (action Action) Str(key string) string

Str returns string value by the key if that key doesn't exist, it will return an empty string

func (Action) Time

func (action Action) Time() (t time.Time, err error)

Time returns the time when the action occurred

func (Action) Type

func (action Action) Type() string

Type returns the action type attribute

type Auth

type Auth interface {
	Verify(w http.ResponseWriter, r *http.Request) (User, error)
}

type Context

type Context interface {
	ActiveUserID() Slug
	Access() Access
	Users() Users
	Groups() Groups
	Index(user Slug) Index
	Pages(group Slug) Pages

	GuestLogin() GuestLogin
}

type Database

type Database interface {
	Context(user Slug) Context
}

type Group

type Group struct {
	ID      Slug
	OwnerID Slug
	Name    string
	Public  bool

	Description string
}

func (*Group) IsCommunity

func (group *Group) IsCommunity() bool

func (*Group) Priority

func (group *Group) Priority(user *User) int

type Groups

type Groups interface {
	ByID(id Slug) (Group, error)
	Create(group Group) error
	Delete(id Slug) error
	List() ([]Group, error)
}

type GuestLogin

type GuestLogin interface {
	Add(name, email, password string) error
	// implement auth.Provider
	Boot() template.HTML
	Verify(name, password string) (User, error)
}

type Index

type Index interface {
	List() ([]PageEntry, error)

	Search(text string) ([]PageEntry, error)
	SearchFilter(text, exclude, include string) ([]PageEntry, error)

	Tags() ([]TagEntry, error)
	ByTag(tag Slug) ([]PageEntry, error)
	ByTagFilter(tag []Slug, exclude, include string) ([]PageEntry, error)

	Groups(min Rights) ([]Group, error)
	ByGroup(groupID Slug) ([]PageEntry, error)

	ByTitle(title Slug) ([]PageEntry, error)

	RecentChanges(n int) ([]PageEntry, error)
	RecentChangesByGroup(n int, groupID Slug) ([]PageEntry, error)
}

type Item

type Item map[string]interface{}

Item represents a federated wiki Story item

func Entry

func Entry(title, synopsis string, slug Slug) Item

func HTML

func HTML(text string) Item

func Image

func Image(caption, url, text string) Item

func ItemsFromEntries

func ItemsFromEntries(entries []PageEntry) []Item

func Paragraph

func Paragraph(text string) Item

func Reference

func Reference(title, url, text string) Item

func Tags

func Tags(tags ...string) Item

func (Item) ID

func (item Item) ID() string

ID returns the `item` identificator

func (Item) Type

func (item Item) Type() string

Type returns the item `type`

func (Item) Val

func (item Item) Val(key string) string

Val returns a string value from key

type Member

type Member struct {
	ID      Slug
	Name    string
	IsGroup bool
	Access  Rights
}

type Module

type Module interface {
	Info() Group
	Pages() []PageEntry
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

type Page

type Page struct {
	Version  int       `json:"version"`
	Slug     Slug      `json:"slug"`
	Title    string    `json:"title"`
	Synopsis string    `json:"synopsis,omitempty"`
	Modified time.Time `json:"modified,omitempty"`
	Story    Story     `json:"story,omitempty"`
}

Page represents a federated wiki page

func ReadJSONPage

func ReadJSONPage(r io.Reader) (*Page, error)

func (*Page) Apply

func (page *Page) Apply(action Action) error

Apply modifies the page with an action

func (*Page) CanonicalizeIDs

func (p *Page) CanonicalizeIDs()

func (*Page) Hash

func (p *Page) Hash() ([]byte, error)

func (*Page) Write

func (p *Page) Write(w io.Writer) error

func (*Page) WriteResponse

func (p *Page) WriteResponse(w http.ResponseWriter) error

type PageEntry

type PageEntry struct {
	Slug     Slug      `json:"slug"`
	Title    string    `json:"title"`
	Synopsis string    `json:"synopsis"`
	Tags     []string  `json:"tags"`
	Modified time.Time `json:"modified"`
}

func PageEntryFrom

func PageEntryFrom(page *Page) PageEntry

func (*PageEntry) HasTag

func (page *PageEntry) HasTag(tag string) bool

type Pages

type Pages interface {
	Create(page *Page) error

	Load(id Slug) (*Page, error)
	LoadRaw(id Slug) ([]byte, error)
	LoadRawVersion(id Slug, version int) ([]byte, error)

	Overwrite(id Slug, version int, page *Page) error
	Edit(id Slug, version int, action Action) error
	Delete(id Slug, version int) error

	BatchReplace(pages map[Slug]*Page, complete func(string, Slug)) error
	BatchReplaceDelta(pages map[Slug]*Page, complete func(string, Slug)) error

	List() ([]PageEntry, error)
	History(id Slug) ([]PageEntry, error)
}

type Params

type Params map[string]interface{}

type Rights

type Rights string

func (Rights) Level

func (r Rights) Level() int

type Server

type Server struct {
	Auth Auth
	Database
	Modules map[Slug]Module
}

func NewServer

func NewServer(auth Auth, database Database) *Server

func (*Server) AddModule

func (server *Server) AddModule(module Module)

func (*Server) AdminContext

func (server *Server) AdminContext(w http.ResponseWriter, r *http.Request) (Context, bool)

func (*Server) GroupContext

func (server *Server) GroupContext(w http.ResponseWriter, r *http.Request, min Rights) (Context, Slug, bool)

func (*Server) IndexContext

func (server *Server) IndexContext(w http.ResponseWriter, r *http.Request) (Context, Index, bool)

func (*Server) ServeHTTP

func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) UserContext

func (server *Server) UserContext(w http.ResponseWriter, r *http.Request) (Context, bool)

type Slug

type Slug string

Slug is a string where Slugify(string(slug)) == slug

func SlugParam

func SlugParam(r *http.Request, name string) Slug

func Slugify

func Slugify(s string) Slug

Slugify converts text to a slug

  • numbers, '/', '=' are emitted
  • letters will be lowercased (if possible)
  • '-', ',', '.', ' ', '_' will be converted to '-'
  • repeated '/' and '=' are removed
  • other symbols or punctuations will be converted to html entity reference name (if there exists such reference name)
  • everything else will be converted to '-'

Example:

"&Hello_世界/+!" ==> "amp-hello-世界/plus-excl"
"Hello  World  //  Test" ==> "hello-world/test"
func TokenizeLink(link string) (owner, page Slug)

func TokenizeLink3

func TokenizeLink3(link string) (owner, title, page Slug)

func (*Slug) Scan

func (slug *Slug) Scan(value interface{}) error

func (Slug) Value

func (slug Slug) Value() (driver.Value, error)

type Story

type Story []Item

Story is the viewable content of the page

func StoryFromEntries

func StoryFromEntries(entries []PageEntry) Story

func (*Story) Append

func (s *Story) Append(item ...Item)

Appends adds the `item` as the last item in story

func (Story) IndexOf

func (s Story) IndexOf(id string) (index int, ok bool)

IndexOf returns the index of an item with `id` ok = false, if that item doesn't exist

func (*Story) InsertAfter

func (s *Story) InsertAfter(id string, item Item) error

InsertAfter adds the `item` after the item with `id`

func (*Story) Move

func (ps *Story) Move(id string, afterId string) error

Move moves the item with `id` after the item with `afterId`

func (*Story) Prepend

func (s *Story) Prepend(item Item)

Prepend adds the `item` as the first item in story

func (*Story) RemoveByID

func (s *Story) RemoveByID(id string) (item Item, err error)

Removes item with `id`

func (Story) SetByID

func (s Story) SetByID(id string, item Item) error

SetByID replaces item with `id` with `item`

type TagEntry

type TagEntry struct {
	Name  string `json:"name"`
	Count int    `json:"count"`
}

type User

type User struct {
	ID        Slug   `json:"id"`
	Email     string `json:"email"`
	Name      string `json:"name"`
	Company   string `json:"company"`
	Admin     bool   `json:"admin"`
	MaxAccess Rights `json:"-"`

	AuthID       string `json:"-"`
	AuthProvider string `json:"-"`
}

type Users

type Users interface {
	ByID(id Slug) (User, error)
	Create(user User) error
	Delete(id Slug) error
	List() ([]User, error)
}

Directories

Path Synopsis
items
stringSlice support based on
stringSlice support based on

Jump to

Keyboard shortcuts

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