Class: Citadel::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/citadel.rb

Instance Method Summary collapse

Constructor Details

#initialize(tenant_url, public_rooms_limit = 100) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
# File 'lib/citadel.rb', line 19

def initialize(tenant_url, public_rooms_limit = 100)
  if tenant_url[-1,1] == '/'
    Citadel.tenant_url = tenant_url[0,tenant_url.length-1]
  else
    Citadel.tenant_url = tenant_url
  end
  Citadel.public_rooms_limit = public_rooms_limit
end

Instance Method Details

#all_joined_rooms_responseObject



69
70
71
72
73
# File 'lib/citadel.rb', line 69

def all_joined_rooms_response
  matrix_paths = MatrixPaths.new
  url = matrix_paths.base_uri + matrix_paths.list_joined_rooms_path
  HTTP.auth(auth_token).get(url)
end

#all_public_rooms_responseObject



63
64
65
66
67
# File 'lib/citadel.rb', line 63

def all_public_rooms_response
  matrix_paths = MatrixPaths.new
  url = matrix_paths.base_uri + matrix_paths.list_public_rooms_path
  HTTP.auth(auth_token).get(url)
end

#auth_tokenObject

AUTH #



32
33
34
35
36
# File 'lib/citadel.rb', line 32

def auth_token
  return @auth_token if defined? @auth_token
  puts 'Citadel: You need to sign in'
  return nil
end

#change_room_visibility(room_id, visibility) ⇒ Object

ROOM MANAGEMENT #



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/citadel.rb', line 180

def change_room_visibility(room_id, visibility)
  matrix_paths = MatrixPaths.new
  url = matrix_paths.base_uri + matrix_paths.change_room_visibility_path(room_id)

  data_hash = { join_rule: visibility }
  data = JSON.generate data_hash

  response = HTTP.auth(auth_token).put(url, body: data)

  matrix_interceptor = MatrixInterceptor.new
  if matrix_interceptor.need_to_wait_and_retry(response)
    response = HTTP.auth(auth_token).put(url)
  end
end

#create_room(room_name, topic) ⇒ Object

ROOM CREATION #



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/citadel.rb', line 79

def create_room(room_name, topic)
  matrix_paths = MatrixPaths.new
  url = matrix_paths.base_uri + matrix_paths.create_room_path
  room_name_alias = room_name.gsub!(' ','_')
  data_hash = { creation_content: { 'm.federate': false },
                name: room_name,
                preset: 'public_chat',
                visibility: 'public',
                room_alias_name: room_name_alias,
                topic: topic}
  data = JSON.generate data_hash

  response = HTTP.auth(auth_token).post(url, body: data)

  matrix_interceptor = MatrixInterceptor.new
  if matrix_interceptor.need_to_wait_and_retry(response)
    response = HTTP.auth(auth_token).post(url, body: data)
  end

  JSON.parse(response.body)['room_id']
end

#invite_in_room(room_id, user_id) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/citadel.rb', line 133

def invite_in_room(room_id, user_id)

  matrix_paths = MatrixPaths.new
  url = matrix_paths.base_uri + matrix_paths.invite_in_room_path(room_id)
  data_hash = { user_id: user_id }
  data = JSON.generate data_hash

  response = HTTP.auth(auth_token).post(url, body: data)

  matrix_interceptor = MatrixInterceptor.new
  if matrix_interceptor.need_to_wait_and_retry(response)
    response = HTTP.auth(auth_token).post(url, body: data)
  end
end

#invite_users_in_room(room_id, users) ⇒ Object

INVITE #



127
128
129
130
131
# File 'lib/citadel.rb', line 127

def invite_users_in_room(room_id, users)
  users.each do |user|
    invite_in_room(room_id, user)
  end
end

#join_room(room_id) ⇒ Object

ROOM MEMBERSHIP #



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/citadel.rb', line 152

def join_room(room_id)
  matrix_paths = MatrixPaths.new
  url = matrix_paths.base_uri + matrix_paths.join_room_path(room_id)

  response = HTTP.auth(auth_token).post(url)

  matrix_interceptor = MatrixInterceptor.new
  if matrix_interceptor.need_to_wait_and_retry(response)
    response = HTTP.auth(auth_token).post(url)
  end
end

#leave_room(room_id) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/citadel.rb', line 164

def leave_room(room_id)
  matrix_paths = MatrixPaths.new
  url = matrix_paths.base_uri + matrix_paths.leave_room_path(room_id)

  response = HTTP.auth(auth_token).post(url)

  matrix_interceptor = MatrixInterceptor.new
  if matrix_interceptor.need_to_wait_and_retry(response)
    response = HTTP.auth(auth_token).post(url)
  end
end

#list_all_joined_roomsObject



57
58
59
60
61
# File 'lib/citadel.rb', line 57

def list_all_joined_rooms
  response = all_joined_rooms_response
  rooms = JSON.parse(response.body)['joined_rooms']
  rooms
end

#list_all_public_roomsObject

LIST ROOMS #



47
48
49
50
51
52
53
54
55
# File 'lib/citadel.rb', line 47

def list_all_public_rooms
  response = all_public_rooms_response
  room_count = JSON.parse(response.body)['total_room_count_estimate'] - 2
  result = []
  (0..room_count).each do
    result << JSON.parse(response.body)['chunk'][i]['room_id']
  end
  result
end

#send_message(room_id, message) ⇒ Object

SEND #



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/citadel.rb', line 105

def send_message(room_id, message)

  matrix_paths = MatrixPaths.new
  randomizer = Random.new
  txn = randomizer.rand(100)
  url = matrix_paths.base_uri + matrix_paths.send_message_path(room_id) + txn.to_s

  data_hash = { msgtype: 'm.text', body: message }
  data = JSON.generate data_hash

  response = HTTP.auth(auth_token).put(url, body: data)

  matrix_interceptor = MatrixInterceptor.new
  if matrix_interceptor.need_to_wait_and_retry(response)
    response = HTTP.auth(auth_token).put(url, body: data)
  end
end

#sign_in(login, password) ⇒ Object



38
39
40
41
# File 'lib/citadel.rb', line 38

def (, password)
  authenticator = Authenticator.new
  @auth_token = 'Bearer ' + authenticator.get_token(, password)
end