Class: MTProto::Type::RPC::Users::Users

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/type/rpc/users/users.rb

Constant Summary collapse

VECTOR_CONSTRUCTOR =
0x1cb5c415
USER_CONSTRUCTOR =
0x020b1422

Class Method Summary collapse

Class Method Details

.parse(response) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mtproto/type/rpc/users/users.rb', line 14

def self.parse(response)
  offset = 0

  constructor = response[offset, 4].unpack1('L<')
  offset += 4

  if constructor == Type::GzipPacked::CONSTRUCTOR
    response = Type::GzipPacked.unpack(response)
    constructor = response[0, 4].unpack1('L<')
    offset = 4
  end

  if constructor == Type::RpcError::CONSTRUCTOR
    error = Type::RpcError.deserialize(response)
    raise MTProto::RpcError.new(error.error_code, error.error_message)
  end

  # Expect Vector<User>
  raise "Expected Vector constructor, got 0x#{constructor.to_s(16)}" unless constructor == VECTOR_CONSTRUCTOR

  count = response[offset, 4].unpack1('L<')
  offset += 4

  users = []
  count.times do
    user_constructor = response[offset, 4].unpack1('L<')
    offset += 4

    next unless user_constructor == USER_CONSTRUCTOR

    # Parse User: flags:# flags2:# id:long access_hash:flags.0?long first_name:flags.1?string ...
    flags = response[offset, 4].unpack1('L<')
    offset += 4

    flags2 = response[offset, 4].unpack1('L<')
    offset += 4

    id = response[offset, 8].unpack1('Q<')
    offset += 8

    access_hash = nil
    if (flags & (1 << 0)) != 0
      access_hash = response[offset, 8].unpack1('Q<')
      offset += 8
    end

    first_name = nil
    if (flags & (1 << 1)) != 0
      len = response[offset].unpack1('C')
      offset += 1
      first_name = response[offset, len]
      offset += len
      padding = (4 - ((1 + len) % 4)) % 4
      offset += padding
    end

    last_name = nil
    if (flags & (1 << 2)) != 0
      len = response[offset].unpack1('C')
      offset += 1
      last_name = response[offset, len]
      offset += len
      padding = (4 - ((1 + len) % 4)) % 4
      offset += padding
    end

    users << {
      id: id,
      access_hash: access_hash,
      first_name: first_name,
      last_name: last_name,
      flags: flags,
      flags2: flags2
    }

    # Skip remaining fields - we don't need them for this test
    break
  end

  users
end