| Name | Message | Date |
|---|---|---|
| 📄 FolderChildrenCache.cs | 4 days ago | |
| 📄 GraphAccessTokenProvider.cs | 4 days ago | |
| 📄 GraphService.cs | 4 days ago |
📄
src/Graph/GraphService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Text; using Microsoft.Graph; using Microsoft.Graph.Models; using Microsoft.Kiota.Abstractions.Authentication; using NotesApp.Models; namespace NotesApp.Graph; public class GraphService { private readonly GraphServiceClient _client; private string? _driveId; public GraphService(Activity activity) { _client = new GraphServiceClient( new BaseBearerTokenAuthenticationProvider(new GraphAccessTokenProvider(activity)) ); } // Me.Drive only exposes the drive resource itself in this SDK; navigating into // root/items/children requires going through Drives[driveId] instead. private async Task<string> GetDriveIdAsync(CancellationToken ct) { if (_driveId != null) return _driveId; var drive = await _client.Me.Drive.GetAsync(cancellationToken: ct); _driveId = drive?.Id ?? throw new InvalidOperationException("Could not resolve OneDrive drive id."); return _driveId; } public async Task<List<DriveFolderItem>> ListChildFoldersAsync( string? folderId, CancellationToken ct = default ) { var driveId = await GetDriveIdAsync(ct); var itemId = folderId ?? "root"; // Graph accepts the literal id "root" as an alias. var page = await _client .Drives[driveId] .Items[itemId] .Children.GetAsync(cfg => cfg.QueryParameters.Select = ["id", "name", "folder"], ct); var results = new List<DriveFolderItem>(); if (page is null) return results; var iterator = PageIterator<DriveItem, DriveItemCollectionResponse>.CreatePageIterator( _client, page, item => { if (item.Folder != null && item.Id != null && item.Name != null) results.Add(new DriveFolderItem(item.Id, item.Name)); return true; } ); await iterator.IterateAsync(ct); return results.OrderBy(f => f.Name, StringComparer.OrdinalIgnoreCase).ToList(); } public async Task<List<DriveNoteMetadata>> ListMarkdownFilesAsync( string folderId, CancellationToken ct = default ) { var driveId = await GetDriveIdAsync(ct); var page = await _client .Drives[driveId] .Items[folderId] .Children.GetAsync( cfg => { cfg.QueryParameters.Select = [ "id", "name", "eTag", "lastModifiedDateTime", "size", "file", ]; cfg.QueryParameters.Top = 200; }, ct ); var results = new List<DriveNoteMetadata>(); if (page is null) return results; // With 1000+ notes this always spans multiple pages; PageIterator follows // @odata.nextLink automatically until the whole folder has been walked. var iterator = PageIterator<DriveItem, DriveItemCollectionResponse>.CreatePageIterator( _client, page, item => { if ( item.File != null && item.Id != null && item.Name != null && item.Name.EndsWith(".md", StringComparison.OrdinalIgnoreCase) ) { results.Add( new DriveNoteMetadata( item.Id, item.Name, item.ETag ?? "", item.LastModifiedDateTime, item.Size ) ); } return true; } ); await iterator.IterateAsync(ct); return results.OrderBy(n => n.Name, StringComparer.OrdinalIgnoreCase).ToList(); } private const int PreviewByteRange = 1024; private const int BatchSize = 20; // Graph's JSON $batch endpoint allows at most 20 sub-requests. // Fetches only the first ~1KB of each note's content so the list preview doesn't require // downloading full note bodies, and batches requests so a first-time sync of a large // library doesn't take one HTTP round trip per note. public async Task<Dictionary<string, string>> GetPreviewsAsync( IReadOnlyList<string> itemIds, CancellationToken ct = default ) { var previews = new Dictionary<string, string>(); if (itemIds.Count == 0) return previews; var driveId = await GetDriveIdAsync(ct); foreach (var chunk in itemIds.Chunk(BatchSize)) { var batch = new BatchRequestContentCollection(_client); var requestIds = new List<(string RequestId, string ItemId)>(); foreach (var itemId in chunk) { var requestInfo = _client .Drives[driveId] .Items[itemId] .Content.ToGetRequestInformation(); requestInfo.Headers.Add("Range", [$"bytes=0-{PreviewByteRange - 1}"]); var requestId = await batch.AddBatchRequestStepAsync(requestInfo, itemId); requestIds.Add((requestId, itemId)); } var response = await _client.Batch.PostAsync(batch, ct); foreach (var (requestId, itemId) in requestIds) { try { var stream = await response.GetResponseStreamByIdAsync(requestId); using var reader = new StreamReader(stream, Encoding.UTF8); var raw = await reader.ReadToEndAsync(ct); previews[itemId] = TrimPreview(raw); } catch { // Leave this item out; its ETag will still differ from the cache next // refresh, so it's retried automatically rather than silently dropped. } } } return previews; } private static string TrimPreview(string raw) { // A byte range can cut mid-line or mid-character; drop the trailing partial line. var lastNewline = raw.LastIndexOf('\n'); var trimmed = lastNewline > 0 ? raw[..lastNewline] : raw; const int maxLength = 300; return trimmed.Length > maxLength ? trimmed[..maxLength] : trimmed; } public async Task<string> GetContentAsync(string itemId, CancellationToken ct = default) { var driveId = await GetDriveIdAsync(ct); var stream = await _client .Drives[driveId] .Items[itemId] .Content.GetAsync(cancellationToken: ct); if (stream is null) return ""; using var reader = new StreamReader(stream, Encoding.UTF8); return await reader.ReadToEndAsync(ct); } }