secret chat refactor

This commit is contained in:
Laura Hausmann 2019-12-13 20:18:49 +01:00
parent b6914d939d
commit e6c53e6a72
Signed by: zotan
GPG key ID: 5EC1D38FFC321311
2 changed files with 117 additions and 22 deletions

View file

@ -41,6 +41,7 @@ namespace telegram
new CloseUnreadCommand(),
new ListSecretChatsCommand(),
new OpenSecretCommand(),
new OpenSecretDirectCommand(),
new NewSecretChatCommand(),
new CloseSecretChatCommand(),
new QuitCommand(),
@ -106,7 +107,7 @@ namespace telegram
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;
@ -167,7 +168,7 @@ namespace telegram
}
}
}
public class NewSecretChatCommand : Command
{
public NewSecretChatCommand() : base("ns", "", "creates a new secret chat. queries contacts", "<query>", -1)
@ -178,18 +179,20 @@ namespace telegram
{
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
var userId = SearchContacts(query);
if (userId == 0)
return;
if (GetSecretChats().Count(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId) > 0)
return; //TODO do something
var chat = CreateSecretChat(userId);
CommandManager.HandleCommand("os " + chat.Id);
CommandManager.HandleCommand("osd " + chat.Id);
}
}
public class OpenSecretCommand : Command
public class OpenSecretDirectCommand : Command
{
public OpenSecretCommand() : base("os", "", "opens a secret chat", "<chat_id>", 1)
public OpenSecretDirectCommand() : base("osd", "", "opens a secret chat by chat id", "<chat_id>", 1)
{
}
@ -200,7 +203,7 @@ namespace telegram
{
return; //todo do something
}
var chat = GetChat(chatId);
if (chat == null)
return; //TODO do something
@ -217,10 +220,10 @@ namespace telegram
{
return; //TODO do something
}
chat.Title = TruncateString(chat.Title, 20);
prefix = $"[{Ansi.Red}sec {Ansi.ResetAll}{chat.Title}]";
prefix = $"[{Ansi.Red}sec {Ansi.ResetAll}{chat.Title}]";
lock (@lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening secret chat: {chat.Title}");
@ -234,7 +237,7 @@ namespace telegram
{
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat is pending. No messages can be sent.");
}
if (chat.UnreadCount >= 5)
{
var capped = chat.UnreadCount > 50;
@ -272,6 +275,91 @@ namespace telegram
}
}
public class OpenSecretCommand : Command
{
public OpenSecretCommand() : base("os", "", "opens a secret chat. queries contacts.", "<query>", -1)
{
}
public override void Handler(List<string> inputParams)
{
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
var userId = SearchContacts(query);
if (userId == 0 || query.Length == 0)
return; //TODO do something;
var chat = GetSecretChats().Find(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId);
if (chat == null)
{
return; //TODO do something
}
TdApi.SecretChat secChat;
if (chat.Type is TdApi.ChatType.ChatTypeSecret secretChat)
{
currentChatUserId = secretChat.UserId;
currentChatId = chat.Id;
currentUserRead = false;
secChat = GetSecretChat(secretChat.SecretChatId);
}
else
{
return; //TODO do something
}
chat.Title = TruncateString(chat.Title, 20);
prefix = $"[{Ansi.Red}sec {Ansi.ResetAll}{chat.Title}]";
lock (@lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening secret chat: {chat.Title}");
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" +
$"{(chat.UnreadCount == 1 ? "." : "s.")}");
if (secChat.State is TdApi.SecretChatState.SecretChatStateClosed)
{
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat has ended. No messages can be sent.");
}
else if (secChat.State is TdApi.SecretChatState.SecretChatStatePending)
{
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat is pending. No messages can be sent.");
}
if (chat.UnreadCount >= 5)
{
var capped = chat.UnreadCount > 50;
GetHistory(chat.Id, capped ? 50 : chat.UnreadCount, isSecret: true).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, isSecret: true);
var rest = GetHistory(chat.Id, 5 - unreads.Count, unreads.First().Id, isSecret: true);
rest.ForEach(AddMessageToQueue);
messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---");
unreads.ForEach(AddMessageToQueue);
}
else
{
GetHistory(chat.Id, isSecret: true).ForEach(AddMessageToQueue);
}
}
var history = GetHistory(currentChatId, 50, isSecret: true);
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 CloseUnreadCommand : Command
{
public CloseUnreadCommand() : base("cu", "", "closes a chat, marking it as unread", "", 0)
@ -384,7 +472,7 @@ namespace telegram
}
}
}
public class ListSecretChatsCommand : Command
{
public ListSecretChatsCommand() : base("ls", "", "displays all open secret chats", "", 0)
@ -430,7 +518,7 @@ namespace telegram
}
}
}
public class QuitCommand : Command
{
public QuitCommand() : base("q", "^D", "quits the program", "", 0)
@ -445,7 +533,8 @@ namespace telegram
public class EditCommand : Command
{
public EditCommand() : base("e", "", "edits last message. param empty adds last message to inputline", "[message]", -1)
public EditCommand() : base("e", "", "edits last message. param empty adds last message to inputline",
"[message]", -1)
{
}
@ -482,7 +571,7 @@ namespace telegram
}
}
}
public class ReplyCommand : Command
{
public ReplyCommand() : base("r", "", "replies to message", "<offset> <message>", -1)
@ -510,7 +599,7 @@ namespace telegram
}
var replyMessage = history[offset - 1];
if (currentChatId == 0) return; //TODO do something
SendMessage(message, currentChatId, replyMessage.Id);

View file

@ -14,7 +14,6 @@ namespace telegram
{
/*
* TODO:
* improve secret chat open: open by username, allow just one sec chat per userId
* make commands & keybinds more consistent (maybe configurable?)
* replace emoji on send & un-replace on edit, two-way dictionary!!
* replace more emojis on send (is there a lib for that)
@ -189,6 +188,7 @@ namespace telegram
ScreenUpdate();
break;
}
break;
}
}
@ -388,16 +388,18 @@ namespace telegram
var username = TruncateString(GetFormattedUsername(sender), 10);
var time = FormatTime(msg.Date);
var isChannel = msg.IsChannelPost;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate || chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate ||
chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isSecret = chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isReply = msg.ReplyToMessageId != 0;
chat.Title = TruncateString(chat.Title, 20);
Td.TdApi.Message replyMessage;
var msgPrefix = $"{Ansi.Bold}{Ansi.Green}[{time}] {(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var msgPrefix =
$"{Ansi.Bold}{Ansi.Green}[{time}] {(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var finalOutput = msgPrefix;
var indent = new string(' ', GetActualStringWidth(msgPrefix));
@ -441,9 +443,13 @@ namespace telegram
var username = GetFormattedUsername(sender);
var time = FormatTime(msg.Date);
var isChannel = msg.IsChannelPost;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate ||
chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isSecret = chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var finalOutput = "";
var replyPrefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} " +
var replyPrefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] " +
$"{(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var indent = new string(' ', GetActualStringWidth(replyPrefix));