@@ -161,14 +161,13 @@ def _rfc(self, con, principal, rfc_id, role="reader"):
161161 self ._require (con , principal , row ["repo_id" ], role , rfc_id )
162162 return dict (row )
163163
164- def _view (self , con , principal , row ):
165- model = json .loads (row ["model" ])
164+ def _view (self , con , principal , row , parsed_model = None ):
165+ model = dict (parsed_model ) if parsed_model is not None else json .loads (row ["model" ])
166+ visible = self ._view_visibility (con , principal , row ["repo_id" ])
166167 model ["observations" ] = {link : item for link , item in model .get ("observations" , {}).items ()
167- if self ._visible_source (con , principal , row ["repo_id" ], item .get ("source" ))}
168- model ["suggestions" ] = [s for s in model .get ("suggestions" , []) if self ._visible_source (
169- con , principal , row ["repo_id" ], s .get ("evidence" , {}).get ("source" ))]
170- model ["features" ] = [f for f in model .get ("features" , []) if self ._visible_source (
171- con , principal , row ["repo_id" ], f .get ("auto_source" ))]
168+ if visible (item .get ("source" ))}
169+ model ["suggestions" ] = [s for s in model .get ("suggestions" , []) if visible (s .get ("evidence" , {}).get ("source" ))]
170+ model ["features" ] = [dict (f ) for f in model .get ("features" , []) if visible (f .get ("auto_source" ))]
172171 if "last_discovery" in model :
173172 model ["last_discovery" ] = {"proposed" : sum (s .get ("status" ) == "proposed" for s in model ["suggestions" ]),
174173 "applied" : sum (s .get ("status" ) == "applied" for s in model ["suggestions" ])}
@@ -198,13 +197,36 @@ def _view(self, con, principal, row):
198197 "updated" : row ["updated" ], "role" : role , "can_write" : ROLES .get (role , 0 ) >= 2 ,
199198 "can_publish" : ROLES .get (role , 0 ) >= 3 }
200199
200+ @staticmethod
201+ def _compact_view (view , mode ):
202+ if mode not in ("detail" , "summary" ):
203+ return view
204+ counts = {}
205+ for suggestion in view .get ("suggestions" , []):
206+ state = suggestion .get ("status" , "proposed" )
207+ counts [state ] = counts .get (state , 0 ) + 1
208+ fields = {"id" , "repo_id" , "title" , "revision" , "content_digest" , "source" , "state" ,
209+ "enrolled" , "restricted" , "updated" , "role" , "can_write" , "can_publish" ,
210+ "implementation" , "acceptance" , "complete" , "sync_status" , "freshness" ,
211+ "next_actions" , "last_discovery" , "legacy_namespace" , "namespace" }
212+ if mode == "detail" :
213+ fields |= {"body" , "features" , "criteria" , "scope" , "auto_add" , "max_auto_additions" ,
214+ "grants" , "historical_claims" , "historical_priorities" , "ambiguities" }
215+ return {** {key : value for key , value in view .items () if key in fields },
216+ "suggestion_counts" : counts , "suggestions_total" : sum (counts .values ()),
217+ "feature_count" : len (view .get ("features" , [])), "criterion_count" : len (view .get ("criteria" , []))}
218+
201219 def _save (self , con , principal , row , model , body = None , title = None ):
202220 body = row ["body" ] if body is None else body
203221 title = row ["title" ] if title is None else title
204- revision = digest (encode ({"body" : body , "title" : title , "model" : model }))
222+ serialized = encode (model )
223+ # Same canonical revision, without encoding the large model twice.
224+ revision = digest ('{"body":' + encode (body ) + ',"model":' + serialized + ',"title":' + encode (title ) + '}' )
225+ updated = self .clock ()
205226 con .execute ("UPDATE rfcs SET title=?,body=?,model=?,revision=?,updated=? WHERE id=?" ,
206- (title , body , encode (model ), revision , self .clock (), row ["id" ]))
207- return self ._view (con , principal , dict (con .execute ("SELECT * FROM rfcs WHERE id=?" , (row ["id" ],)).fetchone ()))
227+ (title , body , serialized , revision , updated , row ["id" ]))
228+ current = {** row , "title" : title , "body" : body , "model" : serialized , "revision" : revision , "updated" : updated }
229+ return self ._view (con , principal , current , parsed_model = model )
208230
209231 def dispatch (self , principal , action , payload = None ):
210232 payload = payload or {}
@@ -226,7 +248,10 @@ def dispatch(self, principal, action, payload=None):
226248 return {"source" : ref .to_dict (), "title" : fetched .get ("title" , "Imported RFC" ), "body" : fetched ["body" ], "revision" : fetched .get ("revision" , "" ), "content_digest" : content_digest (fetched .get ("title" , "Imported RFC" ), fetched ["body" ])}
227249 with self .store .transaction () as con :
228250 principal = self ._current (con , principal )
229- return self ._dispatch (con , principal , action , payload )
251+ result = self ._dispatch (con , principal , action , payload )
252+ if isinstance (result , dict ) and "repo_id" in result and "features" in result :
253+ result = self ._compact_view (result , payload .get ("view" ))
254+ return result
230255
231256 def _dispatch (self , con , p , action , data ):
232257 now = self .clock ()
@@ -361,7 +386,7 @@ def _dispatch(self, con, p, action, data):
361386 view = self ._view (con , p , dict (row ))
362387 if data .get ("query" ) and data ["query" ].casefold () not in (view ["title" ] + view ["body" ]).casefold ():
363388 continue
364- result .append (view )
389+ result .append (self . _compact_view ( view , data . get ( "view" )) )
365390 return {"rfcs" : result }
366391 if action == "rfcs.draft" :
367392 rid = data .get ("repo_id" , "" )
@@ -650,6 +675,36 @@ def _visible_source(self, con, principal, repo_id, source):
650675 except RFCError :
651676 return False
652677
678+ def _view_visibility (self , con , principal , repo_id ):
679+ """Build an ACL index for this transaction only; never cache user authority."""
680+ roots = {r ["id" ]: r ["root" ] for r in con .execute ("SELECT id,root FROM repositories" )}
681+ blocked , blocked_paths = set (), set ()
682+ aliases = {"pull_request" : "pr" , "pull" : "pr" }
683+
684+ def identity (ref ):
685+ repository = ref .repository .casefold () if ref .provider == "github" else ref .repository
686+ return ref .provider , repository , aliases .get (ref .kind , ref .kind ), ref .identifier or ref .path
687+
688+ for row in con .execute ("SELECT id,repo_id,source FROM rfcs WHERE source!='{}'" ):
689+ if ROLES .get (self ._role (con , principal , row ["repo_id" ], row ["id" ]), 0 ) >= 1 :
690+ continue
691+ ref = SourceRef .from_dict (json .loads (row ["source" ]))
692+ blocked .add (identity (ref ))
693+ if ref .provider == "local" :
694+ blocked_paths .add ((Path (roots [row ["repo_id" ]]) / (ref .path or ref .identifier )).resolve ())
695+
696+ def visible (source ):
697+ if not source :
698+ return True
699+ try :
700+ ref = SourceRef .from_dict (source )
701+ if ref .provider == "local" and repo_id in roots :
702+ return (Path (roots [repo_id ]) / (ref .path or ref .identifier )).resolve () not in blocked_paths
703+ return identity (ref ) not in blocked
704+ except RFCError :
705+ return False
706+ return visible
707+
653708 @staticmethod
654709 def _features (features , validate = True ):
655710 if not isinstance (features , list ):
0 commit comments