Fix speed and error

This commit is contained in:
2026-09-16 12:22:27 +02:00
parent 45a883319a
commit bda7b311e3
5 changed files with 149 additions and 54 deletions
+105 -37
View File
@@ -1187,7 +1187,7 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
$cacheKey = "ms_folders_{$userId}";
if (!$forceRefresh) {
$cached = fociz_cache_get($cacheKey);
if ($cached !== null && is_array($cached)) {
if ($cached !== null && is_array($cached) && !empty($cached['success'])) {
return $cached;
}
}
@@ -1206,6 +1206,8 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
'unread_count' => 2,
'total_count' => 5,
'is_inbox' => true,
'parent_id' => null,
'is_child' => false,
],
[
'id' => 'demo-folder-dx',
@@ -1213,6 +1215,8 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
'unread_count' => 1,
'total_count' => 3,
'is_inbox' => false,
'parent_id' => null,
'is_child' => false,
],
[
'id' => 'demo-folder-klanten',
@@ -1220,6 +1224,8 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
'unread_count' => 0,
'total_count' => 2,
'is_inbox' => false,
'parent_id' => null,
'is_child' => false,
],
[
'id' => 'demo-folder-fin',
@@ -1227,6 +1233,8 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
'unread_count' => 1,
'total_count' => 2,
'is_inbox' => false,
'parent_id' => null,
'is_child' => false,
],
];
$totalUnread = array_sum(array_column($demoFolders, 'unread_count'));
@@ -1243,12 +1251,23 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
return [
'success' => false,
'error' => 'Geen geldig Microsoft 365 token beschikbaar.',
'folders' => [],
'folders' => [
[
'id' => 'inbox',
'name' => 'Postvak IN',
'unread_count' => 0,
'total_count' => 0,
'is_inbox' => true,
'parent_id' => null,
'is_child' => false,
]
],
'total_unread' => 0,
];
}
$url = MS_GRAPH_BASE_URL . '/me/mailFolders?$top=100&$select=id,displayName,parentFolderId,childFolderCount,unreadItemCount,totalItemCount,wellKnownName';
// 1. Haal de hoofdmappen op ZONDER restrictief $select (voorkomt 400 Bad Request op wellKnownName)
$url = MS_GRAPH_BASE_URL . '/me/mailFolders?$top=100';
$res = fociz_http_request($url, [
'method' => 'GET',
'headers' => [
@@ -1271,73 +1290,117 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
'unread_count' => 0,
'total_count' => 0,
'is_inbox' => true,
'parent_id' => null,
'is_child' => false,
]
],
'total_unread' => 0,
];
}
$rawFolders = $res['json']['value'];
$rawFolders = is_array($res['json']['value']) ? $res['json']['value'] : [];
// Haal eventuele submappen op voor mappen met childFolderCount > 0
foreach ($res['json']['value'] as $f) {
if (!empty($f['childFolderCount']) && (int)$f['childFolderCount'] > 0 && !empty($f['id'])) {
$childUrl = MS_GRAPH_BASE_URL . '/me/mailFolders/' . urlencode($f['id']) . '/childFolders?$top=50&$select=id,displayName,parentFolderId,childFolderCount,unreadItemCount,totalItemCount,wellKnownName';
$cRes = fociz_http_request($childUrl, [
'method' => 'GET',
'headers' => [
'Authorization' => "Bearer {$token}",
'Accept' => 'application/json',
],
'timeout' => 10,
]);
if ($cRes['status'] === 200 && !empty($cRes['json']['value'])) {
foreach ($cRes['json']['value'] as $child) {
$rawFolders[] = $child;
// 2. Haal ALTIJD expliciet de submappen van Postvak IN op
// (In Outlook bevinden 95% van de door gebruikers aangemaakte mappen zich direct onder Inbox)
$inboxChildUrl = MS_GRAPH_BASE_URL . '/me/mailFolders/inbox/childFolders?$top=100';
$inboxChildRes = fociz_http_request($inboxChildUrl, [
'method' => 'GET',
'headers' => [
'Authorization' => "Bearer {$token}",
'Accept' => 'application/json',
],
'timeout' => 8,
]);
if ($inboxChildRes['status'] === 200 && !empty($inboxChildRes['json']['value']) && is_array($inboxChildRes['json']['value'])) {
foreach ($inboxChildRes['json']['value'] as $child) {
$rawFolders[] = $child;
// Als een eigen submap onder Inbox zelf ook weer submappen heeft: haal die ook op
$cfc = (int)($child['childFolderCount'] ?? 0);
$cId = $child['id'] ?? '';
if ($cfc > 0 && !empty($cId)) {
$subRes = fociz_http_request(MS_GRAPH_BASE_URL . '/me/mailFolders/' . rawurlencode($cId) . '/childFolders?$top=100', [
'method' => 'GET',
'headers' => [
'Authorization' => "Bearer {$token}",
'Accept' => 'application/json',
],
'timeout' => 6,
]);
if ($subRes['status'] === 200 && !empty($subRes['json']['value']) && is_array($subRes['json']['value'])) {
foreach ($subRes['json']['value'] as $subChild) {
$rawFolders[] = $subChild;
}
}
}
}
}
// Uitsluitingen conform gebruikerswens: SPAM / Ongewenst, Prullenbak / Deleted, Outbox en Drafts
$excludedWellKnown = ['junkemail', 'deleteditems', 'outbox', 'drafts', 'conflicts', 'localfailures', 'serverfailures', 'syncissues'];
$excludedTerms = ['junk', 'spam', 'ongewenst', 'ongewenste e-mail', 'ongewenste email', 'deleted items', 'verwijderde items', 'prullenbak', 'trash', 'verwijderd', 'outbox', 'postvak uit', 'drafts', 'concepten'];
// 4. Uitsluitingen conform gebruikerswens:
// "de SPAM , Ongewenste email en Prullenbak hoeven er niet in."
// Ook Outbox, Drafts en technische sync logs uitsluiten
$excludedWellKnown = [
'junkemail', 'deleteditems', 'outbox', 'drafts',
'conflicts', 'localfailures', 'serverfailures', 'syncissues', 'conversationhistory',
'clutter', 'recoverableitemsdeletions'
];
$excludedExactNames = [
// Nederlands
'ongewenste e-mail', 'ongewenste email', 'ongewenst', 'spam', 'junk',
'verwijderde items', 'prullenbak', 'trash', 'verwijderd',
'postvak uit', 'outbox',
'concepten', 'drafts',
'gespreksgeschiedenis', 'conversation history',
'problemen met synchronisatie', 'synchronisatieproblemen', 'conflicten',
// Engels
'junk email', 'deleted items', 'sync issues', 'conflicts', 'local failures', 'server failures'
];
$folders = [];
$seenIds = [];
foreach ($rawFolders as $rf) {
$fId = $rf['id'] ?? '';
if (empty($fId) || isset($seenIds[$fId])) continue;
$seenIds[$fId] = true;
$fId = trim($rf['id'] ?? '');
if (empty($fId) || isset($seenIds[$fId])) {
continue;
}
// Sla verborgen mappen over
if (!empty($rf['isHidden'])) {
continue;
}
$wk = strtolower(trim($rf['wellKnownName'] ?? ''));
if (in_array($wk, $excludedWellKnown, true)) continue;
if (in_array($wk, $excludedWellKnown, true)) {
continue;
}
$dName = trim($rf['displayName'] ?? '');
$dNameLower = strtolower($dName);
$skip = false;
foreach ($excludedTerms as $term) {
if ($dNameLower === $term || strpos($dNameLower, $term) !== false) {
$skip = true;
break;
}
if (in_array($dNameLower, $excludedExactNames, true)) {
continue;
}
if (strpos($dNameLower, 'problemen met synchronisatie') === 0 || strpos($dNameLower, 'sync issues') === 0) {
continue;
}
if ($skip) continue;
$isInbox = ($wk === 'inbox' || $dNameLower === 'inbox' || $dNameLower === 'postvak in');
$seenIds[$fId] = true;
$folders[] = [
'id' => $isInbox ? 'inbox' : $fId,
'name' => $dName ?: 'Map',
'name' => $dName ?: ($isInbox ? 'Postvak IN' : 'Map'),
'unread_count' => (int)($rf['unreadItemCount'] ?? 0),
'total_count' => (int)($rf['totalItemCount'] ?? 0),
'is_inbox' => $isInbox,
'parent_id' => $rf['parentFolderId'] ?? null,
'is_child' => !empty($rf['parentFolderId']) && !$isInbox,
];
}
// Zorg dat Inbox altijd vooraan staat
// Garandeer dat Inbox altijd aanwezig is
$hasInbox = false;
foreach ($folders as $f) {
if (!empty($f['is_inbox'])) {
@@ -1352,9 +1415,12 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
'unread_count' => 0,
'total_count' => 0,
'is_inbox' => true,
'parent_id' => null,
'is_child' => false,
]);
}
// Sorteer: Inbox altijd bovenaan, daarna alfabetisch
usort($folders, function($a, $b) {
if (!empty($a['is_inbox'])) return -1;
if (!empty($b['is_inbox'])) return 1;
@@ -1369,7 +1435,9 @@ function microsoft_fetch_mail_folders(int $userId, bool $forceRefresh = false):
'folders' => $folders,
'total_unread' => $totalUnread,
];
fociz_cache_set($cacheKey, $result, 60); // 60 seconden cache
fociz_cache_set($cacheKey, $result, 300); // 5 minuten cache
return $result;
}
@@ -1428,7 +1496,7 @@ function microsoft_fetch_folder_messages(int $userId, string $folderId = 'inbox'
$queryParams['$filter'] = 'isRead eq false';
}
$folderEndpoint = ($cleanFolderId === 'inbox') ? 'inbox' : urlencode($cleanFolderId);
$folderEndpoint = ($cleanFolderId === 'inbox') ? 'inbox' : rawurlencode($cleanFolderId);
$url = MS_GRAPH_BASE_URL . '/me/mailFolders/' . $folderEndpoint . '/messages?' . http_build_query($queryParams);
$res = fociz_http_request($url, [
'method' => 'GET',