Skip to content

Commit

Permalink
Fix invalid casts when fetching channels from cache (#483)
Browse files Browse the repository at this point in the history
* ref: nullable `animated` for icon/avatar/splash URL properties (#475)

feat: animated icon/avatar/splash URL helpers on null

* Revert #475 (#476)

* Revert "ref: nullable `animated` for icon/avatar/splash URL properties (#475)"

This reverts commit 20c1bc8.

* fix: remove ifs

* Make `true` default value for `animated` prop

* [ci skip] fmt

* Fix invalid casts

* Release 5.0.4

* Default animated to false for CDN urls

* Format files

---------

Co-authored-by: Ventus <[email protected]>
Co-authored-by: Rapougnac <[email protected]>
  • Loading branch information
3 people authored Jun 4, 2023
1 parent 20f0a7b commit 37c432a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 5.0.4
__04.06.2023__

- bug: Fix invalid casts

## 5.0.3
__11.04.2023__

Expand Down
10 changes: 9 additions & 1 deletion lib/src/core/channel/cacheable_text_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ class CacheableTextChannel<S extends ITextChannel> extends Channel implements IC
CacheableTextChannel(INyxx client, Snowflake id, [ChannelType type = ChannelType.unknown]) : super.raw(client, id, type);

@override
S? getFromCache() => client.channels[id] as S?;
S? getFromCache() {
final cached = client.channels[id];

if (cached is S) {
return cached;
}

return null;
}

@override
Future<S> download() => client.httpEndpoints.fetchChannel(id);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/user/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class User extends SnowflakeEntity implements IUser {
@override
FutureOr<IDMChannel> get dmChannel {
try {
return client.channels.values.firstWhere((item) => item is IDMChannel && item.participants.contains(this)) as Future<IDMChannel>;
return client.channels.values.firstWhere((item) => item is IDMChannel && item.participants.contains(this)) as IDMChannel;
} on StateError {
return client.httpEndpoints.createDMChannel(id);
}
Expand Down
10 changes: 9 additions & 1 deletion lib/src/internal/cache/cacheable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,15 @@ class ChannelCacheable<T extends IChannel> extends Cacheable<Snowflake, T> {
ChannelCacheable(INyxx client, Snowflake id) : super(client, id);

@override
T? getFromCache() => client.channels[id] as T?;
T? getFromCache() {
final cached = client.channels[id];

if (cached is T) {
return cached;
}

return null;
}

@override
Future<T> download() => client.httpEndpoints.fetchChannel<T>(id);
Expand Down
8 changes: 2 additions & 6 deletions lib/src/internal/cdn_http_endpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,9 @@ abstract class ICdnHttpEndpoints {

class CdnHttpEndpoints implements ICdnHttpEndpoints {
String _makeAnimatedCdnUrl(ICdnHttpRoute fragment, String hash, {String format = 'webp', int? size, bool animated = false}) {
if (hash.startsWith('a_') && animated) {
animated = true;
} else {
animated = false;
}
final isAnimated = animated && hash.startsWith('a_');

return _makeCdnUrl(fragment, format: format, size: size, animated: animated);
return _makeCdnUrl(fragment, format: format, size: size, animated: isAnimated);
}

String _makeCdnUrl(ICdnHttpRoute fragments, {String format = 'webp', int? size, bool animated = false}) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/internal/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Constants {
static const int apiVersion = 10;

/// Version of Nyxx
static const String version = "5.0.3";
static const String version = "5.0.4";

/// Url to Nyxx repo
static const String repoUrl = "https://github.com/nyxx-discord/nyxx";
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: nyxx
version: 5.0.3
version: 5.0.4
description: A Discord library for Dart. Simple, robust framework for creating discord bots for Dart language.
homepage: https://github.com/nyxx-discord/nyxx
repository: https://github.com/nyxx-discord/nyxx
Expand Down

0 comments on commit 37c432a

Please sign in to comment.