aboutsummaryrefslogtreecommitdiff
path: root/app/Models/FilterActionsTrait.php
blob: 6f4eafd6e665e67f2ea63d337c75310447d4dfad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);

/**
 * Logic to apply filter actions (for feeds, categories, user configuration...).
 */
trait FreshRSS_FilterActionsTrait {

	/** @var list<FreshRSS_FilterAction>|null $filterActions */
	private ?array $filterActions = null;

	/**
	 * @return list<FreshRSS_FilterAction>
	 */
	private function filterActions(): array {
		if (empty($this->filterActions)) {
			$this->filterActions = [];
			$filters = $this->attributeArray('filters') ?? [];
			foreach ($filters as $filter) {
				$filterAction = FreshRSS_FilterAction::fromJSON($filter);
				if ($filterAction != null) {
					$this->filterActions[] = $filterAction;
				}
			}
		}
		return $this->filterActions;
	}

	/**
	 * @param array<FreshRSS_FilterAction>|null $filterActions
	 */
	private function _filterActions(?array $filterActions): void {
		$this->filterActions = is_array($filterActions) ? array_values($filterActions) : null;
		if ($this->filterActions !== null && !empty($this->filterActions)) {
			$this->_attribute('filters', array_map(
				static fn(?FreshRSS_FilterAction $af) => $af == null ? null : $af->toJSON(),
				$this->filterActions));
		} else {
			$this->_attribute('filters', null);
		}
	}

	/** @return list<FreshRSS_BooleanSearch> */
	public function filtersAction(string $action): array {
		$action = trim($action);
		if ($action == '') {
			return [];
		}
		$filters = [];
		$filterActions = $this->filterActions();
		for ($i = count($filterActions) - 1; $i >= 0; $i--) {
			$filterAction = $filterActions[$i];
			if (in_array($action, $filterAction->actions(), true)) {
				$filters[] = $filterAction->booleanSearch();
			}
		}
		return $filters;
	}

	/**
	 * @param array<string> $filters
	 */
	public function _filtersAction(string $action, array $filters): void {
		$action = trim($action);
		if ($action === '') {
			return;
		}
		$filters = array_values(array_unique(array_map('trim', $filters), SORT_STRING));
		$filterActions = $this->filterActions();

		//Check existing filters
		for ($i = count($filterActions) - 1; $i >= 0; $i--) {
			$filterAction = $filterActions[$i];
			if ($filterAction === null || !is_array($filterAction->actions()) || $filterAction->booleanSearch()->toString() === '') {
				array_splice($filterActions, $i, 1);
				continue;
			}
			$actions = $filterAction->actions();
			//Remove existing rules with same action
			for ($j = count($actions) - 1; $j >= 0; $j--) {
				if ($actions[$j] === $action) {
					array_splice($actions, $j, 1);
				}
			}
			//Update existing filter with new action
			for ($k = count($filters) - 1; $k >= 0; $k--) {
				$filter = $filters[$k];
				if ($filter === $filterAction->booleanSearch()->toString()) {
					$actions[] = $action;
					array_splice($filters, $k, 1);
				}
			}
			//Save result
			if (empty($actions)) {
				array_splice($filterActions, $i, 1);
			} else {
				$filterAction->_actions($actions);
			}
		}

		//Add new filters
		for ($k = count($filters) - 1; $k >= 0; $k--) {
			$filter = $filters[$k];
			if ($filter != '') {
				$filterAction = FreshRSS_FilterAction::fromJSON([
					'search' => $filter,
					'actions' => [$action],
				]);
				if ($filterAction != null) {
					$filterActions[] = $filterAction;
				}
			}
		}

		if (empty($filterActions)) {
			$filterActions = null;
		}
		$this->_filterActions($filterActions);
	}

	/**
	 * @param bool $applyLabel Parameter by reference, which will be set to true if the callers needs to apply a label to the article entry.
	 * @param-out bool $applyLabel
	 */
	public function applyFilterActions(FreshRSS_Entry $entry, ?bool &$applyLabel = null): void {
		$applyLabel = false;
		foreach ($this->filterActions() as $filterAction) {
			if ($entry->matches($filterAction->booleanSearch())) {
				foreach ($filterAction->actions() as $action) {
					switch ($action) {
						case 'read':
							if (!$entry->isRead()) {
								$entry->_isRead(true);
								Minz_ExtensionManager::callHook(Minz_HookType::EntryAutoRead, $entry, 'filter');
							}
							break;
						case 'star':
							if (!$entry->isUpdated()) {
								// Do not apply to updated articles, to avoid overruling a user manual action
								$entry->_isFavorite(true);
							}
							break;
						case 'label':
							if (!$entry->isUpdated()) {
								// Do not apply to updated articles, to avoid overruling a user manual action
								$applyLabel = true;
							}
							break;
					}
				}
			}
		}
	}
}