streams/Code/Module/Vote.php

136 lines
3.9 KiB
PHP
Raw Normal View History

2020-01-27 23:54:43 +00:00
<?php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Module;
2020-01-27 23:54:43 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Web\Controller;
use Code\Lib\Activity;
use Code\Daemon\Run;
use Code\Lib\Libsync;
use Code\Lib\Channel;
2020-01-27 23:54:43 +00:00
2021-12-02 23:02:31 +00:00
class Vote extends Controller
{
public function init()
{
$ret = ['success' => false, 'message' => EMPTY_STR];
$channel = App::get_channel();
if (!$channel) {
$ret['message'] = t('Permission denied.');
json_return_and_die($ret);
}
$fetch = null;
$id = argv(1);
$response = $_REQUEST['answer'];
if ($id) {
2021-12-03 03:01:39 +00:00
$fetch = q(
"select * from item where id = %d limit 1",
2021-12-02 23:02:31 +00:00
intval($id)
);
}
if ($fetch && $fetch[0]['obj_type'] === 'Question') {
$obj = json_decode($fetch[0]['obj'], true);
} else {
$ret['message'] = t('Poll not found.');
json_return_and_die($ret);
}
$valid = false;
if ($obj['oneOf']) {
foreach ($obj['oneOf'] as $selection) {
2022-12-20 18:58:59 +00:00
// logger('selection: ' . '"' . $selection['name'] . '"');
// logger('response: ' . '"' . $response . '"');
if ($selection['name'] && trim($selection['name']) === trim($response)) {
2021-12-02 23:02:31 +00:00
$valid = true;
}
}
}
$choices = [];
if ($obj['anyOf']) {
foreach ($obj['anyOf'] as $selection) {
$choices[] = $selection['name'];
}
foreach ($response as $res) {
if (!in_array($res, $choices)) {
$valid = false;
break;
}
$valid = true;
}
}
if (!$valid) {
$ret['message'] = t('Invalid response.');
json_return_and_die($ret);
}
if (!is_array($response)) {
$response = [$response];
}
foreach ($response as $res) {
$item = [];
$item['aid'] = $channel['channel_account_id'];
$item['uid'] = $channel['channel_id'];
$item['item_origin'] = true;
$item['parent'] = $fetch[0]['id'];
$item['parent_mid'] = $fetch[0]['mid'];
$item['thr_parent'] = $fetch[0]['mid'];
$item['uuid'] = new_uuid();
$item['mid'] = z_root() . '/item/' . $item['uuid'];
$item['verb'] = 'Create';
$item['title'] = $res;
$item['author_xchan'] = $channel['channel_hash'];
$item['owner_xchan'] = $fetch[0]['author_xchan'];
2021-12-03 03:01:39 +00:00
// $item['allow_cid'] = '<' . $fetch[0]['author_xchan'] . '>';
// $item['item_private'] = 1;
2021-12-02 23:02:31 +00:00
// These two are placeholder values that will be reset after
// we encode the item
$item['obj_type'] = 'Note';
2022-01-25 01:26:12 +00:00
$item['author'] = Channel::from_id($channel['channel_id']);
2021-12-02 23:02:31 +00:00
$item['obj'] = Activity::encode_item($item, ((get_config('system', 'activitypub', ACTIVITYPUB_ENABLED)) ? true : false));
// now reset the placeholders
$item['obj_type'] = 'Answer';
unset($item['author']);
$x = item_store($item);
retain_item($fetch[0]['id']);
if ($x['success']) {
$itemid = $x['item_id'];
Run::Summon(['Notifier', 'like', $itemid]);
}
2021-12-03 03:01:39 +00:00
$r = q(
"select * from item where id = %d",
2021-12-02 23:02:31 +00:00
intval($itemid)
);
if ($r) {
xchan_query($r);
$sync_item = fetch_post_tags($r);
Libsync::build_sync_packet($channel['channel_id'], ['item' => [encode_item($sync_item[0], true)]]);
}
}
$ret['success'] = true;
$ret['message'] = t('Response submitted. Updates may not appear instantly.');
json_return_and_die($ret);
}
2020-01-27 23:54:43 +00:00
}