More helpers to generically manage Chats & Channels

This commit is contained in:
Wizou 2021-12-31 08:38:41 +01:00
parent e3b7d17d2b
commit 024c5ba705
4 changed files with 76 additions and 14 deletions

View file

@ -228,10 +228,8 @@ var chat = chats.chats[ChatId];
```
After the above code, once you [have obtained](https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#access-hash) an `InputUser` or `User`, you can:
```csharp
// • Directly add the user to a simple Chat:
await client.Messages_AddChatUser(ChatId, user, int.MaxValue);
// • Directly add the user to a Channel/group:
await client.Channels_InviteToChannel((Channel)chat, new[] { user });
// • Directly add the user to a Chat/Channel/group:
await client.AddChatUser(chat, user);
// You may get exception USER_PRIVACY_RESTRICTED if the user has denied the right to be added to a chat
// or exception USER_NOT_MUTUAL_CONTACT if the user left the chat previously and you want to add him again
@ -247,10 +245,8 @@ await client.SendMessageAsync(user, "Join our group with this link: " + invite.l
await client.Messages_EditExportedChatInvite(chat, invite.link, revoked: true);
await client.Messages_DeleteExportedChatInvite(chat, invite.link);
// • Remove the user from a simple Chat:
await client.Messages_DeleteChatUser(ChatId, user);
// • Remove the user from a Channel/group:
await client.Channels_EditBanned((Channel)chat, user, new ChatBannedRights { flags = ChatBannedRights.Flags.view_messages });
// • Remove the user from a Chat/Channel/Group:
await client.DeleteChatUser(ChatId, user);
```
<a name="history"></a>

View file

@ -1523,12 +1523,78 @@ namespace WTelegram
}
}
public Task<UpdatesBase> AddChatUser(InputPeer peer, InputUserBase user, int fwd_limit = int.MaxValue) => peer switch
{
InputPeerChat chat => this.Messages_AddChatUser(chat.chat_id, user, fwd_limit),
InputPeerChannel channel => this.Channels_InviteToChannel(channel, new[] { user }),
_ => throw new ArgumentException("This method works on Chat & Channel only"),
};
public Task<UpdatesBase> DeleteChatUser(InputPeer peer, InputUser user, bool revoke_history = true) => peer switch
{
InputPeerChat chat => this.Messages_DeleteChatUser(chat.chat_id, user, revoke_history),
InputPeerChannel channel => this.Channels_EditBanned(channel, user, new ChatBannedRights { flags = ChatBannedRights.Flags.view_messages }),
_ => throw new ArgumentException("This method works on Chat & Channel only"),
};
public Task<UpdatesBase> LeaveChat(InputPeer peer, bool revoke_history = true) => peer switch
{
InputPeerChat chat => this.Messages_DeleteChatUser(chat.chat_id, InputUser.Self, revoke_history),
InputPeerChannel channel => this.Channels_LeaveChannel(channel),
_ => throw new ArgumentException("This method works on Chat & Channel only"),
};
public async Task<UpdatesBase> EditChatAdmin(InputPeer peer, InputUserBase user, bool is_admin)
{
switch (peer)
{
case InputPeerChat chat:
await this.Messages_EditChatAdmin(chat.chat_id, user, is_admin);
return new Updates { date = DateTime.UtcNow, users = new(), updates = Array.Empty<Update>(),
chats = (await this.Messages_GetChats(new[] { chat.chat_id })).chats };
case InputPeerChannel channel:
return await this.Channels_EditAdmin(channel, user,
new ChatAdminRights { flags = is_admin ? (ChatAdminRights.Flags)0x8BF : 0 }, null);
default:
throw new ArgumentException("This method works on Chat & Channel only");
}
}
public Task<UpdatesBase> EditChatPhoto(InputPeer peer, InputChatPhotoBase photo) => peer switch
{
InputPeerChat chat => this.Messages_EditChatPhoto(chat.chat_id, photo),
InputPeerChannel channel => this.Channels_EditPhoto(channel, photo),
_ => throw new ArgumentException("This method works on Chat & Channel only"),
};
public Task<UpdatesBase> EditChatTitle(InputPeer peer, string title) => peer switch
{
InputPeerChat chat => this.Messages_EditChatTitle(chat.chat_id, title),
InputPeerChannel channel => this.Channels_EditTitle(channel, title),
_ => throw new ArgumentException("This method works on Chat & Channel only"),
};
public Task<Messages_ChatFull> GetFullChat(InputPeer peer) => peer switch
{
InputPeerChat chat => this.Messages_GetFullChat(chat.chat_id),
InputPeerChannel channel => this.Channels_GetFullChannel(channel),
_ => throw new ArgumentException("This method works on Chat & Channel only"),
};
public async Task<UpdatesBase> DeleteChat(InputPeer peer)
{
switch (peer)
{
case InputPeerChat chat:
await this.Messages_DeleteChat(chat.chat_id);
return new Updates { date = DateTime.UtcNow, users = new(), updates = Array.Empty<Update>(),
chats = (await this.Messages_GetChats(new[] { chat.chat_id })).chats };
case InputPeerChannel channel:
return await this.Channels_DeleteChannel(channel);
default:
throw new ArgumentException("This method works on Chat & Channel only");
}
}
#endregion
}
}

View file

@ -224,7 +224,7 @@ namespace WTelegram
}
[TLDef(0x7A19CB76)] //RSA_public_key#7a19cb76 n:bytes e:bytes = RSAPublicKey
public partial class RSAPublicKey : IObject { public byte[] n, e; }
public class RSAPublicKey : IObject { public byte[] n, e; }
public static void LoadPublicKey(string pem)
{

View file

@ -370,7 +370,7 @@ namespace TL
// Below TL types are commented "parsed manually" from https://github.com/telegramdesktop/tdesktop/blob/dev/Telegram/Resources/tl/mtproto.tl
[TLDef(0xF35C6D01)] //rpc_result#f35c6d01 req_msg_id:long result:Object = RpcResult
public partial class RpcResult : IObject
public class RpcResult : IObject
{
public long req_msg_id;
public object result;
@ -378,7 +378,7 @@ namespace TL
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006")]
[TLDef(0x5BB8E511)] //message#5bb8e511 msg_id:long seqno:int bytes:int body:Object = Message
public partial class _Message
public class _Message
{
public long msg_id;
public int seqno;
@ -387,10 +387,10 @@ namespace TL
}
[TLDef(0x73F1F8DC)] //msg_container#73f1f8dc messages:vector<%Message> = MessageContainer
public partial class MsgContainer : IObject { public _Message[] messages; }
public class MsgContainer : IObject { public _Message[] messages; }
[TLDef(0xE06046B2)] //msg_copy#e06046b2 orig_message:Message = MessageCopy
public partial class MsgCopy : IObject { public _Message orig_message; }
public class MsgCopy : IObject { public _Message orig_message; }
[TLDef(0x3072CFA1)] //gzip_packed#3072cfa1 packed_data:bytes = Object
public partial class GzipPacked : IObject { public byte[] packed_data; }
public class GzipPacked : IObject { public byte[] packed_data; }
}