forked from leftypol/leftypol
post.php: use modern matrix API
This commit is contained in:
parent
20a30f2661
commit
4d62dcd9a2
1 changed files with 26 additions and 7 deletions
33
post.php
33
post.php
|
@ -244,19 +244,29 @@ function send_matrix_report(
|
||||||
$post_id = $post['thread'] ? $post['thread'] : $id;
|
$post_id = $post['thread'] ? $post['thread'] : $id;
|
||||||
|
|
||||||
$reported_post_url = "$domain/mod.php?/{$board_dir}{$board_res_dir}{$post_id}.html";
|
$reported_post_url = "$domain/mod.php?/{$board_dir}{$board_res_dir}{$post_id}.html";
|
||||||
$post_url = "$matrix_host/_matrix/client/r0/rooms/$room_id/send/m.room.message?access_token=$access_token";
|
|
||||||
|
|
||||||
$end = strlen($post['body_nomarkup']) > $max_msg_len ? ' [...]' : '';
|
$end = strlen($post['body_nomarkup']) > $max_msg_len ? ' [...]' : '';
|
||||||
$post_content = mb_substr($post['body_nomarkup'], 0, $max_msg_len) . $end;
|
$post_content = mb_substr($post['body_nomarkup'], 0, $max_msg_len) . $end;
|
||||||
$matrix_message = $reported_post_url . ($post['thread'] ? "#$post_id" : '') . " \nReason:\n" . $report_reason . " \nPost:\n" . $post_content . " \n";
|
$text_body = $reported_post_url . ($post['thread'] ? "#$post_id" : '') . " \nReason:\n" . $report_reason . " \nPost:\n" . $post_content . " \n";
|
||||||
$post_data = json_encode([
|
|
||||||
|
$random_transaction_id = mt_rand();
|
||||||
|
$json_body = json_encode([
|
||||||
'msgtype' => 'm.text',
|
'msgtype' => 'm.text',
|
||||||
'body' => $matrix_message
|
'body' => $text_body
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$ch = curl_init($post_url);
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
|
curl_setopt_array($ch, [
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
CURLOPT_URL => "$matrix_host/_matrix/client/v3/rooms/$room_id/send/m.room.message/$random_transaction_id",
|
||||||
|
CURLOPT_CUSTOMREQUEST => 'PUT',
|
||||||
|
CURLOPT_HTTPHEADER => [
|
||||||
|
'Content-Type: application/json',
|
||||||
|
"Authorization: Bearer $access_token"
|
||||||
|
],
|
||||||
|
CURLOPT_POSTFIELDS => $json_body,
|
||||||
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
|
CURLOPT_TIMEOUT => 3,
|
||||||
|
]);
|
||||||
$c_ret = curl_exec($ch);
|
$c_ret = curl_exec($ch);
|
||||||
if ($c_ret === false) {
|
if ($c_ret === false) {
|
||||||
$err_no = curl_errno($ch);
|
$err_no = curl_errno($ch);
|
||||||
|
@ -266,6 +276,15 @@ function send_matrix_report(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
|
$json = json_decode($c_ret, true);
|
||||||
|
if ($json === null) {
|
||||||
|
error_log("Report forwarding failed, matrix returned a non-json value");
|
||||||
|
} elseif (!isset($json["event_id"])) {
|
||||||
|
$code = $json["errcode"] ?? '';
|
||||||
|
$desc = $json["error"] ?? '';
|
||||||
|
error_log("Report forwarding failed, matrix returned code '$code', with description '$desc'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue