this repo has no description
1# Playbook: Rhai
2
3The experimental rhai matcher uses the [rhai](https://rhai.rs/) scripting language to evaluate expressions.
4
5## Builds
6
7To use this feature, the `rhai` feature flag must be enabled at build time.
8
9Locally:
10
11```shell
12cargo run --features rhai
13```
14
15Container:
16
17```shell
18docker build --build-arg=CARGO_FEATURES=rhai .
19```
20
21## Scripting
22
23Rhai matchers evaluate a script that returns a `Match` object or a `string` containing the AT-URI of the post that has matched. Return values of `false` or `0` are considered not matched.
24
25The `upsert_match(aturi)` function is available to create a new `Match` object. It has one parameter, the AT-URI of the post that is matched.
26
27```rhai
28let condition_thing = true;
29// do some stuff ...
30
31if condition_thing {
32 return upsert_match();
33}
34
35false
36```
37
38## Provided Methods
39
40The following methods are available to rhai scripts:
41
42* `build_aturi(event: Event) -> String` - Build an AT-URI from an event. For feed posts, this composes an AT-URI from the event DID, commit collection, and commit rkey.
43
44## Usage
45
46The feed matcher type `rhai` is used with a `source` attribute that points to an rhai script file.
47
48Rhai scripts are evaluated with scope that includes the following variables:
49
50* `event` - The event to match against.
51
52An example config file:
53
54```yaml
55feeds:
56- uri: "at://did:plc:decafbad/app.bsky.feed.generator/Dcuz0bZP1"
57 name: "rhai'ya doing"
58 description: "This feed uses the rhai matcher to match against a complex expression."
59 matchers:
60 - script: "/opt/supercell/rhaiyadoin.rhai"
61 type: rhai
62```
63
64An example rhai script:
65
66```rhai
67// Only match events from the bsky feed where the did is "did:plc:cbkjy5n7bk3ax2wplmtjofq2" (@ngerakines.me).
68if event.did != "did:plc:cbkjy5n7bk3ax2wplmtjofq2" {
69 return false;
70}
71
72// If the event has a commit that has a record that has a $type, set rtype. Otherwise the value will be ().
73let rtype = event?.commit?.record["$type"];
74switch rtype {
75 "app.bsky.feed.post" => {
76 // Compose the at-uri of the post that has matched.
77 return build_aturi(event);
78 }
79 "app.bsky.feed.like" => {
80 // Returns the subject uri of the like event or false if it doesn't exist.
81 return event?.commit?.record?.subject?.uri ?? false;
82 }
83 _ => { }
84}
85
86// Nothing else matches
87false
88```
89