Begin rules

This commit is contained in:
mitteneer 2025-04-18 21:29:00 -04:00
parent ff8cdabbf1
commit 18cdb48b53
3 changed files with 151 additions and 4 deletions

View file

@ -0,0 +1,32 @@
const std = @import("std");
const jetzig = @import("jetzig");
pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig.jobs.JobEnv) !void {
_ = allocator;
_ = env;
//_ = params;
const rule = try params.toJson();
//const rule = struct {
// name: []const u8 = params,
// conditions: []struct {
// match_on: []const u8,
// match_cond: []const u8,
// match_text: []const u8,
// },
// actions: []struct {
// action: []const u8,
// action_cond: []const u8,
// action_text: []const u8,
// },
//};
//var file = try std.fs.cwd().openFile("rules.json", .{});
//_ = try file.write(rule);
try std.fs.cwd().writeFile(.{ "rules.json", rule, .{} });
// Job execution code goes here. Add any code that you would like to run in the background.
//try env.logger.INFO("Running a job.", .{});
}

View file

@ -20,6 +20,26 @@ pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View {
} }
pub fn post(request: *jetzig.Request) !jetzig.View { pub fn post(request: *jetzig.Request) !jetzig.View {
const params = try request.params();
var job = try request.job("process_rule");
_ = try job.params.put("name", params.get("rule-title"));
var conditionals = try job.params.put("conditionals", .array);
var cond0 = try conditionals.append(.object);
try cond0.put("match_on", params.get("match-on"));
try cond0.put("match_cond", params.get("match-cond"));
try cond0.put("match_txt", params.get("match-txt"));
var actions = try job.params.put("actions", .array);
var act0 = try actions.append(.object);
try act0.put("action", params.get("action"));
try act0.put("action_on", params.get("action-on"));
try act0.put("action_txt", params.get("action-txt"));
try job.schedule();
return request.render(.created); return request.render(.created);
} }
@ -38,7 +58,6 @@ pub fn delete(id: []const u8, request: *jetzig.Request) !jetzig.View {
return request.render(.ok); return request.render(.ok);
} }
test "index" { test "index" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit(); defer app.deinit();

View file

@ -1,3 +1,99 @@
<div> <html>
<span>Content goes here</span> <head>
</div> <meta charset="UTF-8">
</head>
<body>
@partial partials/header
<h1>Rules</h1>
Rules allow you change the default Scrobble import behavior based on provided criteria.
Add a rule below.
<br><br>
<form action="/rules" enctype="multipart/form-data" method="POST">
<label for="rule-title">Rule Name:</label>
<input type="text" name="rule-title" id="rule-title">
<br>
If
<select name="match-on" id="match-on">
<option value="artist">artist</option>
<option value="album">album</option>
<option value="song">song</option>
</select>
<select name="match-cond" id="match-cond">
<option value="is">is</option>
<option value="contains">contains</option>
<option value"matches">matches regex</option>
</select>
<input type="text" name="match-txt" id="match-txt">
<button type="button" onclick="condAdd()">
Add Conditional
</button>
<script>
function condAdd() {
const sep = document.getElementById("cond-ins");
const wrapper = document.createElement("div");
const cond = document.createElement("select");
const match_on = document.createElement("select");
const match_cond = document.createElement("select");
const match_txt = document.createElement("input");
const or_opt = document.createElement("option")
or_opt.value = "or";
or_opt.innerHTML = "or";
const and_opt = document.createElement("option")
and_opt.value = "and";
and_opt.innerHTML = "and";
const artist_opt = document.createElement("option")
artist_opt.value = "artist";
artist_opt.innerHTML = "artist";
const album_opt = document.createElement("option")
album_opt.value = "album"
album_opt.innerHTML = "album"
const song_opt = document.createElement("option")
song_opt.value = "song";
song_opt.innerHTML = "song";
const is_opt = document.createElement("option")
is_opt.value = "is";
is_opt.innerHTML = "is";
const contains_opt = document.createElement("option")
contains_opt.value = "contains"
contains_opt.innerHTML = "contains"
match_txt.setAttribute("type","text");
cond.appendChild(or_opt);
cond.appendChild(and_opt);
match_on.appendChild(artist_opt);
match_on.appendChild(album_opt);
match_on.appendChild(song_opt);
match_cond.appendChild(is_opt);
match_cond.appendChild(contains_opt);
wrapper.appendChild(cond);
wrapper.appendChild(match_on);
wrapper.appendChild(match_cond);
wrapper.appendChild(match_txt);
sep.appendChild(wrapper);
}
</script>
<div id="cond-ins"></div>
then
<select name="action" id="action">
<option value="replace">replace</option>
<option value="add">add</option>
</select>
<select name="action-on" id="action-on">
<option value="artist">artist</option>
<option value="album">album</option>
<option value="song">song</option>
</select>
with
<input type="text" name="action-txt" id="action-txt">
<button type="submit" value="Submit">Submit</button>
</form>
Current rules:
</html>