Skip to content

Commit b3c51dd

Browse files
authored
Add (deprecated) video message (#44)
1 parent c0924ec commit b3c51dd

File tree

9 files changed

+350
-1
lines changed

9 files changed

+350
-1
lines changed

examples/e2e.py

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
FileMessage,
1717
ImageMessage,
1818
TextMessage,
19+
VideoMessage,
1920
)
2021

2122

@@ -83,6 +84,25 @@ def send_image(connection):
8384
return (yield from message.send())
8485

8586

87+
@asyncio.coroutine
88+
def send_video(connection):
89+
"""
90+
Send a video including a thumbnail to a specific Threema ID.
91+
92+
Note that the public key will be automatically fetched from the
93+
Threema servers. It is strongly recommended that you cache
94+
public keys to avoid querying the API for each message.
95+
"""
96+
message = VideoMessage(
97+
connection=connection,
98+
to_id='ECHOECHO',
99+
duration=1,
100+
video_path='res/threema.mp4',
101+
thumbnail_path='res/threema.jpg',
102+
)
103+
return (yield from message.send())
104+
105+
86106
@asyncio.coroutine
87107
def send_file(connection):
88108
"""
@@ -132,6 +152,7 @@ def main():
132152
yield from send_cached_key(connection)
133153
yield from send_cached_key_file(connection)
134154
yield from send_image(connection)
155+
yield from send_video(connection)
135156
yield from send_file(connection)
136157
yield from send_file_with_thumbnail(connection)
137158
except GatewayError as exc:

examples/e2e_blocking.py

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
FileMessage,
1515
ImageMessage,
1616
TextMessage,
17+
VideoMessage,
1718
)
1819

1920

@@ -77,6 +78,24 @@ def send_image(connection):
7778
return message.send()
7879

7980

81+
def send_video(connection):
82+
"""
83+
Send a video including a thumbnail to a specific Threema ID.
84+
85+
Note that the public key will be automatically fetched from the
86+
Threema servers. It is strongly recommended that you cache
87+
public keys to avoid querying the API for each message.
88+
"""
89+
message = VideoMessage(
90+
connection=connection,
91+
to_id='ECHOECHO',
92+
duration=1,
93+
video_path='res/threema.mp4',
94+
thumbnail_path='res/threema.jpg',
95+
)
96+
return message.send()
97+
98+
8099
def send_file(connection):
81100
"""
82101
Send a file to a specific Threema ID.
@@ -124,6 +143,7 @@ def main():
124143
send_cached_key(connection)
125144
send_cached_key_file(connection)
126145
send_image(connection)
146+
send_video(connection)
127147
send_file(connection)
128148
send_file_with_thumbnail(connection)
129149
except GatewayError as exc:

tests/conftest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def send(self, get_data_only=False):
6464
class Server:
6565
def __init__(self):
6666
self.threema_jpg = os.path.join(_res_path, 'threema.jpg')
67+
self.threema_mp4 = os.path.join(_res_path, 'threema.mp4')
6768
key = b'4a6a1b34dcef15d43cb74de2fd36091be99fbbaf126d099d47d83d919712c72b'
6869
self.echoecho_key = key
6970
self.echoecho_encoded_key = 'public:' + key.decode('ascii')
@@ -163,7 +164,7 @@ def capabilities(self, request):
163164
elif id_ == 'ECHOECHO':
164165
return web.Response(body=b'text,image,video,file')
165166
elif id_ == '*MOCKING':
166-
return web.Response(body=b'text,image,file')
167+
return web.Response(body=b'text,image,video,file')
167168
return web.Response(status=404)
168169

169170
@asyncio.coroutine

tests/res/threema.mp4

385 KB
Binary file not shown.

tests/test_api.py

+15
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,21 @@ def test_image(self, connection, server):
551551
assert len(server.latest_blob_ids) == 1
552552
assert all((yield from get_latest_blob_ids(server, connection)))
553553

554+
@pytest.mark.asyncio
555+
def test_video(self, connection, server):
556+
server.latest_blob_ids = []
557+
id_ = yield from e2e.VideoMessage(
558+
connection=connection,
559+
to_id='ECHOECHO',
560+
key=server.echoecho_encoded_key,
561+
duration=1,
562+
video_path=server.threema_mp4,
563+
thumbnail_path=server.threema_jpg,
564+
).send()
565+
assert id_ == '1' * 16
566+
assert len(server.latest_blob_ids) == 2
567+
assert all((yield from get_latest_blob_ids(server, connection)))
568+
554569
@pytest.mark.asyncio
555570
def test_file(self, connection, server):
556571
server.latest_blob_ids = []

tests/test_callback.py

+18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ def test_image_message(self, connection, callback_send, callback_receive, server
6060
assert outgoing.to_id == incoming.to_id
6161
assert outgoing.image == incoming.image
6262

63+
@pytest.mark.asyncio
64+
def test_video(self, connection, callback_send, callback_receive, server):
65+
outgoing = e2e.VideoMessage(
66+
connection,
67+
to_id=pytest.msgapi.id,
68+
duration=1,
69+
video_path=server.threema_mp4,
70+
thumbnail_path=server.threema_jpg,
71+
)
72+
response = yield from callback_send(outgoing)
73+
yield from response.release()
74+
incoming = yield from callback_receive()
75+
assert outgoing.from_id == incoming.from_id
76+
assert outgoing.to_id == incoming.to_id
77+
assert outgoing.duration == incoming.duration
78+
assert outgoing.video == incoming.video
79+
assert outgoing.thumbnail_content == incoming.thumbnail_content
80+
6381
@pytest.mark.asyncio
6482
def test_file_message(self, connection, callback_send, callback_receive, server):
6583
outgoing = e2e.FileMessage(

tests/test_cli.py

+22
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ def test_send_image(self, cli, server):
119119
assert output_1 == output_2
120120
assert len(server.latest_blob_ids) == 2
121121

122+
@pytest.mark.asyncio
123+
def test_send_video(self, cli, server):
124+
server.latest_blob_ids = []
125+
output_1 = yield from cli(
126+
'send_video', 'ECHOECHO', pytest.msgapi.id, pytest.msgapi.secret,
127+
pytest.msgapi.private, server.threema_mp4, server.threema_jpg)
128+
assert output_1
129+
assert len(server.latest_blob_ids) == 2
130+
output_2 = yield from cli(
131+
'send_video', 'ECHOECHO', pytest.msgapi.id, pytest.msgapi.secret,
132+
pytest.msgapi.private, server.threema_mp4, server.threema_jpg,
133+
'-k', server.echoecho_encoded_key)
134+
assert output_2
135+
assert output_1 == output_2
136+
assert len(server.latest_blob_ids) == 4
137+
output = yield from cli(
138+
'send_video', 'ECHOECHO', pytest.msgapi.id, pytest.msgapi.secret,
139+
pytest.msgapi.private, server.threema_mp4, server.threema_jpg,
140+
'-d', '1337')
141+
assert output
142+
assert len(server.latest_blob_ids) == 6
143+
122144
@pytest.mark.asyncio
123145
def test_send_file(self, cli, server):
124146
server.latest_blob_ids = []

threema/gateway/bin/gateway_client.py

+52
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,58 @@ def send_image(ctx, **arguments):
342342
click.echo((yield from message.send()))
343343

344344

345+
@cli.command(short_help='Send a video using end-to-end mode.', help="""
346+
Encrypt and send a video ('mp4') including a thumbnail to the given ID.
347+
FROM is the API identity and SECRET is the API secret.
348+
VIDEO_PATH is a relative or absolute path to a video.
349+
THUMBNAIL_PATH is a relative or absolute path to a thumbnail.
350+
Prints the message ID on success.
351+
""")
352+
@click.argument('to')
353+
@click.argument('from')
354+
@click.argument('secret')
355+
@click.argument('private_key')
356+
@click.argument('video_path')
357+
@click.argument('thumbnail_path')
358+
@click.option('-k', '--public-key', help="""
359+
The public key of the recipient. Will be fetched automatically if not provided.
360+
""")
361+
@click.option('-d', '--duration', help="""
362+
Duration of the video in seconds. Defaults to 0.
363+
""", default=0)
364+
@click.pass_context
365+
@util.aio_run_decorator()
366+
def send_video(ctx, **arguments):
367+
# Get key instances
368+
private_key = util.read_key_or_key_file(arguments['private_key'], Key.Type.private)
369+
if arguments['public_key'] is not None:
370+
public_key = util.read_key_or_key_file(arguments['public_key'], Key.Type.public)
371+
else:
372+
public_key = None
373+
374+
# Create connection
375+
connection = Connection(
376+
identity=arguments['from'],
377+
secret=arguments['secret'],
378+
key=private_key,
379+
**ctx.obj
380+
)
381+
382+
with connection:
383+
# Create message
384+
message = e2e.VideoMessage(
385+
connection=connection,
386+
to_id=arguments['to'],
387+
key=public_key,
388+
duration=arguments['duration'],
389+
video_path=arguments['video_path'],
390+
thumbnail_path=arguments['thumbnail_path']
391+
)
392+
393+
# Send message
394+
click.echo((yield from message.send()))
395+
396+
345397
@cli.command(short_help='Send a file using end-to-end mode.', help="""
346398
Encrypt and send a file to the given ID, optionally with a thumbnail.
347399
FROM is the API identity and SECRET is the API secret.

0 commit comments

Comments
 (0)