Module: MTProto::TL::MediaParts

Includes:
Binary
Included in:
SendMultiMedia, UploadMedia
Defined in:
lib/mtproto/tl/objects/media_parts.rb

Overview

The pieces every media-carrying call writes the same way: who it is addressed to, the file just uploaded, and the strings around it.

Held together here because an album is sent in two calls — one to turn each upload into a photo Telegram holds, one to send them as a group — and both write the same peer and the same file.

Constant Summary collapse

INPUT_MEDIA_PHOTO =
0xe3af4434
INPUT_PHOTO =
0x3bb3b94a

Instance Method Summary collapse

Methods included from Binary

#b_u32, #b_u64, #u32_b, #u64_b

Instance Method Details

#serialize_byte_array(bytes) ⇒ Object



60
61
62
63
64
65
# File 'lib/mtproto/tl/objects/media_parts.rb', line 60

def serialize_byte_array(bytes)
  length = bytes.length
  head = length <= 253 ? [length] : [254] + u32_b(length)[0, 3]
  padded = head + bytes
  padded + [0] * ((4 - (padded.length % 4)) % 4)
end

#serialize_file(file) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/mtproto/tl/objects/media_parts.rb', line 44

def serialize_file(file)
  return u32_b(Constructors::INPUT_FILE_BIG) + u64_b(file[:id]) + u32_b(file[:parts]) +
         serialize_text(file[:name].to_s) if file[:big]

  u32_b(Constructors::INPUT_FILE) + u64_b(file[:id]) + u32_b(file[:parts]) +
    serialize_text(file[:name].to_s) + serialize_text(file[:md5_checksum].to_s)
end

#serialize_held_photo(photo) ⇒ Object

A photo Telegram already holds, named by what it answered when the upload was handed to it.



38
39
40
41
42
# File 'lib/mtproto/tl/objects/media_parts.rb', line 38

def serialize_held_photo(photo)
  u32_b(INPUT_MEDIA_PHOTO) + u32_b(0) +
    u32_b(INPUT_PHOTO) + u64_b(photo[:id]) + u64_b(photo[:access_hash]) +
    serialize_raw(photo[:file_reference])
end

#serialize_peer(peer) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mtproto/tl/objects/media_parts.rb', line 17

def serialize_peer(peer)
  case peer[:type]
  when :self
    u32_b(Constructors::INPUT_PEER_SELF)
  when :user
    u32_b(Constructors::INPUT_PEER_USER) + u64_b(peer[:id]) + u64_b(peer[:access_hash] || 0)
  when :chat
    u32_b(Constructors::INPUT_PEER_CHAT) + u64_b(peer[:id])
  when :channel
    u32_b(Constructors::INPUT_PEER_CHANNEL) + u64_b(peer[:id]) + u64_b(peer[:access_hash] || 0)
  else
    raise "Unknown peer type: #{peer[:type]}"
  end
end

#serialize_raw(raw) ⇒ Object



56
57
58
# File 'lib/mtproto/tl/objects/media_parts.rb', line 56

def serialize_raw(raw)
  serialize_byte_array(raw.to_s.b.bytes)
end

#serialize_text(str) ⇒ Object



52
53
54
# File 'lib/mtproto/tl/objects/media_parts.rb', line 52

def serialize_text(str)
  serialize_byte_array(str.encode('UTF-8').bytes)
end

#serialize_uploaded_photo(file) ⇒ Object



32
33
34
# File 'lib/mtproto/tl/objects/media_parts.rb', line 32

def serialize_uploaded_photo(file)
  u32_b(Constructors::INPUT_MEDIA_UPLOADED_PHOTO) + u32_b(0) + serialize_file(file)
end