예제 #1
0
        private async Task BackgroundDonationCheck(CancellationToken token)
        {
            IEnumerable <PatreonCampaignMember> pledges = await this.GetCampaignMembers();

            if (pledges != null && pledges.Count() > 0)
            {
                this.members = pledges.ToList();
                foreach (PatreonCampaignMember member in this.members)
                {
                    if (!this.currentMembersAndTiers.ContainsKey(member.UserID) || !this.currentMembersAndTiers[member.UserID].Equals(member.TierID))
                    {
                        PatreonTier tier = this.Campaign.GetTier(member.TierID);
                        if (tier != null)
                        {
                            CommandParametersModel parameters = new CommandParametersModel();

                            parameters.User = await ChannelSession.Services.User.GetUserFullSearch(member.User.Platform, member.User.PlatformUserID, member.User.PlatformUsername);

                            if (parameters.User != null)
                            {
                                parameters.User.Data.PatreonUserID = member.UserID;
                            }
                            else
                            {
                                parameters.User = UserViewModel.Create(member.User.PlatformUsername);
                            }

                            parameters.SpecialIdentifiers[SpecialIdentifierStringBuilder.PatreonTierNameSpecialIdentifier]   = tier.Title;
                            parameters.SpecialIdentifiers[SpecialIdentifierStringBuilder.PatreonTierAmountSpecialIdentifier] = tier.Amount.ToString();
                            parameters.SpecialIdentifiers[SpecialIdentifierStringBuilder.PatreonTierImageSpecialIdentifier]  = tier.ImageUrl;
                            await ChannelSession.Services.Events.PerformEvent(EventTypeEnum.PatreonSubscribed, parameters);
                        }
                    }
                    this.currentMembersAndTiers[member.UserID] = member.TierID;
                }
            }
        }
예제 #2
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);
        }
예제 #3
0
        public async Task <PatreonCampaign> GetCampaign()
        {
            PatreonCampaign campaign = null;

            try
            {
                JObject jobj = await this.GetAsync <JObject>("campaigns?include=tiers,benefits,tiers.benefits&fields%5Bcampaign%5D=created_at,creation_name,patron_count,published_at&fields%5Btier%5D=amount_cents,description,created_at,patron_count,title,image_url,published,published_at&fields%5Bbenefit%5D=title,benefit_type,rule_type,created_at,is_deleted,is_published");

                if (jobj != null && jobj.ContainsKey("data"))
                {
                    JArray  dataArray = (JArray)jobj["data"];
                    JObject data      = (JObject)dataArray.First;
                    if (data != null && data.ContainsKey("attributes"))
                    {
                        JObject attributes = (JObject)data["attributes"];
                        campaign    = attributes.ToObject <PatreonCampaign>();
                        campaign.ID = data["id"].ToString();
                    }

                    if (campaign != null && jobj.ContainsKey("included"))
                    {
                        JArray includedArray = (JArray)jobj["included"];
                        foreach (JObject included in includedArray)
                        {
                            if (included.ContainsKey("id") && int.TryParse(included["id"].ToString(), out int id) && id > 0)
                            {
                                if (included.ContainsKey("attributes"))
                                {
                                    JObject attributes = (JObject)included["attributes"];
                                    if (included.ContainsKey("type") && included["type"].ToString().Equals("tier"))
                                    {
                                        PatreonTier tier = attributes.ToObject <PatreonTier>();
                                        tier.ID = id.ToString();
                                        campaign.Tiers[tier.ID] = tier;

                                        if (included.ContainsKey("relationships"))
                                        {
                                            JObject relationships = (JObject)included["relationships"];
                                            if (relationships.ContainsKey("benefits"))
                                            {
                                                JObject benefits = (JObject)relationships["benefits"];
                                                if (benefits.ContainsKey("data"))
                                                {
                                                    JArray benefitsDataArray = (JArray)benefits["data"];
                                                    foreach (JObject benefitData in benefitsDataArray)
                                                    {
                                                        tier.BenefitIDs.Add(benefitData["id"].ToString());
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else if (included.ContainsKey("type") && included["type"].ToString().Equals("benefit"))
                                    {
                                        PatreonBenefit benefit = attributes.ToObject <PatreonBenefit>();
                                        benefit.ID = id.ToString();
                                        campaign.Benefits[benefit.ID] = benefit;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                return(null);
            }
            return(campaign);
        }