Last active
November 25, 2022 13:34
-
-
Save SteveDinn/9f5cba8c28bf1a04f05da676890a22a3 to your computer and use it in GitHub Desktop.
PowerShell functions to get and set Mastodon server blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # You need to run the .ps file in PowerShell so that the functions get | |
| # loaded but after that you can just use them like regular commands. | |
| # | |
| # e.g. | |
| # Get-MastodonServerBlock ` | |
| # -domain source.social ` | |
| # -accessToken "source_domain_token" | | |
| # Foreach-Object { | |
| # Add-MastodonServerBlock ` | |
| # -domain target.social ` | |
| # -accessToken "target_domain_token" ` | |
| # -block $_ | |
| # } | |
| # | |
| # If you don't have admin access to another server, and that server is | |
| # publicly allowing their blocks to bee seen, you can leave off the | |
| # -accessToken parameter and get that version of the list. Some of the | |
| # domains may be obfuscated in this case, and the obfuscated domains | |
| # will be ignored by Add-MastodonServerBlock. | |
| ########################################################################## | |
| # Creates an object used by Add-MastodonServerBlock from its constituent | |
| # parameters. Really just a helper method. | |
| function New-MastodonServerBlock { | |
| param ( | |
| [Parameter(Mandatory = $true)][string] $domain, | |
| [Parameter(Mandatory = $true)][string] $severity, | |
| [Parameter(Mandatory = $false)][string] $justification = "", | |
| [Parameter(Mandatory = $false)][string] $comment = "", | |
| [Parameter(Mandatory = $false)][switch] $rejectMedia = $false, | |
| [Parameter(Mandatory = $false)][switch] $rejectReports = $false, | |
| [Parameter(Mandatory = $false)][switch] $obfuscate = $false | |
| ) | |
| $block = @{ | |
| domain = $domain | |
| severity = $severity | |
| public_comment = $justification | |
| private_comment = $comment | |
| reject_media = $rejectMedia | |
| reject_reports = $rejectReports | |
| obfuscate = $obfuscate | |
| } | |
| New-Object -TypeName PSObject -Property $block | |
| } | |
| # Retrieves the list of server blocks from a server. | |
| # If the accessToken is specified, it will attempt to use the admin | |
| # API otherwise if will use the instance API where domains names may | |
| # be obfuscated. | |
| function Get-MastodonServerBlock { | |
| param ( | |
| [Parameter(Mandatory = $true)][string] $domain, | |
| [Parameter(Mandatory = $false)][string] $accessToken = $null | |
| ) | |
| if ($accesstoken -ne $null) { | |
| (Invoke-WebRequest ` | |
| -Uri "https://$domain/api/v1/admin/domain_blocks" ` | |
| -Method GET ` | |
| -Headers @{ Authorization = "Bearer $accesstoken" }).Content | ConvertFrom-Json | |
| } | |
| else { | |
| (Invoke-WebRequest ` | |
| -Uri "https://$domain/api/v1/instance/domain_blocks" ` | |
| -Method GET).Content | ConvertFrom-Json | |
| } | |
| } | |
| # Adds a server block to a server. | |
| # Requires an access token with appropriate access. | |
| function Add-MastodonServerBlock { | |
| param ( | |
| [Parameter(Mandatory = $true)][string] $domain, | |
| [Parameter(Mandatory = $true)][string] $accesstoken, | |
| [Parameter(Mandatory = $true, ValueFromPipeline = $true)] $block | |
| ) | |
| if ($block.domain.IndexOf("*") -ge 0) { | |
| Write-Warning "Ignoring obfuscated domain $($_.domain)" | |
| return | |
| } | |
| Invoke-WebRequest ` | |
| -Uri "https://$domain/api/v1/admin/domain_blocks" ` | |
| -Method POST ` | |
| -Headers @{ Authorization = "Bearer $accesstoken" } ` | |
| -Body $($_ | ConvertTo-Json) ` | |
| -ContentType "application/json" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment