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:
parent
bf0be12a5e
commit
9d58a1a113
|
|
@ -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,37 +189,46 @@ 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++)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var store = stores[i];
|
var store = stores[i];
|
||||||
var root = store.GetRootFolder();
|
var root = store.GetRootFolder();
|
||||||
string rootName = root.Name;
|
var match = FindFolderByPath(root, folderPath);
|
||||||
|
if (match != null)
|
||||||
if (rootName == parts[0])
|
|
||||||
{
|
{
|
||||||
current = root;
|
Marshal.ReleaseComObject(stores);
|
||||||
break;
|
return match;
|
||||||
}
|
}
|
||||||
Marshal.ReleaseComObject(root);
|
Marshal.ReleaseComObject(root);
|
||||||
Marshal.ReleaseComObject(store);
|
Marshal.ReleaseComObject(store);
|
||||||
}
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
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++)
|
{
|
||||||
|
var topFolders = ns.Folders;
|
||||||
|
for (int i = 1; i <= (int)topFolders.Count; i++)
|
||||||
|
{
|
||||||
|
var topFolder = topFolders[i];
|
||||||
|
if ((string)topFolder.Name == parts[0])
|
||||||
|
{
|
||||||
|
dynamic current = topFolder;
|
||||||
|
for (int p = 1; p < parts.Length; p++)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
var subFolders = current.Folders;
|
var subs = current.Folders;
|
||||||
for (int j = 1; j <= (int)subFolders.Count; j++)
|
for (int j = 1; j <= (int)subs.Count; j++)
|
||||||
{
|
{
|
||||||
var sub = subFolders[j];
|
var sub = subs[j];
|
||||||
if ((string)sub.Name == parts[i])
|
if ((string)sub.Name == parts[p])
|
||||||
{
|
{
|
||||||
current = sub;
|
current = sub;
|
||||||
found = true;
|
found = true;
|
||||||
|
|
@ -201,13 +236,22 @@ namespace StarfaceOutlookSync.Services
|
||||||
}
|
}
|
||||||
Marshal.ReleaseComObject(sub);
|
Marshal.ReleaseComObject(sub);
|
||||||
}
|
}
|
||||||
Marshal.ReleaseComObject(subFolders);
|
Marshal.ReleaseComObject(subs);
|
||||||
|
if (!found) { current = null; break; }
|
||||||
if (!found)
|
}
|
||||||
return ns.GetDefaultFolder(OlFolderContacts);
|
if (current != null)
|
||||||
|
{
|
||||||
|
Marshal.ReleaseComObject(topFolders);
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Marshal.ReleaseComObject(topFolder);
|
||||||
|
}
|
||||||
|
Marshal.ReleaseComObject(topFolders);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue