4545 "Vulnerability" : Vulnerability ,
4646}
4747
48- VALID_HEADERS = [
48+ AP_VALID_HEADERS = [
4949 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' ,
5050 "application/activity+json" ,
5151]
5757AP_TARGET = {"cc" : "https://www.w3.org/ns/activitystreams#Public" }
5858
5959
60- def check_ap_context (request ):
60+ def check_and_r_ap_context (request ):
6161 """
6262 check activitypub context request and return request without @context
6363 """
@@ -73,7 +73,7 @@ def add_ap_target(response):
7373 Add target activitypub response
7474 """
7575 if response is not dict :
76- raise Exception ("Invalid response" )
76+ raise KeyError ("Invalid response" )
7777
7878 if not response .get ("cc" ):
7979 response .append (** AP_TARGET )
@@ -83,15 +83,15 @@ def add_ap_target(response):
8383
8484def has_valid_header (view ):
8585 """
86- check if the request header in the VALID_HEADERS if yes return view else return HttpResponseForbidden
86+ check if the request header in the AP_VALID_HEADERS if yes return view else return HttpResponseForbidden
8787 """
8888
8989 def wrapper (request , * args , ** kwargs ):
9090 content_type = request .headers .get ("Content-Type" )
91- if content_type in VALID_HEADERS :
91+ if content_type in AP_VALID_HEADERS :
9292 return view (request , * args , ** kwargs )
9393 else :
94- return HttpResponseForbidden ()
94+ return
9595
9696 return wrapper
9797
@@ -135,10 +135,10 @@ def get_by_type(self):
135135
136136 def get (self ):
137137 obj_id , page_name = full_resolve (self .id )
138- if page_name == "purl-profile" :
139- return Purl .objects .get (string = obj_id ["slug " ])
140- elif page_name == "user-profile" :
141- user = User .objects .get (username = obj_id ["slug " ])
138+ if page_name == "purl-ap- profile" :
139+ return Purl .objects .get (string = obj_id ["purl_string " ])
140+ elif page_name == "user-ap- profile" :
141+ user = User .objects .get (username = obj_id ["username " ])
142142 if hasattr (user , "person" ):
143143 return user .person
144144 elif hasattr (user , "service" ):
@@ -166,8 +166,8 @@ class ApObject:
166166 published : str = None
167167
168168 URL_MAPPER = {
169- "user-profile" : "slug " ,
170- "purl-profile" : "slug " ,
169+ "user-ap- profile" : "username " ,
170+ "purl-ap- profile" : "purl_string " ,
171171 "review-page" : "uuid" ,
172172 "repository-page" : "uuid" ,
173173 "note-page" : "uuid" ,
@@ -194,7 +194,7 @@ def save(self):
194194 return self .failed_ap_rs ()
195195 obj_id , page_name = full_resolve (self .object .id )
196196 if type (actor ) is Person :
197- purl = Purl .objects .get (string = obj_id ["slug " ])
197+ purl = Purl .objects .get (string = obj_id ["purl_string " ])
198198 new_obj , created = Follow .objects .get_or_create (person_id = actor .id , purl = purl )
199199 else :
200200 return self .failed_ap_rs ()
@@ -270,7 +270,15 @@ def save(self):
270270
271271 def succeeded_ap_rs (self , new_obj ):
272272 """Response for successfully deleting the object"""
273- return JsonResponse ({"Location" : new_obj .to_ap }, status = 201 )
273+ Obj_Map = {
274+ "Note" : "note-page" ,
275+ "Review" : "review-page" ,
276+ "Repository" : "repository-page" ,
277+ "Vulnerability" : "vulnerability-page" ,
278+ }
279+ return JsonResponse (
280+ {"Location" : full_reverse (Obj_Map [self .object .type ], new_obj .id )}, status = 201
281+ )
274282
275283 def failed_ap_rs (self ):
276284 """Response for failure deleting the object"""
@@ -362,7 +370,7 @@ def failed_ap_rs(self):
362370def create_activity_obj (data ):
363371 """Convert json object to activity object"""
364372 payload = json .loads (data )
365- payload_without_context = check_ap_context (payload )
373+ payload_without_context = check_and_r_ap_context (payload )
366374 return Activity (** payload_without_context )
367375
368376
@@ -378,7 +386,7 @@ def save(self):
378386 return self .failed_ap_rs ()
379387 else :
380388 obj_id , page_name = full_resolve (self .object .id )
381- purl = Purl .objects .get (string = obj_id ["slug " ])
389+ purl = Purl .objects .get (string = obj_id ["purl_string " ])
382390 follow_obj = Follow .objects .get (person_id = actor .id , purl = purl )
383391 follow_obj .delete ()
384392 return self .succeeded_ap_rs ()
0 commit comments