コード例 #1
0
        public async Task <IActionResult> SaveBirthdayWall(BirthdayWallSchedulerViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("BirthdayWall", model));
            }

            var groupInfo = _userHelperService.GetSelectedGroup(User);
            var idUser    = await _userHelperService.GetUserIdVk(User);

            Messages message = null;

            if (model.IdMessage.HasValue)
            {
                message = await _context.Messages
                          .Include(x => x.Files)
                          .FirstOrDefaultAsync(x => x.Id == model.IdMessage.Value);

                message.Text = model.Message;
                var newFilesInMessage = model.Files.Select(x => x.Id)
                                        .Except(message.Files.Select(x => x.IdFile))
                                        .Select(x => new FilesInMessage()
                {
                    IdFile    = x,
                    IdMessage = message.Id
                });

                await _context.FilesInMessage.AddRangeAsync(newFilesInMessage);
            }
            else
            {
                message = await DbHelper.AddMessage(_context, groupInfo.Key, model.Message, null, false, model.Files.Select(x => x.Id));
            }

            BirthdayWallScenarios scenario = _context.BirthdayWallScenarios.Include(x => x.Message).FirstOrDefault(x => x.IdGroup == groupInfo.Key);

            if (scenario == null)
            {
                scenario = new BirthdayWallScenarios()
                {
                    IdGroup   = groupInfo.Key,
                    IsEnabled = true
                };
                await _context.BirthdayWallScenarios.AddAsync(scenario);
            }
            if (!model.IdMessage.HasValue)
            {
                scenario.IdMessage = message.Id;
            }

            scenario.SendAt   = model.SendAt;
            scenario.IdVkUser = idUser;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        public async Task <IActionResult> Save(EditViewModel editViewModel)
        {
            var groupInfo = _userHelperService.GetSelectedGroup(User);

            Notifications notification = null;

            if (editViewModel.Id.HasValue)
            {
                notification = await _context.Notifications.FindAsync(editViewModel.Id);
            }
            if (notification == null)
            {
                notification = new Notifications()
                {
                    DtCreate = DateTime.UtcNow,
                }
            }
            ;
            notification.ElementType      = editViewModel.ElementType.Value;
            notification.IdGroup          = groupInfo.Key;
            notification.IdElement        = editViewModel.IdElement;
            notification.IsEnabled        = true;
            notification.Name             = editViewModel.Name;
            notification.NotificationType = editViewModel.NotificationType.Value;

            switch (editViewModel.NotificationType)
            {
            case 0:
            {
                notification.NotifyTo = (await _userHelperService.GetUserIdVk(User)).ToString();
                break;
            }

            case 1:
            {
                notification.NotifyTo = editViewModel.NotifyTo;
                break;
            }
            }

            _context.Update(notification);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
    }
コード例 #3
0
        public async Task <IActionResult> SendMessages(EditMessagingViewModel data)
        {
            if (!ModelState.IsValid)
            {
                return(Edit(data));
            }

            var groupInfo = _userHelperService.GetSelectedGroup(User);
            var message   = await DbHelper.AddMessage(_context, groupInfo.Key, data.Message, data.GetVkKeyboard(), data.IsImageFirst, data.Files.Select(x => x.Id));

            long[] userIds = null;

            if (data.IsSelfSend)
            {
                userIds = new long[] { await _userHelperService.GetUserIdVk(User) }
            }
            ;
            //return Json(new { idMessage = message.Id, ids = new long[] { await _userHelperService.GetUserIdVk(User) } });
            else
            {
                userIds = await _context.Subscribers
                          .Where(x => x.IdGroup == groupInfo.Key)
                          .Where(x => x.IsChatAllowed.HasValue && x.IsChatAllowed.Value)
                          .Select(x => x.IdVkUser)
                          .ToArrayAsync();
            }

            await _context.Scheduler_Messaging.AddAsync(new Scheduler_Messaging()
            {
                DtAdd           = DateTime.UtcNow,
                IdMessage       = message.Id,
                Name            = data.Name,
                RecipientsCount = userIds.LongLength,
                Status          = Models.Database.Common.MessagingStatus.Added,
                DtStart         = DateTime.SpecifyKind(DateTime.ParseExact(data.DtStart, "dd.MM.yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture), DateTimeKind.Local).ToUniversalTime(),
                VkUserIds       = string.Join(',', userIds)
            });

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #4
0
        public async Task <IActionResult> RefreshPosts()
        {
            var selectedGroup = _userHelperService.GetSelectedGroup(User);

            var existsPostsIds = await _context.WallPosts
                                 .Where(x => x.IdGroup == selectedGroup.Key)
                                 .OrderByDescending(x => x.DtAdd)
                                 .Select(x => x.IdVk)
                                 .ToArrayAsync();

            var idVkUser = await _userHelperService.GetUserIdVk(User);

            var vkApi = await _vkPoolService.GetUserVkApi(idVkUser);

            var posts = await vkApi.Wall.GetAsync(new VkNet.Model.RequestParams.WallGetParams()
            {
                OwnerId = -selectedGroup.Key,
                Filter  = VkNet.Enums.SafetyEnums.WallFilter.All
            });

            var newWallPosts = posts.WallPosts
                               .Where(x => !existsPostsIds.Contains(x.Id.Value))
                               .Select(x => new WallPosts()
            {
                DtAdd   = x.Date ?? DateTime.UtcNow,
                IdGroup = selectedGroup.Key,
                IdVk    = x.Id.Value,
                Text    = x.Text
            });

            await _context.WallPosts.AddRangeAsync(newWallPosts);

            await _context.SaveChangesAsync();

            var allPosts = await _context.WallPosts
                           .Where(x => x.IdGroup == selectedGroup.Key)
                           .OrderByDescending(x => x.DtAdd)
                           .ToDictionaryAsync(x => x.Id, x => $"{x.DtAdd.ToLocalTime():dd.MM.yyyy HH.mm}: {x.TextPart}");

            return(Json(new { error = 0, posts = allPosts }));
        }
コード例 #5
0
ファイル: GroupsController.cs プロジェクト: green16/sellerbox
        public async Task <IActionResult> Index()
        {
            DateTime?dtExpiresAt = await _userHelperService.GetVkUserAccessTokenExpiresAt(User);

            string idUser   = _userHelperService.GetUserId(User);
            var    idVkUser = await _userHelperService.GetUserIdVk(User);

            var vkApi = await _vkPoolService.GetUserVkApi(idVkUser);

            if (vkApi == null)
            {
                return(RedirectToAction(nameof(AccountController.ExternalLogin), "Account", new { provider = "Vkontakte", returnUrl = "/Groups" }));
            }
            var groups = await vkApi.Groups.GetAsync(new VkNet.Model.RequestParams.GroupsGetParams()
            {
                UserId   = idVkUser,
                Extended = true,
                Filter   = VkNet.Enums.Filters.GroupsFilters.Administrator
            });

            var models = groups.Select(x => new GroupsViewModel()
            {
                IdVk        = x.Id,
                ImageUrl    = x.Photo50,
                Name        = x.Name,
                Link        = $"https://vk.com/club{x.Id}",
                IsConnected = _context.GroupAdmins
                              .Include(y => y.Group)
                              .Any(y => y.IdGroup == x.Id && y.IdUser == idUser && !string.IsNullOrEmpty(y.Group.AccessToken))
            });

            foreach (var connectedGroupModel in models.Where(x => x.IsConnected))
            {
                var connectedGroup = await _context.Groups.FirstAsync(x => x.IdVk == connectedGroupModel.IdVk);

                connectedGroup.Update(groups.First(x => x.Id == connectedGroup.IdVk));
            }
            await _context.SaveChangesAsync();

            return(View(models));
        }