| 1 |
package matrix |
| 2 |
|
| 3 |
// Type definitions for Matrix API message types. |
| 4 |
type ( |
| 5 |
// messageType represents the type of a Matrix message. |
| 6 |
messageType string |
| 7 |
// flowType represents the type of a Matrix login flow. |
| 8 |
flowType string |
| 9 |
// identifierType represents the type of Matrix user identifier. |
| 10 |
identifierType string |
| 11 |
) |
| 12 |
|
| 13 |
// apiResLoginFlows represents the response from the Matrix login flows endpoint. |
| 14 |
type apiResLoginFlows struct { |
| 15 |
Flows []flow `json:"flows"` |
| 16 |
} |
| 17 |
|
| 18 |
// apiReqLogin represents a login request to the Matrix API. |
| 19 |
type apiReqLogin struct { |
| 20 |
Type flowType `json:"type"` |
| 21 |
Identifier *identifier `json:"identifier"` |
| 22 |
|
| 23 |
Password string `json:"password,omitempty"` |
| 24 |
Token string `json:"token,omitempty"` |
| 25 |
DeviceID string `json:"device_id,omitempty"` |
| 26 |
} |
| 27 |
|
| 28 |
// apiResLogin represents the response from a successful Matrix login request. |
| 29 |
type apiResLogin struct { |
| 30 |
AccessToken string `json:"access_token"` |
| 31 |
HomeServer string `json:"home_server"` |
| 32 |
UserID string `json:"user_id"` |
| 33 |
DeviceID string `json:"device_id"` |
| 34 |
} |
| 35 |
|
| 36 |
// apiReqSend represents a request to send a message to a Matrix room. |
| 37 |
type apiReqSend struct { |
| 38 |
MsgType messageType `json:"msgtype"` |
| 39 |
Body string `json:"body"` |
| 40 |
} |
| 41 |
|
| 42 |
// apiResRoom represents the response from joining a Matrix room. |
| 43 |
type apiResRoom struct { |
| 44 |
RoomID string `json:"room_id"` |
| 45 |
} |
| 46 |
|
| 47 |
// apiResJoinedRooms represents the response containing the list of joined rooms. |
| 48 |
type apiResJoinedRooms struct { |
| 49 |
Rooms []string `json:"joined_rooms"` |
| 50 |
} |
| 51 |
|
| 52 |
// apiResEvent represents the response from sending a message event. |
| 53 |
type apiResEvent struct { |
| 54 |
EventID string `json:"event_id"` |
| 55 |
} |
| 56 |
|
| 57 |
// apiResError represents an error response from the Matrix API. |
| 58 |
type apiResError struct { |
| 59 |
Message string `json:"error"` |
| 60 |
Code string `json:"errcode"` |
| 61 |
} |
| 62 |
|
| 63 |
// flow represents a Matrix login flow type. |
| 64 |
type flow struct { |
| 65 |
Type flowType `json:"type"` |
| 66 |
} |
| 67 |
|
| 68 |
// identifier represents a Matrix user identifier for authentication. |
| 69 |
type identifier struct { |
| 70 |
Type identifierType `json:"type"` |
| 71 |
User string `json:"user,omitempty"` |
| 72 |
} |
| 73 |
|
| 74 |
const ( |
| 75 |
// Matrix API endpoint paths. |
| 76 |
apiLogin = "/_matrix/client/v3/login" |
| 77 |
apiRoomJoin = "/_matrix/client/v3/join/%s" |
| 78 |
apiSendMessage = "/_matrix/client/v3/rooms/%s/send/m.room.message/%s" |
| 79 |
apiJoinedRooms = "/_matrix/client/v3/joined_rooms" |
| 80 |
|
| 81 |
contentType = "application/json" |
| 82 |
|
| 83 |
// msgTypeText is the Matrix message type for plain text messages. |
| 84 |
msgTypeText messageType = "m.text" |
| 85 |
// flowLoginPassword is the Matrix login flow type for password authentication. |
| 86 |
flowLoginPassword flowType = "m.login.password" |
| 87 |
//nolint:gosec // This is a Matrix API constant, not a hardcoded credential |
| 88 |
flowLoginToken flowType = "m.login.token" |
| 89 |
// idTypeUser is the Matrix identifier type for username-based authentication. |
| 90 |
idTypeUser identifierType = "m.id.user" |
| 91 |
|
| 92 |
// Matrix error code for rate limiting. |
| 93 |
errCodeLimitExceeded = "M_LIMIT_EXCEEDED" |
| 94 |
) |
| 95 |
|
| 96 |
// Error returns the error message from the Matrix API error response. |
| 97 |
func (e *apiResError) Error() string { |
| 98 |
return e.Message |
| 99 |
} |
| 100 |
|
| 101 |
// IsRateLimited checks if the error is a rate limiting error (M_LIMIT_EXCEEDED). |
| 102 |
func (e *apiResError) IsRateLimited() bool { |
| 103 |
return e.Code == errCodeLimitExceeded |
| 104 |
} |
| 105 |
|
| 106 |
// newUserIdentifier creates a new Matrix user identifier for authentication. |
| 107 |
func newUserIdentifier(user string) *identifier { |
| 108 |
return &identifier{ |
| 109 |
Type: idTypeUser, |
| 110 |
User: user, |
| 111 |
} |
| 112 |
} |