aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/metadata.php
blob: 4619ba8d6993e3d13deba40a467255e1bb8c71e8 (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
<?php namespace HashOver;

// Copyright (C) 2018 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class Metadata
{
	public $setup;
	public $thread;
	public $data;

	public function __construct (Setup $setup, Thread $thread)
	{
		$this->setup = $setup;
		$this->thread = $thread;
		$this->data = $thread->data;
	}

	protected function prependLatestComments ($file, $global = false)
	{
		// Add thread to file if metadata is global
		if ($global === true) {
			$file = $this->setup->threadName . '/' . $file;
		}

		// Initial latest comments metadata array
		$latest = array ($file);

		// Attempt to read existing latest comments metadata
		$metadata = $this->data->readMeta ('latest-comments', 'auto', $global);

		// Check if latest comments metadata read successfully
		if ($metadata !== false) {
			// If so, merge existing comments with initial array
			$latest = array_merge ($latest, $metadata);
		}

		// Maximum number of latest comments to store
		$latest_max = max (10, $this->setup->latestMax);

		// Limit latest comments metadata array to configurable size
		$latest = array_slice ($latest, 0, $latest_max);

		// Attempt to save latest comments metadata
		$this->data->saveMeta ('latest-comments', $latest, 'auto', $global);
	}

	protected function spliceLatestComments ($file, $global = false)
	{
		// Add thread to file if metadata is global
		if ($global === true) {
			$file = $this->setup->threadName . '/' . $file;
		}

		// Attempt to read existing latest comments metadata
		$latest = $this->data->readMeta ('latest-comments', 'auto', $global);

		// Check if latest comments metadata read successfully
		if ($latest !== false) {
			$index = array_search ($file, $latest);

			// Check if the comment is in the latest array
			if ($index !== false) {
				// If so, remove it from the array
				array_splice ($latest, $index, 1);
			}

			// Attempt to save latest comments metadata
			$this->data->saveMeta ('latest-comments', $latest, 'auto', $global);
		}
	}

	public function addLatestComment ($file)
	{
		// Add comment to thread-specific latest comments metadata
		$this->prependLatestComments ($file);

		// Add comment to global latest comments metadata
		$this->prependLatestComments ($file, true);
	}

	public function removeFromLatest ($file)
	{
		// Add comment to thread-specific latest comments metadata
		$this->spliceLatestComments ($file);

		// Add comment to global latest comments metadata
		$this->spliceLatestComments ($file, true);
	}
}