Skip to content

Commit

Permalink
Add Union type checks for POSTs (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions authored Jan 9, 2025
1 parent f43a3c8 commit f1fd358
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"request": "launch",
"preLaunchTask": "build_ffsourcegen",
"program": "${workspaceFolder}/tools/FFSourceGen/bin/Debug/net9.0/FFSourceGen.dll",
"args": [ "generate", "${workspaceFolder}/../atproto/lexicons", "-o", "${workspaceFolder}/src/FishyFlip/" ],
"args": [ "generate", "${workspaceFolder}/../fflexicons/bluesky-social-atproto/lexicons", "-o", "${workspaceFolder}/src/FishyFlip/" ],
"cwd": "${workspaceFolder}/tools/FFSourceGen",
"console": "internalConsole",
"stopAtEntry": false
Expand Down
10 changes: 10 additions & 0 deletions src/FishyFlip/Lexicon/Com/Atproto/Admin/AdminEndpoints.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,16 @@ public static class AdminEndpoints
var endpointUrl = UpdateSubjectStatus.ToString();
var headers = new Dictionary<string, string>();
var inputItem = new UpdateSubjectStatusInput();
switch (subject.Type)
{
case "com.atproto.admin.defs#repoRef":
case "com.atproto.repo.strongRef":
case "com.atproto.admin.defs#repoBlobRef":
break;
default:
atp.Options.Logger?.LogWarning($"Unknown subject type for union: " + subject.Type);
break;
}
inputItem.Subject = subject;
inputItem.Takedown = takedown;
inputItem.Deactivated = deactivated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public static class ModerationEndpoints
headers.Add(Constants.AtProtoProxy, atp.Options.OzoneProxyHeader);
var inputItem = new CreateReportInput();
inputItem.ReasonType = reasonType;
switch (subject.Type)
{
case "com.atproto.admin.defs#repoRef":
case "com.atproto.repo.strongRef":
break;
default:
atp.Options.Logger?.LogWarning($"Unknown subject type for union: " + subject.Type);
break;
}
inputItem.Subject = subject;
inputItem.Reason = reason;
return atp.Post<CreateReportInput, FishyFlip.Lexicon.Com.Atproto.Moderation.CreateReportOutput?>(endpointUrl, atp.Options.SourceGenerationContext.ComAtprotoModerationCreateReportInput!, atp.Options.SourceGenerationContext.ComAtprotoModerationCreateReportOutput!, inputItem, cancellationToken, headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,40 @@ public static class ModerationEndpoints
var headers = new Dictionary<string, string>();
headers.Add(Constants.AtProtoProxy, atp.Options.OzoneProxyHeader);
var inputItem = new EmitEventInput();
switch (@event.Type)
{
case "tools.ozone.moderation.defs#modEventTakedown":
case "tools.ozone.moderation.defs#modEventAcknowledge":
case "tools.ozone.moderation.defs#modEventEscalate":
case "tools.ozone.moderation.defs#modEventComment":
case "tools.ozone.moderation.defs#modEventLabel":
case "tools.ozone.moderation.defs#modEventReport":
case "tools.ozone.moderation.defs#modEventMute":
case "tools.ozone.moderation.defs#modEventUnmute":
case "tools.ozone.moderation.defs#modEventMuteReporter":
case "tools.ozone.moderation.defs#modEventUnmuteReporter":
case "tools.ozone.moderation.defs#modEventReverseTakedown":
case "tools.ozone.moderation.defs#modEventResolveAppeal":
case "tools.ozone.moderation.defs#modEventEmail":
case "tools.ozone.moderation.defs#modEventTag":
case "tools.ozone.moderation.defs#accountEvent":
case "tools.ozone.moderation.defs#identityEvent":
case "tools.ozone.moderation.defs#recordEvent":
break;
default:
atp.Options.Logger?.LogWarning($"Unknown @event type for union: " + @event.Type);
break;
}
inputItem.Event = @event;
switch (subject.Type)
{
case "com.atproto.admin.defs#repoRef":
case "com.atproto.repo.strongRef":
break;
default:
atp.Options.Logger?.LogWarning($"Unknown subject type for union: " + subject.Type);
break;
}
inputItem.Subject = subject;
inputItem.CreatedBy = createdBy;
inputItem.SubjectBlobCids = subjectBlobCids;
Expand Down
26 changes: 25 additions & 1 deletion tools/FFSourceGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,8 +1145,32 @@ private async Task GenerateEndpointGroupAsync(IGrouping<string, ClassGeneration>
for (int i = 1; i < inputProperties.Count - 1; i++)
{
var prop = inputProperties[i].Split(" ")[1];
var realName = prop.Replace("@", string.Empty);
var property = item.Definition.Input?.Schema?.Properties.FirstOrDefault(n => n.Key == realName).Value;
if (property?.Type == "union")
{
if (property?.Refs?.Any() ?? false)
{
// Switch statement to take test the {prop}.Type and check if its listed in any of the refs. If not, warn the user.
sb.AppendLine($" switch ({prop}.Type)");
sb.AppendLine(" {");
foreach (var unionType in property.Refs!)
{
sb.AppendLine($" case \"{unionType}\":");
}

sb.AppendLine(" break;");

sb.AppendLine(" default:");
var name =
sb.AppendLine(
$" atp.Options.Logger?.LogWarning($\"Unknown {prop} type for union: \" + {prop}.Type);");
sb.AppendLine(" break;");
sb.AppendLine(" }");
}
}
sb.AppendLine(
$" inputItem.{prop.Replace("@", string.Empty).ToPascalCase()} = {prop};");
$" inputItem.{realName.ToPascalCase()} = {prop};");
}

sb.AppendLine(
Expand Down

0 comments on commit f1fd358

Please sign in to comment.