Skip to content

Commit

Permalink
Fix sticker pack handling and typing (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding authored Apr 2, 2023
1 parent 2b70d9b commit 82980e7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
1 change: 1 addition & 0 deletions changes/1572.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Renamed `StickerPack.banner_hash` to `StickerPack.banner_asset_id`.
5 changes: 5 additions & 0 deletions changes/1572.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed sticker pack handling and typing:

* Fixed deserialization raising when `"banner_asset_id"` or `"cover_sticker_id"` weren't included in the payload.
* `StickerPack.banner_asset_id` is now correctly typed as `Optional[Snowflake]`.
* `StickerPack.banner_url` and `StickerPack.make_banner_url` both now correctly return `None` when `StickerPack.banner_asset_id` is `None`.
12 changes: 10 additions & 2 deletions hikari/impl/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2759,14 +2759,22 @@ def deserialize_sticker_pack(self, payload: data_binding.JSONObject) -> sticker_
for sticker_payload in payload["stickers"]:
pack_stickers.append(self.deserialize_standard_sticker(sticker_payload))

cover_sticker_id: typing.Optional[snowflakes.Snowflake] = None
if raw_cover_sticker_id := payload.get("cover_sticker_id"):
cover_sticker_id = snowflakes.Snowflake(raw_cover_sticker_id)

banner_asset_id: typing.Optional[snowflakes.Snowflake] = None
if raw_banner_asset_id := payload.get("banner_asset_id"):
banner_asset_id = snowflakes.Snowflake(raw_banner_asset_id)

return sticker_models.StickerPack(
id=snowflakes.Snowflake(payload["id"]),
name=payload["name"],
description=payload["description"],
cover_sticker_id=snowflakes.Snowflake(payload["cover_sticker_id"]),
cover_sticker_id=cover_sticker_id,
stickers=pack_stickers,
sku_id=snowflakes.Snowflake(payload["sku_id"]),
banner_hash=payload["banner_asset_id"],
banner_asset_id=banner_asset_id,
)

def deserialize_partial_sticker(self, payload: data_binding.JSONObject) -> sticker_models.PartialSticker:
Expand Down
26 changes: 14 additions & 12 deletions hikari/stickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,16 @@ class StickerPack(snowflakes.Unique):
sku_id: snowflakes.Snowflake = attr.field(eq=False, hash=False, repr=False)
"""The ID of the packs SKU."""

# This is not exactly how Discord documents it, but we need to keep consistency
banner_hash: str = attr.field(eq=False, hash=False, repr=False)
"""The hash for the pack's banner."""
banner_asset_id: typing.Optional[snowflakes.Snowflake] = attr.field(eq=False, hash=False, repr=False)
"""ID of the sticker pack's banner image, if set."""

@property
def banner_url(self) -> files.URL:
"""Banner URL for the pack."""
def banner_url(self) -> typing.Optional[files.URL]:
"""Banner URL for the pack, if set."""
return self.make_banner_url()

def make_banner_url(self, *, ext: str = "png", size: int = 4096) -> files.URL:
"""Generate the pack's banner image URL.
def make_banner_url(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]:
"""Generate the pack's banner image URL, if set.
Parameters
----------
Expand All @@ -130,17 +129,20 @@ def make_banner_url(self, *, ext: str = "png", size: int = 4096) -> files.URL:
Returns
-------
hikari.files.URL
The URL of the banner.
typing.Optional[hikari.files.URL]
The URL of the banner, if set.
Raises
------
ValueError
If `size` is not a power of two or not between 16 and 4096.
"""
return routes.CDN_STICKER_PACK_BANNER.compile_to_file(
urls.CDN_URL, hash=self.banner_hash, file_format=ext, size=size
)
if self.banner_asset_id is not None:
return routes.CDN_STICKER_PACK_BANNER.compile_to_file(
urls.CDN_URL, hash=self.banner_asset_id, file_format=ext, size=size
)

return None


@attr_extensions.with_copy
Expand Down
13 changes: 11 additions & 2 deletions tests/hikari/impl/test_entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5051,7 +5051,7 @@ def sticker_pack_payload(self, standard_sticker_payload):
"cover_sticker_id": "456",
"stickers": [standard_sticker_payload],
"sku_id": "789",
"banner_asset_id": "hash123",
"banner_asset_id": "342123321",
}

def test_deserialize_partial_sticker(self, entity_factory_impl, partial_sticker_payload):
Expand Down Expand Up @@ -5099,7 +5099,7 @@ def test_deserialize_sticker_pack(self, entity_factory_impl, sticker_pack_payloa
assert pack.description == "My sticker pack description"
assert pack.cover_sticker_id == 456
assert pack.sku_id == 789
assert pack.banner_hash == "hash123"
assert pack.banner_asset_id == 342123321

assert len(pack.stickers) == 1
sticker = pack.stickers[0]
Expand All @@ -5111,6 +5111,15 @@ def test_deserialize_sticker_pack(self, entity_factory_impl, sticker_pack_payloa
assert sticker.sort_value == 96
assert sticker.tags == ["thinking", "thonkang"]

def test_deserialize_sticker_pack_with_optional_fields(self, entity_factory_impl, sticker_pack_payload):
del sticker_pack_payload["cover_sticker_id"]
del sticker_pack_payload["banner_asset_id"]

pack = entity_factory_impl.deserialize_sticker_pack(sticker_pack_payload)

assert pack.cover_sticker_id is None
assert pack.banner_asset_id is None

def test_stickers(self, entity_factory_impl, guild_sticker_payload):
guild_definition = entity_factory_impl.deserialize_gateway_guild(
{"id": "265828729970753537", "stickers": [guild_sticker_payload]}, user_id=123321
Expand Down
12 changes: 9 additions & 3 deletions tests/hikari/test_stickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import mock
import pytest

from hikari import snowflakes
from hikari import stickers
from hikari import urls
from hikari.internal import routes
Expand All @@ -34,10 +35,10 @@ def model(self):
id=123,
name="testing",
description="testing description",
cover_sticker_id=None,
cover_sticker_id=snowflakes.Snowflake(6541234),
stickers=[],
sku_id=123,
banner_hash="abc123",
banner_asset_id=snowflakes.Snowflake(541231),
)

def test_banner_url(self, model):
Expand All @@ -54,11 +55,16 @@ def test_make_banner_url(self, model):

route.compile_to_file.assert_called_once_with(
urls.CDN_URL,
hash="abc123",
hash=541231,
size=512,
file_format="url",
)

def test_make_banner_url_when_no_banner_asset(self, model):
model.banner_asset_id = None

assert model.make_banner_url(ext="url", size=512) is None


class TestPartialSticker:
@pytest.fixture()
Expand Down

0 comments on commit 82980e7

Please sign in to comment.