Skip to content

Commit 78985cc

Browse files
committed
Chore: split user data from feed data
1 parent 68a06ea commit 78985cc

File tree

4 files changed

+160
-14
lines changed

4 files changed

+160
-14
lines changed

lib/Db/FeedMapperV2.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
class FeedMapperV2 extends NewsMapperV2
3030
{
3131
const TABLE_NAME = 'news_feeds';
32+
const USER_TABLE_NAME = 'news_user_feeds';
3233

3334
/**
3435
* FeedMapper constructor.

lib/Db/ItemMapperV2.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
class ItemMapperV2 extends NewsMapperV2
3232
{
3333
const TABLE_NAME = 'news_items';
34+
const USER_TABLE_NAME = 'news_user_items';
3435

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

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

@@ -444,9 +444,10 @@ public function findAllFeed(
444444
): array {
445445
$builder = $this->db->getQueryBuilder();
446446

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

504-
$builder->select('items.*')
505+
$builder->select('items.*', 'users.unread', 'users.starred', 'users.shared_by')
505506
->from($this->tableName, 'items')
506507
->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
508+
->innerJoin('items', self::USER_TABLE_NAME, 'users', 'items.id = users.item_id')
507509
->andWhere('feeds.user_id = :userId')
508510
->andWhere('feeds.deleted_at = 0')
509511
->andWhere($folderWhere)
@@ -550,11 +552,10 @@ public function findAllItems(
550552
): array {
551553
$builder = $this->db->getQueryBuilder();
552554

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OCA\News\Migration;
6+
7+
use Closure;
8+
use OCP\DB\ISchemaWrapper;
9+
use OCP\IDBConnection;
10+
use OCP\Migration\IOutput;
11+
use OCP\Migration\SimpleMigrationStep;
12+
13+
/**
14+
* Auto-generated migration step: Please modify to your needs!
15+
*/
16+
class Version160100Date20210821130702 extends SimpleMigrationStep {
17+
18+
/**
19+
* @var IDBConnection
20+
*/
21+
protected $connection;
22+
23+
public function __construct(IDBConnection $connection)
24+
{
25+
$this->connection = $connection;
26+
}
27+
28+
/**
29+
* @param IOutput $output
30+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
31+
* @param array $options
32+
*/
33+
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
34+
}
35+
36+
/**
37+
* @param IOutput $output
38+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
39+
* @param array $options
40+
* @return null|ISchemaWrapper
41+
*/
42+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
43+
/** @var ISchemaWrapper $schema */
44+
$schema = $schemaClosure();
45+
46+
if (!$schema->hasTable('news_user_items')) {
47+
$table = $schema->createTable('news_user_items');
48+
$table->addColumn('item_id', 'bigint', [
49+
'notnull' => true,
50+
'length' => 8,
51+
'unsigned' => true,
52+
]);
53+
$table->addColumn('user_id', 'string', [
54+
'notnull' => true,
55+
'length' => 64,
56+
]);
57+
$table->addColumn('unread', 'boolean', [
58+
'notnull' => true,
59+
'default' => false,
60+
]);
61+
$table->addColumn('starred', 'boolean', [
62+
'notnull' => true,
63+
'default' => false,
64+
]);
65+
$table->addColumn('last_modified', 'bigint', [
66+
'notnull' => false,
67+
'length' => 8,
68+
'default' => 0,
69+
'unsigned' => true,
70+
]);
71+
$table->addColumn('shared_by', 'string', [
72+
'notnull' => false,
73+
'length' => 64
74+
]);
75+
$table->setPrimaryKey(['item_id', 'user_id']);
76+
}
77+
78+
if (!$schema->hasTable('news_user_feeds')) {
79+
$table = $schema->createTable('news_user_feeds');
80+
$table->addColumn('feed_id', 'bigint', [
81+
'notnull' => true,
82+
'length' => 8,
83+
'unsigned' => true,
84+
]);
85+
$table->addColumn('user_id', 'string', [
86+
'notnull' => true,
87+
'length' => 64,
88+
]);
89+
$table->addColumn('folder_id', 'bigint', [
90+
'notnull' => false,
91+
'length' => 8,
92+
]);
93+
$table->addColumn('deleted_at', 'bigint', [
94+
'notnull' => false,
95+
'length' => 8,
96+
'default' => 0,
97+
'unsigned' => true,
98+
]);
99+
$table->addColumn('added', 'bigint', [
100+
'notnull' => false,
101+
'length' => 8,
102+
'default' => 0,
103+
'unsigned' => true,
104+
]);
105+
$table->addColumn('title', 'text', [
106+
'notnull' => true,
107+
]);
108+
$table->addColumn('last_modified', 'bigint', [
109+
'notnull' => false,
110+
'length' => 8,
111+
'default' => 0,
112+
'unsigned' => true,
113+
]);
114+
$table->setPrimaryKey(['feed_id', 'user_id']);
115+
}
116+
117+
return $schema;
118+
}
119+
120+
/**
121+
* @param IOutput $output
122+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
123+
* @param array $options
124+
*/
125+
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
126+
$qb = $this->connection->getQueryBuilder();
127+
$user_item_table = $qb->getTableName('news_user_items');
128+
$user_feed_table = $qb->getTableName('news_user_feeds');
129+
$item_table = $qb->getTableName('news_items');
130+
$feed_table = $qb->getTableName('news_feeds');
131+
132+
$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 = ?;";
133+
134+
$feeds = $this->connection->executeQuery("SELECT `id`,`user_id` FROM $feed_table;")->fetchAll();
135+
foreach ($feeds as $feed) {
136+
$this->connection->executeUpdate($items_query, [$feed['user_id'], $feed['id']]);
137+
}
138+
139+
$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;");
140+
}
141+
}

tests/Unit/Db/ItemMapperTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,15 @@ public function testFindAllFromUser()
8888

8989
$this->builder->expects($this->once())
9090
->method('innerJoin')
91-
->with('items', 'news_feeds', 'feeds', 'items.feed_id = feeds.id')
91+
->withConsecutive(
92+
['items', 'news_user_items', 'users', 'items.id = users.item_id'],
93+
['a', 'a', 'a', 'a']
94+
)
9295
->will($this->returnSelf());
9396

9497
$this->builder->expects($this->once())
9598
->method('where')
96-
->with('feeds.user_id = :user_id')
99+
->with('users.user_id = :user_id')
97100
->will($this->returnSelf());
98101

99102
$this->builder->expects($this->once())

0 commit comments

Comments
 (0)