Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #16 from nextcloud/feature/noid/keyword-on-item
Browse files Browse the repository at this point in the history
keyword on iitem
  • Loading branch information
ArtificialOwl authored Sep 6, 2019
2 parents 17aacf9 + fc95586 commit d302e37
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 129 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,65 @@ If you want to create an object that is only broadcast to callbacks, you can cre



### Update an existing item

It is possible to create unique item that can be edited over time.

This is used in the Social App when a new account is following yours; the notification is identified by a **keyword** (here: _new_followers_).
Please note that the _Time To Live_ is set to `TTL_FEW_HOUR`
```php
$userId = 'cult';

$notification = new \OC\Push\Model\Helper\PushNotification('social', IPushItem::TTL_FEW_HOURS);
$notification->setTitle('Nextcloud Social');
$notification->setLevel(PushNotification::LEVEL_SUCCESS);
$notification->setMessage('accountA is following you');
$notification->addMetaArray('accounts', ['accountA']);
$notification->setKeyword('new_followers'); // identification of the PushItem
$notification->addUser($userId);

$pushHelper = $this->pushManager->getPushHelper();
$pushHelper->pushNotification($notification);
```


Now, before creating this notification, we should have checked that there is no already existing notifications about _new_followers_in the queue:



```php
$pushService = $this->pushManager->getPushService();
try {
$item = $pushService->getItemByKeyword('social', $userId, 'new_followers');
// already exists, get the current item, edit its content and save the edited object
} catch (ItemNotFoundException $e) {
// does not exists, create a new one.
}
```


If the precedent notification about _new_followers_ have been read by the recipient, we create a new one. If the recipient haven't read it, we can still edit the item:

```php
$item->addMetaArray('accounts', 'accountB');
$accounts = $item->getMeta('accounts');
$newMessage = implode(', ', $accounts) . ' are now following you');

$payload = $item->getPayload();
$payload['message'] = $newMessage;
$item->setPayload($payload);
$pushService = $this->pushManager->getPushService();
$pushService->update($item);
```

_Note about `push()` and `update()`_:
- `push()` will create a new item and erase any old item with the same keyword/userId/appId.
- `update()`will only update existing item. There is no confirmation if the item was already published before or not.


### yay, animation.

This is the Social App using the Push App to update its front-end when a new message is received.
![](https://raw.githubusercontent.com/nextcloud/push/master/documentations/SocialPush.gif)


71 changes: 71 additions & 0 deletions lib/Command/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,27 @@

use Exception;
use OC\Core\Command\Base;
use OC\Push\Model\Helper\PushNotification;
use OCA\Push\Service\ConfigService;
use OCA\Push\Service\MiscService;
use OCP\IUserManager;
use OCP\Push\Exceptions\ItemNotFoundException;
use OCP\Push\Exceptions\PushInstallException;
use OCP\Push\Exceptions\UnknownStreamTypeException;
use OCP\Push\IPushManager;
use OCP\Push\Model\IPushItem;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;


class Test extends Base {


/** @var OutputInterface */
private $output;

/** @var IUserManager */
private $userManager;

Expand Down Expand Up @@ -86,6 +95,9 @@ protected function configure() {
parent::configure();
$this->setName('push:test')
->addArgument('user', InputArgument::REQUIRED, 'user')
->addOption(
'keyword', 'k', InputOption::VALUE_REQUIRED, 'editable content, using keyword', ''
)
->setDescription('Nextcloud Push testing tools');
}

Expand All @@ -97,6 +109,8 @@ protected function configure() {
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$this->output = $output;

$userId = $input->getArgument('user');

$user = $this->userManager->get($userId);
Expand All @@ -108,9 +122,66 @@ protected function execute(InputInterface $input, OutputInterface $output) {
throw new Exception('Nextcloud Push is not available');
}

if (($keyword = $input->getOption('keyword')) !== '') {
$this->manageKeyword($userId, $keyword);

return;
}

$pushHelper = $this->pushManager->getPushHelper();
$pushHelper->test($userId);
}


/**
* @param string $userId
* @param string $keyword
*
* @throws PushInstallException
* @throws UnknownStreamTypeException
*/
private function manageKeyword(string $userId, string $keyword) {

if ($keyword === 'new') {
$notification = new PushNotification('push', IPushItem::TTL_FEW_HOURS);
$notification->setTitle('Testing Push');
$notification->setLevel(PushNotification::LEVEL_SUCCESS);
$notification->setKeyword('test');
$notification->setMessage("If you cannot see this, it means it is not working.");
$notification->addUser($userId);
$pushHelper = $this->pushManager->getPushHelper();
$pushHelper->pushNotification($notification);

return;
}

$pushService = $this->pushManager->getPushService();
try {
$item = $pushService->getItemByKeyword('push', $userId, 'test');
} catch (ItemNotFoundException $e) {
$this->output->writeln('Item not available anymore. Run ./occ push:test --keyword new');

return;
}

$this->output->writeln('Current Item: ');
$this->output->writeln(json_encode($item, JSON_PRETTY_PRINT));

$payload = $item->getPayload();
$payload['message'] = $keyword;
$item->setPayload($payload);
$pushService->update($item);

try {
$item = $pushService->getItemByKeyword('push', $userId, 'test');
$this->output->writeln('');
$this->output->writeln('New Item: ');

$this->output->writeln(json_encode($item, JSON_PRETTY_PRINT));
} catch (ItemNotFoundException $e) {
} catch (UnknownStreamTypeException $e) {
}
}

}

2 changes: 1 addition & 1 deletion lib/Controller/PushController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
namespace OCA\Push\Controller;


use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use daita\NcSmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use Exception;
use OCA\Push\AppInfo\Application;
use OCA\Push\Model\Polling;
Expand Down
10 changes: 9 additions & 1 deletion lib/Db/CoreRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@


use OC;
use OCA\Push\Service\ConfigService;
use OCA\Push\Service\MiscService;
use OCP\IConfig;
use OCP\IDBConnection;
Expand All @@ -54,6 +55,9 @@ class CoreRequestBuilder {
/** @var IDBConnection */
protected $dbConnection;

/** @var ConfigService */
protected $configService;

/** @var MiscService */
protected $miscService;

Expand All @@ -62,12 +66,16 @@ class CoreRequestBuilder {
*
* @param IConfig $config
* @param IDBConnection $connection
* @param ConfigService $configService
* @param MiscService $miscService
*/
public function __construct(IConfig $config, IDBConnection $connection, MiscService $miscService
public function __construct(
IConfig $config, IDBConnection $connection, ConfigService $configService,
MiscService $miscService
) {
$this->config = $config;
$this->dbConnection = $connection;
$this->configService = $configService;
$this->miscService = $miscService;
}

Expand Down
25 changes: 21 additions & 4 deletions lib/Db/PushQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,32 @@
class PushQueryBuilder extends ExtendedQueryBuilder {



/**
* Limit the request to app
*
* @param string $app
*
* @return PushQueryBuilder
*/
public function limitToApp(string $app): PushQueryBuilder {
$this->limitToDBField('app', $app, true);

return $this;
}




/**
* Limit the request to the Interface
* Limit the request to keyword
*
* @param string $recipient
* @param string $keyword
*
* @return PushQueryBuilder
*/
public function limitToRecipient(string $recipient): PushQueryBuilder {
$this->limitToDBField('recipient', $recipient, true);
public function limitToKeyword(string $keyword): PushQueryBuilder {
$this->limitToDBField('keyword', $keyword, true);

return $this;
}
Expand Down
Loading

0 comments on commit d302e37

Please sign in to comment.