Skip to content

Commit c38bb42

Browse files
committed
buttons/core/messages/view: Add spoiler_link to metadata.
This commit: * Adds a boolean value spoiler_link to metadata, indicating whether a link is part of spoiler content. * Adds [spoiler] to link caption if spoiler_link is True. Fixes zulip#688.
1 parent 8713fd7 commit c38bb42

File tree

7 files changed

+71
-55
lines changed

7 files changed

+71
-55
lines changed

tests/ui_tools/test_buttons.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ def spoiler_button(
322322
header: List[Any] = [""],
323323
content: List[Any] = [""],
324324
message: Message = {},
325-
topic_links: Dict[str, Tuple[str, int, bool]] = {},
326-
message_links: Dict[str, Tuple[str, int, bool]] = {},
325+
topic_links: Dict[str, Tuple[str, int, bool, bool]] = {},
326+
message_links: Dict[str, Tuple[str, int, bool, bool]] = {},
327327
time_mentions: List[Tuple[str, str]] = [],
328328
spoilers: List[Tuple[int, List[Any], List[Any]]] = [],
329329
display_attr: Optional[str] = None,

tests/ui_tools/test_messages.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ def test_reactions_view(
18811881
[
18821882
(
18831883
"https://github.com/zulip/zulip-terminal/pull/1",
1884-
("#T1", 1, True),
1884+
("#T1", 1, True, False),
18851885
),
18861886
]
18871887
),
@@ -1893,8 +1893,8 @@ def test_reactions_view(
18931893
case(
18941894
OrderedDict(
18951895
[
1896-
("https://foo.com", ("Foo!", 1, True)),
1897-
("https://bar.com", ("Bar!", 2, True)),
1896+
("https://foo.com", ("Foo!", 1, True, False)),
1897+
("https://bar.com", ("Bar!", 2, True, False)),
18981898
]
18991899
),
19001900
"1: https://foo.com\n2: https://bar.com",
@@ -1913,8 +1913,11 @@ def test_reactions_view(
19131913
case(
19141914
OrderedDict(
19151915
[
1916-
("https://example.com", ("https://example.com", 1, False)),
1917-
("http://example.com", ("http://example.com", 2, False)),
1916+
(
1917+
"https://example.com",
1918+
("https://example.com", 1, False, False),
1919+
),
1920+
("http://example.com", ("http://example.com", 2, False, False)),
19181921
]
19191922
),
19201923
None,
@@ -1925,8 +1928,8 @@ def test_reactions_view(
19251928
case(
19261929
OrderedDict(
19271930
[
1928-
("https://foo.com", ("https://foo.com, Text", 1, True)),
1929-
("https://bar.com", ("Text, https://bar.com", 2, True)),
1931+
("https://foo.com", ("https://foo.com, Text", 1, True, False)),
1932+
("https://bar.com", ("Text, https://bar.com", 2, True, False)),
19301933
]
19311934
),
19321935
"1: https://foo.com\n2: https://bar.com",
@@ -1945,9 +1948,9 @@ def test_reactions_view(
19451948
case(
19461949
OrderedDict(
19471950
[
1948-
("https://foo.com", ("Foo!", 1, True)),
1949-
("http://example.com", ("example.com", 2, False)),
1950-
("https://bar.com", ("Bar!", 3, True)),
1951+
("https://foo.com", ("Foo!", 1, True, False)),
1952+
("http://example.com", ("example.com", 2, False, False)),
1953+
("https://bar.com", ("Bar!", 3, True, False)),
19511954
]
19521955
),
19531956
"1: https://foo.com\n3: https://bar.com",
@@ -1994,7 +1997,7 @@ def test_footlinks_view(
19941997
def test_footlinks_limit(self, maximum_footlinks, expected_instance):
19951998
message_links = OrderedDict(
19961999
[
1997-
("https://github.com/zulip/zulip-terminal", ("ZT", 1, True)),
2000+
("https://github.com/zulip/zulip-terminal", ("ZT", 1, True, False)),
19982001
]
19992002
)
20002003

tests/ui_tools/test_popups.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,9 @@ def test_init(self, message_fixture: Message) -> None:
10541054
assert self.msg_info_view.time_mentions == list()
10551055
assert self.msg_info_view.spoilers == list()
10561056

1057-
def test_pop_up_info_order(self, message_fixture: Message) -> None:
1058-
topic_links = OrderedDict([("https://bar.com", ("topic", 1, True))])
1059-
message_links = OrderedDict([("image.jpg", ("image", 1, True))])
1057+
def test_popup_info_order(self, message_fixture: Message) -> None:
1058+
topic_links = OrderedDict([("https://bar.com", ("topic", 1, True, False))])
1059+
message_links = OrderedDict([("image.jpg", ("image", 1, True, False))])
10601060
msg_info_view = MsgInfoView(
10611061
self.controller,
10621062
message_fixture,
@@ -1302,14 +1302,14 @@ def test_height_reactions(
13021302
],
13031303
[
13041304
(
1305-
OrderedDict([("https://bar.com", ("Foo", 1, True))]),
1305+
OrderedDict([("https://bar.com", ("Foo", 1, True, False))]),
13061306
"1: Foo\nhttps://bar.com",
13071307
{None: "popup_contrast"},
13081308
{None: "selected"},
13091309
15,
13101310
),
13111311
(
1312-
OrderedDict([("https://foo.com", ("", 1, True))]),
1312+
OrderedDict([("https://foo.com", ("", 1, True, False))]),
13131313
"1: https://foo.com",
13141314
{None: "popup_contrast"},
13151315
{None: "selected"},
@@ -1323,7 +1323,7 @@ def test_height_reactions(
13231323
)
13241324
def test_create_link_buttons(
13251325
self,
1326-
initial_link: "OrderedDict[str, Tuple[str, int, bool]]",
1326+
initial_link: "OrderedDict[str, Tuple[str, int, bool, bool]]",
13271327
expected_text: str,
13281328
expected_attr_map: Dict[None, str],
13291329
expected_focus_map: Dict[None, str],
@@ -1588,8 +1588,8 @@ def test_markup_description(
15881588
(
15891589
OrderedDict(
15901590
[
1591-
("https://example.com", ("Example", 1, True)),
1592-
("https://generic.com", ("Generic", 2, True)),
1591+
("https://example.com", ("Example", 1, True, False)),
1592+
("https://generic.com", ("Generic", 2, True, False)),
15931593
]
15941594
),
15951595
"1: https://example.com\n2: https://generic.com",
@@ -1608,7 +1608,7 @@ def test_markup_description(
16081608
)
16091609
def test_footlinks(
16101610
self,
1611-
message_links: "OrderedDict[str, Tuple[str, int, bool]]",
1611+
message_links: "OrderedDict[str, Tuple[str, int, bool, bool]]",
16121612
expected_text: str,
16131613
expected_attrib: List[Tuple[Optional[str], int]],
16141614
expected_footlinks_width: int,

zulipterminal/core.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ def show_topic_edit_mode(self, button: Any) -> None:
262262
def show_msg_info(
263263
self,
264264
msg: Message,
265-
topic_links: Dict[str, Tuple[str, int, bool]],
266-
message_links: Dict[str, Tuple[str, int, bool]],
265+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
266+
message_links: Dict[str, Tuple[str, int, bool, bool]],
267267
time_mentions: List[Tuple[str, str]],
268268
spoilers: List[Tuple[int, List[Any], List[Any]]],
269269
) -> None:
@@ -342,8 +342,8 @@ def show_msg_sender_info(self, user_id: int) -> None:
342342
def show_full_rendered_message(
343343
self,
344344
message: Message,
345-
topic_links: Dict[str, Tuple[str, int, bool]],
346-
message_links: Dict[str, Tuple[str, int, bool]],
345+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
346+
message_links: Dict[str, Tuple[str, int, bool, bool]],
347347
time_mentions: List[Tuple[str, str]],
348348
spoilers: List[Tuple[int, List[Any], List[Any]]],
349349
) -> None:
@@ -363,8 +363,8 @@ def show_full_rendered_message(
363363
def show_full_raw_message(
364364
self,
365365
message: Message,
366-
topic_links: Dict[str, Tuple[str, int, bool]],
367-
message_links: Dict[str, Tuple[str, int, bool]],
366+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
367+
message_links: Dict[str, Tuple[str, int, bool, bool]],
368368
time_mentions: List[Tuple[str, str]],
369369
spoilers: List[Tuple[int, List[Any], List[Any]]],
370370
) -> None:
@@ -384,8 +384,8 @@ def show_full_raw_message(
384384
def show_edit_history(
385385
self,
386386
message: Message,
387-
topic_links: Dict[str, Tuple[str, int, bool]],
388-
message_links: Dict[str, Tuple[str, int, bool]],
387+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
388+
message_links: Dict[str, Tuple[str, int, bool, bool]],
389389
time_mentions: List[Tuple[str, str]],
390390
spoilers: List[Tuple[int, List[Any], List[Any]]],
391391
) -> None:
@@ -494,8 +494,8 @@ def show_spoiler(
494494
self,
495495
content: str,
496496
message: Message,
497-
topic_links: Dict[str, Tuple[str, int, bool]],
498-
message_links: Dict[str, Tuple[str, int, bool]],
497+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
498+
message_links: Dict[str, Tuple[str, int, bool, bool]],
499499
time_mentions: List[Tuple[str, str]],
500500
spoilers: List[Tuple[int, List[Any], List[Any]]],
501501
) -> None:

zulipterminal/ui_tools/buttons.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ def __init__(
325325
header: List[Any],
326326
content: List[Any],
327327
message: Message,
328-
topic_links: Dict[str, Tuple[str, int, bool]],
329-
message_links: Dict[str, Tuple[str, int, bool]],
328+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
329+
message_links: Dict[str, Tuple[str, int, bool, bool]],
330330
time_mentions: List[Tuple[str, str]],
331331
spoilers: List[Tuple[int, List[Any], List[Any]]],
332332
display_attr: Optional[str],

zulipterminal/ui_tools/messages.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None:
6060
self.topic_name = ""
6161
self.email = "" # FIXME: Can we remove this?
6262
self.user_id: Optional[int] = None
63-
self.message_links: Dict[str, Tuple[str, int, bool]] = dict()
64-
self.topic_links: Dict[str, Tuple[str, int, bool]] = dict()
63+
self.message_links: Dict[str, Tuple[str, int, bool, bool]] = dict()
64+
self.topic_links: Dict[str, Tuple[str, int, bool, bool]] = dict()
6565
self.time_mentions: List[Tuple[str, str]] = list()
6666
self.spoilers: List[Tuple[int, List[Any], List[Any]]] = list()
6767
self.last_message = last_message
@@ -77,6 +77,7 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None:
7777
link["text"],
7878
len(self.topic_links) + 1,
7979
True,
80+
False,
8081
)
8182

8283
self.stream_name = self.message["display_recipient"]
@@ -314,7 +315,7 @@ def reactions_view(
314315

315316
@staticmethod
316317
def footlinks_view(
317-
message_links: Dict[str, Tuple[str, int, bool]],
318+
message_links: Dict[str, Tuple[str, int, bool, bool]],
318319
*,
319320
maximum_footlinks: int,
320321
padded: bool,
@@ -331,7 +332,7 @@ def footlinks_view(
331332
footlinks = []
332333
counter = 0
333334
footlinks_width = 0
334-
for link, (text, index, show_footlink) in message_links.items():
335+
for link, (text, index, show_footlink, spoiler_link) in message_links.items():
335336
if counter == maximum_footlinks:
336337
break
337338
if not show_footlink:
@@ -374,7 +375,7 @@ def soup2markup(
374375
cls, soup: Any, metadata: Dict[str, Any], **state: Any
375376
) -> Tuple[
376377
List[Any],
377-
Dict[str, Tuple[str, int, bool]],
378+
Dict[str, Tuple[str, int, bool, bool]],
378379
List[Tuple[str, str]],
379380
List[Tuple[int, List[Any], List[Any]]],
380381
]:
@@ -503,27 +504,35 @@ def soup2markup(
503504
# Do not show as a footlink as the text is sufficient
504505
# to represent the link.
505506
show_footlink = False
507+
508+
spoiler_link = False
506509
if element.find_parent("div", class_="spoiler-block"):
507510
show_footlink = False
511+
spoiler_link = True
508512

509513
# Detect duplicate links to save screen real estate.
510514
if link not in metadata["message_links"]:
511515
metadata["message_links"][link] = (
512516
text,
513517
len(metadata["message_links"]) + 1,
514518
show_footlink,
519+
spoiler_link,
515520
)
516521
else:
517522
# Append the text if its link already exist with a
518523
# different text.
519-
saved_text, saved_link_index, saved_footlink_status = metadata[
520-
"message_links"
521-
][link]
524+
(
525+
saved_text,
526+
saved_link_index,
527+
saved_footlink_status,
528+
spoiler_link,
529+
) = metadata["message_links"][link]
522530
if saved_text != text:
523531
metadata["message_links"][link] = (
524532
f"{saved_text}, {text}",
525533
saved_link_index,
526534
show_footlink or saved_footlink_status,
535+
spoiler_link,
527536
)
528537

529538
markup.extend(
@@ -900,7 +909,7 @@ def transform_content(
900909
cls, content: Any, server_url: str
901910
) -> Tuple[
902911
Tuple[None, Any],
903-
Dict[str, Tuple[str, int, bool]],
912+
Dict[str, Tuple[str, int, bool, bool]],
904913
List[Tuple[str, str]],
905914
List[Tuple[int, List[Any], List[Any]]],
906915
]:

zulipterminal/ui_tools/views.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,8 @@ def __init__(
10841084
title: str,
10851085
content: str,
10861086
message: Message,
1087-
topic_links: Dict[str, Tuple[str, int, bool]],
1088-
message_links: Dict[str, Tuple[str, int, bool]],
1087+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
1088+
message_links: Dict[str, Tuple[str, int, bool, bool]],
10891089
time_mentions: List[Tuple[str, str]],
10901090
spoilers: List[Tuple[int, List[Any], List[Any]]],
10911091
) -> None:
@@ -1609,8 +1609,8 @@ def __init__(
16091609
controller: Any,
16101610
msg: Message,
16111611
title: str,
1612-
topic_links: Dict[str, Tuple[str, int, bool]],
1613-
message_links: Dict[str, Tuple[str, int, bool]],
1612+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
1613+
message_links: Dict[str, Tuple[str, int, bool, bool]],
16141614
time_mentions: List[Tuple[str, str]],
16151615
spoilers: List[Tuple[int, List[Any], List[Any]]],
16161616
) -> None:
@@ -1748,17 +1748,21 @@ def __init__(
17481748

17491749
@staticmethod
17501750
def create_link_buttons(
1751-
controller: Any, links: Dict[str, Tuple[str, int, bool]]
1751+
controller: Any, links: Dict[str, Tuple[str, int, bool, bool]]
17521752
) -> Tuple[List[MessageLinkButton], int]:
17531753
link_widgets = []
17541754
link_width = 0
17551755

17561756
for index, link in enumerate(links):
1757-
text, link_index, _ = links[link]
1757+
text, link_index, _, spoiler_link = links[link]
17581758
if text:
17591759
caption = f"{link_index}: {text}\n{link}"
1760+
if spoiler_link:
1761+
caption = f"{link_index} [spoiler]: {text}\n{link}"
17601762
else:
17611763
caption = f"{link_index}: {link}"
1764+
if spoiler_link:
1765+
caption = f"{link_index} [spoiler]: {link}"
17621766
link_width = max(link_width, len(max(caption.split("\n"), key=len)))
17631767

17641768
display_attr = None if index % 2 else "popup_contrast"
@@ -1884,8 +1888,8 @@ def __init__(
18841888
self,
18851889
controller: Any,
18861890
message: Message,
1887-
topic_links: Dict[str, Tuple[str, int, bool]],
1888-
message_links: Dict[str, Tuple[str, int, bool]],
1891+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
1892+
message_links: Dict[str, Tuple[str, int, bool, bool]],
18891893
time_mentions: List[Tuple[str, str]],
18901894
spoilers: List[Tuple[int, List[Any], List[Any]]],
18911895
title: str,
@@ -2005,8 +2009,8 @@ def __init__(
20052009
self,
20062010
controller: Any,
20072011
message: Message,
2008-
topic_links: Dict[str, Tuple[str, int, bool]],
2009-
message_links: Dict[str, Tuple[str, int, bool]],
2012+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
2013+
message_links: Dict[str, Tuple[str, int, bool, bool]],
20102014
time_mentions: List[Tuple[str, str]],
20112015
spoilers: List[Tuple[int, List[Any], List[Any]]],
20122016
title: str,
@@ -2052,8 +2056,8 @@ def __init__(
20522056
self,
20532057
controller: Any,
20542058
message: Message,
2055-
topic_links: Dict[str, Tuple[str, int, bool]],
2056-
message_links: Dict[str, Tuple[str, int, bool]],
2059+
topic_links: Dict[str, Tuple[str, int, bool, bool]],
2060+
message_links: Dict[str, Tuple[str, int, bool, bool]],
20572061
time_mentions: List[Tuple[str, str]],
20582062
spoilers: List[Tuple[int, List[Any], List[Any]]],
20592063
title: str,

0 commit comments

Comments
 (0)