Documentation
¶
Index ¶
- func AggregateSigningKeysRaw(pdus []federation.PDU) (keysToFetch map[string]map[id.KeyID]time.Time, keyCount int)
- func BulkIntoFederation(evts []*pdu.PDU, roomVersion id.RoomVersion) ([]json.RawMessage, error)
- func CheckPassword(raw, hashed string) bool
- func ComputeChecksumStream(reader io.ReadSeeker) (string, error)
- func CopyAndSumStream(src io.Reader, dst io.Writer) (sum string, size int64, err error)
- func EventID(roomVersion id.RoomVersion, evt *pdu.PDU) id.EventID
- func FederationToPDU(evt federation.PDU) (*pdu.PDU, error)
- func GenerateThumbnail(fd io.ReadSeeker, maxWidth, maxHeight int) (*bytes.Buffer, string, error)
- func GetDimensions(fd io.Reader) (width, height *int64)
- func GetSalt() []byte
- func HashPassword(raw string) string
- func IntoFederation(evt *pdu.PDU, roomVersion id.RoomVersion) (json.RawMessage, error)
- func LimitedRead(body io.Reader, maxBytes int) ([]byte, error)
- func LockContext(lock Lockable, ctx context.Context) bool
- func LockWithTimeout(lock Lockable, ctx context.Context, timeout time.Duration) bool
- func RLockContext(lock *sync.RWMutex, ctx context.Context) bool
- func RLockWithTimeout(lock *sync.RWMutex, ctx context.Context, timeout time.Duration) bool
- func RealIP(r *http.Request) (net.IP, error)
- func RoomID(evt *pdu.PDU) id.RoomID
- func ServerAllowedByACL(serverName string, content *event.ServerACLEventContent) bool
- func SizeSeek(r io.ReadSeeker) (int64, error)
- func UnmarshalJSON(body io.Reader, v any, maxSize int) error
- func VerifyStream(reader io.ReadSeeker, checksum string) (bool, error)
- type HumanDuration
- type ITask
- type KVCache
- func (lc *KVCache[K, V]) Add(key K, value V) *KVCache[K, V]
- func (lc *KVCache[K, V]) Capacity() int
- func (lc *KVCache[K, V]) Clear()
- func (lc *KVCache[K, V]) EvictExpired()
- func (lc *KVCache[K, V]) EvictOldest()
- func (lc *KVCache[K, V]) EvictionPriority(key K) int
- func (lc *KVCache[K, V]) Filter(predicate func(key K, value V) bool) map[K]V
- func (lc *KVCache[K, V]) Get(key K) (V, bool)
- func (lc *KVCache[K, V]) Remove(key K)
- func (lc *KVCache[K, V]) Set(key K, value V) *KVCache[K, V]
- func (lc *KVCache[K, V]) Size() int
- func (lc *KVCache[K, V]) UpdateCapacity(newCapacity int)
- type LockMap
- type Lockable
- type MaybeWriter
- type Notifier
- type RWLockMap
- type Task
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AggregateSigningKeysRaw ¶
func AggregateSigningKeysRaw(pdus []federation.PDU) (keysToFetch map[string]map[id.KeyID]time.Time, keyCount int)
AggregateSigningKeysRaw aggregates signing keys to fetch from a list of raw federation PDUs This is more efficient than parsing the PDUs fully as it inspects the JSON bytes directly, avoiding any overhead from unmarshalling into structs.
func BulkIntoFederation ¶
func BulkIntoFederation(evts []*pdu.PDU, roomVersion id.RoomVersion) ([]json.RawMessage, error)
BulkIntoFederation converts a list of PDUs into their JSON representation suitable for federation transmission.
func CheckPassword ¶
CheckPassword checks if the provided password matches the hashed password The hashed password is expected to be a base64 encoded string raw is the unhashed, user provided password to check.
func ComputeChecksumStream ¶
func ComputeChecksumStream(reader io.ReadSeeker) (string, error)
ComputeChecksumStream computes the SHA-256 checksum of the data read from the provided stream. The stream must implement io.ReadSeeker to allow resetting to the beginning. It returns the checksum as a hexadecimal string.
func CopyAndSumStream ¶
CopyAndSumStream copies data from src to dst while computing the SHA-256 checksum of the data. It returns the computed checksum as a hexadecimal string.
func EventID ¶
EventID gets the event ID from a pdu.PDU, either using its internal cached version, or generating it fresh. Returns an empty string if the ID couldn't be generated if roomVersion is an empty string, only the internal eventCache will be queried.
func FederationToPDU ¶
func FederationToPDU(evt federation.PDU) (*pdu.PDU, error)
FederationToPDU converts a federation PDU into a pdu.PDU. This is a glorified json unmarshal.
func GenerateThumbnail ¶
GenerateThumbnail generates a thumbnail for the given file descriptor, resizing it as close to {width}x{height} while preserving the aspect ratio. It returns a reader for the thumbnail, the content type of the thumbnail, and an error if any occurred during the process.
func GetDimensions ¶
GetDimensions fetches the dimensions of the provided fd. It consumes the entire reader. If the dimensions cannot be determined, (nil, nil) is returned.
func HashPassword ¶
HashPassword hashes the raw password using Argon2id, and returns the hashed password The password is returned as a base64 encoded string.
func IntoFederation ¶
func IntoFederation(evt *pdu.PDU, roomVersion id.RoomVersion) (json.RawMessage, error)
IntoFederation converts a PDU into its JSON representation suitable for federation transmission.
func LimitedRead ¶
LimitedRead limits the number of bytes read from the io.Reader to maxBytes. If there are unconsumed bytes, mautrix.MTooLarge is returned.
func LockContext ¶
LockContext attempts to lock a Lockable (RW/Mutex), but will return false if the provided context is done before the lock is acquired. If the lock is acquired, it returns true. The caller is responsible for unlocking the mutex if true is returned.
func LockWithTimeout ¶
LockWithTimeout attempts to lock a Lockable (RW/Mutex), but will return false if the timeout is reached before the lock is acquired. If the lock is acquired, it returns true. The caller is responsible for unlocking the mutex if true is returned.
func RLockContext ¶
RLockContext attempts to lock a sync.RWMutex for reading, but will return false if the provided context is done before the lock is acquired. If the lock is acquired, it returns true. The caller is responsible for unlocking the mutex if true is returned.
func RLockWithTimeout ¶
RLockWithTimeout attempts to lock a sync.RWMutex for reading, but will return false if the timeout is reached before the lock is acquired. If the lock is acquired, it returns true. The caller is responsible for unlocking the mutex if true is returned.
func RoomID ¶
RoomID extracts the canonical room ID for the given event. For normal events, this is just evt.RoomID. For m.room.create events of room versions where the room ID is the event ID of the create event, this function returns the event ID instead.
func ServerAllowedByACL ¶
func ServerAllowedByACL(serverName string, content *event.ServerACLEventContent) bool
ServerAllowedByACL checks if a server is allowed by the given ACL.
func SizeSeek ¶
func SizeSeek(r io.ReadSeeker) (int64, error)
SizeSeek finds the length of an io.ReadSeeker by seeking to the end and back to the original position.
func UnmarshalJSON ¶
UnmarshalJSON reads JSON data from the io.Reader and unmarshals it into the provided interface. If the JSON cannot be unmarshalled, mautrix.MNotJSON is returned.
func VerifyStream ¶
func VerifyStream(reader io.ReadSeeker, checksum string) (bool, error)
VerifyStream compares the SHA-256 checksum of the provided stream with the given checksum string.
Types ¶
type HumanDuration ¶
HumanDuration is a wrapper around time.Duration that supports marshalling and unmarshalling.
func (*HumanDuration) MarshalJSON ¶
func (d *HumanDuration) MarshalJSON() ([]byte, error)
func (*HumanDuration) MarshalYAML ¶
func (d *HumanDuration) MarshalYAML() (any, error)
func (*HumanDuration) UnmarshalJSON ¶
func (d *HumanDuration) UnmarshalJSON(data []byte) error
func (*HumanDuration) UnmarshalYAML ¶
func (d *HumanDuration) UnmarshalYAML(unmarshal func(any) error) error
type KVCache ¶
type KVCache[K comparable, V any] struct { Mutex sync.RWMutex TTL time.Duration // TTL is exported because it can safely be changed on the fly // contains filtered or unexported fields }
KVCache is a simple key-value cache with a fixed capacity and optional TTL.
NOTE: You should only ever set pointer types in this cache to avoid excessive copying. While this is not enforced, I will be sad if you don't follow this advice.
NOTE 2: Time-based automatic cache eviction is not handled here. The caller is responsible if they wish to periodically evict expired caches. Otherwise, the usual on-demand eviction based on capacity will occur.
func NewKVCache ¶
NewKVCache creates a new KVCache with the specified capacity. Capacity cannot be zero. TTL is the relative time-to-live duration for each entry, set to 0 to disable. Neither capacity nor ttl can be negative, and will panic.
func (*KVCache[K, V]) Add ¶
Add is similar to Set, but only adds the key-value pair if the key does not already exist.
func (*KVCache[K, V]) Clear ¶
func (lc *KVCache[K, V]) Clear()
Clear removes all entries from the cache.
func (*KVCache[K, V]) EvictExpired ¶
func (lc *KVCache[K, V]) EvictExpired()
EvictExpired removes all entries that have exceeded their TTL. The caller is responsible for acquiring a write lock.
func (*KVCache[K, V]) EvictOldest ¶
func (lc *KVCache[K, V]) EvictOldest()
EvictOldest removes eviction candidates until the cache size is under capacity. The caller is responsible for acquiring a write lock.
func (*KVCache[K, V]) EvictionPriority ¶
EvictionPriority returns the eviction priority of a key, ranging from 0 (best candidate) to $capacity (least likely). If the key does not exist, it returns -1. This function does not take into account TTL expiration.
func (*KVCache[K, V]) Filter ¶
Filter returns a map of all entries that satisfy the provided predicate function.
func (*KVCache[K, V]) Get ¶
Get retrieves a value from the cache by key. If the key does not exist or has expired, it returns nil and false.
func (*KVCache[K, V]) Remove ¶
func (lc *KVCache[K, V]) Remove(key K)
Remove manually removes an entry from the cache by key.
func (*KVCache[K, V]) Set ¶
Set adds a key-value pair to the cache. If the cache exceeds its capacity, the oldest entry is evicted.
func (*KVCache[K, V]) UpdateCapacity ¶
UpdateCapacity changes the capacity of the cache. This is a very expensive operation if the cache is in use.
type LockMap ¶
type LockMap[K comparable] struct { // contains filtered or unexported fields }
type MaybeWriter ¶
MaybeWriter is an io.Writer that discards writes if the underlying writer is nil or there is an error writing.
type Notifier ¶
type Notifier struct {
// contains filtered or unexported fields
}
Notifier allows waiting for notifications on specific keys. Like pub/sub but oh my god.
func NewNotifier ¶
NewNotifier creates a new Notifier instance.
type RWLockMap ¶
type RWLockMap[K comparable] struct { // contains filtered or unexported fields }
type Task ¶
type Task struct {
Name string
Callback func(ctx context.Context)
Interval time.Duration
Ticker *time.Ticker
// contains filtered or unexported fields
}
Task represents a scheduled task with a name, callback function, and interval.
func NewTask ¶
func NewTask(ctx context.Context, name string, interval time.Duration, callback func(ctx context.Context)) *Task
NewTask creates a new generic Task, initialising its fields, and forking the context to allow for cancellation.
func (*Task) Reset ¶
func (t *Task) Reset()
Reset stops the task if it's running and starts it again.