activematrix 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MatrixSdk
4
+ # A generic error raised for issues in the MatrixSdk
5
+ class MatrixError < StandardError
6
+ end
7
+
8
+ # An error specialized and raised for failed requests
9
+ class MatrixRequestError < MatrixError
10
+ attr_reader :code, :data, :httpstatus, :message
11
+ alias error message
12
+
13
+ def self.class_by_code(code)
14
+ code = code.to_i
15
+
16
+ return MatrixNotAuthorizedError if code == 401
17
+ return MatrixForbiddenError if code == 403
18
+ return MatrixNotFoundError if code == 404
19
+ return MatrixConflictError if code == 409
20
+ return MatrixTooManyRequestsError if code == 429
21
+
22
+ MatrixRequestError
23
+ end
24
+
25
+ def self.new_by_code(data, code)
26
+ class_by_code(code).new(data, code)
27
+ end
28
+
29
+ def initialize(error, status)
30
+ @code = error[:errcode]
31
+ @httpstatus = status
32
+ @message = error[:error]
33
+ @data = error.reject { |k, _v| %i[errcode error].include? k }
34
+
35
+ super error[:error]
36
+ end
37
+
38
+ def to_s
39
+ "HTTP #{httpstatus} (#{code}): #{message}"
40
+ end
41
+ end
42
+
43
+ class MatrixNotAuthorizedError < MatrixRequestError; end
44
+
45
+ class MatrixForbiddenError < MatrixRequestError; end
46
+
47
+ class MatrixNotFoundError < MatrixRequestError; end
48
+
49
+ class MatrixConflictError < MatrixRequestError; end
50
+
51
+ class MatrixTooManyRequestsError < MatrixRequestError; end
52
+
53
+ # An error raised when errors occur in the connection layer
54
+ class MatrixConnectionError < MatrixError
55
+ def self.class_by_code(code)
56
+ return MatrixTimeoutError if code == 504
57
+
58
+ MatrixConnectionError
59
+ end
60
+ end
61
+
62
+ class MatrixTimeoutError < MatrixConnectionError
63
+ end
64
+
65
+ # An error raised when the homeserver returns an unexpected response to the client
66
+ class MatrixUnexpectedResponseError < MatrixError
67
+ end
68
+ end
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MatrixSdk
4
+ class MXID
5
+ attr_accessor :sigil, :localpart, :domain, :port
6
+
7
+ # @param identifier [String] The Matrix ID string in the format of '&<localpart>:<domain>' where '&' is the sigil
8
+ def initialize(identifier)
9
+ raise ArgumentError, 'Identifier must be a String' unless identifier.is_a? String
10
+ raise ArgumentError, 'Identifier is too long' if identifier.size > 255
11
+ raise ArgumentError, 'Identifier lacks required data' unless identifier =~ %r{^([@!$+#][^:]+:[^:]+(?::\d+)?)|(\$[A-Za-z0-9+/]+)$}
12
+
13
+ # TODO: Community-as-a-Room / Profile-as-a-Room, in case they're going for room aliases
14
+ @sigil = identifier[0]
15
+ @localpart, @domain, @port = identifier[1..].split(':')
16
+ @port = @port.to_i if @port
17
+
18
+ raise ArgumentError, 'Identifier is not a valid MXID' unless valid?
19
+ end
20
+
21
+ # Gets the homeserver part of the ID
22
+ #
23
+ # @example A simple MXID
24
+ # id = MXID.new('@alice:example.org')
25
+ # id.homeserver
26
+ # # => 'example.org'
27
+ #
28
+ # @example A fully qualified MXID
29
+ # id = MXID.new('@user:some.direct.domain:443')
30
+ # id.homeserver
31
+ # # => 'some.direct.domain:443'
32
+ #
33
+ # @return [String]
34
+ def homeserver
35
+ port_s = port ? ":#{port}" : ''
36
+ domain ? domain + port_s : ''
37
+ end
38
+
39
+ # Gets the homserver part of the ID as a suffix (':homeserver')
40
+ def homeserver_suffix
41
+ ":#{homeserver}" if domain
42
+ end
43
+
44
+ def to_s
45
+ "#{sigil}#{localpart}#{homeserver_suffix}"
46
+ end
47
+
48
+ alias to_str to_s
49
+
50
+ # Returns the type of the ID
51
+ #
52
+ # @return [Symbol] The MXID type, one of (:user_id, :room_id, :event_id, :group_id, or :room_alias)
53
+ def type
54
+ {
55
+ '@' => :user_id,
56
+ '!' => :room_id,
57
+ '$' => :event_id,
58
+ '+' => :group_id,
59
+ '#' => :room_alias
60
+ }[sigil]
61
+ end
62
+
63
+ # Checks if the ID is valid
64
+ #
65
+ # @return [Boolean] If the ID is a valid Matrix ID
66
+ def valid?
67
+ !type.nil?
68
+ end
69
+
70
+ # Check if the ID is of a user
71
+ # @return [Boolean] if the ID is of the user_id type
72
+ def user?
73
+ type == :user_id
74
+ end
75
+
76
+ # Check if the ID is of a group
77
+ # @return [Boolean] if the ID is of the group_id type
78
+ def group?
79
+ type == :group_id
80
+ end
81
+
82
+ # Check if the ID is of a room
83
+ # @return [Boolean] if the ID is of the room_id or room_alias types
84
+ def room?
85
+ type == :room_id || type == :room_alias
86
+ end
87
+
88
+ # Check if the ID is of a event
89
+ # @return [Boolean] if the ID is of the event_id type
90
+ def event?
91
+ type == :event_id
92
+ end
93
+
94
+ # Check if the ID is a room_id
95
+ # @return [Boolean] if the ID is of the room_id type
96
+ def room_id?
97
+ type == :room_id
98
+ end
99
+
100
+ # Check if the ID is a room_alias
101
+ # @return [Boolean] if the ID is of the room_alias type
102
+ def room_alias?
103
+ type == :room_alias
104
+ end
105
+
106
+ # Converts the MXID to a matrix: URI according to MSC2312
107
+ # @param event_id [String,MXID] An event ID to append to the URI (only valid for rooms)
108
+ # @param action [String,Symbol] The action that should be requested
109
+ # @param via [Array[String]] The list of servers to use for a join
110
+ # @see https://github.com/matrix-org/matrix-doc/blob/master/proposals/2312-matrix-uri.md
111
+ def to_uri(event_id: nil, action: nil, via: nil)
112
+ uri = ''
113
+
114
+ case sigil
115
+ when '@'
116
+ raise ArgumentError, "can't provide via for user URIs" if via
117
+ raise ArgumentError, "can't provide event_id for user URIs" if event_id
118
+
119
+ uri += 'u'
120
+ when '#'
121
+ uri += 'r'
122
+ when '!'
123
+ uri += 'roomid'
124
+ else
125
+ raise ArgumentError, "this MXID can't be converted to a URI"
126
+ end
127
+
128
+ uri = "matrix:#{uri}/#{localpart}#{homeserver_suffix}"
129
+
130
+ uri += "/e/#{event_id.to_s.delete_prefix('$')}" if event_id
131
+ query = []
132
+ query << "action=#{action}" if action
133
+ [via].flatten.compact.each { |v| query << "via=#{v}" }
134
+
135
+ uri += "?#{query.join('&')}" unless query.empty?
136
+
137
+ URI(uri)
138
+ end
139
+
140
+ # Check if two MXIDs are equal
141
+ # @return [Boolean]
142
+ def ==(other)
143
+ to_s == other.to_s
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MatrixSdk::Protocols::AS
4
+ def self.included(_klass)
5
+ # XXX
6
+ end
7
+ end