public async Task ExecuteRandomLike(int userId) { await using var scope = _transactionScopeFactory.CreateQuery(); var favouriteGroup = await scope .RawSql <FavouriteGroup>( $"select * from public.\"FavouriteGroup\" where \"{nameof(FavouriteGroup.UserId)}\" = {userId} order by random() limit 1") .Select(f => f.GroupId) .FirstOrDefaultAsync(); if (favouriteGroup != default) { using var user = _securityUserManager.SetUser(new SecurityUser { Id = userId }); var rnd = new Random(); var post = (await _vkClient.WallSearch(favouriteGroup, rnd.Next(1, 30), 1))?.Response?.Items ?.FirstOrDefault(); if (post != default && !(post.Likes?.UserLikes ?? true)) { await Task.Delay(300); await _vkLikeService.Like(new VkRepostViewModel(post.OwnerId, post.Id)); } _backgroundJobService.Schedule <IRandomLikeService>(x => x.ExecuteRandomLike(userId), TimeSpan.FromMilliseconds(rnd.Next(60 * 1000 * 3, 60 * 1000 * 60 * 5))); } }
public async Task Repost(int ownerId, int messageId, int userId) { using (_userManager.SetUser(new SecurityUser { Id = userId })) { await _vkClient.Repost(ownerId, messageId); } }
public async Task Invoke(HttpContext context, ISecurityUserManager userManager) { if (!context.User.Identity.IsAuthenticated) { await _next(context); } using (userManager.SetUser(new SecurityUser { Id = context.User.Identity.GetUserId() })) { await _next(context); } }