Skip to content

Commit bc32b40

Browse files
authored
Fixing private message reports. (#4279)
1 parent aab3ca4 commit bc32b40

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

api_tests/prepare-drone-federation-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export RUST_LOG="warn,lemmy_server=debug,lemmy_federate=debug,lemmy_api=debug,le
99
export LEMMY_TEST_FAST_FEDERATION=1 # by default, the persistent federation queue has delays in the scale of 30s-5min
1010

1111
# pictrs setup
12-
if ! [ -f "pict-rs" ]; then
12+
if [ ! -f "pict-rs" ]; then
1313
curl "https://git.asonix.dog/asonix/pict-rs/releases/download/v0.5.0-beta.2/pict-rs-linux-amd64" -o api_tests/pict-rs
1414
chmod +x api_tests/pict-rs
1515
fi

api_tests/src/private_message.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
deletePrivateMessage,
1111
unfollowRemotes,
1212
waitUntil,
13+
reportPrivateMessage,
1314
} from "./shared";
1415

1516
let recipient_id: number;
@@ -109,3 +110,42 @@ test("Delete a private message", async () => {
109110
betaPms1.private_messages.length,
110111
);
111112
});
113+
114+
test("Create a private message report", async () => {
115+
let pmRes = await createPrivateMessage(alpha, recipient_id);
116+
let betaPms1 = await waitUntil(
117+
() => listPrivateMessages(beta),
118+
m =>
119+
!!m.private_messages.find(
120+
e =>
121+
e.private_message.ap_id ===
122+
pmRes.private_message_view.private_message.ap_id,
123+
),
124+
);
125+
let betaPm = betaPms1.private_messages[0];
126+
expect(betaPm).toBeDefined();
127+
128+
// Make sure that only the recipient can report it, so this should fail
129+
await expect(
130+
reportPrivateMessage(
131+
alpha,
132+
pmRes.private_message_view.private_message.id,
133+
"a reason",
134+
),
135+
).rejects.toStrictEqual(Error("couldnt_create_report"));
136+
137+
// This one should pass
138+
let reason = "another reason";
139+
let report = await reportPrivateMessage(
140+
beta,
141+
betaPm.private_message.id,
142+
reason,
143+
);
144+
145+
expect(report.private_message_report_view.private_message.id).toBe(
146+
betaPm.private_message.id,
147+
);
148+
expect(report.private_message_report_view.private_message_report.reason).toBe(
149+
reason,
150+
);
151+
});

api_tests/src/shared.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import {
44
BlockInstance,
55
BlockInstanceResponse,
66
CommunityId,
7+
CreatePrivateMessageReport,
78
GetReplies,
89
GetRepliesResponse,
910
GetUnreadCountResponse,
1011
InstanceId,
1112
LemmyHttp,
1213
PostView,
14+
PrivateMessageReportResponse,
1315
SuccessResponse,
1416
} from "lemmy-js-client";
1517
import { CreatePost } from "lemmy-js-client/dist/types/CreatePost";
@@ -781,6 +783,18 @@ export async function reportComment(
781783
return api.createCommentReport(form);
782784
}
783785

786+
export async function reportPrivateMessage(
787+
api: LemmyHttp,
788+
private_message_id: number,
789+
reason: string,
790+
): Promise<PrivateMessageReportResponse> {
791+
let form: CreatePrivateMessageReport = {
792+
private_message_id,
793+
reason,
794+
};
795+
return api.createPrivateMessageReport(form);
796+
}
797+
784798
export async function listCommentReports(
785799
api: LemmyHttp,
786800
): Promise<ListCommentReportsResponse> {

crates/api/src/private_message_report/create.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ pub async fn create_pm_report(
3131
let private_message_id = data.private_message_id;
3232
let private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
3333

34+
// Make sure that only the recipient of the private message can create a report
35+
if person_id != private_message.recipient_id {
36+
Err(LemmyErrorType::CouldntCreateReport)?
37+
}
38+
3439
let report_form = PrivateMessageReportForm {
3540
creator_id: person_id,
3641
private_message_id,

0 commit comments

Comments
 (0)