public async Task <PatreonUser> GetCurrentUser() { try { JObject jobj = await this.GetJObjectAsync("identity?fields%5Buser%5D=created,first_name,full_name,last_name,url,vanity"); 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) { MixItUp.Base.Util.Logger.Log(ex); } return(null); }
private async Task <bool> InitializeInternal() { this.cancellationTokenSource = new CancellationTokenSource(); this.user = await this.GetCurrentUser(); if (this.user != null) { this.Campaign = await this.GetCampaign(); if (this.Campaign != null) { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Run(this.BackgroundDonationCheck, this.cancellationTokenSource.Token); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed return(true); } } return(false); }
private async Task BackgroundDonationCheck() { try { this.members = new List <PatreonCampaignMember>(await this.GetCampaignMembers()); foreach (PatreonCampaignMember member in this.members) { this.currentMembersAndTiers[member.UserID] = member.TierID; } } catch (Exception ex) { MixItUp.Base.Util.Logger.Log(ex); } while (!this.cancellationTokenSource.Token.IsCancellationRequested) { await Task.Delay(60000); try { IEnumerable <PatreonCampaignMember> pledges = await this.GetCampaignMembers(); if (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) { UserViewModel user = new UserViewModel(0, member.User.LookupName); UserModel userModel = await ChannelSession.Connection.GetUser(user.UserName); if (userModel != null) { user = await ChannelSession.ActiveUsers.GetUserByID(userModel.id); if (user == null) { user = new UserViewModel(userModel); } user.Data.PatreonUserID = member.UserID; await user.RefreshDetails(force : true); } EventCommand command = ChannelSession.Constellation.FindMatchingEventCommand(EnumHelper.GetEnumName(OtherEventTypeEnum.PatreonSubscribed)); if (command != null) { Dictionary <string, string> extraSpecialIdentifiers = new Dictionary <string, string>(); extraSpecialIdentifiers[SpecialIdentifierStringBuilder.PatreonTierNameSpecialIdentifier] = tier.Title; extraSpecialIdentifiers[SpecialIdentifierStringBuilder.PatreonTierAmountSpecialIdentifier] = tier.Amount.ToString(); extraSpecialIdentifiers[SpecialIdentifierStringBuilder.PatreonTierImageSpecialIdentifier] = tier.ImageUrl; await command.Perform(user, arguments : null, extraSpecialIdentifiers : extraSpecialIdentifiers); } } } this.currentMembersAndTiers[member.UserID] = member.TierID; } } } catch (Exception ex) { MixItUp.Base.Util.Logger.Log(ex); } } }
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", 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) { MixItUp.Base.Util.Logger.Log(ex); return(null); } return(results); }