Skip to content

Commit

Permalink
Merge pull request #1543 from nextcloud/eng/noid/favourites
Browse files Browse the repository at this point in the history
favourites and notifications
  • Loading branch information
ArtificialOwl authored Dec 3, 2022
2 parents 6a3f569 + 0ebc496 commit ca96750
Show file tree
Hide file tree
Showing 21 changed files with 416 additions and 134 deletions.
3 changes: 2 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@
['name' => 'Api#instance', 'url' => '/api/v1/instance/', 'verb' => 'GET'],
['name' => 'Api#customEmojis', 'url' => '/api/v1/custom_emojis', 'verb' => 'GET'],
['name' => 'Api#savedSearches', 'url' => '/api/saved_searches/list.json', 'verb' => 'GET'],
['name' => 'Api#timelines', 'url' => '/api/v1/timelines/{timeline}/', 'verb' => 'GET'],
['name' => 'Api#favourites', 'url' => '/api/v1/favourites/', 'verb' => 'GET'],
['name' => 'Api#notifications', 'url' => '/api/v1/notifications', 'verb' => 'GET'],
['name' => 'Api#tag', 'url' => '/api/v1/timelines/tag/{hashtag}', 'verb' => 'GET'],
['name' => 'Api#statusNew', 'url' => '/api/v1/statuses', 'verb' => 'POST'],

// Api for local front-end
Expand Down
9 changes: 0 additions & 9 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,16 @@

namespace OCA\Social\AppInfo;

use Closure;
use OCA\Social\Notification\Notifier;
use OCA\Social\Search\UnifiedSearchProvider;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\UpdateService;
use OCA\Social\WellKnown\WebfingerHandler;
use OCA\Social\Listeners\ProfileSectionListener;
use OCA\Social\Dashboard\SocialWidget;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\QueryException;
use OCP\Profile\BeforeTemplateRenderedEvent;
use OCP\IDBConnection;
use OCP\IServerContainer;
use OC\DB\SchemaWrapper;
use OCP\DB\ISchemaWrapper;
use Throwable;

require_once __DIR__ . '/../../vendor/autoload.php';

Expand Down
1 change: 1 addition & 0 deletions lib/Command/ExtendedBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ protected function outputActor(Person $actor): void {
protected function outputStreams(array $streams) {
if ($this->asJson) {
$this->output->writeln(json_encode($streams, JSON_PRETTY_PRINT));
return;
}

$table = new Table($this->output);
Expand Down
55 changes: 12 additions & 43 deletions lib/Command/Timeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@

use Exception;
use OCA\Social\Db\StreamRequest;
use OCA\Social\Exceptions\UnknownTimelineException;
use OCA\Social\Model\ActivityPub\Stream;
use OCA\Social\Model\Client\Options\TimelineOptions;
use OCA\Social\Service\AccountService;
use OCA\Social\Service\ConfigService;
use OCA\Social\Tools\Exceptions\DateTimeException;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -88,15 +86,15 @@ public function __construct(
*/
protected function configure() {
parent::configure();
$this->setName('social:stream')
$this->setName('social:timeline')
->addArgument('userId', InputArgument::REQUIRED, 'viewer')
->addArgument('timeline', InputArgument::REQUIRED, 'timeline')
->addOption('local', '', InputOption::VALUE_NONE, 'public')
->addOption('count', '', InputOption::VALUE_REQUIRED, 'number of elements', '5')
->addOption('min_id', '', InputOption::VALUE_REQUIRED, 'min_id', 0)
->addOption('max_id', '', InputOption::VALUE_REQUIRED, 'max_id', 0)
->addOption('since', '', InputOption::VALUE_REQUIRED, 'since', 0)
->addOption('limit', '', InputOption::VALUE_REQUIRED, 'limit', 5)
->addOption('crop', '', InputOption::VALUE_REQUIRED, 'crop', 0)
->addOption('json', '', InputOption::VALUE_NONE, 'return JSON format')
->setDescription('Get stream by timeline and viewer');
}

Expand All @@ -111,7 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output = new ConsoleOutput();
$this->output = $output->section();

$this->asJson = $input->getOption('json');
$this->asJson = (strtolower($input->getOption('output')) === 'json');
$this->crop = intval($input->getOption('crop'));

$userId = $input->getArgument('userId');
Expand All @@ -129,46 +127,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$options = new TimelineOptions();
$options->setFormat(Stream::FORMAT_LOCAL);
$options->setLimit(intval($input->getOption('count')))
$options->setLimit(intval($input->getOption('limit')))
->setMinId(intval($input->getOption('min_id')))
->setMaxId(intval($input->getOption('max_id')));

try {
if ($input->getOption('local')) {
$options->setLocal(true);
}
$options->setTimeline($timeline = $input->getArgument('timeline'));
$this->outputStreams($this->streamRequest->getTimeline($options));
} catch (UnknownTimelineException $e) {
$this->displayUnsupportedStream($options);
->setMaxId(intval($input->getOption('max_id')))
->setSince(intval($input->getOption('since')));

if ($input->getOption('local')) {
$options->setLocal(true);
}
$options->setTimeline($input->getArgument('timeline'));
$this->outputStreams($this->streamRequest->getTimeline($options));

return 0;
}


/**
* @param TimelineOptions $options
*
* @throws DateTimeException
*/
private function displayUnsupportedStream(TimelineOptions $options) {
switch ($options->getTimeline()) {
case 'notifications':
$stream = $this->streamRequest->getTimelineNotifications(0, $options->getLimit());
$this->outputStreams($stream);
break;

case 'liked':
$stream = $this->streamRequest->getTimelineLiked(0, $options->getLimit());
$this->outputStreams($stream);
break;

default:
throw new Exception(
'Unknown timeline. Try ' . implode(', ', TimelineOptions::$availableTimelines)
. ', direct, notifications, liked'
);
}
}
}
2 changes: 1 addition & 1 deletion lib/Controller/ActivityPubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\StreamNotFoundException;
use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Model\ActivityPub\Activity\Delete;
use OCA\Social\Service\AccountService;
use OCA\Social\Service\CacheActorService;
use OCA\Social\Service\ConfigService;
Expand Down Expand Up @@ -76,6 +75,7 @@ class ActivityPubController extends Controller {
private FollowService $followService;
private StreamService $streamService;
private ConfigService $configService;
private LoggerInterface $logger;

public function __construct(
IRequest $request,
Expand Down
144 changes: 121 additions & 23 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,6 @@ public function savedSearches(): DataResponse {
}


/**
* @NoCSRFRequired
* @PublicPage
*
* @return DataResponse
*/
public function notifications(): DataResponse {
try {
$this->initViewer(true);

return new DataResponse([], Http::STATUS_OK);
} catch (Exception $e) {
return $this->error($e->getMessage());
}
}


/**
* @NoCSRFRequired
* @PublicPage
Expand Down Expand Up @@ -273,18 +256,133 @@ public function timelines(
bool $local = false,
int $limit = 20,
int $max_id = 0,
int $min_id = 0
int $min_id = 0,
int $since = 0
): DataResponse {
try {
$this->initViewer(true);

$options = new TimelineOptions($this->request);
$options->setFormat(ACore::FORMAT_LOCAL);
$options->setTimeline($timeline)
->setLocal($local)
->setLimit($limit)
->setMaxId($max_id)
->setMinId($min_id)
->setSince($since);

$posts = $this->streamService->getTimeline($options);

return new DataResponse($posts, Http::STATUS_OK);
} catch (Exception $e) {
return $this->error($e->getMessage());
}
}


/**
* @NoCSRFRequired
* @PublicPage
*
* @param int $limit
* @param int $max_id
* @param int $min_id
* @param int $since
*
* @return DataResponse
*/
public function favourites(
int $limit = 20,
int $max_id = 0,
int $min_id = 0,
int $since = 0
): DataResponse {
try {
$this->initViewer(true);

$options = new TimelineOptions($this->request);
$options->setFormat(ACore::FORMAT_LOCAL);
$options->setTimeline(TimelineOptions::TIMELINE_FAVOURITES)
->setLimit($limit)
->setMaxId($max_id)
->setMinId($min_id)
->setSince($since);

$posts = $this->streamService->getTimeline($options);

return new DataResponse($posts, Http::STATUS_OK);
} catch (Exception $e) {
return $this->error($e->getMessage());
}
}


/**
* @NoCSRFRequired
* @PublicPage
*
* @return DataResponse
*/
public function notifications(
int $limit = 20,
int $max_id = 0,
int $min_id = 0,
int $since = 0,
array $types = [],
array $exclude_types = [],
string $accountId = ''
): DataResponse {
try {
$this->initViewer(true);

$options = new TimelineOptions($this->request);
$options->setFormat(ACore::FORMAT_LOCAL);
$options->setTimeline(TimelineOptions::TIMELINE_NOTIFICATIONS)
->setLimit($limit)
->setMaxId($max_id)
->setMinId($min_id)
->setSince($since)
->setTypes($types)
->setExcludeTypes($exclude_types)
->setAccountId($accountId);

$posts = $this->streamService->getTimeline($options);

return new DataResponse($posts, Http::STATUS_OK);
} catch (Exception $e) {
return $this->error($e->getMessage());
}
}


/**
* @NoCSRFRequired
* @PublicPage
*
* @return DataResponse
*/
public function tag(
string $hashtag,
int $limit = 20,
int $max_id = 0,
int $min_id = 0,
int $since = 0,
bool $local = false,
bool $only_media = false
): DataResponse {
try {
$this->initViewer(true);

$options = new TimelineOptions($this->request);
$options->setFormat(ACore::FORMAT_LOCAL);
$options->setTimeline($timeline);
$options->setLocal($local);
$options->setLimit($limit);
$options->setMaxId($max_id);
$options->setMinId($min_id);
$options->setTimeline('hashtag')
->setLimit($limit)
->setMaxId($max_id)
->setMinId($min_id)
->setSince($since)
->setLocal($local)
->setOnlyMedia($only_media)
->setArgument($hashtag);

$posts = $this->streamService->getTimeline($options);

Expand Down
1 change: 0 additions & 1 deletion lib/Controller/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use OCA\Social\Exceptions\InstanceDoesNotExistException;
use OCA\Social\Model\Client\SocialClient;
use OCA\Social\Service\AccountService;
use OCA\Social\Service\CacheActorService;
use OCA\Social\Service\ClientService;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\InstanceService;
Expand Down
7 changes: 3 additions & 4 deletions lib/Db/ActorsRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use OCA\Social\Tools\Traits\TArrayTools;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCP\DB\QueryBuilder\IQueryBuilder;

class ActorsRequestBuilder extends CoreRequestBuilder {
use TArrayTools;
Expand All @@ -42,7 +41,7 @@ class ActorsRequestBuilder extends CoreRequestBuilder {
/**
* Base of the Sql Insert request
*
* @return IQueryBuilder
* @return SocialQueryBuilder
*/
protected function getActorsInsertSql(): SocialQueryBuilder {
$qb = $this->getQueryBuilder();
Expand All @@ -55,7 +54,7 @@ protected function getActorsInsertSql(): SocialQueryBuilder {
/**
* Base of the Sql Update request
*
* @return IQueryBuilder
* @return SocialQueryBuilder
*/
protected function getActorsUpdateSql(): SocialQueryBuilder {
$qb = $this->getQueryBuilder();
Expand Down Expand Up @@ -90,7 +89,7 @@ protected function getActorsSelectSql(): SocialQueryBuilder {
/**
* Base of the Sql Delete request
*
* @return IQueryBuilder
* @return SocialQueryBuilder
*/
protected function getActorsDeleteSql(): SocialQueryBuilder {
$qb = $this->getQueryBuilder();
Expand Down
Loading

0 comments on commit ca96750

Please sign in to comment.