Skip to content

Commit 3bbe901

Browse files
authored
Merge pull request #3 from LemmyNet/rust-allowed-voters
Add Rust example
2 parents 711e5ca + 2d1b659 commit 3bbe901

File tree

14 files changed

+167
-10
lines changed

14 files changed

+167
-10
lines changed

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
.vscode
44

55
# tests
6-
**/node_modules
7-
plugins/typescript_push_webhook/dist
6+
node_modules
87
tests/.yalc
98
tests/yalc.lock
109
tests/pict-rs
1110
tests/log
1211

12+
# plugins
13+
plugins/rust_allowed_voters/target
14+
plugins/rust_allowed_voters/Cargo.lock
15+
plugins/typescript_push_webhook/dist
16+
plugins/typescript_push_webhook/node_modules
17+
1318
# lemmy plugins
1419
*.wasm
1520

.woodpecker.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ steps:
66
when:
77
- event: pull_request
88

9+
build_rust_plugin:
10+
image: rust:1.81
11+
commands:
12+
- rustup target add wasm32-unknown-unknown
13+
- cd plugins/rust_allowed_voters
14+
- cargo build
15+
- cp target/wasm32-unknown-unknown/debug/rust_allowed_voters.wasm ..
16+
when:
17+
- event: [pull_request, tag]
18+
919
build_typescript_plugin:
1020
# need to use ubuntu instead of debian because extism-js requires newer glibc version
1121
# https://github.com/extism/js-pdk/issues/129
@@ -60,6 +70,7 @@ steps:
6070
image: woodpeckerci/plugin-release
6171
settings:
6272
files:
73+
- plugins/rust_allowed_voters.wasm
6374
- plugins/go_replace_words.wasm
6475
- plugins/typescript_push_webhook.wasm
6576
title: ${CI_COMMIT_TAG##v}

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ npm install
2929
npm run build
3030
```
3131

32+
## Rust: Allowed Voters
33+
34+
Listens to `new_vote` hook, then calls `/api/v4/person` to get details about the voter. It only allows downvotes if the user has made at least 5 posts before. See [rust-pdk readme](https://github.com/extism/rust-pdk?tab=readme-ov-file) for setup and detailed documentation.
35+
36+
Use the following steps to compile it:
37+
38+
```bash
39+
apt install cargo
40+
cd plugins/rust_allowed_voters
41+
cargo build
42+
cp target/wasm32-unknown-unknown/debug/rust_allowed_voters.wasm ..
43+
```
44+
3245
## Tests
3346

34-
This repository contains test cases for the plugins. To run them install `pnpm` and `postgresql`, with a database `postgres://lemmy:password@localhost:5432/lemmy`. Then go into `tests` folder and execute `./run.sh`.
47+
This repository contains test cases for the plugins. To run them install `pnpm` and `postgresql`, with a database `postgres://lemmy:password@localhost:5432/lemmy`. Then compile all the plugins as described above, go into `tests` folder and execute `./run.sh`.

lemmy_server

-129 KB
Binary file not shown.

plugins/rust_allowed_voters.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"wasm": [
3+
{
4+
"path": "plugins/rust_allowed_voters.wasm"
5+
}
6+
],
7+
"allowed_hosts": ["0.0.0.0"]
8+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target = "wasm32-unknown-unknown"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "rust_allowed_voters"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
extism-pdk = "1.3.0"
11+
serde = { version = "1", features = ["derive"] }
12+
lemmy_api_common = { git = "https://github.com/LemmyNet/lemmy.git" }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::json::Value;
2+
use extism_pdk::*;
3+
use lemmy_api_common::person::GetPersonDetailsResponse;
4+
use serde::Serialize;
5+
use std::collections::HashMap;
6+
7+
#[derive(Serialize)]
8+
struct Metadata {
9+
name: String,
10+
url: String,
11+
description: String,
12+
}
13+
14+
// Returns info about the plugin which gets included in /api/v4/site
15+
//go:wasmexport metadata
16+
#[plugin_fn]
17+
pub fn metadata() -> FnResult<Json<Metadata>> {
18+
Ok(Json(Metadata {
19+
name: "Test Plugin".to_string(),
20+
url: "https://example.com".to_string(),
21+
description: "Plugin to test Lemmy feature".to_string(),
22+
}))
23+
}
24+
25+
#[plugin_fn]
26+
pub fn post_vote(
27+
Json(vote): Json<HashMap<String, Value>>,
28+
) -> FnResult<Json<HashMap<String, Value>>> {
29+
let lemmy_url = config::get("lemmy_url")?.unwrap();
30+
let person_id = vote.get("person_id").unwrap();
31+
let req = HttpRequest {
32+
url: format!("{lemmy_url}api/v4/person?person_id={person_id}"),
33+
headers: Default::default(),
34+
method: Some("GET".to_string()),
35+
};
36+
let res: GetPersonDetailsResponse = http::request::<()>(&req, None)?.json()?;
37+
let person_post_count = res.person_view.person.post_count;
38+
let vote_score = vote.get("like_score").and_then(Value::as_i64).unwrap();
39+
if person_post_count < 5 && vote_score == -1 {
40+
return Err(Error::msg("user is not allowed to downvote").into());
41+
}
42+
Ok(Json(vote))
43+
}

tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"scripts": {
1111
"lint": "tsc --noEmit && eslint --report-unused-disable-directives && prettier --check 'src/**/*.ts'",
1212
"fix": "prettier --write src && eslint --fix src",
13-
"test": "jest -i typescript.spec.ts && jest -i golang.spec.ts"
13+
"test": "jest -i rust.spec.ts && jest -i golang.spec.ts && jest -i typescript.spec.ts"
1414
},
1515
"devDependencies": {
1616
"@types/jest": "^29.5.12",

0 commit comments

Comments
 (0)