matrix

package module
v0.0.0-...-f6537d3 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: LGPL-3.0 Imports: 8 Imported by: 0

README

matrix

This is a minimal implementation of the endpoints matrix provides for communication inbetween the client and the server.

Functions

The basic functions availible are described below. I'm pretty sure that I won't update these farily often, so you're better of just using go doc for searching through the functionnns.

Login
func Login(username, password, homeserver string) (Authinfo, error)

Used to get an authinfo struct containing information needed by other function for functioning properly, such as the auth key needed to send messages.

Send

func Send(authinfo Authinfo, roomID string, message string) error
func SendImage(authinfo Authinfo, roomID string, image map[string]interface{}) error

Send and message or and image to a room you are in. (Sending images is done by providing a map with information on the image:

	image := map[string]interface{}{
		"msgtype": "m.image",
		"url":     mxc://matrix.emile.space/askhdlkajshdlkajhdslkjh,
		"info": map[string]interface{}{
			"h":        1080,
			"w":        1920,
			"mimetype": "image/jpeg",
			"size":     1337,
		},
		"body": "some text associated with the image",
}

Most important: the url, that actually isn't a url but a mxc URI. To get one upload your image using the upload function, you'll get that uri in the response.

Sync

func Sync(authinfo Authinfo) (RespSync, error)
func SyncPartial(authinfo Authinfo, nextBatch string, eventsChannel chan PackagedEvent) (RespSync, error)

There are two syncs available: Sync and syncPartial. Sync is used to syncronize the first time in order to get all events created. SyncPartial uses a token returned by the Sync function to fetch all events created since that token was given out (sort of a timestamp).

This allows incremental fetches.

Upload

func Upload(authinfo Authinfo, filename string, file *bytes.Buffer) (UploadResponse, error)

The upload function uploads the given buffer to the homeserver that is provided in the authinfo object. The response contains the mxc URI that can be used to send the uploaded image to another participant.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Send

func Send(authinfo Authinfo, roomID string, message string) error

Send allows sending messages

func SendImage

func SendImage(authinfo Authinfo, roomID string, image map[string]interface{}) error

SendImage sends the image with the given mxc ID to the room (currently hardcoded some lines below)

Types

type Authinfo

type Authinfo struct {
	UserID      string `json:"user_id"`
	HomeServer  string `json:"home_server"`
	DeviceID    string `json:"device_id"`
	AccessToken string `json:"access_token"`
}

Authinfo defines the fields returned after logging in

func Login

func Login(username, password, homeserver string) (Authinfo, error)

Login logs in to the homeserver and returns an Authinfo struct containing information such as the UserID, the HomeServer of the entity that was logged and most important: the AccessToken for further verification

type Event

type Event struct {
	StateKey    *string                `json:"state_key,omitempty"`    // The state key for the event. Only present on State Events.
	Sender      string                 `json:"sender"`                 // The user ID of the sender of the event
	Type        string                 `json:"type"`                   // The event type
	Timestamp   int64                  `json:"origin_server_ts"`       // The unix timestamp when this message was sent by the origin server
	ID          string                 `json:"event_id"`               // The unique ID of this event
	RoomID      string                 `json:"room_id"`                // The room the event was sent to. May be nil (e.g. for presence)
	Redacts     string                 `json:"redacts,omitempty"`      // The event ID that was redacted if a m.room.redaction event
	Unsigned    map[string]interface{} `json:"unsigned"`               // The unsigned portions of the event, such as age and prev_content
	Content     map[string]interface{} `json:"content"`                // The JSON content of the event.
	PrevContent map[string]interface{} `json:"prev_content,omitempty"` // The JSON prev_content of the event.
}

Event defines an event

type PackagedEvent

type PackagedEvent struct {
	RoomName string
	Event    Event
}

PackagedEvent bundles an event with more information regarding it, such as the roomname in which the event was produced.

type RespSync

type RespSync struct {
	NextBatch   string `json:"next_batch"`
	AccountData struct {
		Events []Event `json:"events"`
	} `json:"account_data"`
	Presence struct {
		Events []Event `json:"events"`
	} `json:"presence"`
	Rooms struct {
		Leave map[string]struct {
			State struct {
				Events []Event `json:"events"`
			} `json:"state"`
			Timeline struct {
				Events    []Event `json:"events"`
				Limited   bool    `json:"limited"`
				PrevBatch string  `json:"prev_batch"`
			} `json:"timeline"`
		} `json:"leave"`
		Join map[string]struct {
			State struct {
				Events []Event `json:"events"`
			} `json:"state"`
			Timeline struct {
				Events    []Event `json:"events"`
				Limited   bool    `json:"limited"`
				PrevBatch string  `json:"prev_batch"`
			} `json:"timeline"`
		} `json:"join"`
		Invite map[string]struct {
			State struct {
				Events []Event
			} `json:"invite_state"`
		} `json:"invite"`
	} `json:"rooms"`
}

RespSync defines the response from the sync

func Sync

func Sync(authinfo Authinfo) (RespSync, error)

Sync syncs

func SyncPartial

func SyncPartial(authinfo Authinfo, nextBatch string, eventsChannel chan PackagedEvent) (RespSync, error)

SyncPartial syncs the state using sync token obtained in a previous request as a timestamp

type UploadResponse

type UploadResponse struct {
	ContentURI string `json:"content_uri,omitempty"`
}

UploadResponse is the responce recieved from the upload

func Upload

func Upload(authinfo Authinfo, filename string, file *bytes.Buffer) (UploadResponse, error)

Upload uploads stuff to the matrix homeserver returning the files MXC

Jump to

Keyboard shortcuts

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