- remove importers configuration

- add check on userId for findOneByURL for entries
This commit is contained in:
Nicolas Lœuillet 2015-10-26 15:49:44 +01:00 committed by Jeremy Benoist
parent dda57bb944
commit 303768dfe9
7 changed files with 31 additions and 45 deletions

View file

@ -33,14 +33,6 @@ wallabag_core:
import: import:
allow_mimetypes: ['application/octet-stream', 'application/json', 'text/plain'] allow_mimetypes: ['application/octet-stream', 'application/json', 'text/plain']
wallabag_import:
importers:
pocket_urls:
oauth_request: https://getpocket.com/v3/oauth/request
auth_authorize: https://getpocket.com/auth/authorize
oauth_authorize: https://getpocket.com/v3/oauth/authorize
get: https://getpocket.com/v3/get
# Twig Configuration # Twig Configuration
twig: twig:
debug: "%kernel.debug%" debug: "%kernel.debug%"

View file

@ -51,15 +51,15 @@ class EntryController extends Controller
if ($form->isValid()) { if ($form->isValid()) {
$existingEntry = $em $existingEntry = $em
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($entry->getUrl()); ->findOneByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
if (!is_null($existingEntry)) { if (count($existingEntry) > 0) {
$this->get('session')->getFlashBag()->add( $this->get('session')->getFlashBag()->add(
'notice', 'notice',
'Entry already saved on '.$existingEntry->getCreatedAt()->format('d-m-Y') 'Entry already saved on '.$existingEntry[0]->getCreatedAt()->format('d-m-Y')
); );
return $this->redirect($this->generateUrl('view', array('id' => $existingEntry->getId()))); return $this->redirect($this->generateUrl('view', array('id' => $existingEntry[0]->getId())));
} }
$this->updateEntry($entry); $this->updateEntry($entry);

View file

@ -223,4 +223,21 @@ class EntryRepository extends EntityRepository
->getQuery() ->getQuery()
->getResult(); ->getResult();
} }
/**
* Find an entry by its url and its owner.
*
* @param $url
* @param $userId
*
* @return array
*/
public function findOneByUrlAndUserId($url, $userId)
{
return $this->createQueryBuilder('e')
->where('e.url = :url')->setParameter('url', $url)
->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
->getQuery()
->getResult();
}
} }

View file

@ -2,7 +2,6 @@
namespace Wallabag\ImportBundle\DependencyInjection; namespace Wallabag\ImportBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\ConfigurationInterface;
@ -13,22 +12,6 @@ class Configuration implements ConfigurationInterface
$treeBuilder = new TreeBuilder(); $treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('wallabag_import'); $rootNode = $treeBuilder->root('wallabag_import');
$rootNode
->children()
->arrayNode('importers')
->append($this->getURLs())
->end()
->end()
;
return $treeBuilder; return $treeBuilder;
} }
private function getURLs()
{
$node = new ArrayNodeDefinition('pocket_urls');
$node->prototype('scalar')->end();
return $node;
}
} }

View file

@ -13,7 +13,6 @@ class WallabagImportExtension extends Extension
{ {
$configuration = new Configuration(); $configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs); $config = $this->processConfiguration($configuration, $configs);
$container->setParameter('wallabag_import.pocket', $config['importers']['pocket_urls']);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml'); $loader->load('services.yml');

View file

@ -15,19 +15,15 @@ class PocketImport implements ImportInterface
private $session; private $session;
private $em; private $em;
private $consumerKey; private $consumerKey;
private $skippedEntries; private $skippedEntries = 0;
private $importedEntries; private $importedEntries = 0;
private $pocketURL;
public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey, $pocketURL) public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
{ {
$this->user = $tokenStorage->getToken()->getUser(); $this->user = $tokenStorage->getToken()->getUser();
$this->session = $session; $this->session = $session;
$this->em = $em; $this->em = $em;
$this->consumerKey = $consumerKey; $this->consumerKey = $consumerKey;
$this->skippedEntries = 0;
$this->importedEntries = 0;
$this->pocketURL = $pocketURL;
} }
public function getName() public function getName()
@ -121,9 +117,9 @@ class PocketImport implements ImportInterface
$existingEntry = $this->em $existingEntry = $this->em
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($url); ->findOneByUrlAndUserId($url, $this->user->getId());
if (!is_null($existingEntry)) { if (count($existingEntry) > 0) {
++$this->skippedEntries; ++$this->skippedEntries;
continue; continue;
} }
@ -153,7 +149,7 @@ class PocketImport implements ImportInterface
} }
if (!empty($pocketEntry['tags'])) { if (!empty($pocketEntry['tags'])) {
// $this->assignTagsToEntry($entry, $pocketEntry['tags']); $this->assignTagsToEntry($entry, $pocketEntry['tags']);
} }
$this->em->persist($entry); $this->em->persist($entry);
@ -166,7 +162,7 @@ class PocketImport implements ImportInterface
public function oAuthRequest($redirectUri, $callbackUri) public function oAuthRequest($redirectUri, $callbackUri)
{ {
$client = $this->createClient(); $client = $this->createClient();
$request = $client->createRequest('POST', $this->pocketURL['oauth_request'], $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
[ [
'body' => json_encode([ 'body' => json_encode([
'consumer_key' => $this->consumerKey, 'consumer_key' => $this->consumerKey,
@ -181,14 +177,14 @@ class PocketImport implements ImportInterface
// store code in session for callback method // store code in session for callback method
$this->session->set('pocketCode', $values['code']); $this->session->set('pocketCode', $values['code']);
return $this->pocketURL['auth_authorize'].'?request_token='.$values['code'].'&redirect_uri='.$callbackUri; return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
} }
public function oAuthAuthorize() public function oAuthAuthorize()
{ {
$client = $this->createClient(); $client = $this->createClient();
$request = $client->createRequest('POST', $this->pocketURL['oauth_authorize'], $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
[ [
'body' => json_encode([ 'body' => json_encode([
'consumer_key' => $this->consumerKey, 'consumer_key' => $this->consumerKey,
@ -206,7 +202,7 @@ class PocketImport implements ImportInterface
{ {
$client = $this->createClient(); $client = $this->createClient();
$request = $client->createRequest('POST', $this->pocketURL['get'], $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
[ [
'body' => json_encode([ 'body' => json_encode([
'consumer_key' => $this->consumerKey, 'consumer_key' => $this->consumerKey,

View file

@ -6,4 +6,3 @@ services:
- @session - @session
- @doctrine.orm.entity_manager - @doctrine.orm.entity_manager
- %pocket_consumer_key% - %pocket_consumer_key%
- %wallabag_import.pocket%