Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
￴￴ committed Oct 15, 2024
1 parent 7c0b79f commit 05a3fdd
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 938 deletions.
10 changes: 9 additions & 1 deletion package/telegram_client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ linux/flutter/ephemeral
macos/Flutter/ephemeral
windows/ephemeral
tmp/
temp/
temp/

wasm/


dev/

tg_db/
tg_file/
755 changes: 195 additions & 560 deletions package/telegram_client/lib/tdlib/tdlib_core.dart

Large diffs are not rendered by default.

65 changes: 23 additions & 42 deletions package/telegram_client/lib/tdlib/tdlib_library/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ import 'package:telegram_client/tdlib/update_td.dart';
import 'package:universal_io/io.dart';

abstract class TdlibBaseCore {
Future<bool> is_td_initialized() async{
return false;
}

/// create client id for multi client
String platformType() {
return "unknown";
Expand Down Expand Up @@ -114,11 +118,9 @@ abstract class TdlibBaseCore {
///
abstract class TdlibBase implements TdlibBaseCore {
final ReceivePort receivePort = ReceivePort();
final TelegramClientLibraryTdlibOptionParameter client_option =
TelegramClientLibraryTdlibOptionParameter.create(
final TelegramClientLibraryTdlibOptionParameter client_option = TelegramClientLibraryTdlibOptionParameter.create(
api_id: num.tryParse("OTQ1NzU=".general_lib_utils_decryptFromBase64()),
api_hash: 'YTM0MDZkZThkMTcxYmI0MjJiYjZkZGYzYmJkODAwZTI='
.general_lib_utils_decryptFromBase64(),
api_hash: 'YTM0MDZkZThkMTcxYmI0MjJiYjZkZGYzYmJkODAwZTI='.general_lib_utils_decryptFromBase64(),
database_directory: "tg_db",
files_directory: "tg_file",
use_file_database: true,
Expand All @@ -129,8 +131,7 @@ abstract class TdlibBase implements TdlibBaseCore {
system_language_code: 'en',
new_verbosity_level: 0,
application_version: 'v1',
device_model: 'VGVsZWdyYW0gQ2xpZW50IEFaS0FERVYgR0xPQkFMIENPUlBPUkFUSU9O'
.general_lib_utils_decryptFromBase64(),
device_model: 'VGVsZWdyYW0gQ2xpZW50IEFaS0FERVYgR0xPQkFMIENPUlBPUkFUSU9O'.general_lib_utils_decryptFromBase64(),
system_version: Platform.operatingSystemVersion,
database_key: "",
start: true,
Expand All @@ -157,12 +158,9 @@ abstract class TdlibBase implements TdlibBaseCore {
Duration invoke_time_out = Duration(minutes: 10);
double timeOutUpdate;
bool is_invoke_throw_on_error = false;
FutureOr<void> Function(dynamic update, TdlibBase libTdJson)?
on_receive_update;
FutureOr<String> Function(int client_id, TdlibBase libTdJson)?
on_generate_extra_invoke;
FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson)?
on_get_invoke_data;
FutureOr<void> Function(dynamic update, TdlibBase libTdJson)? on_receive_update;
FutureOr<String> Function(int client_id, TdlibBase libTdJson)? on_generate_extra_invoke;
FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson)? on_get_invoke_data;
int task_max_count;
int task_min_cooldown;
TdlibBase({
Expand Down Expand Up @@ -216,15 +214,12 @@ abstract class TdlibBase implements TdlibBaseCore {
TdlibIsolateReceiveData tdlibIsolateReceiveData = update;
try {
if (tdlibIsolateReceiveData.updateData["@extra"] is String) {
event_emitter.emit(
eventName: event_invoke, value: tdlibIsolateReceiveData);
event_emitter.emit(eventName: event_invoke, value: tdlibIsolateReceiveData);
} else {
event_emitter.emit(
eventName: event_update, value: tdlibIsolateReceiveData);
event_emitter.emit(eventName: event_update, value: tdlibIsolateReceiveData);
}
} catch (e) {
event_emitter.emit(
eventName: event_update, value: tdlibIsolateReceiveData);
event_emitter.emit(eventName: event_update, value: tdlibIsolateReceiveData);
}
} else if (update is TdlibIsolateReceiveDataError) {
is_init_isolate = false;
Expand Down Expand Up @@ -343,9 +338,7 @@ abstract class TdlibBase implements TdlibBaseCore {
client_new_option[key] = value;
}
});
final Map<String, num> tdlib_option_should_not_empty_num = {
"api_id": client_option.api_id ?? 0
};
final Map<String, num> tdlib_option_should_not_empty_num = {"api_id": client_option.api_id ?? 0};
tdlib_option_should_not_empty_num.forEach((key, value) {
try {
if (client_new_option[key] is num == false) {
Expand Down Expand Up @@ -518,11 +511,8 @@ abstract class TdlibBase implements TdlibBaseCore {
required String extra,
required bool isAutoGetChat,
required bool isInvokeThrowOnError,
required FutureOr<String> Function(int client_id, TdlibBase libTdJson)?
onGenerateExtraInvoke,
required FutureOr<Map> Function(
String extra, int client_id, TdlibBase libTdJson)?
onGetInvokeData,
required FutureOr<String> Function(int client_id, TdlibBase libTdJson)? onGenerateExtraInvoke,
required FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson)? onGetInvokeData,
}) async {
final Completer<Map> completer = Completer<Map>();
late final EventEmitterListener listener;
Expand All @@ -544,8 +534,7 @@ abstract class TdlibBase implements TdlibBaseCore {
if (extra.isEmpty) {
parameters["@extra"] = extra;
} else if (onGenerateExtraInvoke != null) {
parameters["@extra"] =
await onGenerateExtraInvoke(clientId, this);
parameters["@extra"] = await onGenerateExtraInvoke(clientId, this);
} else {
parameters["@extra"] = generateUuid(15);
}
Expand All @@ -558,9 +547,7 @@ abstract class TdlibBase implements TdlibBaseCore {
return extra_procces;
}();

if (isAutoGetChat &&
RegExp(r"^(sendMessage|getChatMember)$", caseSensitive: false)
.hashData(parameters["@type"])) {
if (isAutoGetChat && RegExp(r"^(sendMessage|getChatMember)$", caseSensitive: false).hashData(parameters["@type"])) {
if (parameters["chat_id"] is int) {
td_send(clientId, {
"@type": "getChat",
Expand Down Expand Up @@ -644,9 +631,7 @@ abstract class TdlibBase implements TdlibBaseCore {
event_emitter.off(listener: listener);
} catch (e) {}

if (result.isEmpty ||
result["@type"] is String == false ||
result["@type"] == "error") {
if (result.isEmpty || result["@type"] is String == false || result["@type"] == "error") {
if (isInvokeThrowOnError) {
result["@type"] = "error";
throw result;
Expand Down Expand Up @@ -679,10 +664,8 @@ abstract class TdlibBase implements TdlibBaseCore {
String? extra,
bool? isAutoGetChat,
bool? isInvokeThrowOnError,
FutureOr<String> Function(int client_id, TdlibBase libTdJson)?
onGenerateExtraInvoke,
FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson)?
onGetInvokeData,
FutureOr<String> Function(int client_id, TdlibBase libTdJson)? onGenerateExtraInvoke,
FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson)? onGetInvokeData,
}) async {
isUseCache ??= false;
durationCacheExpire ??= Duration(
Expand Down Expand Up @@ -724,10 +707,8 @@ abstract class TdlibBase implements TdlibBaseCore {
bool? isUseCache,
Duration? durationCacheExpire,
bool? isAutoGetChat,
FutureOr<String> Function(int client_id, TdlibBase libTdJson)?
onGenerateExtraInvoke,
FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson)?
onGetInvokeData,
FutureOr<String> Function(int client_id, TdlibBase libTdJson)? onGenerateExtraInvoke,
FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson)? onGetInvokeData,
bool? isInvokeThrowOnError,
}) async {
return await invoke(
Expand Down
5 changes: 5 additions & 0 deletions package/telegram_client/lib/tdlib/tdlib_library/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,9 @@ class TdlibNative extends TdlibBase {
String platformType() {
return "io";
}

@override
Future<bool> is_td_initialized() async{
return true;
}
}
6 changes: 5 additions & 1 deletion package/telegram_client/lib/tdlib/tdlib_library/none.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class TdlibNative extends TdlibBase {
super.task_min_cooldown,
super.timeOutUpdate,
});

@override
int td_create_client_id() {
// TODO: implement td_create_client_id
Expand All @@ -95,4 +94,9 @@ class TdlibNative extends TdlibBase {
String platformType() {
return "none";
}

@override
Future<bool> is_td_initialized() async{
return true;
}
}
2 changes: 1 addition & 1 deletion package/telegram_client/lib/tdlib/tdlib_library/tdlib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Bukan maksud kami menipu itu karena harga yang sudah di kalkulasi + bantuan tiba
export "base.dart";
export "none.dart"
if (dart.library.io) 'io.dart'
if (dart.library.html) 'web.dart';
if (dart.library.js_interop) 'web.dart';
// export 'tdlib_io.dart';
// //
// export "tdlib_none.dart";
42 changes: 22 additions & 20 deletions package/telegram_client/lib/tdlib/tdlib_library/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ Bukan maksud kami menipu itu karena harga yang sudah di kalkulasi + bantuan tiba

// import 'dart:ffi';

import 'dart:async';

import 'package:telegram_client/tdlib/tdlib_library/base.dart';
import 'package:universal_io/io.dart';
import 'package:wasm_ffi/ffi.dart';
import 'package:wasm_ffi/ffi.dart';

typedef TdCharNative = Pointer<Char>;
typedef TdReceiveNative = TdCharNative Function(Double timout);
Expand Down Expand Up @@ -68,6 +69,7 @@ typedef TdExecuteNative = TdCharNative Function(TdCharNative parameters);
/// ````
///
class TdlibNative extends TdlibBase {
static final Completer<bool> _td_completer = Completer<bool>();
TdlibNative({
super.clientOption,
super.delayInvoke,
Expand All @@ -86,20 +88,17 @@ class TdlibNative extends TdlibBase {
super.task_min_cooldown,
super.timeOutUpdate,
}) {
opentdLib(pathTdlib: path_tdlib).then((a){


if (client_option["start"] == true) {
invokeSync(
parameters: {
"@type": "setLogVerbosityLevel",
"new_verbosity_level": client_option['new_verbosity_level'],
},
);
ensureInitialized();
}
opentdLib(pathTdlib: path_tdlib).then((a) {
if (client_option["start"] == true) {
invokeSync(
parameters: {
"@type": "setLogVerbosityLevel",
"new_verbosity_level": client_option['new_verbosity_level'],
},
);
ensureInitialized();
}
});

}
static late DynamicLibrary tdLib;
static bool is_open_tdlib = false;
Expand Down Expand Up @@ -129,6 +128,11 @@ class TdlibNative extends TdlibBase {
String platformType() {
return "web";
}

@override
Future<bool> is_td_initialized() {
return _td_completer.future;
}
}

Future<void> opentdLib({
Expand All @@ -138,14 +142,12 @@ Future<void> opentdLib({
if (TdlibNative.is_open_tdlib) {
return;
}
if (Platform.isIOS || Platform.isMacOS) {
TdlibNative.tdLib = await DynamicLibrary.open(pathTdlib);
} else {
TdlibNative.tdLib = await DynamicLibrary.open(pathTdlib);
}

TdlibNative.tdLib = await DynamicLibrary.open(pathTdlib);
TdlibNative.is_open_tdlib = true;
TdlibNative.td_execute_native_function = TdlibNative.tdLib.lookupFunction<TdExecuteNative, TdExecuteNative>('td_execute');
TdlibNative.td_send_function = TdlibNative.tdLib.lookupFunction<TdSendNative, TdSendDart>('td_send');
TdlibNative.td_pointer_native_function = TdlibNative.tdLib.lookupFunction<TdCreateClientIdNative, TdCreateClientIdDart>('td_create_client_id');
TdlibNative.td_receive_function = TdlibNative.tdLib.lookupFunction<TdReceiveNative, TdReceiveDart>('td_receive');
TdlibNative._td_completer.complete(true);
}
Loading

0 comments on commit 05a3fdd

Please sign in to comment.