friendica/src/Module/Api/Mastodon/Conversations.php

101 lines
3 KiB
PHP
Raw Normal View History

2021-05-19 23:56:50 +02:00
<?php
/**
2022-01-02 08:27:47 +01:00
* @copyright Copyright (C) 2010-2022, the Friendica project
2021-05-19 23:56:50 +02:00
*
* @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\Module\Api\Mastodon;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/timelines/conversations/
*/
2021-05-21 20:00:03 +02:00
class Conversations extends BaseApi
2021-05-19 23:56:50 +02:00
{
protected function delete(array $request = [])
2021-05-19 23:56:50 +02:00
{
self::checkAllowedScope(self::SCOPE_WRITE);
2021-05-19 23:56:50 +02:00
$uid = self::getCurrentUserID();
if (!empty($this->parameters['id'])) {
2021-05-19 23:56:50 +02:00
DI::mstdnError()->UnprocessableEntity();
}
2021-05-20 00:10:05 +02:00
DBA::delete('conv', ['id' => $this->parameters['id'], 'uid' => $uid]);
DBA::delete('mail', ['convid' => $this->parameters['id'], 'uid' => $uid]);
2021-05-19 23:56:50 +02:00
System::jsonExit([]);
}
/**
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
protected function rawContent(array $request = [])
2021-05-19 23:56:50 +02:00
{
self::checkAllowedScope(self::SCOPE_READ);
2021-05-19 23:56:50 +02:00
$uid = self::getCurrentUserID();
$request = $this->getRequest([
2021-05-19 23:56:50 +02:00
'limit' => 20, // Maximum number of results. Defaults to 20. Max 40.
'max_id' => 0, // Return results older than this ID. Use HTTP Link header to paginate.
'since_id' => 0, // Return results newer than this ID. Use HTTP Link header to paginate.
'min_id' => 0, // Return results immediately newer than this ID. Use HTTP Link header to paginate.
], $request);
2021-05-19 23:56:50 +02:00
$params = ['order' => ['id' => true], 'limit' => $request['limit']];
2021-05-20 00:33:40 +02:00
$condition = ['uid' => $uid];
if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
}
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
}
if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
2021-05-20 05:53:48 +02:00
2021-05-20 00:33:40 +02:00
$params['order'] = ['id'];
}
$convs = DBA::select('conv', ['id'], $condition, $params);
2021-05-20 00:10:05 +02:00
2021-05-19 23:56:50 +02:00
$conversations = [];
2021-05-20 06:39:45 +02:00
while ($conv = DBA::fetch($convs)) {
2021-06-16 17:02:33 +02:00
self::setBoundaries($conv['id']);
2021-05-19 23:56:50 +02:00
$conversations[] = DI::mstdnConversation()->CreateFromConvId($conv['id']);
}
2021-05-20 06:39:45 +02:00
DBA::close($convs);
2021-05-20 00:33:40 +02:00
if (!empty($request['min_id'])) {
$conversations = array_reverse($conversations);
2021-05-20 00:33:40 +02:00
}
2021-06-16 17:02:33 +02:00
self::setLinkHeader();
2021-05-19 23:56:50 +02:00
System::jsonExit($conversations);
}
}