Merge branch 'master' of github.com:kanopeld/gomatrix into kanopeld/room-aliases

This commit is contained in:
Ilya Beltsiukou 2021-04-02 21:55:09 +03:00
commit 34a43c9a39
9 changed files with 53 additions and 5139 deletions

View File

@ -548,9 +548,9 @@ func (cli *Client) SetStatus(presence, status string) (err error) {
// SendMessageEvent sends a message event into a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid // SendMessageEvent sends a message event into a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
// contentJSON should be a pointer to something that can be encoded as JSON using json.Marshal. // contentJSON should be a pointer to something that can be encoded as JSON using json.Marshal.
func (cli *Client) SendMessageEvent(roomID string, eventType string, contentJSON interface{}) (resp *RespSendEvent, err error) { func (cli *Client) SendMessageEvent(roomID string, eventType EventType, contentJSON interface{}) (resp *RespSendEvent, err error) {
txnID := txnID() txnID := txnID()
urlPath := cli.BuildURL("rooms", roomID, "send", eventType, txnID) urlPath := cli.BuildURL("rooms", roomID, "send", eventType.String(), txnID)
err = cli.MakeRequest(http.MethodPut, urlPath, contentJSON, &resp) err = cli.MakeRequest(http.MethodPut, urlPath, contentJSON, &resp)
return return
} }
@ -566,21 +566,21 @@ func (cli *Client) SendStateEvent(roomID, eventType, stateKey string, contentJSO
// SendText sends an m.room.message event into the given room with a msgtype of m.text // SendText sends an m.room.message event into the given room with a msgtype of m.text
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-text // See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-text
func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) { func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) {
return cli.SendMessageEvent(roomID, "m.room.message", return cli.SendMessageEvent(roomID, MessageEventType,
TextMessage{MsgType: TextMessageType, Body: text}) TextMessage{MsgType: TextMessageType, Body: text})
} }
// SendFormattedText sends an m.room.message event into the given room with a msgtype of m.text, supports a subset of HTML for formatting. // SendFormattedText sends an m.room.message event into the given room with a msgtype of m.text, supports a subset of HTML for formatting.
// See https://matrix.org/docs/spec/client_server/r0.6.0#m-text // See https://matrix.org/docs/spec/client_server/r0.6.0#m-text
func (cli *Client) SendFormattedText(roomID, text, formattedText string) (*RespSendEvent, error) { func (cli *Client) SendFormattedText(roomID, text, formattedText string) (*RespSendEvent, error) {
return cli.SendMessageEvent(roomID, "m.room.message", return cli.SendMessageEvent(roomID, MessageEventType,
TextMessage{MsgType: TextMessageType, Body: text, FormattedBody: formattedText, Format: "org.matrix.custom.html"}) TextMessage{MsgType: TextMessageType, Body: text, FormattedBody: formattedText, Format: "org.matrix.custom.html"})
} }
// SendImage sends an m.room.message event into the given room with a msgtype of m.image // SendImage sends an m.room.message event into the given room with a msgtype of m.image
// See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-image // See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-image
func (cli *Client) SendImage(roomID, body, url string) (*RespSendEvent, error) { func (cli *Client) SendImage(roomID, body, url string) (*RespSendEvent, error) {
return cli.SendMessageEvent(roomID, "m.room.message", return cli.SendMessageEvent(roomID, MessageEventType,
ImageMessage{ ImageMessage{
MsgType: ImageMessageType, MsgType: ImageMessageType,
Body: body, Body: body,
@ -591,7 +591,7 @@ func (cli *Client) SendImage(roomID, body, url string) (*RespSendEvent, error) {
// SendVideo sends an m.room.message event into the given room with a msgtype of m.video // SendVideo sends an m.room.message event into the given room with a msgtype of m.video
// See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-video // See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-video
func (cli *Client) SendVideo(roomID, body, url string) (*RespSendEvent, error) { func (cli *Client) SendVideo(roomID, body, url string) (*RespSendEvent, error) {
return cli.SendMessageEvent(roomID, "m.room.message", return cli.SendMessageEvent(roomID, MessageEventType,
VideoMessage{ VideoMessage{
MsgType: VideoMessageType, MsgType: VideoMessageType,
Body: body, Body: body,
@ -602,7 +602,7 @@ func (cli *Client) SendVideo(roomID, body, url string) (*RespSendEvent, error) {
// SendNotice sends an m.room.message event into the given room with a msgtype of m.notice // SendNotice sends an m.room.message event into the given room with a msgtype of m.notice
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-notice // See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-notice
func (cli *Client) SendNotice(roomID, text string) (*RespSendEvent, error) { func (cli *Client) SendNotice(roomID, text string) (*RespSendEvent, error) {
return cli.SendMessageEvent(roomID, "m.room.message", return cli.SendMessageEvent(roomID, MessageEventType,
TextMessage{MsgType: NoticeMessageType, Body: text}) TextMessage{MsgType: NoticeMessageType, Body: text})
} }
@ -691,8 +691,8 @@ func (cli *Client) UserTyping(roomID string, typing bool, timeout int64) (resp *
// StateEvent gets a single state event in a room. It will attempt to JSON unmarshal into the given "outContent" struct with // StateEvent gets a single state event in a room. It will attempt to JSON unmarshal into the given "outContent" struct with
// the HTTP response body, or return an error. // the HTTP response body, or return an error.
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype-statekey // See http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype-statekey
func (cli *Client) StateEvent(roomID, eventType, stateKey string, outContent interface{}) (err error) { func (cli *Client) StateEvent(roomID string, eventType EventType, stateKey string, outContent interface{}) (err error) {
u := cli.BuildURL("rooms", roomID, "state", eventType, stateKey) u := cli.BuildURL("rooms", roomID, "state", eventType.String(), stateKey)
err = cli.MakeRequest(http.MethodGet, u, nil, outContent) err = cli.MakeRequest(http.MethodGet, u, nil, outContent)
return return
} }

View File

@ -10,7 +10,7 @@ func Example_sync() {
cli.Store.SaveFilterID("@example:matrix.org", "2") // Optional: if you know it already cli.Store.SaveFilterID("@example:matrix.org", "2") // Optional: if you know it already
cli.Store.SaveNextBatch("@example:matrix.org", "111_222_333_444") // Optional: if you know it already cli.Store.SaveNextBatch("@example:matrix.org", "111_222_333_444") // Optional: if you know it already
syncer := cli.Syncer.(*DefaultSyncer) syncer := cli.Syncer.(*DefaultSyncer)
syncer.OnEventType("m.room.message", func(ev *Event) { syncer.OnEventType(MessageEventType, func(ev *Event) {
fmt.Println("Message: ", ev) fmt.Println("Message: ", ev)
}) })
@ -80,7 +80,7 @@ func ExampleClient_StateEvent() {
Name string `json:"name"` Name string `json:"name"`
}{} }{}
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456") cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456")
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil { if err := cli.StateEvent("!foo:bar", NameEventType, "", &content); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -76,7 +76,7 @@ func TestClient_StateEvent(t *testing.T) {
Name string `json:"name"` Name string `json:"name"`
}{} }{}
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil { if err := cli.StateEvent("!foo:bar", NameEventType, "", &content); err != nil {
t.Fatalf("StateEvent: error, got %s", err.Error()) t.Fatalf("StateEvent: error, got %s", err.Error())
} }
if content.Name != "Room Name Goes Here" { if content.Name != "Room Name Goes Here" {

33
event_types.go Normal file
View File

@ -0,0 +1,33 @@
package gomatrix
type EventType string
const (
AliasesEventType EventType = "m.room.aliases"
CanonicalAliasEventType EventType = "m.room.canonical_alias"
CreateEventType EventType = "m.room.create"
JoinRulesEventType EventType = "m.room.join_rules"
MemberEventType EventType = "m.room.member"
PowerLevelsEventType EventType = "m.room.power_levels"
RedactionEventType EventType = "m.room.redaction"
MessageEventType EventType = "m.room.message"
MessageFeedbackEventType EventType = "m.room.message.feedback"
NameEventType EventType = "m.room.name"
TopicEventType EventType = "m.room.topic"
AvatarEventType EventType = "m.room.avatar"
TypingEventType EventType = "m.typing"
ReceiptEventType EventType = "m.receipt"
PresenceEventType EventType = "m.presence"
HistoryVisibilityEventType EventType = "m.room.history_visibility"
ThirdPartyInviteEventType EventType = "m.room.third_party_invite"
GuestAccessEventType EventType = "m.room.guest_access"
TagEventType EventType = "m.tag"
)
func (e EventType) String() string {
return string(e)
}
func (e EventType) KindOf(target EventType) bool {
return e == target
}

View File

@ -9,7 +9,7 @@ import (
type Event struct { type Event struct {
StateKey *string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events. 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 Sender string `json:"sender"` // The user ID of the sender of the event
Type string `json:"type"` // The event type Type EventType `json:"type"` // The event type
Timestamp int64 `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server 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 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) RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@ package gomatrix
// Room represents a single Matrix room. // Room represents a single Matrix room.
type Room struct { type Room struct {
ID string ID string
State map[string]map[string]*Event State map[EventType]map[string]*Event
} }
// PublicRoom represents the information about a public room obtainable from the room directory // PublicRoom represents the information about a public room obtainable from the room directory
@ -30,7 +30,7 @@ func (room Room) UpdateState(event *Event) {
} }
// GetStateEvent returns the state event for the given type/state_key combo, or nil. // GetStateEvent returns the state event for the given type/state_key combo, or nil.
func (room Room) GetStateEvent(eventType string, stateKey string) *Event { func (room Room) GetStateEvent(eventType EventType, stateKey string) *Event {
stateEventMap := room.State[eventType] stateEventMap := room.State[eventType]
event := stateEventMap[stateKey] event := stateEventMap[stateKey]
return event return event
@ -58,6 +58,6 @@ func NewRoom(roomID string) *Room {
// Init the State map and return a pointer to the Room // Init the State map and return a pointer to the Room
return &Room{ return &Room{
ID: roomID, ID: roomID,
State: make(map[string]map[string]*Event), State: make(map[EventType]map[string]*Event),
} }
} }

View File

@ -25,7 +25,7 @@ type Syncer interface {
type DefaultSyncer struct { type DefaultSyncer struct {
UserID string UserID string
Store Storer Store Storer
listeners map[string][]OnEventListener // event type to listeners array listeners map[EventType][]OnEventListener // event type to listeners array
} }
// OnEventListener can be used with DefaultSyncer.OnEventType to be informed of incoming events. // OnEventListener can be used with DefaultSyncer.OnEventType to be informed of incoming events.
@ -36,7 +36,7 @@ func NewDefaultSyncer(userID string, store Storer) *DefaultSyncer {
return &DefaultSyncer{ return &DefaultSyncer{
UserID: userID, UserID: userID,
Store: store, Store: store,
listeners: make(map[string][]OnEventListener), listeners: make(map[EventType][]OnEventListener),
} }
} }
@ -92,7 +92,7 @@ func (s *DefaultSyncer) ProcessResponse(res *RespSync, since string) (err error)
// OnEventType allows callers to be notified when there are new events for the given event type. // OnEventType allows callers to be notified when there are new events for the given event type.
// There are no duplicate checks. // There are no duplicate checks.
func (s *DefaultSyncer) OnEventType(eventType string, callback OnEventListener) { func (s *DefaultSyncer) OnEventType(eventType EventType, callback OnEventListener) {
_, exists := s.listeners[eventType] _, exists := s.listeners[eventType]
if !exists { if !exists {
s.listeners[eventType] = []OnEventListener{} s.listeners[eventType] = []OnEventListener{}
@ -116,7 +116,7 @@ func (s *DefaultSyncer) shouldProcessResponse(resp *RespSync, since string) bool
for roomID, roomData := range resp.Rooms.Join { for roomID, roomData := range resp.Rooms.Join {
for i := len(roomData.Timeline.Events) - 1; i >= 0; i-- { for i := len(roomData.Timeline.Events) - 1; i >= 0; i-- {
e := roomData.Timeline.Events[i] e := roomData.Timeline.Events[i]
if e.Type == "m.room.member" && e.StateKey != nil && *e.StateKey == s.UserID { if e.Type == MemberEventType && e.StateKey != nil && *e.StateKey == s.UserID {
m := e.Content["membership"] m := e.Content["membership"]
mship, ok := m.(string) mship, ok := m.(string)
if !ok { if !ok {