Ejemplo n.º 1
0
        public async Task <TLDialogs> AutoKickAsync()
        {
            var dialogs = await _messagesTL.GetDialogsAsync();

            if (dialogs?.dialogs == null || dialogs.dialogs.lists.Count == 0)
            {
                return(dialogs);
            }
            var autoKickChats = await _chatRepository.GetAutoKickAsync();

            foreach (var dialog in dialogs.dialogs.lists.Where(d => d.unread_count > 0 && d.peer is TLPeerChat).ToList())
            {
                var chatId = ((TLPeerChat)dialog.peer).chat_id;
                if (autoKickChats.All(c => c.Id != chatId))
                {
                    continue;
                }
                var users =
                    await _messagesTL.GetAddUserMessagesAsync(chatId, dialog.unread_count);

                foreach (var tlUser in users)
                {
                    var user = await _userRepository.GetUserByTLIdAsync(tlUser.id) ?? new User
                    {
                        Id = tlUser.id
                    };
                    UserService.CopyUserProps(user, tlUser, false);
                    if (user.UserId == 0)
                    {
                        await _userRepository.AddAsync(user);
                    }
                    else
                    {
                        await _userRepository.SaveChangesAsync();
                    }

                    try
                    {
                        await _chatTL.RemoveUserAsync(new Chat { Id = chatId }, user);
                    }
                    catch (InvalidOperationException ex)
                    {
                        if (!ex.Message.Equals("USER_NOT_PARTICIPANT", StringComparison.InvariantCultureIgnoreCase))
                        {
                            throw;
                        }
                    }
                }

                await _messagesTL.MarkUserMessagesAsReadAsync(new TLInputPeerChat { chat_id = chatId });
            }

            return(dialogs);
        }
Ejemplo n.º 2
0
        public async Task <ChatActionResult> RemoveUserAsync(string title, string username, User actionUser)
        {
            var user = await _userService.FindUserByUsernameAsync(username);

            var chats = actionUser.IsGlobalAdmin
                        ? await _chatRepository.FindByTitleAsync(title)
                        : (await _chatRepository.GetUserChats(actionUser.UserId)).Where(c => c.Title.Contains(title)).ToList();

            if (chats.Count == 0)
            {
                chats = await _chatTL.FindByTitleAsync(title);

                foreach (var chat in chats)
                {
                    await _chatRepository.AddAsync(chat);
                }
            }

            if (chats.Count == 0)
            {
                throw new ArgumentException($"Chat {title} doesnot exists");
            }
            if (chats.Count > 1)
            {
                return new ChatActionResult
                       {
                           Success = false,
                           Chats   = chats,
                           User    = user
                       }
            }
            ;

            await _chatTL.RemoveUserAsync(chats[0], user);

            return(new ChatActionResult {
                Success = true
            });
        }