gotktrix

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2022 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const TimelimeLimit = state.TimelineKeepLast

TimelimeLimit is the number of timeline events that the database keeps.

Variables

View Source
var (
	// InfoEnabled is true if GOMATRIX_INFO is non-empty.
	InfoEnabled bool
	// WarnEnabled is true if GOMATRIX_WARN is non-empty.
	WarnEnabled bool
)

AvatarProvider is the image provider that all avatar widgets should use.

View Source
var DefaultTransport = &http.Transport{
	Proxy:                 http.ProxyFromEnvironment,
	ForceAttemptHTTP2:     true,
	MaxIdleConns:          100,
	IdleConnTimeout:       90 * time.Second,
	TLSHandshakeTimeout:   10 * time.Second,
	ExpectContinueTimeout: 1 * time.Second,
	WriteBufferSize:       256 << 10,
	ReadBufferSize:        256 << 10,
	DialContext: (&net.Dialer{
		Timeout: 10 * time.Second,
	}).DialContext,
}

DefaultTransport is the default HTTP transport configuration to use.

A few notes:

  • We use a high ResponseHeaderTimeout because the server presumably won't send us a header while it's preparing the response to be sent. We don't want to mark the connection as timed out.
View Source
var EachBreak = db.EachBreak

EachBreak can be returned if the user wants to break out of an interation.

View Source
var SyncOptions = gotrix.SyncOptions{
	Filter: event.GlobalFilter{
		Room: event.RoomFilter{
			State: event.StateFilter{
				LazyLoadMembers:         true,
				IncludeRedundantMembers: true,
			},
			Timeline: event.RoomEventFilter{
				Limit:           TimelimeLimit,
				LazyLoadMembers: true,
			},
		},
	},
	Timeout:        1 * time.Minute,
	MinBackoffTime: 1 * time.Second,
	MaxBackoffTime: 10 * time.Second,
}

SyncOptions is used to sync.

Functions

func Base64UserID

func Base64UserID(uID matrix.UserID) string

Base64UserID creates a path-friendly base64 string from the given user ID.

func Cancelled

func Cancelled() context.Context

Cancelled gets a cancelled context.

func LatestMessage

func LatestMessage(events []event.RoomEvent) *event.RoomMessageEvent

LatestMessage finds the latest room message event from the given list of events. The list is assumed to have the latest events last.

func MXCProvider added in v0.1.3

func MXCProvider(w, h int, flags ImageFlags) imgutil.Provider

MXCProvider returns a new universal resource provider that handles MXC URLs.

func MaxSize

func MaxSize(w, h, maxW, maxH int) (int, int)

MaxSize returns the maximum size that can fit within the given max width and height. Aspect ratio is preserved.

func WithClient

func WithClient(ctx context.Context, c *Client) context.Context

WithClient injects the given client into a new context.

Types

type Client

type Client struct {
	*gotrix.Client
	*handler.Registry
	State       *state.State
	Index       *indexer.Indexer
	Interceptor *httptrick.Interceptor
	// contains filtered or unexported fields
}

Client extends a gotrix.Client to implement additional functions useful for gotktrix.

func FromContext

func FromContext(ctx context.Context) *Client

FromContext returns the client inside the context wrapped with WithClient. If the context isn't yet wrapped, then nil is returned.

func New

func New(serverName, token string, opts Opts) (*Client, error)

New wraps around gotrix.NewWithClient.

func (*Client) AddHandler deprecated

func (c *Client) AddHandler(function interface{}) error

AddHandler will panic.

Deprecated: Use c.On() instead.

func (*Client) AddSyncInterceptFull

func (c *Client) AddSyncInterceptFull(f httptrick.InterceptFullFunc) func()

AddSyncInterceptFull adds an InterceptFullFunc for the Sync endpoint.

func (*Client) AsyncSetConfig

func (c *Client) AsyncSetConfig(ev event.Event, done func(error))

AsyncSetConfig updates the state cache first, and then updates the API in the background.

If done is given, then it's called once the API is updated. Most of the time, done should only be used to display errors; to know when things are updated, use a handler. Because of that, done may be invoked before AsyncConfigSet has been returned when there's an error. Done might also be called in a different goroutine.

func (*Client) Close

func (c *Client) Close() error

Close closes the event loop and the internal database, as well as halting all ongoing requests.

func (*Client) EachRoomState deprecated

func (c *Client) EachRoomState(
	roomID matrix.RoomID, typ event.Type, f func(string, event.StateEvent) error) error

EachRoomState calls f on every raw event in the room state. It satisfies the EachRoomState method requirement inside gotrix.State, but most callers should not use this method, since there is no length information.

Deprecated: Use EachRoomStateLen.

func (*Client) EachRoomStateLen

func (c *Client) EachRoomStateLen(
	roomID matrix.RoomID, typ event.Type, f func(ev event.StateEvent, total int) error) error

EachRoomStateLen is a variant of EachRoomState, but a length parameter is precalculated.

func (*Client) EachTimeline

func (c *Client) EachTimeline(roomID matrix.RoomID, f func(event.RoomEvent) error) error

EachTimeline iterates through the timeline.

func (*Client) EachTimelineReverse

func (c *Client) EachTimelineReverse(roomID matrix.RoomID, f func(event.RoomEvent) error) error

EachTimelineReverse iterates through the timeline in reverse.

func (*Client) HasPower

func (c *Client) HasPower(roomID matrix.RoomID, action PowerAction) bool

HasPower checks if the current user can perform the given action inside the given room.

func (*Client) ImageThumbnail

func (c *Client) ImageThumbnail(msg *event.RoomMessageEvent, maxW, maxH, scale int) (string, error)

ImageThumbnail gets the thumbnail or direct URL of the image from the message.

func (*Client) IsDirect

func (c *Client) IsDirect(roomID matrix.RoomID) bool

IsDirect returns true if the given room is a direct messaging room.

func (*Client) IsRoomCreator

func (c *Client) IsRoomCreator(roomID matrix.RoomID) bool

IsRoomCreator returns true if the current user is the user who made this room.

func (*Client) MarkRoomAsRead

func (c *Client) MarkRoomAsRead(roomID matrix.RoomID, eventID matrix.EventID) error

MarkRoomAsRead sends to the server that the current user has seen up to the given event in the given room.

func (*Client) MemberName

func (c *Client) MemberName(
	roomID matrix.RoomID, userID matrix.UserID, check bool) (MemberName, error)

MemberName calculates the display name of a member. Note that a user joining might invalidate some names if they share the same display name as disambiguation will become necessary.

Use the Client.MemberNames variant when generating member name for multiple users to reduce duplicate work.

If check is true, then the MemberName's Ambiguous field will be set to true if the display name collides with someone else's. This check is quite expensive, so it should only be enabled when needed.

func (*Client) MessageMediaURL

func (c *Client) MessageMediaURL(msg *event.RoomMessageEvent) (string, error)

MessageMediaURL gets the message's media URL, if any.

func (*Client) NotifyMessage

func (c *Client) NotifyMessage(msg *event.RoomMessageEvent, action NotifyMessageAction) NotifyMessageAction

NotifyMessage returns true if msg should be notified with action. The returned NotifyMessageAction contains enabled bits for the actions that the found rule wants.

Note that this isn't perfect: only a single rule is accounted for, which is the first one that happens to match the message, so some conditions may be missed.

func (*Client) Offline

func (c *Client) Offline() *Client

Offline returns a Client that does not use the API.

func (*Client) Online

func (c *Client) Online(ctx context.Context) *Client

Online returns a Client that uses the given context instead of the cancelled context. It is an alias to WithContext; the only difference is that the name implies the client may be offline prior to this call.

func (*Client) Open

func (c *Client) Open() error

Open opens the client with the last next batch string.

func (*Client) Redact

func (c *Client) Redact(roomID matrix.RoomID, ev matrix.EventID, reason string) error

Redact redacts a room event.

func (*Client) RoomCountUnread

func (c *Client) RoomCountUnread(roomID matrix.RoomID) (n int, more bool)

RoomCountUnread counts the number of unread events in a room. More is true if the user has never seen any of the messages in the room. The user should display that info as "${n}+" with the trailing plus.

func (*Client) RoomEnsureMembers

func (c *Client) RoomEnsureMembers(roomID matrix.RoomID) error

RoomEnsureMembers ensures that the given room has all its members fetched.

func (*Client) RoomEvent

func (c *Client) RoomEvent(roomID matrix.RoomID, typ event.Type) (event.Event, error)

RoomEvent queries the event with the given type. If the event type implies a state event, then the empty key is tried.

func (*Client) RoomIsSpace

func (c *Client) RoomIsSpace(roomID matrix.RoomID) bool

RoomIsSpace returns true if the room with the given ID is a space-room.

func (*Client) RoomLatestReadEvent

func (c *Client) RoomLatestReadEvent(roomID matrix.RoomID) matrix.EventID

RoomLatestReadEvent gets the latest read eventID. The event ID is an empty string if the user hasn't read anything.

func (*Client) RoomMembers

func (c *Client) RoomMembers(roomID matrix.RoomID) ([]event.RoomMemberEvent, error)

RoomMembers returns a list of room members.

func (*Client) RoomName

func (c *Client) RoomName(roomID matrix.RoomID) (string, error)

func (*Client) RoomPaginator

func (c *Client) RoomPaginator(roomID matrix.RoomID, limit int) *RoomPaginator

RoomPaginator returns a new paginator that can fetch messages from the bottom up.

func (*Client) RoomState

func (c *Client) RoomState(
	roomID matrix.RoomID, typ event.Type, key string) (event.StateEvent, error)

RoomState queries the internal State for the given RoomEvent. If the State does not have that event, it queries the homeserver directly.

func (*Client) RoomTimeline

func (c *Client) RoomTimeline(roomID matrix.RoomID) ([]event.RoomEvent, error)

RoomTimeline queries the state cache for the timeline of the given room. If it's not available, the API will be queried directly. The order of these events is guaranteed to be latest last.

func (*Client) RoomTimelineEvent added in v0.1.3

func (c *Client) RoomTimelineEvent(roomID matrix.RoomID, id matrix.EventID) (event.RoomEvent, error)

RoomTimelineEvent fetches a single room timeline event by its ID.

func (*Client) RoomType

func (c *Client) RoomType(roomID matrix.RoomID) string

RoomType returns the room's type. An empty string signifies a regular room.

func (*Client) Rooms

func (c *Client) Rooms() ([]matrix.RoomID, error)

Rooms returns the list of rooms the user is in.

func (*Client) ScaledThumbnail

func (c *Client) ScaledThumbnail(mURL matrix.URL, w, h, scale int) (string, error)

ScaledThumbnail is like Thumbnail, except the image URL in the image respects the original aspect ratio and not the requested one.

func (*Client) SendRoomEvent

func (c *Client) SendRoomEvent(roomID matrix.RoomID, ev event.Event) error

SendRoomEvent is a convenient function around RoomEventSend.

func (*Client) SquareThumbnail

func (c *Client) SquareThumbnail(mURL matrix.URL, size, scale int) (string, error)

SquareThumbnail is a helper function around MediaThumbnailURL. The given size is assumed to be a square, and the size will be scaled up to the next power of 2 and multiplied up for ensured HiDPI support of up to 2x.

func (*Client) Thumbnail

func (c *Client) Thumbnail(mURL matrix.URL, w, h, scale int) (string, error)

Thumbnail is a helper function around MediaThumbnailURL. It works similarly to SquareThumbnail, except the dimensions are unchanged.

func (*Client) UpdateRoomTags

func (c *Client) UpdateRoomTags(roomID matrix.RoomID) error

UpdateRoomTags updates the internal state with the latest room tag information.

func (*Client) UserEvent

func (c *Client) UserEvent(typ event.Type) (event.Event, error)

UserEvent gets the user event from the state or the API.

func (*Client) Whoami

func (c *Client) Whoami() (matrix.UserID, error)

Whoami is a cached version of the Whoami method.

func (*Client) WithContext

func (c *Client) WithContext(ctx context.Context) *Client

WithContext replaces the client's internal context with the given one.

type ClientAuth

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

ClientAuth holds a partial client.

func Discover

func Discover(serverName string, opts Opts) (*ClientAuth, error)

Discover wraps around gotrix.DiscoverWithClienT.

func (*ClientAuth) LoginMethods

func (a *ClientAuth) LoginMethods() ([]matrix.LoginMethod, error)

LoginMethods returns the login methods supported by the homeserver.

func (*ClientAuth) LoginPassword

func (a *ClientAuth) LoginPassword(username, password string) (*Client, error)

LoginPassword authenticates the client using the provided username and password.

func (*ClientAuth) LoginSSO

func (a *ClientAuth) LoginSSO(done func(*Client, error)) (string, error)

LoginSSO returns the HTTP address for logging in as SSO and the channel indicating if the user is done or not.

func (*ClientAuth) LoginToken

func (a *ClientAuth) LoginToken(token string) (*Client, error)

LoginToken authenticates the client using the provided token.

func (*ClientAuth) WithContext

func (a *ClientAuth) WithContext(ctx context.Context) *ClientAuth

WithContext creates a copy of ClientAuth that uses the provided context.

type ConfigPather added in v0.1.3

type ConfigPather interface {
	ConfigPath(tails ...string) string
}

ConfigPather is an interface describing any instance that can generate a ConfigPath for gotktrix. Realistically, app.Application implements this.

type ImageFlags added in v0.1.3

type ImageFlags uint8

ImageFlags is describes boolean attributes for fetching Matrix images.

const (
	// ImageNormal is the 0 flag.
	ImageNormal ImageFlags = 0
	// MatrixNoCrop asks the server to scale the image down to fit the frame
	// instead of cropping the image.
	ImageNoCrop ImageFlags = 1 << (iota - 1)
	// ImageSkip1xScale skips the 1x scale factor. This is useful if the
	// specified image size is large enough for either 1x or 2x, since it works
	// better with the image cache.
	ImageSkip1xScale
)

func (ImageFlags) Has added in v0.1.3

func (f ImageFlags) Has(this ImageFlags) bool

Has returns true if f has this.

type MemberName

type MemberName struct {
	Name      string
	Ambiguous bool
}

MemberName describes a member name.

type NotifyMessageAction

type NotifyMessageAction uint8

NotifyMessageAction is a simple enum to determine what kind of notification action the application should do.

const (
	NotifyMessage NotifyMessageAction = 1 << iota
	NotifySoundMessage
	HighlightMessage
)

type Opts added in v0.1.3

type Opts struct {
	Client     httputil.Client
	ConfigPath ConfigPather
}

Opts describes the client options when constructing.

type PowerAction

type PowerAction uint8

PowerAction describes 1 out of the 4 actions in a PowerLevels event.

const (
	BanAction PowerAction
	InviteAction
	KickAction
	RedactAction
)

type RoomPaginator

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

RoomPaginator is used to fetch older messages from the API client.

func (*RoomPaginator) Paginate

func (p *RoomPaginator) Paginate(ctx context.Context) ([]event.RoomEvent, error)

Paginate paginates from the client and the server if the database is drained.

Directories

Path Synopsis
events
emojis
Package emojis provides an implementation of the im.ponies emoji protocol.
Package emojis provides an implementation of the im.ponies emoji protocol.
m
Package m provides Matrix-namespace events.
Package m provides Matrix-namespace events.
pronouns
Package pronouns provides an implementation of the pronouns personal protocol.
Package pronouns provides an implementation of the pronouns personal protocol.
sys
Package sys works around the shortcomings of gotrix/event.
Package sys works around the shortcomings of gotrix/event.
internal
db

Jump to

Keyboard shortcuts

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