fedcontext

package
v0.0.0-...-7cc0ef5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2021 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddContext

func AddContext(s db.FedStorage, pa pub.FederatingActor, hf pub.HandlerFunc) func(http.Handler) http.Handler

Return a middleware function that installs a FedContext on the request. If the request contained cookies, these are loaded into the context.

func Flash

func Flash(r *http.Request, s string)

Add s to the list of flashes to display on the web interface.

func FlashError

func FlashError(r *http.Request, s string)

Add s to the list of errors to display on the web interface.

func FlashWarning

func FlashWarning(r *http.Request, s string)

Add s to the list of warnings to display on the web interface.

func LocalUsername

func LocalUsername(fc FedClient) (username string, ok bool)

If fc is a client for a user on this instance, return the username of that user.

func Redirect

func Redirect(w http.ResponseWriter, r *http.Request, addr string)

Redirect to addr preserving the persisted part of the current FedContext.

That means writing the cookie.

func Selected

func Selected(r *http.Request, tab string)

Hint the context that the specified tab should be highlighted in the web interface.

If the tab was already set for this request, this call is a no-op.

func Status

func Status(r *http.Request, status int)

Hint the context that the HTTP request should reply with given HTTP status code.

If the status was already set for this request, this call is a no-op.

func Title

func Title(r *http.Request, title string)

Hint the context that the rendered webpage should have given title.

If the title was already set for this request, this call is a no-op.

Types

type CookieContext

type CookieContext struct {
	// Token that authenticates this users session. This is identical
	// to OAuth tokens. Might be nil.
	Token *string

	// Flashes to display on top of the page. Might be nil.
	Flashs []string

	// Warning (yellow) flashes to display on the top of the page.
	// Might be nil.
	Warnings []string

	// Error (red) flashes to display on the top of the page.
	// Might be nil.
	Errors []string
}

Persistent context of the web interface. This information is restored when installing the context.

func (*CookieContext) ClearFlashes

func (cc *CookieContext) ClearFlashes()

Set all flash slices to nil. It makes sense to call this method after we ensured that all flashes were shown to the user.

func (*CookieContext) LoadFromCookie

func (cc *CookieContext) LoadFromCookie(r *http.Request) error

Load persisted fields from cookie into context. This function is called when installing the FedContext into the HTTP request.

func (*CookieContext) WriteToCookie

func (cc *CookieContext) WriteToCookie(w http.ResponseWriter) error

Write persisted fields from context into cookie. This way, once the user comes back, the persisted fields can be restored with a call to LoadFromCookie.

type FedClient

type FedClient interface {
	// The name of this user.
	Username() string

	// Return the clients actor IRI.
	IRI() *url.URL

	// Return the stream iterator. Stream is a combination of both
	// inbox and outbox.
	Stream() (fetch.Iter, error)

	// Return an iterator of the users inbox.
	Inbox() (fetch.Iter, error)

	// Return the IRI of the users inbox.
	InboxIRI() *url.URL

	// Return an iterator of the users outbox.
	Outbox() (fetch.Iter, error)

	// Return the IRI of the users outbox.
	OutboxIRI() *url.URL

	// Return an iterator of the objects this user liked.
	Liked() (fetch.Iter, error)

	// Return the IRI of the users liked collection.
	LikedIRI() *url.URL

	// Return an iterator of actors that follow the user.
	Followers() (fetch.Iter, error)

	// Return the IRI to the collection of actors that follow this user.
	FollowersIRI() *url.URL

	// Wrap event into an Create activity and submit
	// it to the users outbox.
	Create(event vocab.Type) error

	// Like the object at iri.
	Like(iri *url.URL) error
}

A client that can be used to issue command from the point of view of an actor on the ActivityPub fediverse.

func NewRemoteClient

func NewRemoteClient(actoraddr, token string) (FedClient, error)

type FedContext

type FedContext struct {
	RequestContext
	WebContext
	CookieContext
}

A struct that contains all data we attach to each HTTP request. Handlers and other functions that work on that request can use and update the contents to their liking.

func Context

func Context(r *http.Request) (fc *FedContext)

Return the FedContext associated with HTTP request r.

func From

func From(c context.Context) (fc *FedContext)

Return the FedContext in c.

func (*FedContext) LoggedIn

func (fwc *FedContext) LoggedIn() bool

Return whether a user is currently logged in.

type RequestContext

type RequestContext struct {
	// The connection to the database you can use to read and write
	// metadata and activity pub objects.
	Storage db.FedStorage

	// The federating actor from the go-fed library. You can use it
	// to take care of activity pub requests to inbox and outbox.
	PubActor pub.FederatingActor

	// The HTTP handler function from the go-fed library. You can use
	// it to take care of all sorts of requests that request an
	// ActivityPub document type.
	PubHandler pub.HandlerFunc

	// The HTTP status code to reply with. After being set the
	// first time with function Status(), it will not change anymore.
	// This means a handler can (1) set the status and then (2)
	// just call another handler to take care of the request w/o
	// changing the HTTP status code.
	//
	// Initialized to 200.
	Status int

	// Currently logged in user for this session. Might be nil in
	// which case nobody is logged in.
	Client FedClient
}

Context relevant to every request, be it an request to some API or to the web interface.

type WebContext

type WebContext struct {
	// The name of the tab that should be highlighted in the
	// navigation bar. If empty, not tab will be highlighted.
	Selected string

	// The title that should be used.
	Title string
}

Volatile context of the web interface. This information is only valid for one request and is zeroed with every new request.

Jump to

Keyboard shortcuts

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