Skip to content

Commit a31a74d

Browse files
committed
Added feature to drop article if title matches a regex provided by the user
1 parent 76f5b06 commit a31a74d

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

lib/Controller/PageController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function index(): TemplateResponse
8181
'oldestFirst',
8282
'showAll',
8383
'disableRefresh',
84+
'titleFilterRegex',
8485
'displaymode',
8586
'splitmode',
8687
'starredOpenState'

lib/Service/FeedServiceV2.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCP\AppFramework\Db\Entity;
3030
use OCP\AppFramework\Db\DoesNotExistException;
3131
use OCP\IAppConfig;
32+
use OCP\Config\IUserConfig;
3233

3334
use OCA\News\Db\Feed;
3435
use OCA\News\Db\Item;
@@ -84,6 +85,7 @@ class FeedServiceV2 extends Service
8485
* @param HtmlSanitizer $purifier HTML Sanitizer
8586
* @param LoggerInterface $logger Logger
8687
* @param IAppConfig $config App config
88+
* @param IUserConfig $userConfig User config
8789
*/
8890
public function __construct(
8991
FeedMapperV2 $mapper,
@@ -93,6 +95,7 @@ public function __construct(
9395
HtmlSanitizer $purifier,
9496
LoggerInterface $logger,
9597
IAppConfig $config,
98+
IUserConfig $userConfig,
9699
AppData $appData
97100
) {
98101
parent::__construct($mapper, $logger);
@@ -102,6 +105,7 @@ public function __construct(
102105
$this->explorer = $explorer;
103106
$this->purifier = $purifier;
104107
$this->config = $config;
108+
$this->userConfig = $userConfig;
105109
$this->appData = $appData;
106110
}
107111

@@ -384,7 +388,14 @@ public function fetch(Entity $feed): Entity
384388
$feed->setFaviconLink($fetchedFavicon);
385389
}
386390

387-
foreach (array_reverse($items) as &$item) {
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() ] );
395+
continue;
396+
}
397+
398+
388399
$item->setFeedId($feed->getId())
389400
->setBody($this->purifier->purify($item->getBody()));
390401

src/components/modals/AppSettingsDialog.vue

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020

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

2732
<NcAppSettingsSection id="settings-display" :name="t('news', 'Appearance')">
@@ -218,6 +223,7 @@ import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
218223
import NcNoteCard from '@nextcloud/vue/components/NcNoteCard'
219224
import NcRadioGroup from '@nextcloud/vue/components/NcRadioGroup'
220225
import NcRadioGroupButton from '@nextcloud/vue/components/NcRadioGroupButton'
226+
import NcTextField from '@nextcloud/vue/components/NcTextField'
221227
import DownloadIcon from 'vue-material-design-icons/Download.vue'
222228
import UploadIcon from 'vue-material-design-icons/Upload.vue'
223229
import { DISPLAY_MODE, SPLIT_MODE } from '../../enums/index.ts'
@@ -240,6 +246,7 @@ export default defineComponent({
240246
NcNoteCard,
241247
NcRadioGroup,
242248
NcRadioGroupButton,
249+
NcTextField,
243250
DownloadIcon,
244251
UploadIcon,
245252
},
@@ -353,6 +360,15 @@ export default defineComponent({
353360
},
354361
},
355362
363+
titleFilterRegex: {
364+
get() {
365+
return this.$store.getters.titleFilterRegex
366+
},
367+
set(newValue) {
368+
this.saveSetting('titleFilterRegex', newValue)
369+
},
370+
},
371+
356372
uploadOpmlStatusMessage() {
357373
return this.$store.getters.lastOpmlImportMessage?.message
358374
},

src/store/app.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type AppInfoState = {
1818
preventReadOnScroll: boolean
1919
showAll: boolean
2020
disableRefresh: boolean
21+
titleFilterRegex: string
2122
lastViewedFeedId: string
2223
lastViewedFeedType: string
2324
starredOpenState: boolean
@@ -34,6 +35,7 @@ const state: AppInfoState = reactive({
3435
preventReadOnScroll: loadState('news', 'preventReadOnScroll', null) === '1',
3536
showAll: loadState('news', 'showAll', null) === '1',
3637
disableRefresh: loadState('news', 'disableRefresh', null) === '1',
38+
titleFilterRegex: loadState('news', 'titleFilterRegex', ''),
3739
lastViewedFeedId: loadState('news', 'lastViewedFeedId', '0'),
3840
lastViewedFeedType: loadState('news', 'lastViewedFeedType', '6'),
3941
starredOpenState: loadState('news', 'starredOpenState', null) === '1',
@@ -71,6 +73,9 @@ const getters = {
7173
disableRefresh(state: AppInfoState) {
7274
return state.disableRefresh
7375
},
76+
titleFilterRegex(state: AppInfoState) {
77+
return state.titleFilterRegex
78+
},
7479
lastViewedFeedId(state: AppInfoState) {
7580
return state.lastViewedFeedId
7681
},
@@ -149,6 +154,12 @@ export const mutations = {
149154
) {
150155
state.disableRefresh = value
151156
},
157+
titleFilterRegex(
158+
state: AppInfoState,
159+
{ value }: { value: string },
160+
) {
161+
state.titleFilterRegex = value
162+
},
152163
starredOpenState(
153164
state: AppInfoState,
154165
{ value }: { value: boolean },

0 commit comments

Comments
 (0)