clean history on save, fix soem PM endpoints
This commit is contained in:
parent
e57af3ad36
commit
7cdb516f71
102
Check_PCA.php
102
Check_PCA.php
@ -209,25 +209,36 @@ class API
|
|||||||
json_encode($resp_dict['meta']),
|
json_encode($resp_dict['meta']),
|
||||||
'ERROR'
|
'ERROR'
|
||||||
);
|
);
|
||||||
// die();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_messages($clubs)
|
public function get_messages($clubs)
|
||||||
{
|
{
|
||||||
$num_unread_endpoint =
|
$num_unread_endpoint = self::$api_endpoint
|
||||||
self::$api_endpoint . '/users/me/channels/num_unread/pm';
|
. '/users/me/channels/num_unread?channel_types=io.pnut.core.pm';
|
||||||
$num_unread_pms = $this->get_data($num_unread_endpoint)['data'];
|
$num_unread_pms_data = $this->get_data($num_unread_endpoint)['data'];
|
||||||
$messages = []; //Keys: username, values: message-array of messages by the user
|
if (array_key_exists('io.pnut.core.pm', $num_unread_pms_data)) {
|
||||||
|
$num_unread_pms = $num_unread_pms_data['io.pnut.core.pm'];
|
||||||
|
} else {
|
||||||
|
$num_unread_pms = 0;
|
||||||
|
write_pca_log(
|
||||||
|
'Could not load number of unread PMs from server.'
|
||||||
|
. 'Response did not include the key "io.pnut.core.pm"!'
|
||||||
|
. json_encode($num_unread_pms_data),
|
||||||
|
'ERROR'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Keys: username, values: message-array of messages by the user
|
||||||
|
$messages = [];
|
||||||
write_pca_log($num_unread_pms . ' unread PMs');
|
write_pca_log($num_unread_pms . ' unread PMs');
|
||||||
if ($num_unread_pms > 0) {
|
if ($num_unread_pms > 0) {
|
||||||
$channels_endpoint = self::$api_endpoint .
|
$channels_endpoint = self::$api_endpoint
|
||||||
'/users/me/channels/subscribed?include_read=0&channel_types=io.pnut.core.pm';
|
. '/users/me/channels/subscribed'
|
||||||
|
. '?include_read=0&channel_types=io.pnut.core.pm';
|
||||||
$channels = $this->get_data($channels_endpoint);
|
$channels = $this->get_data($channels_endpoint);
|
||||||
foreach ($channels['data'] as $channel) {
|
foreach ($channels['data'] as $channel) {
|
||||||
write_pca_log('Channel: ' . $channel['id'], 'DEBUG');
|
write_pca_log('Channel: ' . $channel['id'], 'DEBUG');
|
||||||
$messages_endpoint =
|
$messages_endpoint = self::$api_endpoint .
|
||||||
self::$api_endpoint .
|
|
||||||
'/channels/' .
|
'/channels/' .
|
||||||
$channel['id'] .
|
$channel['id'] .
|
||||||
'/messages?include_deleted=0&include_html=0&include_client=0&include_marker=1';
|
'/messages?include_deleted=0&include_html=0&include_client=0&include_marker=1';
|
||||||
@ -732,4 +743,77 @@ $f = fopen($last_notification_file, 'w');
|
|||||||
fwrite($f, json_encode($last_notification_dict));
|
fwrite($f, json_encode($last_notification_dict));
|
||||||
fclose($f);
|
fclose($f);
|
||||||
|
|
||||||
|
$history_file = 'history.json';
|
||||||
|
$history_file_by_user = 'history_keyed.json';
|
||||||
|
$contents = file_get_contents($history_file);
|
||||||
|
if ($contents === false) {
|
||||||
|
write_pca_log('Could not read history file', 'ERROR');
|
||||||
|
$history = [];
|
||||||
|
}
|
||||||
|
$history = json_decode($contents, true);
|
||||||
|
if ($history == null) {
|
||||||
|
write_pca_log('Could not decode history file', 'ERROR');
|
||||||
|
$history = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$history_keyed = [];
|
||||||
|
foreach ($history as $history_entry) {
|
||||||
|
if (array_key_exists($history_entry['user'], $history_keyed)) {
|
||||||
|
$user_history = $history_keyed[$history_entry['user']];
|
||||||
|
} else {
|
||||||
|
$user_history = [];
|
||||||
|
}
|
||||||
|
$user_history_entry = [
|
||||||
|
'date' => $history_entry['date'],
|
||||||
|
'pca' => $history_entry['pca'],
|
||||||
|
'post_id' => $history_entry['post_id'],
|
||||||
|
];
|
||||||
|
$last_element = end($user_history);
|
||||||
|
if ($last_element == false
|
||||||
|
|| $last_element['pca'] !== $user_history_entry['pca']
|
||||||
|
) {
|
||||||
|
$user_history[] = $user_history_entry;
|
||||||
|
usort(
|
||||||
|
$user_history,
|
||||||
|
fn($a, $b) => strcmp($a['date'], $b['date'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$history_keyed[$history_entry['user']] = $user_history;
|
||||||
|
}
|
||||||
|
file_put_contents($history_file_by_user, json_encode($history_keyed));
|
||||||
|
|
||||||
|
usort(
|
||||||
|
$history,
|
||||||
|
fn($a, $b) => strcmp($a['date'], $b['date'])
|
||||||
|
);
|
||||||
|
$history_clean = [];
|
||||||
|
foreach ($history as $key => $history_entry) {
|
||||||
|
if ($key === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$prev_entry = $history[$key - 1];
|
||||||
|
if ($history_entry['user'] !== $prev_entry['user']
|
||||||
|
&& $history_entry['pca'] !== $prev_entry['pca']
|
||||||
|
) {
|
||||||
|
$history_clean[] = $history_entry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!empty($history_clean)) {
|
||||||
|
if (copy($history_file, $history_file . '.bak')) {
|
||||||
|
file_put_contents($history_file, json_encode($history_clean));
|
||||||
|
} else {
|
||||||
|
write_pca_log(
|
||||||
|
'Could not create backup history file. Not overwriting new one'
|
||||||
|
);
|
||||||
|
$suffix = (new \DateTime())->format('YmdHis');
|
||||||
|
file_put_contents(
|
||||||
|
$history_file . $suffix,
|
||||||
|
json_encode($history_clean)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
write_pca_log('Cleaned history would be empty. Not saving!', 'ERROR');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user