class Citadel::Client

Public Class Methods

new(tenant_url, public_rooms_limit = 100) click to toggle source
# 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

Public Instance Methods

all_joined_rooms_response() click to toggle source
# 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_response() click to toggle source
# 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_token() click to toggle source

AUTH #

# 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) click to toggle source

ROOM MANAGEMENT #

# 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) click to toggle source

ROOM CREATION #

# 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) click to toggle source
# 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) click to toggle source

INVITE #

# 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) click to toggle source

ROOM MEMBERSHIP #

# 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) click to toggle source
# 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_rooms() click to toggle source
# 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_rooms() click to toggle source

LIST ROOMS #

# 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) click to toggle source

SEND #

# 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) click to toggle source
# File lib/citadel.rb, line 38
def sign_in(login, password)
  authenticator = Authenticator.new
  @auth_token = 'Bearer ' + authenticator.get_token(login, password)
end