revert contacts, refactor
This commit is contained in:
parent
ef91665a3f
commit
d52abd93e0
|
@ -40,13 +40,14 @@ namespace telegram
|
|||
new UnreadsCommand(),
|
||||
new CloseUnreadCommand(),
|
||||
new ListChatsCommand(),
|
||||
new NewChatCommand(),
|
||||
new ListSecretChatsCommand(),
|
||||
new OpenSecretCommand(),
|
||||
new OpenSecretDirectCommand(),
|
||||
new NewSecretChatCommand(),
|
||||
new CloseSecretChatCommand(),
|
||||
new SearchUserCommand(),
|
||||
new AddContactCommand(),
|
||||
//new AddContactCommand(),
|
||||
new QuitCommand(),
|
||||
new HelpCommand(),
|
||||
new LogoutCommand(),
|
||||
|
@ -152,6 +153,75 @@ namespace telegram
|
|||
}
|
||||
}
|
||||
|
||||
public class NewChatCommand : Command
|
||||
{
|
||||
public NewChatCommand() : base("n", "", "starts a new chat.", "<username>", 1)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
var chat = GetChatByUsernameGlobal(inputParams[0]);
|
||||
if (chat == null) return; //TODO do something
|
||||
|
||||
currentChatId = 0;
|
||||
currentChatUserId = 0;
|
||||
currentUserRead = false;
|
||||
|
||||
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat)
|
||||
{
|
||||
currentChatUserId = privChat.UserId;
|
||||
}
|
||||
|
||||
currentChatId = chat.Id;
|
||||
|
||||
chat.Title = TruncateString(chat.Title, 20);
|
||||
|
||||
prefix = $"[{chat.Title}";
|
||||
lock (@lock)
|
||||
{
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening chat: {chat.Title}");
|
||||
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" +
|
||||
$"{(chat.UnreadCount == 1 ? "." : "s.")}");
|
||||
|
||||
if (chat.UnreadCount >= 5)
|
||||
{
|
||||
var capped = chat.UnreadCount > 50;
|
||||
GetHistory(chat.Id, capped ? 50 : chat.UnreadCount).ForEach(AddMessageToQueue);
|
||||
if (capped)
|
||||
messageQueue.Add(
|
||||
$"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
|
||||
}
|
||||
else if (chat.UnreadCount > 0)
|
||||
{
|
||||
var unreads = GetHistory(chat.Id, chat.UnreadCount);
|
||||
Console.WriteLine(unreads.Count);
|
||||
var rest = GetHistory(chat.Id, 5 - unreads.Count, unreads.First().Id);
|
||||
rest.ForEach(AddMessageToQueue);
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---");
|
||||
unreads.ForEach(AddMessageToQueue);
|
||||
}
|
||||
else
|
||||
{
|
||||
GetHistory(chat.Id).ForEach(AddMessageToQueue);
|
||||
}
|
||||
}
|
||||
|
||||
var history = GetHistory(currentChatId, 50);
|
||||
if (history.Count != 0)
|
||||
MarkRead(chat.Id, history.First().Id);
|
||||
var last = history.LastOrDefault(p => p.IsOutgoing);
|
||||
if (last == null)
|
||||
{
|
||||
currentUserRead = true;
|
||||
return;
|
||||
}
|
||||
|
||||
lastMessage = last;
|
||||
currentUserRead = IsMessageRead(last.ChatId, last.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public class CloseSecretChatCommand : Command
|
||||
{
|
||||
public CloseSecretChatCommand() : base("cs", "", "closes a secret chat (permanently)", "", 0)
|
||||
|
@ -177,17 +247,16 @@ namespace telegram
|
|||
|
||||
public class NewSecretChatCommand : Command
|
||||
{
|
||||
public NewSecretChatCommand() : base("ns", "", "creates a new secret chat. queries contacts", "<query>", -1)
|
||||
public NewSecretChatCommand() : base("ns", "", "creates a new secret chat.", "<username>", 1)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
|
||||
var userId = SearchContacts(query);
|
||||
var userId = GetUserIdByUsername(inputParams[0]);
|
||||
|
||||
if (userId == 0)
|
||||
return;
|
||||
return; //TODO do something
|
||||
if (GetSecretChats().Count(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId) > 0)
|
||||
return; //TODO do something
|
||||
|
||||
|
@ -283,14 +352,14 @@ namespace telegram
|
|||
|
||||
public class OpenSecretCommand : Command
|
||||
{
|
||||
public OpenSecretCommand() : base("os", "", "opens a secret chat. queries contacts.", "<query>", -1)
|
||||
public OpenSecretCommand() : base("os", "", "opens a secret chat. queries chat list.", "<query>", -1)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
|
||||
var userId = SearchContacts(query);
|
||||
var userId = SearchUserInChats(query);
|
||||
if (userId == 0 || query.Length == 0)
|
||||
return; //TODO do something;
|
||||
var chat = GetSecretChats().Find(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId);
|
||||
|
@ -513,7 +582,7 @@ namespace telegram
|
|||
|
||||
public class SearchUserCommand : Command
|
||||
{
|
||||
public SearchUserCommand() : base("su", "", "searches user globally", "<query>", -1)
|
||||
public SearchUserCommand() : base("s", "", "searches for users globally", "<query>", -1)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -556,14 +625,10 @@ namespace telegram
|
|||
|
||||
if (chat.Type is TdApi.ChatType.ChatTypePrivate type)
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"AddUserToContacts({type.UserId}, {chat.Title});");
|
||||
var user = SearchContacts("@" + GetUser(type.UserId).Username);
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{user}");
|
||||
//TODO implement when TDLib 1.6 is released
|
||||
}
|
||||
else
|
||||
return; //TODO
|
||||
return; //TODO do something
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -103,6 +103,7 @@ namespace telegram
|
|||
{
|
||||
if (limit <= 0)
|
||||
{
|
||||
if (total == 0) return history;
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] " +
|
||||
$"Limit cannot be less than one. Usage: /history <count>");
|
||||
|
@ -168,34 +169,56 @@ namespace telegram
|
|||
{
|
||||
Query = query
|
||||
}).Result;
|
||||
return response.ChatIds.Select(GetChat).ToList();
|
||||
|
||||
var chats = response.ChatIds.Select(GetChat).ToList();
|
||||
|
||||
chats.AddRange(client.ExecuteAsync(new SearchChats
|
||||
{
|
||||
Query = query,
|
||||
Limit = int.MaxValue
|
||||
}).Result.ChatIds.Select(GetChat));
|
||||
|
||||
return chats;
|
||||
}
|
||||
|
||||
public static Chat GetChatByUsernameGlobal(string username)
|
||||
{
|
||||
var response = client.ExecuteAsync(new SearchPublicChat
|
||||
try
|
||||
{
|
||||
Username = username
|
||||
}).Result;
|
||||
return response;
|
||||
var response = client.ExecuteAsync(new SearchPublicChat
|
||||
{
|
||||
Username = username
|
||||
}).Result;
|
||||
return response;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetUserIdByUsername(string username)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = client.ExecuteAsync(new SearchPublicChat
|
||||
{
|
||||
Username = username
|
||||
}).Result;
|
||||
|
||||
if (response.Type is ChatType.ChatTypePrivate priv)
|
||||
return priv.UserId;
|
||||
return 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddUserToContacts(int userId, string name)
|
||||
{
|
||||
//TODO this is broken
|
||||
var response = client.ExecuteAsync(new ImportContacts
|
||||
{
|
||||
Contacts = new[]
|
||||
{
|
||||
new Contact
|
||||
{
|
||||
UserId = userId,
|
||||
FirstName = name,
|
||||
LastName = "",
|
||||
PhoneNumber = ""
|
||||
}
|
||||
}
|
||||
}).Result;
|
||||
//TODO implement when TDLib 1.6 is released
|
||||
}
|
||||
|
||||
public static List<Chat> GetSecretChats()
|
||||
|
@ -348,7 +371,7 @@ namespace telegram
|
|||
{
|
||||
try
|
||||
{
|
||||
var results = client.ExecuteAsync(new SearchChatsOnServer
|
||||
var results = client.ExecuteAsync(new SearchChats
|
||||
{
|
||||
Query = query,
|
||||
Limit = 5
|
||||
|
@ -368,8 +391,25 @@ namespace telegram
|
|||
}
|
||||
}
|
||||
|
||||
public static int SearchUserInChats(string query)
|
||||
{
|
||||
var results = client.ExecuteAsync(new SearchChatsOnServer
|
||||
{
|
||||
Query = query,
|
||||
Limit = 5
|
||||
}).Result;
|
||||
if (results.ChatIds.Length == 0)
|
||||
return 0;
|
||||
return results.ChatIds
|
||||
.Select(GetChat)
|
||||
.Where(p => p.Type is ChatType.ChatTypePrivate)
|
||||
.Select(p => ((ChatType.ChatTypePrivate) p.Type).UserId)
|
||||
.First();
|
||||
}
|
||||
|
||||
public static int SearchContacts(string query)
|
||||
{
|
||||
//TODO implement when TDLib 1.6 is released
|
||||
try
|
||||
{
|
||||
var results = client.ExecuteAsync(new SearchContacts
|
||||
|
|
|
@ -15,7 +15,6 @@ namespace telegram
|
|||
/*
|
||||
* TODO:
|
||||
* waaay more error messages instead of just doing nothing or crashing (search for TODOs and "do something")
|
||||
* command /sg -> search globally, some way to add contacts?
|
||||
* mute,unmute chats
|
||||
* photo & document download & show externally
|
||||
* publish AUR package
|
||||
|
@ -41,7 +40,7 @@ namespace telegram
|
|||
public static volatile List<string> messageQueue = new List<string>();
|
||||
public static volatile List<string> missedMessages = new List<string>();
|
||||
public static volatile string prefix = "[tgcli";
|
||||
public static volatile bool silent = false;
|
||||
public static volatile bool silent;
|
||||
|
||||
public static volatile object @lock = new object();
|
||||
|
||||
|
|
Loading…
Reference in a new issue