Skip to content

Commit 33bf955

Browse files
committed
Added check if regex pattern is valid + fixed code format + Added userConfig in FeedServiceTest.php
Signed-off-by: Markus87 <6339979+Markus87@users.noreply.github.com>
1 parent 73d3d7d commit 33bf955

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

lib/Service/FeedServiceV2.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ public function create(
286286
return $this->mapper->insert($feed);
287287
}
288288

289+
/**
290+
* Get regex pattern for filtering article titles
291+
*
292+
* @param String $userId UserId for configuration access
293+
*
294+
* @return valid regex pattern or empty string if invalid
295+
*/
296+
private function getTitleFilterRegex(String $userId): String
297+
{
298+
$pattern = $this->userConfig->getValueString($userId, 'news', 'titleFilterRegex');
299+
if (empty($pattern)) {
300+
return '';
301+
}
302+
if (@preg_match($pattern, '') === false) {
303+
$this->logger->warning('Pattern in titleFilterRegex is not valid: {pattern}', [ 'pattern' => $pattern ]);
304+
return '';
305+
}
306+
return $pattern;
307+
}
289308

290309
/**
291310
* Update a feed
@@ -388,10 +407,10 @@ public function fetch(Entity $feed): Entity
388407
$feed->setFaviconLink($fetchedFavicon);
389408
}
390409

391-
foreach (array_reverse($items) as &$item) {
392-
$filterBy = $this->userConfig->getValueString( $feed->getUserId(), 'news', 'titleFilterRegex');
393-
if( $item->getTitle() !== null && !empty($filterBy ) && preg_match( $filterBy, $item->getTitle() ) ) {
394-
$this->logger->trace( 'Item filtered: {title}', [ 'title' => $item->getTitle() ] );
410+
$filterBy = $this->getTitleFilterRegex($feed->getUserId());
411+
foreach (array_reverse($items) as &$item) {
412+
if ($item->getTitle() !== null && !empty($filterBy) && preg_match($filterBy, $item->getTitle())) {
413+
$this->logger->info('Item filtered: matched by = {filterBy} title = {title}', [ 'title' => $item->getTitle(), 'filterBy' => $filterBy ]);
395414
continue;
396415
}
397416

src/components/modals/AppSettingsDialog.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020

2121
<NcFormBoxSwitch
2222
v-model="disableRefresh"
23-
:label="t('news', 'Disable automatic refresh')" />
23+
:label="t('news', 'Disable automatic refresh')" />
2424
</NcFormBox>
2525
<NcTextField
2626
v-model="titleFilterRegex"
2727
:label="t('news', 'Drop new articles with title matching regex')"
28-
:placeholder="t('news', 'spam|ads')"
29-
/>
28+
:placeholder="t('news', '/spam|ads/')" />
3029
</NcAppSettingsSection>
3130

3231
<NcAppSettingsSection id="settings-display" :name="t('news', 'Appearance')">
@@ -364,10 +363,11 @@ export default defineComponent({
364363
get() {
365364
return this.$store.getters.titleFilterRegex
366365
},
366+
367367
set(newValue) {
368368
this.saveSetting('titleFilterRegex', newValue)
369369
},
370-
},
370+
},
371371
372372
uploadOpmlStatusMessage() {
373373
return this.$store.getters.lastOpmlImportMessage?.message

src/store/app.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const getters = {
7474
return state.disableRefresh
7575
},
7676
titleFilterRegex(state: AppInfoState) {
77-
return state.titleFilterRegex
77+
return state.titleFilterRegex
7878
},
7979
lastViewedFeedId(state: AppInfoState) {
8080
return state.lastViewedFeedId
@@ -155,10 +155,10 @@ export const mutations = {
155155
state.disableRefresh = value
156156
},
157157
titleFilterRegex(
158-
state: AppInfoState,
159-
{ value }: { value: string },
158+
state: AppInfoState,
159+
{ value }: { value: string },
160160
) {
161-
state.titleFilterRegex = value
161+
state.titleFilterRegex = value
162162
},
163163
starredOpenState(
164164
state: AppInfoState,

tests/Unit/Service/FeedServiceTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCA\News\Utility\HtmlSanitizer;
2828
use OCP\AppFramework\Db\DoesNotExistException;
2929
use OCP\IAppConfig;
30+
use OCP\IUserConfig;
3031

3132
use OCA\News\Db\Feed;
3233
use OCA\News\Db\Item;
@@ -129,11 +130,15 @@ protected function setUp(): void
129130
->getMockBuilder(IAppConfig::class)
130131
->disableOriginalConstructor()
131132
->getMock();
133+
$this->userConfig = $this
134+
->getMockBuilder(IUserConfig::class)
135+
->disableOriginalConstructor()
136+
->getMock();
132137
$this->appData = $this
133138
->getMockBuilder(AppData::class)
134139
->disableOriginalConstructor()
135140
->getMock();
136-
141+
137142
$this->class = new FeedServiceV2(
138143
$this->mapper,
139144
$this->fetcher,
@@ -142,6 +147,7 @@ protected function setUp(): void
142147
$this->purifier,
143148
$this->logger,
144149
$this->config,
150+
$this->userConfig,
145151
$this->appData
146152
);
147153
$this->uid = 'jack';

0 commit comments

Comments
 (0)