small fixes, newlines, mark as unread

This commit is contained in:
Laura Hausmann 2019-12-10 09:49:58 +01:00
parent 344e677eed
commit 7522be5a3a
Signed by: zotan
GPG key ID: 5EC1D38FFC321311
4 changed files with 183 additions and 165 deletions

View file

@ -1,7 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="36e5cfaf-0aa9-4137-8110-a5678a9c5443" name="Default Changelist" comment="" />
<list default="true" id="36e5cfaf-0aa9-4137-8110-a5678a9c5443" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.idea.tgcli/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.tgcli/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/telegram/Command.cs" beforeDir="false" afterPath="$PROJECT_DIR$/telegram/Command.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/telegram/Util.cs" beforeDir="false" afterPath="$PROJECT_DIR$/telegram/Util.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/telegram/tgcli.cs" beforeDir="false" afterPath="$PROJECT_DIR$/telegram/tgcli.cs" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -29,6 +34,7 @@
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/39AD1EAB-EC41-4054-96D6-7E75046A6833/98/e27fdd21/EventHandler`1.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/tdsharp/TDLib.Api/Functions/EditMessageText.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/tdsharp/TDLib.Api/Functions/GetAuthorizationState.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/7B2C2408-23DE-40E4-9D78-A7E306473419/fe/a4516fce/ConsoleModifiers.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/tdsharp/TDLib.Api/Objects/TdlibParameters.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/tdsharp/TDLib.Api/Objects/Message.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/telegram/Util.cs" root0="FORCE_HIGHLIGHTING" />
@ -51,8 +57,8 @@
<option value="$PROJECT_DIR$/tgcli.core/tgcli.cs" />
<option value="$PROJECT_DIR$/tgcli.core/Util.cs" />
<option value="$PROJECT_DIR$/tgcli.core/Command.cs" />
<option value="$PROJECT_DIR$/telegram/Util.cs" />
<option value="$PROJECT_DIR$/telegram/Command.cs" />
<option value="$PROJECT_DIR$/telegram/Util.cs" />
<option value="$PROJECT_DIR$/telegram/tgcli.cs" />
</list>
</option>
@ -115,7 +121,7 @@
<workItem from="1575536428992" duration="12000" />
<workItem from="1575536474314" duration="39215000" />
<workItem from="1575724030612" duration="6335000" />
<workItem from="1575806456895" duration="24330000" />
<workItem from="1575806456895" duration="26434000" />
</task>
<servers />
</component>

View file

@ -11,182 +11,179 @@ namespace telegram
{
public static void ParseCommand(string command)
{
var split = command.Split(" ");
if (split[0].Equals("open") || split[0].Equals("o"))
while (true)
{
if (split.Length < 2)
return;
var query = command.Substring(split[0].Length);
var chatId = searchChatId(query);
if (chatId == 0)
return;
currentChatId = 0;
currentChatUserId = 0;
currentUserOnline = false;
currentUserTyping = false;
currentUserRead = false;
ctsTyping?.Cancel();
var chat = getChat(chatId);
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat)
var split = command.Split(" ");
if (split[0].Equals("open") || split[0].Equals("o"))
{
currentChatUserId = privChat.UserId;
}
if (split.Length < 2) return;
currentChatId = chat.Id;
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.")}");
while (getHistory(chatId, 50).Count == 1)
var query = command.Substring(split[0].Length);
var chatId = searchChatId(query);
if (chatId == 0) return;
currentChatId = 0;
currentChatUserId = 0;
currentUserOnline = false;
currentUserTyping = false;
currentUserRead = false;
ctsTyping?.Cancel();
var chat = getChat(chatId);
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat)
{
getHistory(chatId, 10);
currentChatUserId = privChat.UserId;
}
if (chat.UnreadCount >= 5)
{
var capped = chat.UnreadCount > 50;
getHistory(chatId, 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(chatId, chat.UnreadCount);
var rest = getHistory(chatId, 5 - unreads.Count, unreads.First().Id);
rest.ForEach(AddMessageToQueue);
messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---");
unreads.ForEach(AddMessageToQueue);
}
else
{
getHistory(chatId).ForEach(AddMessageToQueue);
}
}
markRead(chat.Id, getHistory(chat.Id).First().Id);
var history = getHistory(currentChatId, 50);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
currentUserRead = true;
return;
}
lastMessage = last;
currentUserRead = isMessageRead(last.ChatId, last.Id);
}
else if (split[0].Equals("close") || split[0].Equals("c"))
{
if (split.Length != 1 || currentChatId == 0)
return;
currentChatId = 0;
currentChatUserId = 0;
currentUserOnline = false;
currentUserTyping = false;
currentUserRead = false;
ctsTyping?.Cancel();
lastMessage = null;
prefix = "[tgcli";
lock (_lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Closing chat.");
var count = missedMessages.Count;
if (count == 0)
return;
messageQueue.Add($"{Ansi.Yellow}" +
$"[tgcli] You have {count} missed message" +
$"{(count == 1 ? "." : "s.")}");
messageQueue.AddRange(missedMessages);
missedMessages.Clear();
}
}
else if (split[0].Equals("history") || split[0].Equals("h"))
{
if (split.Length != 1 && split.Length != 2 || currentChatId == 0)
{
currentChatId = chat.Id;
prefix = $"[{chat.Title}";
lock (_lock)
messageQueue.Add($"{Ansi.Red}[tgcli] " +
"No chat selected. Select a chat with /open <query>");
return;
}
var history = split.Length > 1 && int.TryParse(split[1], out var limit)
? getHistory(currentChatId, limit)
: getHistory(currentChatId);
lock (_lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Last {history.Count} messages in " +
$"{getChat(currentChatId).Title}");
}
foreach (var msg in history)
{
AddMessageToQueue(msg);
}
}
else if (split[0].Equals("clear") || split[0].Equals("cl"))
{
lock (_lock)
{
Console.Clear();
}
}
else if (split[0].Equals("quit") || split[0].Equals("q"))
{
quitting = true;
}
else if (split[0].Equals("unreads") || split[0].Equals("u"))
{
var unreads = getUnreadChats(split.Length == 2 && split[1].Equals("all"));
lock (_lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] You have {unreads.Count} unread chats.");
unreads.ForEach(chat =>
{
string line;
if (chat.UnreadCount == 0)
line = $"{Ansi.Bold}{Ansi.Yellow}[M] {chat.Title}";
messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening chat: {chat.Title}");
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" + $"{(chat.UnreadCount == 1 ? "." : "s.")}");
while (getHistory(chatId, 50).Count == 1)
{
getHistory(chatId, 10);
}
if (chat.UnreadCount >= 5)
{
var capped = chat.UnreadCount > 50;
getHistory(chatId, 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(chatId, chat.UnreadCount);
var rest = getHistory(chatId, 5 - unreads.Count, unreads.First().Id);
rest.ForEach(AddMessageToQueue);
messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---");
unreads.ForEach(AddMessageToQueue);
}
else
line = $"{Ansi.Bold}{Ansi.Green}[{chat.UnreadCount}] {chat.Title}";
messageQueue.Add(line);
});
}
}
else if (split[0].Equals("edit") || split[0].Equals("e"))
{
if (currentChatId == 0)
return;
{
getHistory(chatId).ForEach(AddMessageToQueue);
}
}
if (split.Length == 1)
{
currentInputLine = "/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text;
return;
}
if (lastMessage == null)
{
//try to find last message
markRead(chat.Id, getHistory(chat.Id).First().Id);
var history = getHistory(currentChatId, 50);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
currentUserRead = true;
return;
}
lastMessage = last;
currentUserRead = isMessageRead(last.ChatId, last.Id);
}
else if (split[0].Equals("cu") || split[0].Equals("closeunread"))
{
if (currentChatId == 0) return;
markUnread(currentChatId);
command = "close";
continue;
}
else if (split[0].Equals("close") || split[0].Equals("c"))
{
if (split.Length != 1 || currentChatId == 0) return;
currentChatId = 0;
currentChatUserId = 0;
currentUserOnline = false;
currentUserTyping = false;
currentUserRead = false;
ctsTyping?.Cancel();
lastMessage = null;
prefix = "[tgcli";
lock (_lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Closing chat.");
var count = missedMessages.Count;
if (count == 0) return;
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {count} missed message" + $"{(count == 1 ? "." : "s.")}");
messageQueue.AddRange(missedMessages);
missedMessages.Clear();
}
}
else if (split[0].Equals("history") || split[0].Equals("h"))
{
if (split.Length != 1 && split.Length != 2 || currentChatId == 0)
{
lock (_lock) messageQueue.Add($"{Ansi.Red}[tgcli] " + "No chat selected. Select a chat with /open <query>");
return;
}
var history = split.Length > 1 && int.TryParse(split[1], out var limit)
? getHistory(currentChatId, limit)
: getHistory(currentChatId);
lock (_lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Last {history.Count} messages in " + $"{getChat(currentChatId).Title}");
}
foreach (var msg in history)
{
AddMessageToQueue(msg);
}
}
else if (split[0].Equals("clear") || split[0].Equals("cl"))
{
lock (_lock)
{
Console.Clear();
}
}
else if (split[0].Equals("quit") || split[0].Equals("q"))
{
quitting = true;
}
else if (split[0].Equals("unreads") || split[0].Equals("u"))
{
var unreads = getUnreadChats(split.Length == 2 && split[1].Equals("all"));
lock (_lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] You have {unreads.Count} unread chats.");
unreads.ForEach(chat =>
{
string line;
if (chat.UnreadCount == 0)
line = $"{Ansi.Bold}{Ansi.Yellow}[M] {chat.Title}";
else
line = $"{Ansi.Bold}{Ansi.Green}[{chat.UnreadCount}] {chat.Title}";
messageQueue.Add(line);
});
}
}
else if (split[0].Equals("edit") || split[0].Equals("e"))
{
if (currentChatId == 0) return;
if (split.Length == 1)
{
currentInputLine = "/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text;
return;
}
if (lastMessage == null)
{
//try to find last message
var history = getHistory(currentChatId, 50);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null) return;
lastMessage = last;
}
var message = command.Substring(split[0].Length);
editMessage(message, lastMessage);
}
var message = command.Substring(split[0].Length);
editMessage(message, lastMessage);
break;
}
}
}

View file

@ -142,6 +142,7 @@ namespace telegram
{
if (string.IsNullOrWhiteSpace(message))
return;
message = message.Replace("⏎", "\n");
client.ExecuteAsync(new SendMessage
{
ChatId = chatId,
@ -187,6 +188,15 @@ namespace telegram
});
}
public static void markUnread(long chatId)
{
client.ExecuteAsync(new ToggleChatIsMarkedAsUnread
{
ChatId = chatId,
IsMarkedAsUnread = true,
});
}
public static long searchChatId(string query)
{
try

View file

@ -14,8 +14,7 @@ namespace telegram
* reply to x messages ago
* cap length & truncate extremely long chat names!
* make emojis not break terminal -> :shrug: and stuff
* mark chat as unread
* some way of typing newlines
* make typing newlines actually good (inputline as list?)
* fix history not fully displaying on channel open (query returning less results than expected?)
* waaay more error messages instead of just doing nothing
* make Util.getActualStringWidth better
@ -179,7 +178,6 @@ namespace telegram
Thread.Sleep(1);
}
Command.ParseCommand("u");
ScreenUpdate();
while (!quitting)
MainLoop();
@ -293,9 +291,16 @@ namespace telegram
break;
default:
{
if (key.Key == ConsoleKey.N && key.Modifiers.HasFlag(ConsoleModifiers.Control))
{
currentInputLine += "⏎";
ScreenUpdate();
return;
}
if (!specialKeys.Contains(key.Key))
{
currentInputLine += key.KeyChar;
//TODO currentInputLine = currentInputLine.Replace(":xd:", "😂");
ScreenUpdate();
}