コード例 #1
0
        protected override async Task <Result> InitializeInternal()
        {
            this.cancellationTokenSource = new CancellationTokenSource();

            this.user = await this.GetCurrentUser();

            if (this.user != null)
            {
                this.Campaign = await this.GetCampaign();

                if (this.Campaign != null)
                {
                    try
                    {
                        this.members = new List <PatreonCampaignMember>(await this.GetCampaignMembers());
                        foreach (PatreonCampaignMember member in this.members)
                        {
                            this.currentMembersAndTiers[member.UserID] = member.TierID;
                        }
                    }
                    catch (Exception ex) { Logger.Log(ex); }

                    AsyncRunner.RunBackgroundTask(this.cancellationTokenSource.Token, 60000, this.BackgroundDonationCheck);
                    this.TrackServiceTelemetry("Patreon");
                    return(new Result());
                }
                return(new Result("Failed to get Campaign data"));
            }
            return(new Result("Failed to get User data"));
        }
コード例 #2
0
        protected override async Task <Result> InitializeInternal()
        {
            this.cancellationTokenSource = new CancellationTokenSource();

            this.user = await this.GetCurrentUser();

            if (this.user != null)
            {
                this.Campaign = await this.GetCampaign();

                if (this.Campaign != null)
                {
                    try
                    {
                        this.members = new List <PatreonCampaignMember>(await this.GetCampaignMembers());
                        foreach (PatreonCampaignMember member in this.members)
                        {
                            this.currentMembersAndTiers[member.UserID] = member.TierID;
                        }
                    }
                    catch (Exception ex) { Logger.Log(ex); }

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                    AsyncRunner.RunAsyncBackground(this.BackgroundDonationCheck, this.cancellationTokenSource.Token, 60000);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

                    this.TrackServiceTelemetry("Patreon");
                    return(new Result());
                }
                return(new Result(Resources.PatreonCampaignDataFailed));
            }
            return(new Result(Resources.PatreonUserDataFailed));
        }
コード例 #3
0
        public async Task <PatreonUser> GetCurrentUser()
        {
            try
            {
                JObject jobj = await this.GetJObjectAsync("identity?fields%5Buser%5D=created,first_name,full_name,last_name,url,vanity,social_connections");

                if (jobj != null && jobj.ContainsKey("data"))
                {
                    JObject data = (JObject)jobj["data"];
                    if (data != null && data.ContainsKey("attributes"))
                    {
                        JObject     attributes = (JObject)data["attributes"];
                        PatreonUser user       = attributes.ToObject <PatreonUser>();
                        user.ID = data["id"].ToString();
                        return(user);
                    }
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
            return(null);
        }
コード例 #4
0
        public async Task <IEnumerable <PatreonCampaignMember> > GetCampaignMembers()
        {
            List <PatreonCampaignMember> results = new List <PatreonCampaignMember>();
            string next = string.Format("campaigns/{0}/members?include=user,currently_entitled_tiers&fields%5Bmember%5D=patron_status,full_name,will_pay_amount_cents,currently_entitled_amount_cents,lifetime_support_cents&fields%5Buser%5D=created,first_name,full_name,last_name,url,vanity,social_connections", this.Campaign.ID);

            try
            {
                do
                {
                    Dictionary <string, PatreonCampaignMember> currentResults = new Dictionary <string, PatreonCampaignMember>();

                    JObject jobj = await this.GetAsync <JObject>(next);

                    next = null;

                    if (jobj != null && jobj.ContainsKey("data"))
                    {
                        JArray dataArray = (JArray)jobj["data"];
                        foreach (JObject data in dataArray)
                        {
                            PatreonCampaignMember pledge = new PatreonCampaignMember();
                            pledge.ID = data["id"].ToString();

                            if (data.ContainsKey("attributes"))
                            {
                                JObject attributes = (JObject)data["attributes"];
                                if (attributes.ContainsKey("will_pay_amount_cents"))
                                {
                                    pledge.AmountToPay         = (int)attributes["will_pay_amount_cents"];
                                    pledge.CurrentAmountPaying = (int)attributes["currently_entitled_amount_cents"];
                                    pledge.LifetimeAmountPaid  = (int)attributes["lifetime_support_cents"];
                                    pledge.PatronStatus        = attributes["patron_status"].ToString();
                                }
                            }

                            if (data.ContainsKey("relationships"))
                            {
                                JObject relationships = (JObject)data["relationships"];

                                if (relationships.ContainsKey("currently_entitled_tiers"))
                                {
                                    JObject entitledTiers = (JObject)relationships["currently_entitled_tiers"];
                                    if (entitledTiers.ContainsKey("data"))
                                    {
                                        JArray entitledTiersData = (JArray)entitledTiers["data"];
                                        if (entitledTiersData.Count > 0)
                                        {
                                            JObject entitledTierData = (JObject)entitledTiersData.First;
                                            pledge.TierID = entitledTierData["id"].ToString();
                                        }
                                    }
                                }

                                if (relationships.ContainsKey("user"))
                                {
                                    JObject user = (JObject)relationships["user"];
                                    if (user.ContainsKey("data"))
                                    {
                                        JObject userData = (JObject)user["data"];
                                        pledge.UserID = userData["id"].ToString();
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(pledge.ID) && !string.IsNullOrEmpty(pledge.UserID))
                            {
                                if (string.IsNullOrEmpty(pledge.TierID) && pledge.CurrentAmountPaying > 0)
                                {
                                    PatreonTier tier = this.Campaign.ActiveTiers.OrderByDescending(t => t.AmountCents).FirstOrDefault(t => pledge.CurrentAmountPaying >= t.AmountCents);
                                    if (tier != null)
                                    {
                                        pledge.TierID = tier.ID;
                                    }
                                }

                                if (!string.IsNullOrEmpty(pledge.TierID))
                                {
                                    currentResults[pledge.UserID] = pledge;
                                }
                            }
                        }

                        if (jobj.ContainsKey("included"))
                        {
                            JArray includedArray = (JArray)jobj["included"];
                            foreach (JObject included in includedArray)
                            {
                                if (included.ContainsKey("type") && included["type"].ToString().Equals("user") && included.ContainsKey("attributes"))
                                {
                                    JObject     attributes = (JObject)included["attributes"];
                                    PatreonUser user       = attributes.ToObject <PatreonUser>();
                                    user.ID = included["id"].ToString();
                                    if (currentResults.ContainsKey(user.ID))
                                    {
                                        currentResults[user.ID].User = user;
                                        results.Add(currentResults[user.ID]);
                                    }
                                }
                            }
                        }

                        if (jobj.ContainsKey("links"))
                        {
                            JObject links = (JObject)jobj["links"];
                            if (links.ContainsKey("next") && links["next"] != null)
                            {
                                next = links["next"].ToString();
                            }
                        }
                    }
                } while (!string.IsNullOrEmpty(next));
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                return(null);
            }
            return(results);
        }