Skip to content

Commit

Permalink
Chore: split user data from feed data
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
  • Loading branch information
SMillerDev committed Mar 19, 2022
1 parent 68a06ea commit 52354a8
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/Db/FeedMapperV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
class FeedMapperV2 extends NewsMapperV2
{
const TABLE_NAME = 'news_feeds';
const USER_TABLE_NAME = 'news_user_feeds';

/**
* FeedMapper constructor.
Expand Down
25 changes: 13 additions & 12 deletions lib/Db/ItemMapperV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class ItemMapperV2 extends NewsMapperV2
{
const TABLE_NAME = 'news_items';
const USER_TABLE_NAME = 'news_user_items';

/**
* ItemMapper constructor.
Expand All @@ -56,9 +57,8 @@ public function findAllFromUser(string $userId, array $params = []): array
$builder = $this->db->getQueryBuilder();
$builder->select('items.*')
->from($this->tableName, 'items')
->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
->where('feeds.user_id = :user_id')
->andWhere('feeds.deleted_at = 0')
->innerJoin('items', self::USER_TABLE_NAME, 'users', 'items.id = users.item_id')
->where('users.user_id = :user_id')
->setParameter('user_id', $userId, IQueryBuilder::PARAM_STR);

foreach ($params as $key => $value) {
Expand Down Expand Up @@ -92,10 +92,10 @@ public function findFromUser(string $userId, int $id): Entity
$builder = $this->db->getQueryBuilder();
$builder->select('items.*')
->from($this->tableName, 'items')
->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
->where('feeds.user_id = :user_id')
->innerJoin('items', self::USER_TABLE_NAME, 'users', 'items.id = users.item_id')
->where('users.user_id = :user_id')
->andWhere('items.id = :item_id')
->andWhere('feeds.deleted_at = 0')
->andWhere('users.deleted_at = 0')
->setParameter('user_id', $userId, IQueryBuilder::PARAM_STR)
->setParameter('item_id', $id, IQueryBuilder::PARAM_INT);

Expand Down Expand Up @@ -444,9 +444,10 @@ public function findAllFeed(
): array {
$builder = $this->db->getQueryBuilder();

$builder->select('items.*')
$builder->select('items.*', 'users.unread', 'users.starred', 'users.shared_by')
->from($this->tableName, 'items')
->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
->innerJoin('items', self::USER_TABLE_NAME, 'users', 'items.id = users.item_id')
->andWhere('feeds.deleted_at = 0')
->andWhere('feeds.user_id = :userId')
->andWhere('items.feed_id = :feedId')
Expand Down Expand Up @@ -501,9 +502,10 @@ public function findAllFolder(
$folderWhere = $builder->expr()->eq('feeds.folder_id', new Literal($folderId), IQueryBuilder::PARAM_INT);
}

$builder->select('items.*')
$builder->select('items.*', 'users.unread', 'users.starred', 'users.shared_by')
->from($this->tableName, 'items')
->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
->innerJoin('items', self::USER_TABLE_NAME, 'users', 'items.id = users.item_id')
->andWhere('feeds.user_id = :userId')
->andWhere('feeds.deleted_at = 0')
->andWhere($folderWhere)
Expand Down Expand Up @@ -550,11 +552,10 @@ public function findAllItems(
): array {
$builder = $this->db->getQueryBuilder();

$builder->select('items.*')
$builder->select('items.*', 'users.unread', 'users.starred', 'users.shared_by')
->from($this->tableName, 'items')
->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
->andWhere('feeds.user_id = :userId')
->andWhere('feeds.deleted_at = 0')
->innerJoin('items', self::USER_TABLE_NAME, 'users', 'items.id = users.item_id')
->andWhere('users.user_id = :userId')
->setParameter('userId', $userId)
->addOrderBy('items.id', ($oldestFirst ? 'ASC' : 'DESC'));

Expand Down
141 changes: 141 additions & 0 deletions lib/Migration/Version160100Date20210821130702.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

declare(strict_types=1);

namespace OCA\News\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version160100Date20210821130702 extends SimpleMigrationStep {

/**
* @var IDBConnection
*/
protected $connection;

public function __construct(IDBConnection $connection)
{
$this->connection = $connection;
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

if (!$schema->hasTable('news_user_items')) {
$table = $schema->createTable('news_user_items');
$table->addColumn('item_id', 'bigint', [
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->addColumn('unread', 'boolean', [
'notnull' => true,
'default' => false,
]);
$table->addColumn('starred', 'boolean', [
'notnull' => true,
'default' => false,
]);
$table->addColumn('last_modified', 'bigint', [
'notnull' => false,
'length' => 8,
'default' => 0,
'unsigned' => true,
]);
$table->addColumn('shared_by', 'string', [
'notnull' => false,
'length' => 64
]);
$table->setPrimaryKey(['item_id', 'user_id']);
}

if (!$schema->hasTable('news_user_feeds')) {
$table = $schema->createTable('news_user_feeds');
$table->addColumn('feed_id', 'bigint', [
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->addColumn('folder_id', 'bigint', [
'notnull' => false,
'length' => 8,
]);
$table->addColumn('deleted_at', 'bigint', [
'notnull' => false,
'length' => 8,
'default' => 0,
'unsigned' => true,
]);
$table->addColumn('added', 'bigint', [
'notnull' => false,
'length' => 8,
'default' => 0,
'unsigned' => true,
]);
$table->addColumn('title', 'text', [
'notnull' => true,
]);
$table->addColumn('last_modified', 'bigint', [
'notnull' => false,
'length' => 8,
'default' => 0,
'unsigned' => true,
]);
$table->setPrimaryKey(['feed_id', 'user_id']);
}

return $schema;
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
$qb = $this->connection->getQueryBuilder();
$user_item_table = $qb->getTableName('news_user_items');
$user_feed_table = $qb->getTableName('news_user_feeds');
$item_table = $qb->getTableName('news_items');
$feed_table = $qb->getTableName('news_feeds');

$items_query = "REPLACE INTO $user_item_table SELECT id AS 'item_id', ? AS 'user_id',`unread`,`starred`,`last_modified`,`shared_by` FROM $item_table where feed_id = ?;";

$feeds = $this->connection->executeQuery("SELECT `id`,`user_id` FROM $feed_table;")->fetchAll();
foreach ($feeds as $feed) {
$this->connection->executeUpdate($items_query, [$feed['user_id'], $feed['id']]);
}

$this->connection->executeUpdate("REPLACE INTO $user_feed_table SELECT id AS 'feed_id',user_id,folder_id,deleted_at,added,title,last_modified FROM $feed_table;");
}
}
7 changes: 5 additions & 2 deletions tests/Unit/Db/ItemMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ public function testFindAllFromUser()

$this->builder->expects($this->once())
->method('innerJoin')
->with('items', 'news_feeds', 'feeds', 'items.feed_id = feeds.id')
->withConsecutive(
['items', 'news_user_items', 'users', 'items.id = users.item_id'],
['a', 'a', 'a', 'a']
)
->will($this->returnSelf());

$this->builder->expects($this->once())
->method('where')
->with('feeds.user_id = :user_id')
->with('users.user_id = :user_id')
->will($this->returnSelf());

$this->builder->expects($this->once())
Expand Down

0 comments on commit 52354a8

Please sign in to comment.