We can now store incoming questions

This commit is contained in:
Michael 2022-04-20 06:28:02 +00:00
parent 8dbd1d0e52
commit 834844573b
10 changed files with 318 additions and 6 deletions

View file

@ -0,0 +1,57 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Model\Post;
use \BadMethodCallException;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
class Question
{
/**
* Update a post question entry
*
* @param integer $uri_id
* @param array $data
* @param bool $insert_if_missing
* @return bool
* @throws \Exception
*/
public static function update(int $uri_id, array $data = [], bool $insert_if_missing = true)
{
if (empty($uri_id)) {
throw new BadMethodCallException('Empty URI_id');
}
$fields = DBStructure::getFieldsForTable('post-question', $data);
// Remove the key fields
unset($fields['uri-id']);
if (empty($fields)) {
return true;
}
return DBA::update('post-question', $fields, ['uri-id' => $uri_id], $insert_if_missing ? true : []);
}
}

View file

@ -0,0 +1,59 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Model\Post;
use \BadMethodCallException;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
class QuestionOption
{
/**
* Update a post question-option entry
*
* @param integer $uri_id
* @param integer $id
* @param array $data
* @param bool $insert_if_missing
* @return bool
* @throws \Exception
*/
public static function update(int $uri_id, int $id, array $data = [], bool $insert_if_missing = true)
{
if (empty($uri_id)) {
throw new BadMethodCallException('Empty URI_id');
}
$fields = DBStructure::getFieldsForTable('post-question-option', $data);
// Remove the key fields
unset($fields['uri-id']);
unset($fields['id']);
if (empty($fields)) {
return true;
}
return DBA::update('post-question-option', $fields, ['uri-id' => $uri_id, 'id' => $id], $insert_if_missing ? true : []);
}
}

View file

@ -152,6 +152,37 @@ class Processor
}
}
/**
* Store attachment data
*
* @param array $activity
* @param array $item
*/
private static function storeQuestion($activity, $item)
{
if (empty($activity['question'])) {
return;
}
$question = ['multiple' => $activity['question']['multiple']];
if (!empty($activity['question']['voters'])) {
$question['voters'] = $activity['question']['voters'];
}
if (!empty($activity['question']['end-time'])) {
$question['end-time'] = $activity['question']['end-time'];
}
Post\Question::update($item['uri-id'], $question);
foreach ($activity['question']['options'] as $key => $option) {
$option = ['name' => $option['name'], 'replies' => $option['replies']];
Post\QuestionOption::update($item['uri-id'], $key, $option);
}
Logger::debug('Storing incoming question', ['type' => $activity['type'], 'uri-id' => $item['uri-id'], 'question' => $activity['question']]);
}
/**
* Updates a message
*
@ -178,6 +209,7 @@ class Processor
$item = self::processContent($activity, $item);
self::storeAttachments($activity, $item);
self::storeQuestion($activity, $item);
if (empty($item)) {
return;
@ -347,6 +379,7 @@ class Processor
$item['plink'] = $activity['alternate-url'] ?? $item['uri'];
self::storeAttachments($activity, $item);
self::storeQuestion($activity, $item);
// We received the post via AP, so we set the protocol of the server to AP
$contact = Contact::getById($item['author-id'], ['gsid']);

View file

@ -553,12 +553,13 @@ class Receiver
$object_data['from-relay'] = $activity['from-relay'];
}
if (in_array('as:Question', [$object_data['object_type'] ?? '', $object_data['object_object_type'] ?? ''])) {
self::storeUnhandledActivity(false, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer);
}
switch ($type) {
case 'as:Create':
if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
if ($object_data['object_type'] == 'as:Question') {
self::storeUnhandledActivity(false, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer);
}
$item = ActivityPub\Processor::createItem($object_data);
ActivityPub\Processor::postItem($object_data, $item);
} elseif (in_array($object_data['object_type'], ['pt:CacheFile'])) {
@ -1460,7 +1461,13 @@ class Receiver
return [];
}
// @todo Check if "closed" is a thing, see here: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-closed
$closed = JsonLD::fetchElement($object, 'as:closed', '@value');
if (!empty($closed)) {
$question['end-time'] = $closed;
} else {
$question['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
}
$question['voters'] = (int)JsonLD::fetchElement($object, 'toot:votersCount', '@value');
$question['options'] = [];