Fix Starface JSON parsing and Outlook folder/contact reading

Starface API:
- Handle both array and object responses from /contacts endpoint
- Try common wrapper fields (items, contacts, data, results)

Outlook contacts:
- Add FindFolderByPath that matches exact FolderPath property
- Fallback to Namespace.Folders navigation if store lookup fails
- Fallback: try reading contact fields even if Class != 40
- Add debug logging for folder path and item count

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
duffyduck 2026-04-03 12:08:50 +02:00
parent bf0be12a5e
commit 9d58a1a113
2 changed files with 139 additions and 37 deletions

View File

@ -153,6 +153,32 @@ namespace StarfaceOutlookSync.Services
} }
} }
private dynamic FindFolderByPath(dynamic folder, string targetPath)
{
try
{
string currentPath = folder.FolderPath;
if (currentPath == targetPath)
return folder;
var subs = folder.Folders;
for (int i = 1; i <= (int)subs.Count; i++)
{
try
{
var sub = subs[i];
var match = FindFolderByPath(sub, targetPath);
if (match != null) return match;
Marshal.ReleaseComObject(sub);
}
catch { }
}
Marshal.ReleaseComObject(subs);
}
catch { }
return null;
}
private dynamic GetFolderByPath(string folderPath) private dynamic GetFolderByPath(string folderPath)
{ {
var app = GetOutlookApp(); var app = GetOutlookApp();
@ -163,51 +189,69 @@ namespace StarfaceOutlookSync.Services
try try
{ {
var parts = folderPath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); // Versuch 1: Alle Kontaktordner durchsuchen und per FolderPath matchen
dynamic current = null;
var stores = ns.Stores; var stores = ns.Stores;
for (int i = 1; i <= (int)stores.Count; i++) for (int i = 1; i <= (int)stores.Count; i++)
{ {
var store = stores[i]; try
var root = store.GetRootFolder();
string rootName = root.Name;
if (rootName == parts[0])
{ {
current = root; var store = stores[i];
break; var root = store.GetRootFolder();
var match = FindFolderByPath(root, folderPath);
if (match != null)
{
Marshal.ReleaseComObject(stores);
return match;
}
Marshal.ReleaseComObject(root);
Marshal.ReleaseComObject(store);
} }
Marshal.ReleaseComObject(root); catch { }
Marshal.ReleaseComObject(store);
} }
Marshal.ReleaseComObject(stores); Marshal.ReleaseComObject(stores);
if (current == null) // Versuch 2: Namespace.Folders direkt navigieren
return ns.GetDefaultFolder(OlFolderContacts); var parts = folderPath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 1)
for (int i = 1; i < parts.Length; i++)
{ {
bool found = false; var topFolders = ns.Folders;
var subFolders = current.Folders; for (int i = 1; i <= (int)topFolders.Count; i++)
for (int j = 1; j <= (int)subFolders.Count; j++)
{ {
var sub = subFolders[j]; var topFolder = topFolders[i];
if ((string)sub.Name == parts[i]) if ((string)topFolder.Name == parts[0])
{ {
current = sub; dynamic current = topFolder;
found = true; for (int p = 1; p < parts.Length; p++)
break; {
bool found = false;
var subs = current.Folders;
for (int j = 1; j <= (int)subs.Count; j++)
{
var sub = subs[j];
if ((string)sub.Name == parts[p])
{
current = sub;
found = true;
break;
}
Marshal.ReleaseComObject(sub);
}
Marshal.ReleaseComObject(subs);
if (!found) { current = null; break; }
}
if (current != null)
{
Marshal.ReleaseComObject(topFolders);
return current;
}
} }
Marshal.ReleaseComObject(sub); Marshal.ReleaseComObject(topFolder);
} }
Marshal.ReleaseComObject(subFolders); Marshal.ReleaseComObject(topFolders);
if (!found)
return ns.GetDefaultFolder(OlFolderContacts);
} }
return current; System.Diagnostics.Debug.WriteLine($"Folder not found by path: {folderPath}, using default");
return ns.GetDefaultFolder(OlFolderContacts);
} }
catch catch
{ {
@ -221,24 +265,51 @@ namespace StarfaceOutlookSync.Services
try try
{ {
var folder = GetFolderByPath(folderPath); var folder = GetFolderByPath(folderPath);
var items = folder.Items; System.Diagnostics.Debug.WriteLine($"Reading contacts from: {(string)folder.FolderPath}");
for (int i = 1; i <= (int)items.Count; i++) var items = folder.Items;
int count = (int)items.Count;
System.Diagnostics.Debug.WriteLine($"Items count: {count}");
for (int i = 1; i <= count; i++)
{ {
dynamic item = null; dynamic item = null;
try try
{ {
item = items[i]; item = items[i];
// Nur ContactItems verarbeiten (Class = 40 = olContact) int itemClass = -1;
if ((int)item.Class == 40) try { itemClass = (int)item.Class; } catch { }
// olContact = 40, aber manche Kontakte melden Class anders
// Versuch einfach die Kontakt-Felder zu lesen
if (itemClass == 40)
{ {
contacts.Add(MapFromOutlook(item)); contacts.Add(MapFromOutlook(item));
} }
else
{
// Fallback: Pruefen ob es trotzdem ein Kontakt ist
try
{
string eid = item.EntryID;
string fn = item.FirstName;
string ln = item.LastName;
// Hat Kontakt-Felder -> ist ein Kontakt
contacts.Add(MapFromOutlook(item));
}
catch
{
// Kein Kontakt, ueberspringen
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error reading item {i}: {ex.Message}");
} }
catch { }
finally finally
{ {
if (item != null) Marshal.ReleaseComObject(item); if (item != null) try { Marshal.ReleaseComObject(item); } catch { }
} }
} }

View File

@ -172,7 +172,38 @@ namespace StarfaceOutlookSync.Services
var resp = await _http.GetAsync($"{_baseUrl}/contacts?{query}"); var resp = await _http.GetAsync($"{_baseUrl}/contacts?{query}");
if (!resp.IsSuccessStatusCode) break; if (!resp.IsSuccessStatusCode) break;
var array = JArray.Parse(await resp.Content.ReadAsStringAsync()); var body = await resp.Content.ReadAsStringAsync();
JArray array;
// Die API gibt je nach Version ein Array oder ein Objekt mit "items" zurueck
var token = JToken.Parse(body);
if (token is JArray directArray)
{
array = directArray;
}
else if (token is JObject obj)
{
// Versuche gaengige Felder: items, contacts, data, results
array = (obj["items"] ?? obj["contacts"] ?? obj["data"] ?? obj["results"]) as JArray;
if (array == null)
{
// Einzelnes Kontakt-Objekt? Dann in Array wrappen
if (obj["id"] != null && obj["blocks"] != null)
{
array = new JArray { obj };
}
else
{
System.Diagnostics.Debug.WriteLine($"Unerwartete Starface-Antwort: {body.Substring(0, Math.Min(200, body.Length))}");
break;
}
}
}
else
{
break;
}
if (array.Count == 0) break; if (array.Count == 0) break;
foreach (var item in array) foreach (var item in array)