Skip to content

Commit 74701af

Browse files
phireskydessalines
andauthored
Community post tags (part 2: API methods) (#5389)
* post tags part 2 * post tags part 2 (api methods) * re-remove tracing * api tests * update according to comments * update lints and api package * validate tags, fix compile * insert many simultaneously * o * temp revert for tests * update dep * Revert "temp revert for tests" This reverts commit 62f17de. * fix deps * limit tag length * fix comments * rename display_name * lint * lint * bullshit * clarify comment * fix after merge * lint.sh * upgrade dep * rename * dep --------- Co-authored-by: Dessalines <[email protected]>
1 parent 9bfaad8 commit 74701af

File tree

35 files changed

+701
-148
lines changed

35 files changed

+701
-148
lines changed

.woodpecker.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ variables:
1212
- &install_binstall "wget -O- https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-unknown-linux-musl.tgz | tar -xvz -C /usr/local/cargo/bin"
1313
- install_diesel_cli: &install_diesel_cli
1414
- apt-get update && apt-get install -y postgresql-client
15-
- cargo install --locked diesel_cli --no-default-features --features postgres
15+
# [email protected] is the last version that supports rust 1.81, which we are currently locked on due to perf regressions on rust 1.82+ :(
16+
- cargo install --locked [email protected] --no-default-features --features postgres
1617
- export PATH="$CARGO_HOME/bin:$PATH"
1718
- &slow_check_paths
1819
- event: pull_request

api_tests/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,27 @@
1010
"scripts": {
1111
"lint": "tsc --noEmit && eslint --report-unused-disable-directives && prettier --check 'src/**/*.ts'",
1212
"fix": "prettier --write src && eslint --fix src",
13-
"api-test": "jest -i follow.spec.ts && jest -i image.spec.ts && jest -i user.spec.ts && jest -i private_message.spec.ts && jest -i community.spec.ts && jest -i private_community.spec.ts && jest -i post.spec.ts && jest -i comment.spec.ts ",
13+
"api-test": "jest -i follow.spec.ts && jest -i image.spec.ts && jest -i user.spec.ts && jest -i private_message.spec.ts && jest -i community.spec.ts && jest -i private_community.spec.ts && jest -i post.spec.ts && jest -i comment.spec.ts && jest -i tags.spec.ts",
1414
"api-test-follow": "jest -i follow.spec.ts",
1515
"api-test-comment": "jest -i comment.spec.ts",
1616
"api-test-post": "jest -i post.spec.ts",
1717
"api-test-user": "jest -i user.spec.ts",
1818
"api-test-community": "jest -i community.spec.ts",
1919
"api-test-private-community": "jest -i private_community.spec.ts",
2020
"api-test-private-message": "jest -i private_message.spec.ts",
21-
"api-test-image": "jest -i image.spec.ts"
21+
"api-test-image": "jest -i image.spec.ts",
22+
"api-test-tags": "jest -i tags.spec.ts"
2223
},
2324
"devDependencies": {
25+
"@eslint/js": "^9.21.0",
2426
"@types/jest": "^29.5.12",
25-
"@types/joi": "^17.2.3",
2627
"@types/node": "^22.13.1",
2728
"@typescript-eslint/eslint-plugin": "^8.24.0",
2829
"@typescript-eslint/parser": "^8.24.0",
2930
"eslint": "^9.20.0",
3031
"eslint-plugin-prettier": "^5.2.3",
3132
"jest": "^29.5.0",
32-
"lemmy-js-client": "1.0.0-site-person-ban.1",
33+
"lemmy-js-client": "npm:@phiresky/lemmy-js-client@1.0.0-post-tags.7",
3334
"prettier": "^3.5.0",
3435
"ts-jest": "^29.1.0",
3536
"tsoa": "^6.6.0",

api_tests/pnpm-lock.yaml

Lines changed: 20 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api_tests/src/tags.spec.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
jest.setTimeout(120000);
2+
3+
import {
4+
alpha,
5+
setupLogins,
6+
createCommunity,
7+
unfollows,
8+
randomString,
9+
createPost,
10+
} from "./shared";
11+
import { CreateCommunityTag } from "lemmy-js-client/dist/types/CreateCommunityTag";
12+
import { UpdateCommunityTag } from "lemmy-js-client/dist/types/UpdateCommunityTag";
13+
import { DeleteCommunityTag } from "lemmy-js-client/dist/types/DeleteCommunityTag";
14+
import { EditPost } from "lemmy-js-client";
15+
16+
beforeAll(setupLogins);
17+
afterAll(unfollows);
18+
19+
test("Create, update, delete community tag", async () => {
20+
// Create a community first
21+
let communityRes = await createCommunity(alpha);
22+
const communityId = communityRes.community_view.community.id;
23+
24+
// Create a tag
25+
const tagName = randomString(10);
26+
let createForm: CreateCommunityTag = {
27+
display_name: tagName,
28+
community_id: communityId,
29+
};
30+
let createRes = await alpha.createCommunityTag(createForm);
31+
expect(createRes.id).toBeDefined();
32+
expect(createRes.display_name).toBe(tagName);
33+
expect(createRes.community_id).toBe(communityId);
34+
35+
// Update the tag
36+
const newTagName = randomString(10);
37+
let updateForm: UpdateCommunityTag = {
38+
tag_id: createRes.id,
39+
display_name: newTagName,
40+
};
41+
let updateRes = await alpha.updateCommunityTag(updateForm);
42+
expect(updateRes.id).toBe(createRes.id);
43+
expect(updateRes.display_name).toBe(newTagName);
44+
expect(updateRes.community_id).toBe(communityId);
45+
46+
// List tags
47+
let listRes = await alpha.getCommunity({ id: communityId });
48+
expect(listRes.community_view.post_tags.length).toBe(1);
49+
expect(
50+
listRes.community_view.post_tags.find(t => t.id === createRes.id)
51+
?.display_name,
52+
).toBe(newTagName);
53+
54+
// Delete the tag
55+
let deleteForm: DeleteCommunityTag = {
56+
tag_id: createRes.id,
57+
};
58+
let deleteRes = await alpha.deleteCommunityTag(deleteForm);
59+
expect(deleteRes.id).toBe(createRes.id);
60+
61+
// Verify tag is deleted
62+
listRes = await alpha.getCommunity({ id: communityId });
63+
expect(
64+
listRes.community_view.post_tags.find(t => t.id === createRes.id),
65+
).toBeUndefined();
66+
expect(listRes.community_view.post_tags.length).toBe(0);
67+
});
68+
69+
test("Update post tags", async () => {
70+
// Create a community
71+
let communityRes = await createCommunity(alpha);
72+
const communityId = communityRes.community_view.community.id;
73+
74+
// Create two tags
75+
const tag1Name = randomString(10);
76+
let createForm1: CreateCommunityTag = {
77+
display_name: tag1Name,
78+
community_id: communityId,
79+
};
80+
let tag1Res = await alpha.createCommunityTag(createForm1);
81+
expect(tag1Res.id).toBeDefined();
82+
83+
const tag2Name = randomString(10);
84+
let createForm2: CreateCommunityTag = {
85+
display_name: tag2Name,
86+
community_id: communityId,
87+
};
88+
let tag2Res = await alpha.createCommunityTag(createForm2);
89+
expect(tag2Res.id).toBeDefined();
90+
91+
// Create a post
92+
let postRes = await alpha.createPost({
93+
name: randomString(10),
94+
community_id: communityId,
95+
});
96+
expect(postRes.post_view.post.id).toBeDefined();
97+
98+
// Update post tags
99+
let updateForm: EditPost = {
100+
post_id: postRes.post_view.post.id,
101+
tags: [tag1Res.id, tag2Res.id],
102+
};
103+
let updateRes = await alpha.editPost(updateForm);
104+
expect(updateRes.post_view.post.id).toBe(postRes.post_view.post.id);
105+
expect(updateRes.post_view.tags?.length).toBe(2);
106+
expect(updateRes.post_view.tags?.map(t => t.id).sort()).toEqual(
107+
[tag1Res.id, tag2Res.id].sort(),
108+
);
109+
110+
// Update post to remove one tag
111+
updateForm.tags = [tag1Res.id];
112+
updateRes = await alpha.editPost(updateForm);
113+
expect(updateRes.post_view.post.id).toBe(postRes.post_view.post.id);
114+
expect(updateRes.post_view.tags?.length).toBe(1);
115+
expect(updateRes.post_view.tags?.[0].id).toBe(tag1Res.id);
116+
});
117+
118+
test("Post author can update post tags", async () => {
119+
// Create a community
120+
let communityRes = await createCommunity(alpha);
121+
const communityId = communityRes.community_view.community.id;
122+
123+
// Create a tag
124+
const tagName = randomString(10);
125+
let createForm: CreateCommunityTag = {
126+
display_name: tagName,
127+
community_id: communityId,
128+
};
129+
let tagRes = await alpha.createCommunityTag(createForm);
130+
expect(tagRes.id).toBeDefined();
131+
132+
let postRes = await createPost(
133+
alpha,
134+
communityId,
135+
"https://example.com/",
136+
"post with tags",
137+
);
138+
expect(postRes.post_view.post.id).toBeDefined();
139+
140+
// Alpha should be able to update tags on their own post
141+
let updateForm: EditPost = {
142+
post_id: postRes.post_view.post.id,
143+
tags: [tagRes.id],
144+
};
145+
let updateRes = await alpha.editPost(updateForm);
146+
expect(updateRes.post_view.post.id).toBe(postRes.post_view.post.id);
147+
expect(updateRes.post_view.tags?.length).toBe(1);
148+
expect(updateRes.post_view.tags?.[0].id).toBe(tagRes.id);
149+
});

0 commit comments

Comments
 (0)