From 253a4b4b2b5f8a8fcaa89007e92057bbe71873b9 Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Sun, 3 Mar 2024 12:48:09 +1100 Subject: [PATCH] more poll issues --- Code/Lib/Activity.php | 154 ++++++++++++++++++++++-------------------- include/items.php | 6 +- 2 files changed, 84 insertions(+), 76 deletions(-) diff --git a/Code/Lib/Activity.php b/Code/Lib/Activity.php index 6e2135e61..42c1555fa 100644 --- a/Code/Lib/Activity.php +++ b/Code/Lib/Activity.php @@ -2785,130 +2785,134 @@ class Activity return '@{' . $id . '}'; } - public static function update_poll($item, $post) + public static function update_poll($pollItem, $response) { logger('updating poll'); $multi = false; - $mid = $post['mid']; - $content = trim($post['title']); - if (!$item) { + + if (!$pollItem) { logger('no item'); return false; } - $channel = Channel::from_id($item['uid']); + $channel = Channel::from_id($pollItem['uid']); if (!$channel) { logger('no channel'); return false; } - $o = json_decode($item['obj'], true); + $o = json_decode($pollItem['obj'], true); if ($o && array_key_exists('anyOf', $o)) { $multi = true; } - $r = q( - "select mid, title from item where parent_mid = '%s' and author_xchan = '%s' and mid != parent_mid ", - dbesc($item['mid']), - dbesc($post['author_xchan']) - ); + if ($response) { + $mid = $response['mid']; + $content = trim($response['title']); - // prevent any duplicate votes by same author for oneOf and duplicate votes with same author and same answer for anyOf - if ($r) { + $r = q( + "select mid, title from item where parent_mid = '%s' and author_xchan = '%s' and mid != parent_mid ", + dbesc($pollItem['mid']), + dbesc($response['author_xchan']) + ); + + // prevent any duplicate votes by same author for oneOf and duplicate votes with same author and same answer for anyOf + + if ($r) { + if ($multi) { + foreach ($r as $rv) { + if (trim($rv['title']) === $content && $rv['mid'] !== $mid) { + logger('already voted multi'); + return false; + } + } + } else { + foreach ($r as $rv) { + if ($rv['mid'] !== $mid && $content) { + logger('already voted'); + return false; + } + } + } + } + + $answer_found = false; + $foundPrevious = false; if ($multi) { - foreach ($r as $rv) { - if (trim($rv['title']) === $content && $rv['mid'] !== $mid) { - logger('already voted multi'); - return false; + for ($c = 0; $c < count($o['anyOf']); $c++) { + if (trim($o['anyOf'][$c]['name']) === $content) { + $answer_found = true; + if (is_array($o['anyOf'][$c]['replies'])) { + foreach ($o['anyOf'][$c]['replies'] as $reply) { + if (is_array($reply) && array_key_exists('id', $reply) && $reply['id'] === $mid) { + $foundPrevious = true; + } + } + } + + if (!$foundPrevious) { + $o['anyOf'][$c]['replies']['totalItems']++; + $o['anyOf'][$c]['replies']['items'][] = ['id' => $mid, 'type' => 'Note']; + } } } } else { - foreach ($r as $rv) { - if ($rv['mid'] !== $mid && $content) { - logger('already voted'); - return false; + for ($c = 0; $c < count($o['oneOf']); $c++) { + if (trim($o['oneOf'][$c]['name']) === $content) { + $answer_found = true; + if (is_array($o['oneOf'][$c]['replies'])) { + foreach ($o['oneOf'][$c]['replies'] as $reply) { + if (is_array($reply) && array_key_exists('id', $reply) && $reply['id'] === $mid) { + $foundPrevious = true; + } + } + } + + if (!$foundPrevious) { + $o['oneOf'][$c]['replies']['totalItems']++; + $o['oneOf'][$c]['replies']['items'][] = ['id' => $mid, 'type' => 'Note']; + } } } } } - - $answer_found = false; - $foundPrevious = false; - if ($multi) { - for ($c = 0; $c < count($o['anyOf']); $c++) { - if (trim($o['anyOf'][$c]['name']) === $content) { - $answer_found = true; - if (is_array($o['anyOf'][$c]['replies'])) { - foreach ($o['anyOf'][$c]['replies'] as $reply) { - if (is_array($reply) && array_key_exists('id', $reply) && $reply['id'] === $mid) { - $foundPrevious = true; - } - } - } - - if (!$foundPrevious) { - $o['anyOf'][$c]['replies']['totalItems']++; - $o['anyOf'][$c]['replies']['items'][] = ['id' => $mid, 'type' => 'Note']; - } - } - } - } else { - for ($c = 0; $c < count($o['oneOf']); $c++) { - if (trim($o['oneOf'][$c]['name']) === $content) { - $answer_found = true; - if (is_array($o['oneOf'][$c]['replies'])) { - foreach ($o['oneOf'][$c]['replies'] as $reply) { - if (is_array($reply) && array_key_exists('id', $reply) && $reply['id'] === $mid) { - $foundPrevious = true; - } - } - } - - if (!$foundPrevious) { - $o['oneOf'][$c]['replies']['totalItems']++; - $o['oneOf'][$c]['replies']['items'][] = ['id' => $mid, 'type' => 'Note']; - } - } - } - } - - if ($item['comments_closed'] > NULL_DATE) { - if ($item['comments_closed'] > datetime_convert()) { - $o['closed'] = datetime_convert('UTC', 'UTC', $item['comments_closed'], ATOM_TIME); + if ($pollItem['comments_closed'] > NULL_DATE) { + if ($pollItem['comments_closed'] > datetime_convert()) { + $o['closed'] = datetime_convert('UTC', 'UTC', $pollItem['comments_closed'], ATOM_TIME); // set this to force an update $answer_found = true; } } logger('updated_poll: ' . print_r($o, true), LOGGER_DATA); - if ($answer_found && !$foundPrevious) { + if ($response && $answer_found && !$foundPrevious) { // undo moderation if it was applied. q( "update item set obj = '%s', edited = '%s', item_blocked = 0 where id = %d", dbesc(json_encode($o)), dbesc(datetime_convert()), - intval($item['id']) + intval($pollItem['id']) ); $i = q("select * from item where id = %d", - intval($item['id']) + intval($pollItem['id']) ); xchan_query($i, true); $items = fetch_post_tags($i); - $item = array_shift($items); - $item['obj'] = json_decode($item['obj'],true); - $item['target'] = json_decode($item['target'], true); - $item['attach'] = json_decode($item['attach'], true); + $pollItem = array_shift($items); + $pollItem['obj'] = json_decode($pollItem['obj'],true); + $pollItem['target'] = json_decode($pollItem['target'], true); + $pollItem['attach'] = json_decode($pollItem['attach'], true); - Run::Summon(['Notifier', 'edit_post', $item['id']]); - $related = find_related($item); + Run::Summon(['Notifier', 'edit_post', $pollItem['id']]); + $related = find_related($pollItem); // logger('related: ' . print_r($related,true), LOGGER_DATA); if ($related) { // Update the object in the collection activity and the timestamp - $newObj = array_merge(Activity::ap_context(), Activity::encode_activity($item)); + $newObj = array_merge(Activity::ap_context(), Activity::encode_activity($pollItem)); $newObj['proof'] = (new JcsEddsa2022())->sign($newObj, $channel); $r = q("update item set obj = '%s', edited = '%s' where id = %d", dbesc(json_encode($newObj)), diff --git a/include/items.php b/include/items.php index 64045514b..827b231e1 100644 --- a/include/items.php +++ b/include/items.php @@ -1738,7 +1738,7 @@ function item_store($arr, $deliver = true, $addAndSync = true) { if(intval($parent_item['item_uplink']) && (! $parent_item['item_private'])) $arr['item_private'] = 0; - if(in_array($arr['obj_type'], ['Note','Answer']) && $parent_item['obj_type'] === 'Question' && intval($parent_item['item_wall'])) { + if(in_array($arr['obj_type'], ['Note','Answer']) && $parent_item['obj_type'] === 'Question') { Activity::update_poll($parent_item, $arr); } } @@ -2249,6 +2249,10 @@ function item_store_update($arr, $deliver = true, $addAndSync = true) { return $ret; } + if($arr['obj_type'] === 'Question') { + Activity::update_poll($parent_item, NULL); + } + // fetch an unescaped complete copy of the stored item $r = q("select * from item where id = %d",