diff --git a/follow.py b/follow.py index bda044d2a..3ed0d2b8d 100644 --- a/follow.py +++ b/follow.py @@ -24,6 +24,29 @@ from webfinger import webfingerHandle from auth import createBasicAuthHeader from session import postJson +def isFollowingActor(baseDir: str,nickname: str,domain: str,actor: str) -> bool: + """Is the given actor a follower of the given nickname? + """ + if ':' in domain: + domain=domain.split(':')[0] + handle=nickname+'@'+domain + if not os.path.isdir(baseDir+'/accounts/'+handle): + return False + followersFile=baseDir+'/accounts/'+handle+'/followers.txt' + if not os.path.isfile(followersFile): + return False + if actor in open(followersFile).read(): + return True + followerNickname=getNicknameFromActor(actor) + followerDomain,followerPort=getDomainFromActor(actor) + followerHandle=followerNickname+'@'+followerDomain + if followerPort: + if followerPort!=80 and followerPort!=443: + followerHandle+=':'+str(followerPort) + if followerHandle in open(followersFile).read(): + return True + return False + def getFollowersOfPerson(baseDir: str, \ nickname: str,domain: str, \ followFile='following.txt') -> []: @@ -33,7 +56,7 @@ def getFollowersOfPerson(baseDir: str, \ followers=[] if ':' in domain: domain=domain.split(':')[0] - handle=nickname.lower()+'@'+domain.lower() + handle=nickname+'@'+domain if not os.path.isdir(baseDir+'/accounts/'+handle): return followers for subdir, dirs, files in os.walk(baseDir+'/accounts'): diff --git a/webinterface.py b/webinterface.py index 3f5a65081..00af7fad1 100644 --- a/webinterface.py +++ b/webinterface.py @@ -15,6 +15,7 @@ from person import personBoxJson from utils import getNicknameFromActor from utils import getDomainFromActor from posts import getPersonBox +from follow import isFollowingActor def htmlGetLoginCredentials(loginParams: str,lastLoginTime: int) -> (str,str): """Receives login credentials via HTTPServer POST @@ -196,7 +197,7 @@ def htmlProfilePosts(baseDir: str,httpPrefix: str, \ for item in outboxFeed['orderedItems']: if item['type']=='Create': profileStr+= \ - individualPostAsHtml(session,wfRequest,personCache, \ + individualPostAsHtml(baseDir,session,wfRequest,personCache, \ nickname,domain,port,item) return profileStr @@ -402,7 +403,8 @@ def individualFollowAsHtml(session,wfRequest: {}, \ '
'+titleStr+'
'+ \ '\n' -def individualPostAsHtml(session,wfRequest: {},personCache: {}, \ +def individualPostAsHtml(baseDir: str, \ + session,wfRequest: {},personCache: {}, \ nickname: str,domain: str,port: int, \ postJsonObject: {}) -> str: avatarPosition='' @@ -462,14 +464,21 @@ def individualPostAsHtml(session,wfRequest: {},personCache: {}, \ avatarDropdown= \ ' ' \ - ' ' + ' ' + if fullDomain+'/users/'+nickname not in postJsonObject['actor']: + # if not following then show "Follow" in the dropdown + followUnfollowStr='Follow' + # if following then show "Unfollow" in the dropdown + if isFollowingActor(baseDir,nickname,domain,postJsonObject['actor']): + followUnfollowStr='Unfollow' + avatarDropdown= \ ' ' for item in timelineJson['orderedItems']: if item['type']=='Create': - tlStr+=individualPostAsHtml(session,wfRequest,personCache, \ + tlStr+=individualPostAsHtml(baseDir,session,wfRequest,personCache, \ nickname,domain,port,item) tlStr+=htmlFooter() return tlStr @@ -539,12 +548,12 @@ def htmlOutbox(session,baseDir: str,wfRequest: {},personCache: {}, \ return htmlTimeline(session,baseDir,wfRequest,personCache, \ nickname,domain,port,outboxJson,'outbox') -def htmlIndividualPost(session,wfRequest: {},personCache: {}, \ +def htmlIndividualPost(baseDir: str,session,wfRequest: {},personCache: {}, \ nickname: str,domain: str,port: int,postJsonObject: {}) -> str: """Show an individual post as html """ return htmlHeader()+ \ - individualPostAsHtml(session,wfRequest,personCache, \ + individualPostAsHtml(baseDir,session,wfRequest,personCache, \ nickname,domain,port,postJsonObject)+ \ htmlFooter()