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)
|
||||
{
|
||||
var app = GetOutlookApp();
|
||||
|
|
@ -163,51 +189,69 @@ namespace StarfaceOutlookSync.Services
|
|||
|
||||
try
|
||||
{
|
||||
var parts = folderPath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
dynamic current = null;
|
||||
|
||||
// Versuch 1: Alle Kontaktordner durchsuchen und per FolderPath matchen
|
||||
var stores = ns.Stores;
|
||||
for (int i = 1; i <= (int)stores.Count; i++)
|
||||
{
|
||||
var store = stores[i];
|
||||
var root = store.GetRootFolder();
|
||||
string rootName = root.Name;
|
||||
|
||||
if (rootName == parts[0])
|
||||
try
|
||||
{
|
||||
current = root;
|
||||
break;
|
||||
var store = stores[i];
|
||||
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);
|
||||
Marshal.ReleaseComObject(store);
|
||||
catch { }
|
||||
}
|
||||
Marshal.ReleaseComObject(stores);
|
||||
|
||||
if (current == null)
|
||||
return ns.GetDefaultFolder(OlFolderContacts);
|
||||
|
||||
for (int i = 1; i < parts.Length; i++)
|
||||
// Versuch 2: Namespace.Folders direkt navigieren
|
||||
var parts = folderPath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length >= 1)
|
||||
{
|
||||
bool found = false;
|
||||
var subFolders = current.Folders;
|
||||
for (int j = 1; j <= (int)subFolders.Count; j++)
|
||||
var topFolders = ns.Folders;
|
||||
for (int i = 1; i <= (int)topFolders.Count; i++)
|
||||
{
|
||||
var sub = subFolders[j];
|
||||
if ((string)sub.Name == parts[i])
|
||||
var topFolder = topFolders[i];
|
||||
if ((string)topFolder.Name == parts[0])
|
||||
{
|
||||
current = sub;
|
||||
found = true;
|
||||
break;
|
||||
dynamic current = topFolder;
|
||||
for (int p = 1; p < parts.Length; p++)
|
||||
{
|
||||
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);
|
||||
|
||||
if (!found)
|
||||
return ns.GetDefaultFolder(OlFolderContacts);
|
||||
Marshal.ReleaseComObject(topFolders);
|
||||
}
|
||||
|
||||
return current;
|
||||
System.Diagnostics.Debug.WriteLine($"Folder not found by path: {folderPath}, using default");
|
||||
return ns.GetDefaultFolder(OlFolderContacts);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
|
@ -221,24 +265,51 @@ namespace StarfaceOutlookSync.Services
|
|||
try
|
||||
{
|
||||
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;
|
||||
try
|
||||
{
|
||||
item = items[i];
|
||||
// Nur ContactItems verarbeiten (Class = 40 = olContact)
|
||||
if ((int)item.Class == 40)
|
||||
int itemClass = -1;
|
||||
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));
|
||||
}
|
||||
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
|
||||
{
|
||||
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}");
|
||||
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;
|
||||
|
||||
foreach (var item in array)
|
||||
|
|
|
|||
Loading…
Reference in New Issue