Skip to content

Commit

Permalink
Narrows to stream/user and opens images
Browse files Browse the repository at this point in the history
Narrows to stream/user successfuly. Opens selected image in default viewer.
Gives 401 error while trying to open gifs or videos.
  • Loading branch information
shreyamalviya committed Jul 11, 2019
1 parent d1425b8 commit 7228bcd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
22 changes: 21 additions & 1 deletion zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)
from mypy_extensions import TypedDict

import os
import os, sys, requests, subprocess, tempfile

Message = Dict[str, Any]

Expand Down Expand Up @@ -359,3 +359,23 @@ def match_user(user: Any, text: str) -> bool:
if keyword.startswith(text.lower()):
return True
return False

def open_media(url: str):
img_name = url.split("/")[-1]
img_dir_path = os.path.join(tempfile.gettempdir(), "ZulipTerminal_media")
img_path = os.path.join(img_dir_path, img_name)
try:
os.mkdir(img_dir_path)
except FileExistsError:
pass
with requests.get(url, stream=True) as r:
if r.status_code == 200:
with open(img_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
r.close()
imageViewerFromCommandLine = {'linux': 'xdg-open',
'win32': 'explorer',
'darwin': 'open'}[sys.platform]
subprocess.run([imageViewerFromCommandLine, img_path])
17 changes: 12 additions & 5 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import urwid

from zulipterminal.config.keys import KEY_BINDINGS, is_command_key
from zulipterminal.helper import asynch, match_user
from zulipterminal.helper import asynch, match_user, open_media
from zulipterminal.ui_tools.buttons import (
TopicButton,
UnreadPMButton,
Expand Down Expand Up @@ -681,6 +681,7 @@ def __init__(self, controller: Any, msg: Any):
def perform_action(self, button):
# If stream
if self.msg['links'][button.caption].startswith('/#narrow'):
found_stream = False
for stream_details in self.controller.model.stream_dict.values():
# Check mentioned stream with all subscribed streams
if stream_details['name'] == button.caption[1:]:
Expand All @@ -693,9 +694,14 @@ def perform_action(self, button):
width=self.width,
count=0)
self.controller.narrow_to_stream(btn)
found_stream = True
break
if not found_stream:
self.controller.view.set_footer_text("You are not subscribed to this stream.", 3)

# If user
elif self.msg['links'][button.caption].startswith('@'):
found_user = False
for user in self.controller.model.users:
if user['full_name'] == button.caption[1:]:
btn = UserButton(user,
Expand All @@ -705,12 +711,13 @@ def perform_action(self, button):
color=user['status'],
count=0)
self.controller.narrow_to_user(btn)
btn._narrow_with_compose(btn)
found_user = True
break

if not found_user:
self.controller.view.set_footer_text("User not found in realm. Their account may be deactivated.", 3)
else:
# Will open images and links in default image viewer and browser
pass

open_media(self.msg['links'][button.caption])

def keypress(self, size: Tuple[int, int], key: str) -> str:
if is_command_key('GO_BACK', key) or is_command_key('MSG_LINKS', key):
Expand Down

0 comments on commit 7228bcd

Please sign in to comment.