Beispiel #1
0
        public async Task <bool?> Handle(InlineQuery data, object context = default, CancellationToken cancellationToken = default)
        {
            IReadOnlyCollection <InlineQueryResultBase> inlineQueryResults;

            string query             = null;
            Portal portal            = null;
            bool   exRaidGym         = false;
            string switchPmParameter = null;

            foreach (var queryPart in data.Query.Split(' ', StringSplitOptions.RemoveEmptyEntries))
            {
                switch (queryPart)
                {
                case string _ when queryPart.StartsWith(GymInlineQueryHandler.PREFIX):
                    var guid = queryPart.Substring(GymInlineQueryHandler.PREFIX.Length);

                    if (guid.EndsWith('+'))
                    {
                        guid      = guid.Substring(0, guid.Length - 1);
                        exRaidGym = true;
                    }
                    var portalGuid = PortalEx.DecodeGuid(guid);
                    portal = await myIngressClient.Get(portalGuid, data.Location, cancellationToken);

                    break;

                default:
                    query += (query == null ? default(char?) : ' ') + queryPart;
                    break;
                }
            }
            if (string.IsNullOrWhiteSpace(data.Query)) // check whole query for sharing branch
            {
                inlineQueryResults = await myShareInlineQueryHandler.GetActivePolls(data.From, cancellationToken);
            }
            else if (string.IsNullOrWhiteSpace(query))
            {
                inlineQueryResults = new[]
                {
                    new InlineQueryResultArticle($"EnterPollTopic", "Enter a topic",
                                                 new InputTextMessageContent("Enter a topic to create a poll"))
                    {
                        Description = "to create a poll",
                        ThumbUrl    = myUrlHelper.AssetsContent("static_assets/png/POI_Submission_Illustration_02.png").ToString()
                    }
                };
            }
            else
            {
                var pollId = await myRaidService.GetPollId(new Poll(data) { Title = query, Portal = portal }, cancellationToken);

                switchPmParameter = portal == null ? $"{SwitchToGymParameter}{pollId}" : null;
                ICollection <VoteEnum> voteFormats = await myDb.Set <Settings>().GetFormats(data.From.Id, cancellationToken).ToListAsync(cancellationToken);

                if (voteFormats.Count == 0)
                {
                    voteFormats = VoteEnumEx.DefaultVoteFormats;
                }
                inlineQueryResults = voteFormats
                                     .Select(format => new Poll
                {
                    Id           = exRaidGym ? -pollId : pollId,
                    Title        = query,
                    AllowedVotes = format,
                    Portal       = portal,
                    ExRaidGym    = exRaidGym
                })
                                     .Select((fakePoll, i) => new InlineQueryResultArticle(fakePoll.GetInlineId(i), fakePoll.GetTitle(myUrlHelper),
                                                                                           fakePoll.GetMessageText(myUrlHelper, disableWebPreview: fakePoll.DisableWebPreview()))
                {
                    Description = fakePoll.AllowedVotes?.Format(new StringBuilder("Create a poll ")).ToString(),
                    HideUrl     = true,
                    ThumbUrl    = fakePoll.GetThumbUrl(myUrlHelper).ToString(),
                    ReplyMarkup = fakePoll.GetReplyMarkup()
                })
                                     .ToArray();
            }

            await myBot.AnswerInlineQueryWithValidationAsync(data.Id, inlineQueryResults,
                                                             switchPmText : switchPmParameter != null? "Link the poll to a gym" : null, switchPmParameter : switchPmParameter,
                                                             cacheTime : 0, cancellationToken : cancellationToken);

            await myDb.SaveChangesAsync(cancellationToken);

            return(true);
        }
        public async Task <bool?> Handle(InlineQuery data, object context = default, CancellationToken cancellationToken = default)
        {
            IReadOnlyCollection <InlineQueryResult> inlineQueryResults;

            Task <Location> location = null;
            async Task <Location> GetLocation() => await(location ??= myDb.Set <UserSettings>().GetLocation(data, cancellationToken));

            string           query             = null;
            Portal           portal            = null;
            bool             exRaidGym         = false;
            string           switchPmParameter = null;
            List <VoteLimit> limits            = default;

            foreach (var queryPart in data.Query.Split(' ', StringSplitOptions.RemoveEmptyEntries))
            {
                switch (queryPart)
                {
                case { } when queryPart.StartsWith(GymInlineQueryHandler.PREFIX):
                    var guid = queryPart.Substring(GymInlineQueryHandler.PREFIX.Length);

                    if (guid.EndsWith('+'))
                    {
                        guid      = guid.Substring(0, guid.Length - 1);
                        exRaidGym = true;
                    }
                    var portalGuid = PortalEx.DecodeGuid(guid);
                    portal = await myIngressClient.Get(portalGuid, await GetLocation(), cancellationToken);

                    break;

                case { } when ProcessLimitQueryString(ref limits, queryPart):
                    break;

                default:
                    query += (query == null ? default(char?) : ' ') + queryPart;
                    break;
                }
            }
            if (string.IsNullOrWhiteSpace(data.Query)) // check whole query for sharing branch
            {
                inlineQueryResults = await myShareInlineQueryHandler.GetActivePolls(data.From, cancellationToken);
            }
            else if (string.IsNullOrWhiteSpace(query))
            {
                inlineQueryResults = new[]
                {
                    new InlineQueryResultArticle($"EnterPollTopic", "Enter a topic",
                                                 new InputTextMessageContent("Enter a topic to create a poll"))
                    {
                        Description = "to create a poll",
                        ThumbUrl    = myUrlHelper.AssetsContent("static_assets/png/POI_Submission_Illustration_02.png").ToString()
                    }
                };
            }
            else
            {
                var poll = await new Poll(data)
                {
                    Title = query, Portal = portal, Limits = limits
                }
                .DetectRaidTime(myTimeZoneService, GetLocation, async ct => myClock.GetCurrentInstant().InZone(await myGeoCoder.GetTimeZone(data, ct)), cancellationToken);
                var pollId = await myRaidService.GetPollId(poll, data.From, cancellationToken);

                switchPmParameter = portal == null ? $"{SwitchToGymParameter}{pollId}" : null;
                ICollection <VoteEnum> voteFormats = await myDb.Set <Settings>().GetFormats(data.From.Id).ToListAsync(cancellationToken);

                if (voteFormats.Count == 0)
                {
                    voteFormats = VoteEnumEx.DefaultVoteFormats;
                }
                inlineQueryResults = voteFormats
                                     .Select(format => new Poll(poll)
                {
                    Id           = exRaidGym ? -pollId : pollId,
                    AllowedVotes = format,
                    ExRaidGym    = exRaidGym
                }.InitImplicitVotes(data.From, myBot.BotId))
                                     .Select((fakePoll, i) => GetInlineResult(fakePoll, i))
                                     .ToArray();
            }

            await myBot.AnswerInlineQueryWithValidationAsync(data.Id, inlineQueryResults,
                                                             switchPmText : switchPmParameter != null? "Link the poll to a gym" : null, switchPmParameter : switchPmParameter,
                                                             cacheTime : 0, cancellationToken : cancellationToken);

            await myDb.SaveChangesAsync(cancellationToken);

            return(true);
        }