streams/Code/Module/Pin.php

69 lines
1.6 KiB
PHP
Raw Normal View History

2019-12-03 02:44:32 +00:00
<?php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Module;
2019-12-03 02:44:32 +00:00
/*
* Pinned post processing
*/
use App;
2022-02-16 04:08:28 +00:00
use Code\Lib\Libsync;
use Code\Lib\PConfig;
use Code\Web\Controller;
2019-12-03 02:44:32 +00:00
2021-12-02 23:02:31 +00:00
class Pin extends Controller
{
public function init()
{
2021-12-03 03:01:39 +00:00
if (argc() !== 1) {
2021-12-02 23:02:31 +00:00
http_status_exit(400, 'Bad request');
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
if (!local_channel()) {
2021-12-02 23:02:31 +00:00
http_status_exit(403, 'Forbidden');
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
public function post()
{
$item_id = intval(str_replace('pin-', '', $_POST['id']));
2021-12-02 23:02:31 +00:00
if ($item_id <= 0) {
http_status_exit(404, 'Not found');
}
$channel = App::get_channel();
2021-12-03 03:01:39 +00:00
$r = q(
"SELECT * FROM item WHERE id = %d AND id = parent AND uid = %d AND owner_xchan = '%s' AND item_private = 0 LIMIT 1",
2021-12-02 23:02:31 +00:00
intval($item_id),
intval($channel['channel_id']),
dbesc($channel['channel_hash'])
);
if (!$r) {
notice(t('Unable to locate original post.'));
http_status_exit(404, 'Not found');
} else {
$pinned = PConfig::Get($channel['channel_id'], 'pinned', $r[0]['item_type'], []);
if (in_array($r[0]['mid'], $pinned)) {
$narr = [];
foreach ($pinned as $p) {
if ($p !== $r[0]['mid']) {
2023-06-04 19:38:16 +00:00
$narr[] = $p;
2021-12-02 23:02:31 +00:00
}
}
} else {
$narr = $pinned;
$narr[] = $r[0]['mid'];
}
PConfig::Set($channel['channel_id'], 'pinned', $r[0]['item_type'], $narr);
Libsync::build_sync_packet($channel['channel_id']);
}
}
2019-12-03 02:44:32 +00:00
}