Support IDictionary in CollectUsersChats (#137)

This commit is contained in:
Wizou 2023-04-09 14:14:56 +02:00
parent 0c6a8dd0a9
commit d53dc5f07c
4 changed files with 12 additions and 12 deletions

View file

@ -47,7 +47,7 @@ namespace WTelegram
return length;
}
public static V GetValueOrDefault<K, V>(this Dictionary<K, V> dictionary, K key, V defaultValue = default)
public static V GetValueOrDefault<K, V>(this IReadOnlyDictionary<K, V> dictionary, K key, V defaultValue = default)
=> dictionary.TryGetValue(key, out V value) ? value : defaultValue;
public static void Deconstruct<K, V>(this KeyValuePair<K, V> kvp, out K key, out V value) { key = kvp.Key; value = kvp.Value; }

View file

@ -15,7 +15,7 @@ namespace TL
public override long ID => 0;
internal IDictionary<long, User> _users;
internal IDictionary<long, ChatBase> _chats;
protected internal override IPeerInfo UserOrChat(IDictionary<long, User> users, IDictionary<long, ChatBase> chats)
protected internal override IPeerInfo UserOrChat(Dictionary<long, User> users, Dictionary<long, ChatBase> chats)
{
lock (_users)
foreach (var user in users.Values)
@ -51,7 +51,7 @@ namespace TL
/// <param name="premium">Generate premium entities if any</param>
/// <param name="users">Dictionary used for <c>tg://user?id=</c> notation</param>
/// <returns>The array of formatting entities that you can pass (along with the plain text) to <see cref="Client.SendMessageAsync">SendMessageAsync</see> or <see cref="Client.SendMediaAsync">SendMediaAsync</see></returns>
public static MessageEntity[] MarkdownToEntities(this Client _, ref string text, bool premium = false, Dictionary<long, User> users = null)
public static MessageEntity[] MarkdownToEntities(this Client _, ref string text, bool premium = false, IReadOnlyDictionary<long, User> users = null)
{
var entities = new List<MessageEntity>();
var sb = new StringBuilder(text);
@ -251,7 +251,7 @@ namespace TL
/// <param name="premium">Generate premium entities if any</param>
/// <param name="users">Dictionary used for <c>tg://user?id=</c> notation</param>
/// <returns>The array of formatting entities that you can pass (along with the plain text) to <see cref="Client.SendMessageAsync">SendMessageAsync</see> or <see cref="Client.SendMediaAsync">SendMediaAsync</see></returns>
public static MessageEntity[] HtmlToEntities(this Client _, ref string text, bool premium = false, Dictionary<long, User> users = null)
public static MessageEntity[] HtmlToEntities(this Client _, ref string text, bool premium = false, IReadOnlyDictionary<long, User> users = null)
{
var entities = new List<MessageEntity>();
var sb = new StringBuilder(text);

View file

@ -118,25 +118,25 @@ namespace TL
partial class Peer
{
public abstract long ID { get; }
protected internal abstract IPeerInfo UserOrChat(IDictionary<long, User> users, IDictionary<long, ChatBase> chats);
protected internal abstract IPeerInfo UserOrChat(Dictionary<long, User> users, Dictionary<long, ChatBase> chats);
}
partial class PeerUser
{
public override string ToString() => "user " + user_id;
public override long ID => user_id;
protected internal override IPeerInfo UserOrChat(IDictionary<long, User> users, IDictionary<long, ChatBase> chats) => users.TryGetValue(user_id, out var user) ? user : null;
protected internal override IPeerInfo UserOrChat(Dictionary<long, User> users, Dictionary<long, ChatBase> chats) => users.TryGetValue(user_id, out var user) ? user : null;
}
partial class PeerChat
{
public override string ToString() => "chat " + chat_id;
public override long ID => chat_id;
protected internal override IPeerInfo UserOrChat(IDictionary<long, User> users, IDictionary<long, ChatBase> chats) => chats.TryGetValue(chat_id, out var chat) ? chat : null;
protected internal override IPeerInfo UserOrChat(Dictionary<long, User> users, Dictionary<long, ChatBase> chats) => chats.TryGetValue(chat_id, out var chat) ? chat : null;
}
partial class PeerChannel
{
public override string ToString() => "channel " + channel_id;
public override long ID => channel_id;
protected internal override IPeerInfo UserOrChat(IDictionary<long, User> users, IDictionary<long, ChatBase> chats) => chats.TryGetValue(channel_id, out var chat) ? chat : null;
protected internal override IPeerInfo UserOrChat(Dictionary<long, User> users, Dictionary<long, ChatBase> chats) => chats.TryGetValue(channel_id, out var chat) ? chat : null;
}
partial class UserBase : IPeerInfo

View file

@ -168,9 +168,9 @@ namespace TL
else if (type == typeof(Int256))
return new Int256(reader);
else if (type == typeof(Dictionary<long, User>))
return reader.ReadTLDictionary<User>(u => u.ID);
return reader.ReadTLDictionary<User>();
else if (type == typeof(Dictionary<long, ChatBase>))
return reader.ReadTLDictionary<ChatBase>(c => c.ID);
return reader.ReadTLDictionary<ChatBase>();
else
return reader.ReadTLObject();
default:
@ -238,7 +238,7 @@ namespace TL
throw new WTelegram.WTException($"Cannot deserialize {type.Name} with ctor #{ctorNb:x}");
}
internal static Dictionary<long, T> ReadTLDictionary<T>(this BinaryReader reader, Func<T, long> getID) where T : class
internal static Dictionary<long, T> ReadTLDictionary<T>(this BinaryReader reader) where T : class, IPeerInfo
{
uint ctorNb = reader.ReadUInt32();
var elementType = typeof(T);
@ -249,7 +249,7 @@ namespace TL
for (int i = 0; i < count; i++)
{
var value = (T)reader.ReadTLValue(elementType);
dict[getID(value)] = value is UserEmpty ? null : value;
dict[value.ID] = value is UserEmpty ? null : value;
}
return dict;
}