Skip to content

Commit 300b06b

Browse files
authored
New version - Develop to Main (#13)
* chore: add dropdown button to channel name * feat: deleting shared feat: refresh after import feat: remember tab * chore: improves * feat: improve import shared files and folders * fix: solve anonymous tab issue * chore: avoid check certificate
1 parent 1581ecc commit 300b06b

10 files changed

Lines changed: 129 additions & 13 deletions

File tree

TelegramDownloader/Configuration/config.example.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
{
33
"api_id": "",
44
"hash_id": "",
5-
"mongo_connection_string": ""
5+
"mongo_connection_string": "",
6+
"avoid_checking_certificate": false
67
}

TelegramDownloader/Controllers/FileController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ public async Task<IActionResult> ShareFiles(string id, string? bsonId, string? f
282282
sfm.id = id;
283283
sfm.name = fileName;
284284
sfm.fileName = fileName;
285+
sfm.invitation = await _ts.getInvitationHash(Convert.ToInt64(id));
285286
sfm.files = await _fs.ShareFile(id, bsonId);
286287
var json = System.Text.Json.JsonSerializer.Serialize(sfm);
287288
var bytes = System.Text.Encoding.UTF8.GetBytes(json);

TelegramDownloader/Data/FileService.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Components.Forms;
1+
using BlazorBootstrap;
2+
using Microsoft.AspNetCore.Components.Forms;
23
using Microsoft.AspNetCore.StaticFiles;
34
using Microsoft.Extensions.Logging;
45
using MongoDB.Driver;
@@ -232,15 +233,17 @@ public class FileService : IFileService
232233
private IDbService _db { get; set; }
233234
private ILogger<IFileService> _logger { get; set; }
234235
private TransactionInfoService _tis { get; set; }
236+
private ToastService _toastService { get; set; }
235237

236238
const int MaxSize = 1024 * 1024 * 1000; // 1GB
237239

238240

239-
public FileService(ITelegramService ts, IDbService db, ILogger<IFileService> logger, TransactionInfoService tis)
241+
public FileService(ITelegramService ts, IDbService db, ILogger<IFileService> logger, TransactionInfoService tis, ToastService toastService)
240242
{
241243
_ts = ts;
242244
_db = db;
243245
_tis = tis;
246+
_toastService = toastService;
244247
_logger = logger;
245248
createTempFolder();
246249
}
@@ -736,7 +739,13 @@ private async Task downloadFromTelegram(string dbName, int messageId, string des
736739
}
737740
model._transmitted = 0;
738741
model.callbacks = new Callbacks();
739-
model.channelName = _ts.getChatName(Convert.ToInt64(dbName));
742+
try
743+
{
744+
model.channelName = _ts.getChatName(Convert.ToInt64(dbName));
745+
} catch(Exception ex) {
746+
model.channelName = "Public or Shared";
747+
}
748+
740749
model.callbacks.callback = async () => await DownloadFileNow(dbName, messageId, destPath, model);
741750
_tis.addToPendingDownloadList(model);
742751
}
@@ -831,6 +840,19 @@ public async Task importSharedData(ShareFilesModel sfm, GenericNotificationProgr
831840

832841
try
833842
{
843+
if (!_ts.isInChat(Convert.ToInt64(sfm.id)) && sfm.invitation != null && !string.IsNullOrEmpty(sfm.invitation.invitationHash))
844+
try
845+
{
846+
await _ts.joinChatInvitationHash(sfm.invitation.invitationHash);
847+
_toastService.Notify(new(ToastType.Success, "Join chat", $"Joined to chat"));
848+
}
849+
catch (Exception ex)
850+
{
851+
_toastService.Notify(new(ToastType.Danger, $"Error: {ex.Message}."));
852+
}
853+
else
854+
_toastService.Notify(new(ToastType.Info, "You were already joined the chat"));
855+
834856
BsonSharedInfoModel bsi = new BsonSharedInfoModel();
835857
bsi.ChannelId = sfm.id;
836858
bsi.Name = sfm.name;

TelegramDownloader/Data/ITelegramService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using TelegramDownloader.Models;
22
using TL;
3+
using WTelegram;
34

45
namespace TelegramDownloader.Data
56
{
@@ -10,6 +11,9 @@ public interface ITelegramService
1011
bool checkChannelExist(string id);
1112
bool checkUserLogin();
1213
Task deleteFile(string chatId, int idMessage);
14+
bool isInChat(long id);
15+
Task<InvitationInfo?> getInvitationHash(long id);
16+
Task joinChatInvitationHash(string? hash);
1317
Task<User> CallQrGenerator(Action<string> func, CancellationToken ct, bool logoutFirst = false);
1418
Task<string> DownloadFile(ChatMessages message, string fileName = null, string folder = null, DownloadModel model = null);
1519
Task<Stream> DownloadFileAndReturn(ChatMessages message, Stream ms = null, string fileName = null, string folder = null, DownloadModel model = null);

TelegramDownloader/Data/TelegramService.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ private void createDownloadFolder()
4444
private void newClient()
4545
{
4646
client = new WTelegram.Client(Convert.ToInt32(GeneralConfigStatic.tlconfig?.api_id ?? Environment.GetEnvironmentVariable("api_id")), GeneralConfigStatic.tlconfig?.hash_id ?? Environment.GetEnvironmentVariable("hash_id"), UserService.USERDATAFOLDER + "/WTelegram.session");
47-
WTelegram.Helpers.Log = (lvl, str) => { }; // WTelegram.Helpers.Log = (lvl, str) => WTelegramLogs.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{"TDIWE!"[lvl]}] {str}");
47+
if (GeneralConfigStatic.config.ShouldShowLogInTerminal)
48+
{
49+
// WTelegram.Helpers.Log = (lvl, str) => Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{"TDIWE!"[lvl]}] {str}");
50+
} else
51+
{
52+
WTelegram.Helpers.Log = (lvl, str) => { };
53+
}
4854

4955
}
5056

@@ -168,6 +174,52 @@ public async Task logOff()
168174
client.Dispose();
169175
newClient();
170176
}
177+
178+
public async Task<InvitationInfo?> getInvitationHash(long id)
179+
{
180+
var peer = chats.chats[id];
181+
var invites = await client.Messages_GetExportedChatInvites(peer, client.User);
182+
if (invites != null && invites.invites.Count() > 0)
183+
{
184+
185+
if (invites.invites.FirstOrDefault() is ChatInviteExported invi)
186+
{
187+
return buildInvitationInfo(invi);
188+
}
189+
}
190+
var invitation = await client.Messages_ExportChatInvite(peer);
191+
if(invitation is ChatInviteExported inv)
192+
{
193+
return buildInvitationInfo(inv);
194+
}
195+
return null;
196+
}
197+
198+
private InvitationInfo buildInvitationInfo(ChatInviteExported invi)
199+
{
200+
var hash = invi.link.Split("/").LastOrDefault();
201+
if (hash[0] == '+')
202+
return new InvitationInfo(hash.Substring(1), invi.link);
203+
return new InvitationInfo(hash, invi.link);
204+
}
205+
206+
public async Task joinChatInvitationHash(string? hash)
207+
{
208+
await client.Messages_ImportChatInvite(hash);
209+
}
210+
211+
public bool isInChat(long id)
212+
{
213+
try
214+
{
215+
var _chat = chats.chats[id];
216+
return true;
217+
} catch(Exception)
218+
{
219+
return false;
220+
}
221+
}
222+
171223
public string getChatName(long id)
172224
{
173225
List<ChatMessages> cm = new List<ChatMessages>();

TelegramDownloader/Models/GeneralConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,6 @@ public class TLConfig
8181
public string? api_id { get; set; }
8282
public string? hash_id { get; set;}
8383
public string? mongo_connection_string { get; set; }
84+
public bool? avoid_checking_certificate { get; set; }
8485
}
8586
}

TelegramDownloader/Models/ShareFilesModel.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ public class ShareFilesModel
1010
public string id { get; set; }
1111
[MaxLength(40)]
1212
public string name { get; set; }
13+
public string chatName { get; set; }
1314
public string fileName { get; set; }
1415
public string description { get; set; }
16+
public InvitationInfo? invitation { get; set; }
1517
public List<BsonFileManagerModel> files { get; set; }
1618
}
1719

@@ -31,4 +33,17 @@ public class BsonSharedInfoModel
3133
public string Description { get; set; }
3234

3335
}
36+
37+
public class InvitationInfo
38+
{
39+
public string? invitationHash { get; set; }
40+
public string? invitationLink { get; set; }
41+
42+
public InvitationInfo() { }
43+
public InvitationInfo(string? invitationHash, string? invitationLink)
44+
{
45+
this.invitationHash = invitationHash;
46+
this.invitationLink = invitationLink;
47+
}
48+
}
3449
}

TelegramDownloader/Pages/Modals/ImportDataModal.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
@using TelegramDownloader.Data
22
@using TelegramDownloader.Models
3-
@inject ILogger<FileManager> Logger
4-
@inject IFileService fs
3+
@inject ILogger<FileManager> Logger;
4+
@inject IFileService fs;
5+
@inject ToastService toastService;
56

67
<div class="modal @ModalClass" tabindex="-1" role="dialog" style="display:@ModalDisplay;">
78
<div class="modal-dialog modal-dialog-centered" role="document">

TelegramDownloader/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using TelegramDownloader.Models;
1515
using TelegramDownloader.Services.GitHub;
1616
using System.Net.Http;
17+
using System.Net;
1718

1819
var extensionProvider = new FileExtensionContentTypeProvider();
1920
foreach(var mime in FileService.MIMETypesDictionary)
@@ -23,6 +24,13 @@
2324

2425
GeneralConfigStatic.loadDbConfig();
2526

27+
if (GeneralConfigStatic.tlconfig?.avoid_checking_certificate != null || Environment.GetEnvironmentVariable("avoid_checking_certificate") != null)
28+
if (GeneralConfigStatic.tlconfig?.avoid_checking_certificate ?? Convert.ToBoolean(Environment.GetEnvironmentVariable("avoid_checking_certificate")))
29+
ServicePointManager.ServerCertificateValidationCallback +=
30+
(sender, certificate, chain, errors) => {
31+
return true;
32+
};
33+
2634

2735
var builder = WebApplication.CreateBuilder(args);
2836
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Ngo9BigBOggjHTQxAR8/V1NDaF5cWWtCf1FpRmJGdld5fUVHYVZUTXxaS00DNHVRdkdnWXZccXRQR2FZVEVzWUQ=");

TelegramDownloader/Shared/NavMenu.razor

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,30 @@
247247

248248
private async Task OnTabShowingAsync(TabsEventArgs args)
249249
{
250-
await ProtectedSessionStore.SetAsync("tabmenu", args.ActiveTabTitle);
250+
try
251+
{
252+
await ProtectedSessionStore.SetAsync("tabmenu", args.ActiveTabTitle);
253+
} catch(Exception) {}
251254
}
252255

253256
private async Task getActiveTab()
254257
{
255-
var tab = await ProtectedSessionStore.GetAsync<string>("tabmenu");
256-
if (!tab.Success || string.IsNullOrEmpty(tab.Value))
258+
try
257259
{
258-
await tabs.ShowTabByNameAsync("Mine");
259-
} else
260+
var tab = await ProtectedSessionStore.GetAsync<string>("tabmenu");
261+
if (!tab.Success || string.IsNullOrEmpty(tab.Value))
262+
{
263+
await tabs.ShowTabByNameAsync("Mine");
264+
}
265+
else
266+
{
267+
await tabs.ShowTabByNameAsync(tab.Value);
268+
}
269+
} catch(Exception)
260270
{
261-
await tabs.ShowTabByNameAsync(tab.Value);
271+
262272
}
273+
263274
}
264275

265276
private async Task removeShared(string id, string collecionId)

0 commit comments

Comments
 (0)