Class: MatrixSdk::Rooms::Space

Inherits:
MatrixSdk::Room show all
Defined in:
lib/matrix_sdk/rooms/space.rb

Constant Summary collapse

TYPE =
'm.space'

Constants included from Util::Tinycache

Util::Tinycache::CACHE_LEVELS

Instance Attribute Summary

Attributes inherited from MatrixSdk::Room

#client, #event_history_limit, #events, #id, #on_ephemeral_event, #on_event, #on_state_event

Instance Method Summary collapse

Methods inherited from MatrixSdk::Room

#add_alias, #add_tag, #admin!, #admin?, #aliases, #all_members, #allow_guests=, #avatar_url, #avatar_url=, #backfill_messages, #ban_user, #canonical_alias, #creation_info, #display_name, #get_account_data, #guest_access, #guest_access=, #guest_access?, #history_visibility, #initialize, #inspect, #invite_only=, #invite_only?, #invite_user, #join_rule, #join_rule=, #joined_members, #kick_user, #leave, #members, #moderator!, #moderator?, #modify_required_power_levels, #modify_user_power_levels, #name, #name=, #power_levels, #redact_message, #reload!, #reload_aliases!, #reload_name!, #reload_topic!, #remove_tag, #report_message, #room_type, #room_version, #send_audio, #send_emote, #send_file, #send_html, #send_image, #send_location, #send_notice, #send_text, #send_video, #set_account_data, #set_user_profile, #space?, #tags, #to_s, #to_space, #topic, #topic=, #unban_user, #user_powerlevel, #world_readable?

Methods included from Extensions

#events, #ignore_inspect

Methods included from Util::Tinycache

adapter, adapter=, #cached, extended, #tinycache_adapter_config

Methods included from Logging

#logger, #logger=

Constructor Details

This class inherits a constructor from MatrixSdk::Room

Instance Method Details

#tree(suggested_only: nil, max_rooms: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/matrix_sdk/rooms/space.rb', line 7

def tree(suggested_only: nil, max_rooms: nil)
  begin
    data = client.api.request :get, :client_unstable, "/org.matrix.msc2946/rooms/#{id}/spaces", query: {
      suggested_only: suggested_only,
      max_rooms_per_space: max_rooms
    }.compact
  rescue MatrixRequestError
    data = client.api.request :get, :client_r0, "/rooms/#{id}/spaces", query: {
      suggested_only: suggested_only,
      max_rooms_per_space: max_rooms
    }.compact
  end

  rooms = data.rooms.map do |r|
    next if r[:room_id] == id

    room = client.ensure_room(r[:room_id])
    room.instance_variable_set :@room_type, r[:room_type] if r.key? :room_type
    room = room.to_space if room.space?

    # Inject available room information
    r.each do |k, v|
      if room.respond_to?("#{k}_cached?".to_sym) && send("#{k}_cached?".to_sym)
        room.send(:tinycache_adapter).write(k, v)
      elsif room.instance_variable_defined? "@#{k}"
        room.instance_variable_set("@#{k}", v)
      end
    end
    room
  end
  rooms.compact!

  grouping = {}
  data.events.each do |ev|
    next unless ev[:type] == 'm.space.child'
    next unless ev[:content].key? :via

    d = (grouping[ev[:room_id]] ||= [])
    d << ev[:state_key]
  end

  build_tree = proc do |entry|
    next if entry.nil?

    room = self if entry == id
    room ||= rooms.find { |r| r.id == entry }
    puts "Unable to find room for entry #{entry}" unless room
    # next if room.nil?

    ret = {
      room => []
    }

    grouping[entry]&.each do |child|
      if grouping.key?(child)
        ret[room] << build_tree.call(child)
      else
        child_r = self if child == id
        child_r ||= rooms.find { |r| r.id == child }

        ret[room] << child_r
      end
    end

    ret[room].compact!

    ret
  end

  build_tree.call(id)
end