Post Prepper v1.0.0

This commit is contained in:
bunnybeam 2025-05-04 14:01:18 +01:00
parent cb4667775f
commit 65b30ef80c
Signed by: bunnybeam
GPG key ID: 9A56D320981D30AF

145
post-prepper.ais Normal file
View file

@ -0,0 +1,145 @@
/// @ 0.19.0
### {
name: "Post Prepper"
version: "1.0.0"
author: "@bunnybeam@ck.catwithaclari.net"
description: "Creates format presets that can be applied to a note by clicking a post form action. Can also undo and redo from backups."
permissions: ["read:account", "write:account"]
config: {
apply_notif: {
type: "boolean"
label: "Popup notification on select"
description: "If enabled, selecting a preset will display a popup dialog."
default: true
}
presets: {
type: "string"
label: "Presets"
description: 'JSON-formatted list of presets. Each preset may have the following fields: "label" - Name of the preset that shows up in the post form action list; "text" - Format for the body of the note; "cw" - Format for the content warning of the note; "username" - Format for changing your profile\'s display name. Fields may contain the following tags, which are replaced when activated: %[text] - Raw body text of the note; %[cw] - Raw content warning of the note; %[cwsep] - Separator for content warning elements (won\'t appear if the content warning is empty); %[username] - Current display name; %[br] - Line break.'
default: '[{"label": "Test preset (Add text)", "text": "%[text]%[br]Added text"}, {"label": "Test preset (Add CW item)", "cw": "%[cw]%[cwsep]New CW item"}, {"label": "Test preset (Change display name)", "text": "%[text]%[br]Your display name has been changed! Remember to change it back!", "username":"%[username] (Testing a plugin)"}]'
}
}
}
let undo_stack = []
let redo_stack = []
// Get the list of presets.
@get_preset_list() {
Json:parse(Plugin:config.presets)
}
// Handle an API response. Returns true if there is no error, otherwise displays a dialog and returns false.
@error_response(res, title, action, permission) {
if Core:type(res) != "error" {
return true
}
var message = `An unknown error occurred when attempting to {action}. Code: {res.info.code}`
if res.info.code == "PERMISSION_DENIED" {
message = `The "{permission}" permission must be enabled in order to {action}.`
}
Mk:dialog(`Error {title}`, message, "error")
false
}
// Format a field using data from the note.
@format_field(note, format) {
var output = format
output = output.replace("%[br]", Str:lf)
let cw = if note.cw == null || note.cw == "" {
output = output.replace("%[cwsep]", "")
""
} else {
output = output.replace("%[cwsep]", ", ")
note.cw
}
let text = if note.text == null {
""
} else {
note.text
}
output = output.replace("%[cw]", cw)
output.replace("%[text]", text)
}
// Attempt to change the display name. Returns the old name if successful, null otherwise.
@change_display_name(format) {
// Attempt to read the display name
let read_res = Mk:api("i", {})
let read_success = error_response(read_res, "reading display name", "change your display name", "View account information")
if !read_success return null
let old_name = read_res.name
let new_name = format.replace("%[username]", old_name)
// Attempt to set the display name
let write_res = Mk:api("i/update", {name: new_name})
let write_success = error_response(write_res, "changing display name", "change your display name", "Edit account information")
if write_success {
old_name
} else {
null
}
}
// Handle an undo/redo step. Pops an item from the main stack, applies it, and moves it to the bin stack.
@undo_redo(main_stack, bin_stack, label, note, rewrite) {
if main_stack.len == 0 {
Mk:dialog(`Nothing to {label}do`, `Nothing to {label}do!`, "warning")
return null
}
let item = main_stack.pop()
let bin_item = {note: note}
if Obj:has(item, "username") {
let old_name = change_display_name(item.username)
if old_name == null return null
bin_item.username = old_name
}
bin_stack.push(bin_item)
// `rewrite` doesn't work if we pass `null`, so we have to pass an empty string.
let cw = if item.note.cw == null {
""
} else {
item.note.cw
}
rewrite("cw", cw)
rewrite("text", item.note.text)
if Plugin:config.apply_notif {
Mk:dialog(`Applied {label}do`, `Successfully {label}did prep.`, "success")
}
}
// Register post form actions for each preset.
each let preset, get_preset_list() {
let label = if Obj:has(preset, "label") {
preset.label
} else {
"<unnamed>"
}
Plugin:register_post_form_action(`Prep: {label}`, @(note, rewrite) {
let undo_item = {note: note}
if Obj:has(preset, "username") {
let old_name = change_display_name(preset.username)
if old_name == null return null
undo_item.username = old_name
}
if Obj:has(preset, "cw") {
rewrite("cw", format_field(note, preset.cw))
}
if Obj:has(preset, "text") {
rewrite("text", format_field(note, preset.text))
}
undo_stack.push(undo_item)
if Plugin:config.apply_notif {
Mk:dialog("Prep applied", `Prepped the post with the "{label}" preset.`, "success")
}
})
}
Plugin:register_post_form_action(`Undo last prep`, @(note, rewrite) {
undo_redo(undo_stack, redo_stack, "un", note, rewrite)
})
Plugin:register_post_form_action(`Redo last undo`, @(note, rewrite) {
undo_redo(redo_stack, undo_stack, "re", note, rewrite)
})