mirror of
				https://github.com/matrix-org/gomatrix
				synced 2025-11-03 22:08:04 +00:00 
			
		
		
		
	Merge branch 'master' of github.com:kanopeld/gomatrix into kanopeld/room-aliases
This commit is contained in:
		
						commit
						34a43c9a39
					
				
					 9 changed files with 53 additions and 5139 deletions
				
			
		
							
								
								
									
										18
									
								
								client.go
									
										
									
									
									
								
							
							
						
						
									
										18
									
								
								client.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
// 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()
 | 
			
		||||
	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)
 | 
			
		||||
	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
 | 
			
		||||
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-text
 | 
			
		||||
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})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
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"})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
func (cli *Client) SendImage(roomID, body, url string) (*RespSendEvent, error) {
 | 
			
		||||
	return cli.SendMessageEvent(roomID, "m.room.message",
 | 
			
		||||
	return cli.SendMessageEvent(roomID, MessageEventType,
 | 
			
		||||
		ImageMessage{
 | 
			
		||||
			MsgType: ImageMessageType,
 | 
			
		||||
			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
 | 
			
		||||
// See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-video
 | 
			
		||||
func (cli *Client) SendVideo(roomID, body, url string) (*RespSendEvent, error) {
 | 
			
		||||
	return cli.SendMessageEvent(roomID, "m.room.message",
 | 
			
		||||
	return cli.SendMessageEvent(roomID, MessageEventType,
 | 
			
		||||
		VideoMessage{
 | 
			
		||||
			MsgType: VideoMessageType,
 | 
			
		||||
			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
 | 
			
		||||
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-notice
 | 
			
		||||
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})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
// 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
 | 
			
		||||
func (cli *Client) StateEvent(roomID, eventType, stateKey string, outContent interface{}) (err error) {
 | 
			
		||||
	u := cli.BuildURL("rooms", roomID, "state", eventType, stateKey)
 | 
			
		||||
func (cli *Client) StateEvent(roomID string, eventType EventType, stateKey string, outContent interface{}) (err error) {
 | 
			
		||||
	u := cli.BuildURL("rooms", roomID, "state", eventType.String(), stateKey)
 | 
			
		||||
	err = cli.MakeRequest(http.MethodGet, u, nil, outContent)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,7 +10,7 @@ func Example_sync() {
 | 
			
		|||
	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
 | 
			
		||||
	syncer := cli.Syncer.(*DefaultSyncer)
 | 
			
		||||
	syncer.OnEventType("m.room.message", func(ev *Event) {
 | 
			
		||||
	syncer.OnEventType(MessageEventType, func(ev *Event) {
 | 
			
		||||
		fmt.Println("Message: ", ev)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -80,7 +80,7 @@ func ExampleClient_StateEvent() {
 | 
			
		|||
		Name string `json:"name"`
 | 
			
		||||
	}{}
 | 
			
		||||
	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)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -76,7 +76,7 @@ func TestClient_StateEvent(t *testing.T) {
 | 
			
		|||
		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())
 | 
			
		||||
	}
 | 
			
		||||
	if content.Name != "Room Name Goes Here" {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										33
									
								
								event_types.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								event_types.go
									
										
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -9,7 +9,7 @@ import (
 | 
			
		|||
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
 | 
			
		||||
	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
 | 
			
		||||
	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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										1407
									
								
								requests_easyjson.go
									
										
									
									
									
								
							
							
						
						
									
										1407
									
								
								requests_easyjson.go
									
										
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										6
									
								
								room.go
									
										
									
									
									
								
							
							
						
						
									
										6
									
								
								room.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -3,7 +3,7 @@ package gomatrix
 | 
			
		|||
// Room represents a single Matrix room.
 | 
			
		||||
type Room struct {
 | 
			
		||||
	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
 | 
			
		||||
| 
						 | 
				
			
			@ -30,7 +30,7 @@ func (room Room) UpdateState(event *Event) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// 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]
 | 
			
		||||
	event := stateEventMap[stateKey]
 | 
			
		||||
	return event
 | 
			
		||||
| 
						 | 
				
			
			@ -58,6 +58,6 @@ func NewRoom(roomID string) *Room {
 | 
			
		|||
	// Init the State map and return a pointer to the Room
 | 
			
		||||
	return &Room{
 | 
			
		||||
		ID:    roomID,
 | 
			
		||||
		State: make(map[string]map[string]*Event),
 | 
			
		||||
		State: make(map[EventType]map[string]*Event),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										8
									
								
								sync.go
									
										
									
									
									
								
							
							
						
						
									
										8
									
								
								sync.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -25,7 +25,7 @@ type Syncer interface {
 | 
			
		|||
type DefaultSyncer struct {
 | 
			
		||||
	UserID    string
 | 
			
		||||
	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.
 | 
			
		||||
| 
						 | 
				
			
			@ -36,7 +36,7 @@ func NewDefaultSyncer(userID string, store Storer) *DefaultSyncer {
 | 
			
		|||
	return &DefaultSyncer{
 | 
			
		||||
		UserID:    userID,
 | 
			
		||||
		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.
 | 
			
		||||
// 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]
 | 
			
		||||
	if !exists {
 | 
			
		||||
		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 i := len(roomData.Timeline.Events) - 1; i >= 0; 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"]
 | 
			
		||||
				mship, ok := m.(string)
 | 
			
		||||
				if !ok {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue