Skip to content

Commit

Permalink
- Updating Alejo pronouns to use new API setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Olivo committed Oct 19, 2024
1 parent 987447d commit 2361319
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
2 changes: 2 additions & 0 deletions MixItUp.Base/Model/User/UserV2Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static UserV2Model CreateUnassociated(string username = null)

[DataMember]
public string AlejoPronounID { get; set; }
[DataMember]
public string AlejoAltPronounID { get; set; }

[DataMember]
public uint ModerationStrikes { get; set; }
Expand Down
84 changes: 63 additions & 21 deletions MixItUp.Base/Services/External/AlejoPronounsService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StreamingClient.Base.Util;
using StreamingClient.Base.Web;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace MixItUp.Base.Services.External
{
[DataContract]
public class AlejoPronoun
{
[DataMember]
public string name { get; set; }
[DataMember]
public string subject { get; set; }
[DataMember]
[JsonProperty("object")]
public string obj { get; set; }
[DataMember]
public bool singular { get; set; }
}

[DataContract]
public class AlejoUserPronoun
{
[DataMember]
public string channel_id { get; set; }
[DataMember]
public string channel_login { get; set; }
[DataMember]
public string pronoun_id { get; set; }
[DataMember]
public string alt_pronoun_id { get; set; }
}

public class AlejoPronounsService
{
public const string BaseAddress = "https://pronouns.alejo.io/api/";
public const string BaseAddress = "https://api.pronouns.alejo.io/v1/";

private Dictionary<string, string> pronounIDLookup = new Dictionary<string, string>();
private Dictionary<string, AlejoPronoun> pronounIDLookup = new Dictionary<string, AlejoPronoun>();

public AlejoPronounsService() { }

Expand All @@ -20,21 +49,13 @@ public async Task Initialize()
this.pronounIDLookup = await this.GetPronounIDs();
}

public async Task<string> GetPronounID(string twitchLogin)
public async Task<AlejoUserPronoun> GetPronounData(string twitchLogin)
{
try
{
using (AdvancedHttpClient client = new AdvancedHttpClient(AlejoPronounsService.BaseAddress))
{
string result = await client.GetStringAsync($"users/{twitchLogin}");
if (!string.IsNullOrEmpty(result))
{
JObject jobject = JArray.Parse(result).First as JObject;
if (jobject != null)
{
return jobject["pronoun_id"].ToString();
}
}
return await client.GetAsync<AlejoUserPronoun>($"users/{twitchLogin}");
}
}
catch (Exception ex)
Expand All @@ -44,28 +65,49 @@ public async Task<string> GetPronounID(string twitchLogin)
return null;
}

public string GetPronoun(string pronounID)
public string GetPronoun(string pronounID, string altPronounID)
{
if (!string.IsNullOrEmpty(pronounID) && this.pronounIDLookup.TryGetValue(pronounID, out string pronoun))
try
{
return pronoun;
if (string.IsNullOrEmpty(pronounID) || !this.pronounIDLookup.TryGetValue(pronounID, out AlejoPronoun pronoun))
{
return string.Empty;
}

if (pronoun.singular)
{
return pronoun.subject;
}

string first = pronoun.subject;
string second = pronoun.obj;
if (!string.IsNullOrWhiteSpace(altPronounID) && this.pronounIDLookup.TryGetValue(altPronounID, out AlejoPronoun altPronoun))
{
second = altPronoun.subject;
}

return $"{first}/{second}";
}
catch (Exception ex)
{
Logger.Log(ex);
return string.Empty;
}
return string.Empty;
}

private async Task<Dictionary<string, string>> GetPronounIDs()
private async Task<Dictionary<string, AlejoPronoun>> GetPronounIDs()
{
Dictionary<string, string> results = new Dictionary<string, string>();
Dictionary<string, AlejoPronoun> results = new Dictionary<string, AlejoPronoun>();
try
{
using (AdvancedHttpClient client = new AdvancedHttpClient(AlejoPronounsService.BaseAddress))
{
string result = await client.GetStringAsync("pronouns");
if (!string.IsNullOrEmpty(result))
{
foreach (JObject jobject in JArray.Parse(result))
foreach (var kvp in JObject.Parse(result))
{
results[jobject["name"].ToString()] = jobject["display"].ToString();
results[kvp.Key] = kvp.Value.ToObject<AlejoPronoun>();
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions MixItUp.Base/ViewModel/User/UserV2ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public bool IsExternalSubscriber
}
}

public string AlejoPronoun { get { return ServiceManager.Get<AlejoPronounsService>().GetPronoun(this.Model.AlejoPronounID); } }
public string AlejoPronoun { get { return ServiceManager.Get<AlejoPronounsService>().GetPronoun(this.Model.AlejoPronounID, this.Model.AlejoAltPronounID); } }

public bool IsFollower { get { return this.HasRole(UserRoleEnum.Follower) || this.HasRole(UserRoleEnum.YouTubeSubscriber); } }
public bool IsRegular { get { return this.HasRole(UserRoleEnum.Regular); } }
Expand Down Expand Up @@ -588,7 +588,12 @@ public async Task Refresh(bool force = false)

if (ChannelSession.Settings.ShowAlejoPronouns && this.Platform == StreamingPlatformTypeEnum.Twitch)
{
this.Model.AlejoPronounID = await ServiceManager.Get<AlejoPronounsService>().GetPronounID(this.Username);
AlejoUserPronoun pronouns = await ServiceManager.Get<AlejoPronounsService>().GetPronounData(this.Username);
if (pronouns != null)
{
this.Model.AlejoPronounID = pronouns.pronoun_id;
this.Model.AlejoAltPronounID = pronouns.alt_pronoun_id;
}
}

this.RefreshPatreonProperties();
Expand Down

0 comments on commit 2361319

Please sign in to comment.