-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComAtprotoRepo_ApplyWrites.cs
More file actions
199 lines (161 loc) · 6.48 KB
/
Copy pathComAtprotoRepo_ApplyWrites.cs
File metadata and controls
199 lines (161 loc) · 6.48 KB
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
using System.Security.Cryptography;
using System.Text.Json.Nodes;
using dnproto.repo;
using Microsoft.AspNetCore.Http;
namespace dnproto.pds.xrpc;
public class ComAtprotoRepo_ApplyWrites : BaseXrpcCommand
{
public IResult GetResponse()
{
IncrementStatistics();
Statistics.IncrementStatistics_ApplyWrites(HttpContext, Pds.PdsDb, Pds.Logger);
//
// Require auth
//
if(UserIsAuthenticated() == false)
{
var (response, statusCode) = GetAuthenticationFailureResponse();
return Results.Json(response, statusCode: statusCode);
}
//
// Get body input
//
JsonNode? requestBody = GetRequestBodyAsJson();
string? repo;
if(requestBody is null
|| !CheckRequestBodyParam(requestBody, "repo", out repo)
|| string.IsNullOrEmpty(repo)
)
{
return Results.Json(new { error = "InvalidRequest", message = "Error: invalid params." }, statusCode: 400);
}
//
// Get writes array
//
JsonArray? writesArray = requestBody["writes"]?.AsArray();
if(writesArray is null || writesArray.Count == 0)
{
return Results.Json(new { error = "InvalidRequest", message = "Error: writes array is required." }, statusCode: 400);
}
//
// Optional: swapCommit check
//
string? swapCommit = requestBody["swapCommit"]?.ToString();
if(!string.IsNullOrEmpty(swapCommit))
{
var currentCommit = Pds.PdsDb.GetRepoCommit();
if(currentCommit?.Cid?.Base32 != swapCommit)
{
return Results.Json(new { error = "InvalidSwap", message = "Commit CID mismatch." }, statusCode: 400);
}
}
//
// Parse write operations
//
List<UserRepo.ApplyWritesOperation> writes = new List<UserRepo.ApplyWritesOperation>();
foreach(JsonNode? writeNode in writesArray)
{
if(writeNode is null)
{
return Results.Json(new { error = "InvalidRequest", message = "Error: null write operation." }, statusCode: 400);
}
string? type = writeNode["$type"]?.ToString();
string? collection = writeNode["collection"]?.ToString();
string? rkey = writeNode["rkey"]?.ToString();
if(string.IsNullOrEmpty(type) || string.IsNullOrEmpty(collection))
{
return Results.Json(new { error = "InvalidRequest", message = "Error: missing $type or collection in write operation." }, statusCode: 400);
}
// Generate rkey if not provided for create operations
if(string.IsNullOrEmpty(rkey))
{
if(type == UserRepo.ApplyWritesType.Create)
{
rkey = RecordKey.GenerateTid();
}
else
{
return Results.Json(new { error = "InvalidRequest", message = "Error: rkey is required for update/delete operations." }, statusCode: 400);
}
}
// Parse record for create/update operations
DagCborObject? record = null;
if(type == UserRepo.ApplyWritesType.Create || type == UserRepo.ApplyWritesType.Update)
{
string? valueStr = JsonData.ConvertToJsonString(writeNode["value"]);
//
// 2/3/26
// added this trace logic here, because I was seeing odd behavior.
// An image ref would start in the json as "ref: { "$link: {}}" but after
// the call to FromJsonString, it would get flattened to just the ref with a string.
// I suspect it was in DagCborObject.FromJsonElement - see related fix in there.
//
Pds.Logger.LogTrace($"ApplyWrites Operation Value JSON:\n{valueStr}");
if(string.IsNullOrEmpty(valueStr))
{
return Results.Json(new { error = "InvalidRequest", message = "Error: value is required for create/update operations." }, statusCode: 400);
}
record = DagCborObject.FromJsonString(valueStr);
Pds.Logger.LogTrace($"ApplyWrites Operation Parsed DagCbor:\n{DagCborObject.GetRecursiveDebugString(record, 0)}");
if(record is null)
{
return Results.Json(new { error = "InvalidRequest", message = "Error: failed to parse record value." }, statusCode: 400);
}
}
writes.Add(new UserRepo.ApplyWritesOperation
{
Type = type,
Collection = collection,
Rkey = rkey,
Record = record
});
}
//
// Apply writes using UserRepo.ApplyWrites
//
List<UserRepo.ApplyWritesResult> results = Pds.UserRepo.ApplyWrites(writes, GetCallerIpAddress(), GetCallerUserAgent());
if(results.Count == 0)
{
return Results.Json(new { error = "ApplyWritesFailed", message = "Error applying writes." }, statusCode: 400);
}
//
// Build response
//
var repoCommit = Pds.PdsDb.GetRepoCommit();
if(repoCommit is null || repoCommit.Cid is null || string.IsNullOrEmpty(repoCommit.Rev))
{
return Results.Json(new { error = "ApplyWritesFailed", message = "Error applying writes." }, statusCode: 400);
}
JsonArray resultsArray = new JsonArray();
foreach(var result in results)
{
var resultObj = new JsonObject
{
["$type"] = result.Type
};
if(result.Uri is not null)
{
resultObj["uri"] = result.Uri;
}
if(result.Cid is not null)
{
resultObj["cid"] = result.Cid.Base32;
}
if(result.ValidationStatus is not null)
{
resultObj["validationStatus"] = result.ValidationStatus;
}
resultsArray.Add(resultObj);
}
var responseObj = new JsonObject
{
["commit"] = new JsonObject
{
["cid"] = repoCommit.Cid.Base32,
["rev"] = repoCommit.Rev
},
["results"] = resultsArray
};
return Results.Json(responseObj, statusCode: 200);
}
}