From dd52baed150cc245616ac7f5b032220c2f6cbdba Mon Sep 17 00:00:00 2001 From: bill-auger Date: Mar 21 2019 06:51:29 +0000 Subject: distinguish person by canonical id in path --- diff --git a/forgefed_constants.py b/forgefed_constants.py index 8f19cd6..299c175 100644 --- a/forgefed_constants.py +++ b/forgefed_constants.py @@ -1,8 +1,25 @@ -PATH_REGEX = r'^/forge-fed/' -EXPECTED_PATH_SPLIT_LEN = 3 # (/NAME/CHANNEL?k=v) +LOCAL_CONFIG = \ +{ \ + "$SCHEME" : "https" , \ + "$HOST" : "example.net" , \ -DB_ID_PREFIX = 'http://localhost:5000/' -GLOBAL_ID_SUFFIX = '@localhost' + "Person.id" : "$id@$HOST" , \ + "Person.likes" : "$id/likes" , \ + "Person.following" : "$id/following" , \ + "Person.followers" : "$id/followers" , \ + "Person.liked" : "$id/liked" , \ + "Person.inbox" : "$id/inbox" , \ + "Person.outbox" : "$id/outbox" , \ + "Person.url" : "$DOMAIN/$id" , \ + + "Note.id" : "$temp_uuid" , \ + 'Note.attributedTo' : "$from_id" , \ + 'Note.content' : "

$source.content

" , \ + 'Note.published' : "$NOW" , \ + 'Note.url' : "$DOMAIN/$from_id/note/$temp_uuid" +} + +PATH_REGEX = r'^/forge-fed/' STATUS_OK = '200 OK' STATUS_NOT_FOUND = '404 NOT FOUND' diff --git a/forgefed_controller.py b/forgefed_controller.py index d84d72c..9504470 100644 --- a/forgefed_controller.py +++ b/forgefed_controller.py @@ -1,12 +1,38 @@ -def inbox_get_handler(env): return [ '200 OK' , "GET/inbox" ] -def followers_get_handler(env): return [ '200 OK' , "GET/followers" ] -def following_get_handler(env): return [ '200 OK' , "GET/following" ] -def liked_get_handler(env): return [ '200 OK' , "GET/liked" ] -def likes_get_handler(env): return [ '200 OK' , "GET/likes" ] -def outbox_get_handler(env): return [ '200 OK' , "GET/outbox" ] -def inbox_post_handler(env): return [ '200 OK' , "POST/inbox" ] -def followers_post_handler(env): return [ '200 OK' , "POST/followers" ] -def following_post_handler(env): return [ '200 OK' , "POST/following" ] -def liked_post_handler(env): return [ '200 OK' , "POST/liked" ] -def likes_post_handler(env): return [ '200 OK' , "POST/likes" ] -def outbox_post_handler(env): return [ '200 OK' , "POST/outbox" ] +from forgefed_constants import RESP_NOT_FOUND , STATUS_OK +from forgefed_model import GetPerson + + +def user_get_handler(person_id , ap_dict): + person = GetPerson(person_id) + + return [ STATUS_OK , "this is " + person_id ] if person != None else RESP_NOT_FOUND + + +def inbox_get_handler(person_id , ap_dict): return [ STATUS_OK , "GET/inbox" ] +def followers_get_handler(person_id , ap_dict): return [ STATUS_OK , "GET/followers" ] +def following_get_handler(person_id , ap_dict): return [ STATUS_OK , "GET/following" ] +def liked_get_handler(person_id , ap_dict): return [ STATUS_OK , "GET/liked" ] +def likes_get_handler(person_id , ap_dict): return [ STATUS_OK , "GET/likes" ] +def outbox_get_handler(person_id , ap_dict): return [ STATUS_OK , "GET/outbox" ] + +def inbox_post_handler(person_id , ap_dict): return [ STATUS_OK , "GET/inbox" ] +def followers_post_handler(person_id , ap_dict): return [ STATUS_OK , "POST/followers" ] +def following_post_handler(person_id , ap_dict): return [ STATUS_OK , "POST/following" ] +def liked_post_handler(person_id , ap_dict): return [ STATUS_OK , "POST/liked" ] +def likes_post_handler(person_id , ap_dict): return [ STATUS_OK , "POST/likes" ] +def outbox_post_handler(person_id , ap_dict): return [ STATUS_OK , "POST/outbox" ] + + +# DEBUG BEGIN +from forgefed_model import CreateNote , CreatePerson + +Alice = CreatePerson(person_id='alice') +Bob = CreatePerson(person_id='bob') +AliceNote = CreateNote(from_id=Alice.id , to_id=Bob.id , body='Hello Note') + +from forgefed_model import GetActivity +print("Alice=" + str(Alice ) + " => " + str(GetPerson (Alice .id))) +print("Bob=" + str(Bob ) + " => " + str(GetPerson (Bob .id))) +print("AliceNote=" + str(AliceNote) + " => " + str(GetActivity(AliceNote.id))) +from forgefed_model import Db ; from pprint import pprint ; print("Db=") ; pprint(vars(Db)) +# DEBUG END diff --git a/forgefed_model.py b/forgefed_model.py index bc091f4..c53eca3 100644 --- a/forgefed_model.py +++ b/forgefed_model.py @@ -1,23 +1,16 @@ from activitypub.database import ListDatabase from activitypub.manager import Manager -from forgefed_constants import * +from forgefed_constants import LOCAL_CONFIG -def DbId(id): - return DB_ID_PREFIX + id +## getters/setters ## - -def MakeGlobalId(id): - return id + GLOBAL_ID_SUFFIX - - -def CreatePerson(id): - person = GetPerson(id) +def CreatePerson(person_id): + person = GetPerson(person_id) if not person: - person = ApManager.Person(id=id) - person.global_id = MakeGlobalId(id) + person = ApManager.Person(id=person_id) Db.actors.insert_one(person.to_dict()) print("created Person(" + person.id + ")") @@ -25,30 +18,31 @@ def CreatePerson(id): return person -def GetPerson(id): - return Db.actors.find_one({'id' : DbId(id)}) +def CreateNote(from_id , to_id , body , media_type='text/plain'): + note = ApManager.Note(**{'from_id' : from_id , \ + 'to' : [ to_id ] , \ + 'cc' : [ from_id + "/followers" ] , \ + 'tag' : [] , \ + 'source' : { 'mediaType' : media_type , + 'content' : body } , \ + 'sensitive' : False , \ + 'temp_uuid' : "$UUID" , \ + 'temp_text' : body } ) + Db.activities.insert_one(note.to_dict()) + print("created Note(" + note.id + ") " + from_id + " -> " + to_id) + + return note + + +def GetPerson(person_id): + return Db.actors.find_one({'id' : person_id}) + + +def GetActivity(activity_id): + return Db.activities.find_one({'id' : activity_id}) -## main entry ## +## setup ## Db = ListDatabase() -ApManager = Manager(database=Db) -Alice = CreatePerson(id='alice') -AliceNote = ApManager.Note(**{ 'sensitive' : False , \ - 'attributedTo': 'http://localhost:5000' , \ - 'cc' : [ 'http://localhost:5005/followers' ] , \ - 'to' : [ 'https://www.w3.org/ns/activitystreams#Public'] , \ - 'content' : '

$source.content

' , \ - 'tag' : [] , \ - 'source' : { 'mediaType' : 'text/markdown' , \ - 'content' : '$temp_text' } , \ - 'published' : '$NOW' , \ - 'temp_uuid' : "$UUID" , \ - 'temp_text' : 'Hello' , \ - 'id' : 'http://localhost:5005/outbox/$temp_uuid/activity' , \ - 'url' : 'http://localhost:5005/note/$temp_uuid' } ) - - -# DEBUG BEGIN -print("init Alice=" + str(Alice) + " => " + str(GetPerson('alice'))) -# DEBUG END +ApManager = Manager(defaults=LOCAL_CONFIG , database=Db) diff --git a/forgefed_wsgi.py b/forgefed_wsgi.py index 60fb8cb..7ef1df4 100644 --- a/forgefed_wsgi.py +++ b/forgefed_wsgi.py @@ -4,58 +4,52 @@ #import site ; site.addsitedir('~/.local/lib/python3.7/site-packages/') -from activitypub.manager import Manager -from activitypub.database import ListDatabase - - -def inbox_get_handler(env): return [ STATUS_OK , "GET/inbox" ] -def followers_get_handler(env): return [ STATUS_OK , "GET/followers" ] -def following_get_handler(env): return [ STATUS_OK , "GET/following" ] -def liked_get_handler(env): return [ STATUS_OK , "GET/liked" ] -def likes_get_handler(env): return [ STATUS_OK , "GET/likes" ] -def outbox_get_handler(env): return [ STATUS_OK , "GET/outbox" ] -def inbox_post_handler(env): return [ STATUS_OK , "POST/inbox" ] -def followers_post_handler(env): return [ STATUS_OK , "POST/followers" ] -def following_post_handler(env): return [ STATUS_OK , "POST/following" ] -def liked_post_handler(env): return [ STATUS_OK , "POST/liked" ] -def likes_post_handler(env): return [ STATUS_OK , "POST/likes" ] -def outbox_post_handler(env): return [ STATUS_OK , "POST/outbox" ] -ROUTES = { - 'GET/inbox' : inbox_get_handler , - 'GET/followers' : followers_get_handler , - 'GET/following' : following_get_handler , - 'GET/liked' : liked_get_handler , - 'GET/likes' : likes_get_handler , - 'GET/outbox' : outbox_get_handler , - 'POST/inbox' : inbox_post_handler , - 'POST/followers' : followers_post_handler , - 'POST/following' : following_post_handler , - 'POST/liked' : liked_post_handler , - 'POST/likes' : likes_post_handler , - 'POST/outbox' : outbox_post_handler +import re + +from forgefed_constants import PATH_REGEX , RESP_NOT_FOUND +from forgefed_controller import * +from forgefed_model import GetPerson + + +ROUTES = \ +{ \ + 'GET-' : user_get_handler , \ + 'GET-inbox' : inbox_get_handler , \ + 'GET-followers' : followers_get_handler , \ + 'GET-following' : following_get_handler , \ + 'GET-liked' : liked_get_handler , \ + 'GET-likes' : likes_get_handler , \ + 'GET-outbox' : outbox_get_handler , \ + 'POST-inbox' : inbox_post_handler , \ + 'POST-followers' : followers_post_handler , \ + 'POST-following' : following_post_handler , \ + 'POST-liked' : liked_post_handler , \ + 'POST-likes' : likes_post_handler , \ + 'POST-outbox' : outbox_post_handler } def application(env , start_response): # DEBUG BEGIN - import json + import datetime ; print("\nnew request " + str(datetime.datetime.now())) #status = STATUS_OK #body = 'Hello World!' - #body = 'Hello World!\n' + str(alice.to_dict()) - #body = 'Hello World!\n' + json.dumps(str(alice.to_dict()) , sort_keys=True , indent=2 , separators=(', ', ': ')) + '
'
-  #body    = 'Hello World!\n\nalice =>' + str(alice.to_dict()).replace(',' , ',\n\t').replace('{' , '{ ').replace('}' , ' }') + '\n' + \
-  #                          '\nenv =>' + str(env            ).replace(',' , ',\n\t').replace('{' , '{ ').replace('}' , ' }')
+  #body    = 'env =>' + str(env).replace(',' , ',\n\t').replace('{' , '{ ').replace('}' , ' }')
   # DEBUG END
 
 
-  path         = env.get('PATH_INFO').replace('/forge-fed/' , '/')
+  full_path    = env.get('PATH_INFO')
   method       = env.get('REQUEST_METHOD')
   query        = env.get('QUERY_STRING')
-  routes_key   = method + path
+  path         = re.sub(PATH_REGEX , '/' , full_path).split('/')
+  person_id    = path[1] if len(path) >= 2 else ''
+  channel      = path[2] if len(path) >= 3 else ''
+  person       = GetPerson(person_id)
+  routes_key   = method + '-' + channel if person != None else ''
   route_fn     = ROUTES.get(routes_key)
   is_valid_req = route_fn != None
-  resp         = route_fn(env) if is_valid_req else RESP_NOT_FOUND
+  resp         = route_fn(person_id , query) if is_valid_req else RESP_NOT_FOUND
   status       = resp[0]
   body         = resp[1]
   content_type = 'text/plain'
@@ -65,42 +59,33 @@ def application(env , start_response):
 
 
   # DEBUG BEGIN
-  print('path     = ' + path          + '\n' + \
-        'method   = ' + method        + '\n' + \
-        'query    = ' + query         + '\n' + \
-        'route_fn = ' + str(route_fn) + '\n' + \
-        'status   = ' + status        + '\n' + \
-        'body     = ' + body          + '\n' )
+  DbgTraceResp(status , body)
   # DEBUG END
 
 
+  ## respond ##
+
   start_response(status , headers)
 
   return [ body.encode('utf8') ]
 
 
-## main entry ##
-
-Db        = ListDatabase()
-ApManager = Manager(database=Db)
-Alice     = ApManager.Person(id="alice")
-AliceNote = ApManager.Note(**{ 'sensitive'   : False                                              , \
-                               'attributedTo': 'http://localhost:5000'                            , \
-                               'cc'          : [ 'http://localhost:5005/followers' ]              , \
-                               'to'          : [ 'https://www.w3.org/ns/activitystreams#Public']  , \
-                               'content'     : '

$source.content

' , \ - 'tag' : [] , \ - 'source' : { 'mediaType' : 'text/markdown' , \ - 'content' : '$temp_text' } , \ - 'published' : '$NOW' , \ - 'temp_uuid' : "$UUID" , \ - 'temp_text' : 'Hello' , \ - 'id' : 'http://localhost:5005/outbox/$temp_uuid/activity' , \ - 'url' : 'http://localhost:5005/note/$temp_uuid' } ) - -Db.actors.insert_one(Alice.to_dict()) - - # DEBUG BEGIN -print("init Alice=" + str(Alice) + " => " + str(Db.actors.find_one({ 'id' : 'http://localhost:5000/alice' }))) +import json + +def DbgTraceReq(full_path , path , person_id , channel , method , req_body , ap_dict , routes_key , route_fn): + print("full_path = " + full_path ) + print("path = " + '/'.join(path)) + print("person_id = " + person_id ) + print("channel = " + channel ) + print("method = " + method ) + print("req_body = " + req_body ) + print("ap_dict = " + json.dumps(ap_dict , sort_keys=True , indent=2)) + print("routes_key = " + routes_key ) + print("route_fn = " + str(route_fn) ) + +def DbgTraceResp(status , body): + print("status = " + status ) + print("body = " + body ) + # DEBUG END