Skip to content

Commit

Permalink
Merge #313 from xooniverse/dev
Browse files Browse the repository at this point in the history
v2.1.1
  • Loading branch information
HeySreelal authored Dec 20, 2024
2 parents 17138cc + a062d35 commit 18cb4bc
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 113 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Publish to pub.dev

on:
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest

permissions:
id-token: write

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Dart
uses: dart-lang/setup-dart@v1

- name: Install dependencies
run: dart pub get

- name: Verify package
run: dart pub publish --dry-run

- name: Publish package
run: dart pub publish --force
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.1.1

- Updated switch statements to Switch expressions.
- Fix: `BackgroundTypeType.chatTheme` typo

# 2.1.0

- 🤖 Bot API 8.1
Expand Down
16 changes: 6 additions & 10 deletions lib/src/telegram/models/abstracts/background_fill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ abstract class BackgroundFill {
/// Creates a new [BackgroundFill] object.
/// This method decides which [BackgroundFill] subclass to use based on the [type] field.
factory BackgroundFill.fromJson(Map<String, dynamic> json) {
switch (json['type']) {
case 'solid':
return BackgroundFillSolid.fromJson(json);
case 'gradient':
return BackgroundFillGradient.fromJson(json);
case 'freeform_gradient':
return BackgroundFillFreeformGradient.fromJson(json);
default:
throw ArgumentError('Invalid background fill type');
}
return switch (BackgroundFillType.fromJson(json['type'])) {
BackgroundFillType.solid => BackgroundFillSolid.fromJson(json),
BackgroundFillType.gradient => BackgroundFillGradient.fromJson(json),
BackgroundFillType.freeformGradient =>
BackgroundFillFreeformGradient.fromJson(json),
};
}

/// Creates a new [BackgroundFill] object from JSON.
Expand Down
18 changes: 6 additions & 12 deletions lib/src/telegram/models/abstracts/background_type_fill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@ abstract class BackgroundType {
/// Creates a new [BackgroundType] object.
/// This method decides which [BackgroundType] subclass to use based on the [type] field.
static BackgroundType create(Map<String, dynamic> json) {
switch (json['type']) {
case 'fill':
return BackgroundTypeFill.fromJson(json);
case 'wallpaper':
return BackgroundTypeWallpaper.fromJson(json);
case 'pattern':
return BackgroundTypePattern.fromJson(json);
case 'chat_theme':
return BackgroundTypeChatTheme.fromJson(json);
default:
throw ArgumentError('Invalid background type');
}
return switch (BackgroundTypeType.fromJson(json['type'])) {
BackgroundTypeType.fill => BackgroundTypeFill.fromJson(json),
BackgroundTypeType.wallpaper => BackgroundTypeWallpaper.fromJson(json),
BackgroundTypeType.pattern => BackgroundTypePattern.fromJson(json),
BackgroundTypeType.chatTheme => BackgroundTypeChatTheme.fromJson(json),
};
}

/// Creates a new [BackgroundType] object from JSON.
Expand Down
20 changes: 5 additions & 15 deletions lib/src/telegram/models/abstracts/chat_boost_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,10 @@ abstract class ChatBoostSource {

/// Creates a `ChatBoostSource` object from a JSON object.
factory ChatBoostSource.fromJson(Map<String, dynamic> json) {
switch (json['type']) {
case 'premium':
return ChatBoostSourcePremium.fromJson(json);
case 'gift_code':
return ChatBoostSourceGiftCode.fromJson(json);
case 'giveaway':
return ChatBoostSourceGiveaway.fromJson(json);
default:
throw TeleverseException(
"Invalid value '${json['type']}' for ChatBoostSourceType.",
description:
'The given value does not match any ChatBoostSourceType.',
type: TeleverseExceptionType.invalidParameter,
);
}
return switch (ChatBoostSourceType.fromJson(json['type'])) {
ChatBoostSourceType.premium => ChatBoostSourcePremium.fromJson(json),
ChatBoostSourceType.giftCode => ChatBoostSourceGiftCode.fromJson(json),
ChatBoostSourceType.giveaway => ChatBoostSourceGiveaway.fromJson(json),
};
}
}
28 changes: 8 additions & 20 deletions lib/src/telegram/models/abstracts/chat_member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,14 @@ abstract class ChatMember implements WithUser {
/// Creates a new [ChatMember] object from a JSON object.
/// This method decides which [ChatMember] subclass to use based on the [status] field.
static ChatMember fromJson(Map<String, dynamic> json) {
switch (json['status'] as String) {
case 'creator':
return ChatMemberOwner.fromJson(json);
case 'administrator':
return ChatMemberAdministrator.fromJson(json);
case 'member':
return ChatMemberMember.fromJson(json);
case 'restricted':
return ChatMemberRestricted.fromJson(json);
case 'left':
return ChatMemberLeft.fromJson(json);
case 'kicked':
return ChatMemberBanned.fromJson(json);
default:
throw TeleverseException(
'Unknown ChatMember status: ${json['status']}',
description: 'The given status does not match any ChatMemberStatus.',
type: TeleverseExceptionType.invalidParameter,
);
}
return switch (ChatMemberStatus.fromJson(json['status'])) {
ChatMemberStatus.creator => ChatMemberOwner.fromJson(json),
ChatMemberStatus.administrator => ChatMemberAdministrator.fromJson(json),
ChatMemberStatus.member => ChatMemberMember.fromJson(json),
ChatMemberStatus.restricted => ChatMemberRestricted.fromJson(json),
ChatMemberStatus.left => ChatMemberLeft.fromJson(json),
ChatMemberStatus.kicked => ChatMemberBanned.fromJson(json),
};
}

/// Converts a [ChatMember] to a [Map] for JSON encoding.
Expand Down
8 changes: 4 additions & 4 deletions lib/src/telegram/models/abstracts/menu_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ abstract class MenuButton {
/// Creates a new [MenuButton] object.
/// This method decides which [MenuButton] subclass to use based on the [type] field.
static MenuButton create(Map<String, dynamic> json) {
return switch (json['type']) {
'commands' => MenuButtonCommands.fromJson(json),
'web_app' => MenuButtonWebApp.fromJson(json),
_ => MenuButtonDefault.fromJson(json),
return switch (MenuButtonType.fromJson(json['type'])) {
MenuButtonType.commands => MenuButtonCommands.fromJson(json),
MenuButtonType.webApp => MenuButtonWebApp.fromJson(json),
MenuButtonType.defaultButton => MenuButtonDefault.fromJson(json),
};
}

Expand Down
23 changes: 6 additions & 17 deletions lib/src/telegram/models/abstracts/message_origin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,12 @@ abstract class MessageOrigin {

/// Creates a new [MessageOrigin] instance from a JSON object.
factory MessageOrigin.fromJson(Map<String, dynamic> json) {
switch (json['type']) {
case 'user':
return MessageOriginUser.fromJson(json);
case 'hidden_user':
return MessageOriginHiddenUser.fromJson(json);
case 'chat':
return MessageOriginChat.fromJson(json);
case 'channel':
return MessageOriginChannel.fromJson(json);
default:
throw TeleverseException(
"Unknown message origin type",
description:
"The given JSON object does not match any MessageOrigin type.",
type: TeleverseExceptionType.invalidParameter,
);
}
return switch (MessageOriginType.from(json['type'])) {
MessageOriginType.user => MessageOriginUser.fromJson(json),
MessageOriginType.hiddenUser => MessageOriginHiddenUser.fromJson(json),
MessageOriginType.chat => MessageOriginChat.fromJson(json),
MessageOriginType.channel => MessageOriginChannel.fromJson(json),
};
}

/// Converts [MessageOrigin] instance to a JSON object.
Expand Down
9 changes: 4 additions & 5 deletions lib/src/telegram/models/abstracts/paid_media.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ abstract class PaidMedia {
/// Creates a new [PaidMedia] object.
/// This method decides which [PaidMedia] subclass to use based on the [type] field.
static PaidMedia fromJson(Map<String, dynamic> json) {
return switch (json['type']) {
'preview' => PaidMediaPreview.fromJson(json),
'photo' => PaidMediaPhoto.fromJson(json),
'video' => PaidMediaVideo.fromJson(json),
_ => throw ArgumentError('Invalid paid media type')
return switch (PaidMediaType.fromJson(json['type'])) {
PaidMediaType.preview => PaidMediaPreview.fromJson(json),
PaidMediaType.photo => PaidMediaPhoto.fromJson(json),
PaidMediaType.video => PaidMediaVideo.fromJson(json),
};
}

Expand Down
18 changes: 8 additions & 10 deletions lib/src/telegram/models/abstracts/revenue_withdrawal_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ abstract class RevenueWithdrawalState {
/// Creates a new [RevenueWithdrawalState] object.
/// This method decides which [RevenueWithdrawalState] subclass to use based on the [type] field.
static RevenueWithdrawalState fromJson(Map<String, dynamic> json) {
switch (RevenueWithdrawalStateType.fromJson(json['type'])) {
case RevenueWithdrawalStateType.pending:
return RevenueWithdrawalStatePending.fromJson(json);
case RevenueWithdrawalStateType.succeeded:
return RevenueWithdrawalStateSucceeded.fromJson(json);
case RevenueWithdrawalStateType.failed:
return RevenueWithdrawalStateFailed.fromJson(json);
default:
throw ArgumentError('Invalid revenue withdrawal state type');
}
return switch (RevenueWithdrawalStateType.fromJson(json['type'])) {
RevenueWithdrawalStateType.pending =>
RevenueWithdrawalStatePending.fromJson(json),
RevenueWithdrawalStateType.succeeded =>
RevenueWithdrawalStateSucceeded.fromJson(json),
RevenueWithdrawalStateType.failed =>
RevenueWithdrawalStateFailed.fromJson(json),
};
}

/// Creates a new [RevenueWithdrawalState] object from JSON.
Expand Down
17 changes: 11 additions & 6 deletions lib/src/telegram/models/abstracts/transaction_partner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ abstract class TransactionPartner {
/// Creates a new [TransactionPartner] object.
/// This method decides which [TransactionPartner] subclass to use based on the [type] field.
static TransactionPartner fromJson(Map<String, dynamic> json) {
return switch (json['type']) {
'fragment' => TransactionPartnerFragment.fromJson(json),
'user' => TransactionPartnerUser.fromJson(json),
'other' => TransactionPartnerOther.fromJson(json),
'telegram_ads' => TransactionPartnerTelegramAds.fromJson(json),
_ => throw ArgumentError('Invalid transaction partner type')
return switch (TransactionPartnerType.fromJson(json['type'])) {
TransactionPartnerType.fragment =>
TransactionPartnerFragment.fromJson(json),
TransactionPartnerType.user => TransactionPartnerUser.fromJson(json),
TransactionPartnerType.other => TransactionPartnerOther.fromJson(json),
TransactionPartnerType.telegramAds =>
TransactionPartnerTelegramAds.fromJson(json),
TransactionPartnerType.affiliateProgram =>
TransactionPartnerAffiliateProgram.fromJson(json),
TransactionPartnerType.telegramApi =>
TransactionPartnerTelegramApi.fromJson(json),
};
}

Expand Down
15 changes: 3 additions & 12 deletions lib/src/types/background_fill_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum BackgroundFillType {
gradient("gradient"),

/// The background is a freeform gradient that rotates after every message in the chat.
freeformGradient("freeformGradient"),
freeformGradient("freeform_gradient"),
;

/// The value of this enum.
Expand All @@ -18,16 +18,7 @@ enum BackgroundFillType {
/// Constructs a new [BackgroundFillType].
const BackgroundFillType(this.value);

factory BackgroundFillType.fromString(String val) {
switch (val) {
case "solid":
return solid;
case "gradient":
return gradient;
case "freeform_gradient":
return freeformGradient;
default:
throw ArgumentError("That's not a possible value");
}
factory BackgroundFillType.fromJson(String json) {
return values.singleWhere((e) => e.value == json);
}
}
9 changes: 8 additions & 1 deletion lib/src/types/background_type_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ enum BackgroundTypeType {
pattern("pattern"),

/// The background is taken directly from a built-in chat theme.
chatTheme("chatTheme"),
chatTheme("chat_theme"),
;

/// The value of this enum.
final String value;

/// Constructs a new [BackgroundFillType].
const BackgroundTypeType(this.value);

/// Creates the type object from passed type string.
static BackgroundTypeType fromJson(String json) {
return values.firstWhere((e) {
return e.value == json;
});
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: televerse
description: Televerse lets you create your own efficient Telegram bots with ease in Dart. Supports latest Telegram Bot API - 8.1!
version: 2.1.0
version: 2.1.1
homepage: https://televerse.xooniverse.com
repository: https://github.com/xooniverse/televerse
topics:
Expand Down

0 comments on commit 18cb4bc

Please sign in to comment.