Skip to content

Commit

Permalink
🍴 Merge #238: Forked Scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySreelal authored Apr 15, 2024
2 parents 15f2935 + f540e67 commit 8055602
Show file tree
Hide file tree
Showing 8 changed files with 523 additions and 178 deletions.
582 changes: 449 additions & 133 deletions lib/src/televerse/bot.dart

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/src/televerse/conversation/conversation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class Conversation {
_bot._handlerScopes.add(
HandlerScope(
isConversation: true,
name: scopeName,
options: ScopeOptions(name: scopeName),
predicate: (ctx) =>
_isSameChat(ctx.update, chatId) && filter(ctx.update),
types: UpdateType.values,
Expand Down
23 changes: 12 additions & 11 deletions lib/src/televerse/models/chat_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ abstract class ID {
///
/// If the passed [value] is neither an integer nor a string, this method will throw a [TeleverseException].
static ID create(dynamic value) {
switch (value.runtimeType) {
case int:
return ChatID.create(value);
case String:
return SupergroupID.create(value);
default:
throw TeleverseException(
"The passed value is not a valid chat id. The value must be an integer or a string.",
description: "The passed value is of type ${value.runtimeType}.",
type: TeleverseExceptionType.invalidParameter,
);
if (value is int) {
return ChatID.create(value);
}

if (value is String) {
return ChannelID.create(value);
}

throw TeleverseException(
"The passed value is not a valid chat id. The value must be an integer or a string.",
description: "The passed value is of type ${value.runtimeType}.",
type: TeleverseExceptionType.invalidParameter,
);
}

/// Returns the id as a string or an integer.
Expand Down
38 changes: 23 additions & 15 deletions lib/src/televerse/models/handler_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ part of 'models.dart';

/// A Handler Scope is used to define the scope and related information of a handler method.
class HandlerScope {
/// Optional. The name of the handler. (For debugging purposes)
final String? name;

/// Whether the handler is a special handler.
///
/// True if it's a command handler or a RegExp handler.
///
/// - If it's a command handler, we set `args` to the parameter of the command.
/// - If it's a RegExp handler, we'll set the `MessageContext.matches` to the matches of the RegExp.
final bool special;

/// If it's a command handler, we set `args` to the parameter of the command.
final bool isCommand;

Expand All @@ -23,6 +12,8 @@ class HandlerScope {
final RegExp? pattern;

/// Handler
///
/// Required unless [isConversation] is `true`.
final Handler? handler;

/// The update type
Expand All @@ -37,9 +28,14 @@ class HandlerScope {
/// Chat ID of the conversation.
final ID? chatId;

/// Scope Options - Additional parameters for the scope.
final ScopeOptions? options;

/// Flag whether already executed
bool isExecuted;

/// Creates a new [HandlerScope].
const HandlerScope({
this.name,
HandlerScope({
this.handler,
required this.predicate,
required this.types,
Expand All @@ -48,6 +44,18 @@ class HandlerScope {
this.pattern,
this.isConversation = false,
this.chatId,
}) : special = isCommand || isRegExp,
assert(handler != null || isConversation);
this.options,
this.isExecuted = false,
}) : assert(handler != null || isConversation);

/// Whether the scope is forked or not.
bool get forked => options?.forked ?? false;

/// Name of the scope
String? get name => options?.name;

/// Sets executiion status to true.
void executed() {
isExecuted = true;
}
}
33 changes: 33 additions & 0 deletions lib/src/televerse/models/scope_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
part of '../../../televerse.dart';

/// Represents additional options that can be passed to create Handler Scope
class ScopeOptions {
/// Name of the Handler Scope.
///
/// This can be used to remove a Handler later on your code.
final String? name;

/// Whether the scope is forked.
///
/// By default fork is set to `false` that is once a matching scope is found
/// the processor only executes that particular scope and skips all the rest.
///
/// When fork is set to `true` the successive Handler Scope is also considered
/// and executed if it satisfies the predicate.
final bool forked;

/// Constructs a `ScopeOption`.
///
/// - [name] - Name of the Handler Scope. This can be used to remove the scope later.
///
/// - [forked] - A flag used to determine whether the succeeding handler should also be executed
const ScopeOptions({
this.forked = false,
this.name,
});

/// Constructs a `ScopeOption` that is forked. This scope will allow the processing of subsequent handler scope as well.
///
/// - [name] - Name of the Handler Scope. This can be used to remove the scope later.
const ScopeOptions.forked({this.name}) : forked = true;
}
19 changes: 2 additions & 17 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,13 @@ enum _GetMeStatus {
class _PendingCall {
final Function fn;
final List<dynamic> params;
final Map<Symbol, dynamic> namedParams;

const _PendingCall({
required this.fn,
required this.params,
this.namedParams = const <Symbol, dynamic>{},
});

void call() {
switch (params.length) {
case 0:
fn.call();
break;
case 1:
fn.call(params[0]);
break;
case 2:
fn.call(params[0], params[1]);
break;
case 3:
fn.call(params[0], params[1], params[2]);
break;
}
}
}

/// Extension on `Chat` to create ID of the chat
Expand Down
2 changes: 2 additions & 0 deletions lib/televerse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io' as io;
import 'dart:math';
import 'dart:mirrors';
import 'package:dio/dio.dart';
import 'package:televerse/telegram.dart';
import 'package:televerse/televerse.dart';
Expand Down Expand Up @@ -60,6 +61,7 @@ part 'src/televerse/builders/message_content_generator.dart';

/// Extras
part 'src/televerse/extras/tg_exceptions.dart';
part 'src/televerse/models/scope_options.dart';

/// The main class of the library.
///
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ topics:
- bot-framework

environment:
sdk: ">=2.17.7 <4.0.0"
sdk: ">=3.0.0 <4.0.0"

dependencies:
dio: ^5.4.0
Expand Down

0 comments on commit 8055602

Please sign in to comment.