Skip to content

Commit

Permalink
πŸ§‘πŸ»β€πŸ’» Reworked on all edit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySreelal committed Jun 18, 2024
1 parent bc4b459 commit b44a7c4
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 133 deletions.
215 changes: 82 additions & 133 deletions lib/src/televerse/context/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,6 @@ class Context {
}
}

/// Internal method to check whether the passed type argument is correct
void _verifyMessageOrBoolean<MessageOrBool>() {
if (MessageOrBool != Message && MessageOrBool != bool) {
throw TeleverseException.typeParameterRequired(
"editMessageMedia",
MessageOrBool,
[Message, bool],
);
}
}

/// Reply a Text Message to the user.
Future<Message> reply(
String text, {
Expand Down Expand Up @@ -832,7 +821,7 @@ class Context {
/// Edit message live location
///
/// This method will edit the message live location in the current context.
Future<MessageOrBool> editMessageLiveLocation<MessageOrBool>({
Future<bool> editMessageLiveLocation({
String? inlineMessageId,
double? latitude,
double? longitude,
Expand All @@ -842,32 +831,23 @@ class Context {
InlineKeyboardMarkup? replyMarkup,
String? businessConnectionId,
}) async {
_verifyMessageOrBoolean<MessageOrBool>();
if (_isInline()) {
return api.editInlineMessageLiveLocation(
_inlineMsgId!,
latitude: latitude,
longitude: longitude,
horizontalAccuracy: horizontalAccuracy,
heading: heading,
proximityAlertRadius: proximityAlertRadius,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
) as MessageOrBool;
} else {
if (!_isInline()) {
_verifyInfo([_chatId, _msgId], APIMethod.editMessageLiveLocation);
return api.editMessageLiveLocation(
id,
_msgId!,
latitude: latitude,
longitude: longitude,
horizontalAccuracy: horizontalAccuracy,
heading: heading,
proximityAlertRadius: proximityAlertRadius,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
) as MessageOrBool;
}

await api._editMessageLiveLocation<_Ignore>(
chatId: id,
messageId: _msgId,
inlineMessageId: _inlineMsgId,
latitude: latitude,
longitude: longitude,
horizontalAccuracy: horizontalAccuracy,
heading: heading,
proximityAlertRadius: proximityAlertRadius,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
);
return true;
}

/// Forward the message.
Expand Down Expand Up @@ -1241,27 +1221,23 @@ class Context {
/// Context aware method for stopping live location of message: [APIMethod.stopMessageLiveLocation].
///
/// Use this method to stop updating a live location message before live_period expires. On success, if the message is not an inline message, the edited Message is returned, otherwise True is returned.
Future<MessageOrBool> stopMessageLiveLocation<MessageOrBool>({
Future<bool> stopMessageLiveLocation({
InlineKeyboardMarkup? replyMarkup,
String? businessConnectionId,
}) async {
_verifyMessageOrBoolean<MessageOrBool>();

if (_isInline()) {
return api.stopInlineMessageLiveLocation(
_inlineMsgId!,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
) as MessageOrBool;
if (!_isInline()) {
_verifyInfo([_chatId, _msgId], APIMethod.stopMessageLiveLocation);
}

_verifyInfo([_chatId, _msgId], APIMethod.stopMessageLiveLocation);
return api.stopMessageLiveLocation(
id,
_msgId!,
await api._stopMessageLiveLocation<_Ignore>(
chatId: id,
messageId: _msgId,
inlineMessageId: _inlineMsgId,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
) as MessageOrBool;
);

return true;
}

/// Context aware method for send "Typing..." action: [APIMethod.sendChatAction].
Expand Down Expand Up @@ -1707,7 +1683,7 @@ class Context {
return _inlineMsgId != null;
}

/// Edit the message text
/// Edit the message text. Returns True on success.
Future<bool> editMessageText(
String text, {
ParseMode? parseMode,
Expand All @@ -1716,33 +1692,26 @@ class Context {
LinkPreviewOptions? linkPreviewOptions,
String? businessConnectionId,
}) async {
if (_isInline()) {
await api.editInlineMessageText(
_inlineMsgId!,
text,
parseMode: parseMode,
entities: entities,
replyMarkup: replyMarkup,
linkPreviewOptions: linkPreviewOptions,
businessConnectionId: businessConnectionId,
);
} else {
if (!_isInline()) {
_verifyInfo([_chatId, _msgId], APIMethod.editMessageText);
await api.editMessageText(
id,
_msgId!,
text,
parseMode: parseMode,
entities: entities,
replyMarkup: replyMarkup,
linkPreviewOptions: linkPreviewOptions,
businessConnectionId: businessConnectionId,
);
}

await api._editMessageText<_Ignore>(
text: text,
chatId: id,
inlineMessageId: _inlineMsgId,
messageId: _msgId,
parseMode: parseMode,
entities: entities,
replyMarkup: replyMarkup,
linkPreviewOptions: linkPreviewOptions,
businessConnectionId: businessConnectionId,
);

return true;
}

/// Edit the message caption
/// Edit the message caption. Returns `true` on success.
Future<bool> editMessageCaption({
String? caption,
ParseMode? parseMode,
Expand All @@ -1751,89 +1720,69 @@ class Context {
bool? showCaptionAboveMedia,
String? businessConnectionId,
}) async {
if (_isInline()) {
await api.editInlineMessageCaption(
_inlineMsgId!,
caption: caption,
parseMode: parseMode,
captionEntities: captionEntities,
replyMarkup: replyMarkup,
showCaptionAboveMedia: showCaptionAboveMedia,
businessConnectionId: businessConnectionId,
);
} else {
if (!_isInline()) {
_verifyInfo([_chatId, _msgId], APIMethod.editMessageCaption);
await api.editMessageCaption(
id,
_msgId!,
caption: caption,
parseMode: parseMode,
captionEntities: captionEntities,
replyMarkup: replyMarkup,
showCaptionAboveMedia: showCaptionAboveMedia,
businessConnectionId: businessConnectionId,
);
}

await api._editMessageCaption<_Ignore>(
chatId: id,
messageId: _msgId,
inlineMessageId: _inlineMsgId,
caption: caption,
parseMode: parseMode,
captionEntities: captionEntities,
replyMarkup: replyMarkup,
showCaptionAboveMedia: showCaptionAboveMedia,
businessConnectionId: businessConnectionId,
);

return true;
}

/// Edit the message media
/// Use this method to edit animation, audio, document, photo, or video messages.
///
/// Make sure to pass the type parameter for correct typing. If the message to be edited is an
/// inline message, the type parameter should be `bool`, otherwise `Message` should be passed.
///
/// Since Televerse don't deal with `dynamic` types on the public interface, if you do not pass
/// type parameter, this method will throw a `TeleverseException`.
Future<MessageOrBool> editMessageMedia<MessageOrBool>(
/// This method ignores the result and always returns True on success.
Future<bool> editMessageMedia(
InputMedia media, {
InlineKeyboardMarkup? replyMarkup,
String? businessConnectionId,
}) async {
_verifyMessageOrBoolean<MessageOrBool>();

if (_isInline()) {
return await api.editInlineMessageMedia(
_inlineMsgId!,
media,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
) as MessageOrBool;
} else {
if (!_isInline()) {
_verifyInfo([_chatId, _msgId], APIMethod.editMessageMedia);
return await api.editMessageMedia(
id,
_msgId!,
media,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
) as MessageOrBool;
}

await api._editMessageMedia<_Ignore>(
inlineMessageId: _inlineMsgId,
chatId: id,
messageId: _msgId,
media: media,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
);

return true;
}

/// Edit the message reply markup
/// Use this method to edit only the reply markup of messages.
Future<MessageOrBool> editMessageReplyMarkup<MessageOrBool>({
Future<bool> editMessageReplyMarkup({
InlineKeyboardMarkup? replyMarkup,
String? businessConnectionId,
}) async {
_verifyMessageOrBoolean<MessageOrBool>();

if (_isInline()) {
return await api.editInlineMessageReplyMarkup(
_inlineMsgId!,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
) as MessageOrBool;
} else {
if (!_isInline()) {
_verifyInfo([_chatId, _msgId], APIMethod.editMessageReplyMarkup);
return await api.editMessageReplyMarkup(
id,
_msgId!,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
) as MessageOrBool;
}

await api._editMessageReplyMarkup<_Ignore>(
chatId: id,
messageId: _msgId,
inlineMessageId: _inlineMsgId,
replyMarkup: replyMarkup,
businessConnectionId: businessConnectionId,
);

return true;
}

/// Answer inline query
Expand Down
25 changes: 25 additions & 0 deletions lib/src/televerse/raw_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,10 @@ class RawAPI {
params,
);

if (MessageOrBool == _Ignore) {
return _ignore as MessageOrBool;
}

if (MessageOrBool == Message) {
return Message.fromJson(response) as MessageOrBool;
}
Expand Down Expand Up @@ -1022,6 +1026,10 @@ class RawAPI {
params,
);

if (MessageOrBool == _Ignore) {
return _ignore as MessageOrBool;
}

if (MessageOrBool == Message) {
return Message.fromJson(response) as MessageOrBool;
}
Expand Down Expand Up @@ -2320,6 +2328,10 @@ class RawAPI {
params,
);

if (MessageOrBool == _Ignore) {
return _ignore as MessageOrBool;
}

if (MessageOrBool == Message) {
return Message.fromJson(response) as MessageOrBool;
}
Expand All @@ -2343,6 +2355,7 @@ class RawAPI {
String? businessConnectionId,
}) async {
return await _editMessageText<Message>(
chatId: chatId,
text: text,
messageId: messageId,
parseMode: parseMode,
Expand Down Expand Up @@ -2409,6 +2422,10 @@ class RawAPI {
params,
);

if (MessageOrBool == _Ignore) {
return _ignore as MessageOrBool;
}

if (MessageOrBool == Message) {
return Message.fromJson(response) as MessageOrBool;
}
Expand Down Expand Up @@ -2512,6 +2529,10 @@ class RawAPI {
);
}

if (MessageOrBool == _Ignore) {
return _ignore as MessageOrBool;
}

if (MessageOrBool == Message) {
return Message.fromJson(response) as MessageOrBool;
}
Expand Down Expand Up @@ -2583,6 +2604,10 @@ class RawAPI {
params,
);

if (MessageOrBool == _Ignore) {
return _ignore as MessageOrBool;
}

if (MessageOrBool == Message) {
return Message.fromJson(response) as MessageOrBool;
}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,11 @@ extension GetUserChatID on User {
return false;
}
}

/// (Internal) Ignore value
class _Ignore {
const _Ignore();
}

/// Ignores the value
const _ignore = _Ignore();

0 comments on commit b44a7c4

Please sign in to comment.