From e04db12604122500e8bcff0e48193cda58c3efcd Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 2 Aug 2019 15:36:03 +0100 Subject: [PATCH] Blocking and upblocking from web interface dropdown --- daemon.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ webinterface.py | 72 ++++++++++++++++++++++++++++++++++++- 2 files changed, 167 insertions(+), 1 deletion(-) diff --git a/daemon.py b/daemon.py index 9d3a0436..f7416b9b 100644 --- a/daemon.py +++ b/daemon.py @@ -53,6 +53,8 @@ from like import outboxLike from like import outboxUndoLike from blocking import outboxBlock from blocking import outboxUndoBlock +from blocking import addBlock +from blocking import removeBlock from config import setConfigParam from roles import outboxDelegate from skills import outboxSkills @@ -624,6 +626,23 @@ class PubServer(BaseHTTPRequestHandler): self.server.GETbusy=False return + # block a person from the web interface by selecting Block on the dropdown + if '/users/' in self.path: + if '?block=' in self.path: + blockStr=self.path.split('?block=')[1] + originPathStr=self.path.split('?block=')[0] + if ';' in blockStr: + blockActor=blockStr.split(';')[0] + blockProfileUrl=blockStr.split(';')[1] + # show the confirm block screen + self._set_headers('text/html',cookie) + self.wfile.write(htmlBlockConfirm(self.server.baseDir,originPathStr,blockActor,blockProfileUrl).encode()) + self.server.GETbusy=False + return + self._redirect_headers(originPathStr,cookie) + self.server.GETbusy=False + return + # search for a fediverse address from the web interface by selecting search icon if '/users/' in self.path: if self.path.endswith('/search'): @@ -650,6 +669,23 @@ class PubServer(BaseHTTPRequestHandler): self.server.GETbusy=False return + # Unblock a person from the web interface by selecting Unblock on the dropdown + if '/users/' in self.path: + if '?unblock=' in self.path: + blockStr=self.path.split('?unblock=')[1] + originPathStr=self.path.split('?unblock=')[0] + if ';' in blockStr: + blockActor=blockStr.split(';')[0] + blockProfileUrl=blockStr.split(';')[1] + # show the confirm unblock screen + self._set_headers('text/html',cookie) + self.wfile.write(htmlUnblockConfirm(self.server.baseDir,originPathStr,blockActor,blockProfileUrl).encode()) + self.server.GETbusy=False + return + self._redirect_headers(originPathStr,cookie) + self.server.GETbusy=False + return + # announce/repeat from the web interface if authorized and '?repeat=' in self.path: repeatUrl=self.path.split('?repeat=')[1] @@ -1872,6 +1908,66 @@ class PubServer(BaseHTTPRequestHandler): self.server.POSTbusy=False return + # decision to unblock in the web interface is confirmed + if authorized and self.path.endswith('/unblockconfirm'): + originPathStr=self.path.split('/unblockconfirm')[0] + blockerNickname=getNicknameFromActor(originPathStr) + length = int(self.headers['Content-length']) + blockConfirmParams=self.rfile.read(length).decode('utf-8') + if '&submitYes=' in blockConfirmParams: + blockingActor=blockConfirmParams.replace('%3A',':').replace('%2F','/').split('actor=')[1] + if '&' in blockingActor: + blockingActor=blockingActor.split('&')[0] + blockingNickname=getNicknameFromActor(blockingActor) + blockingDomain,blockingPort=getDomainFromActor(blockingActor) + blockingDomainFull=blockingDomain + if blockingPort: + if blockingPort!=80 and blockingPort!=443: + blockingDomainFull=blockingDomain+':'+str(blockingPort) + if blockerNickname==blockingNickname and \ + blockingDomain==self.server.domain and \ + blockingPort==self.server.port: + if self.server.debug: + print('You cannot unblock yourself!') + else: + if self.server.debug: + print(blockerNickname+' stops blocking '+blockingActor) + removeBlock(self.server.baseDir,blockerNickname,self.server.domain, \ + blockingNickname,blockingDomainFull) + self._redirect_headers(originPathStr,cookie) + self.server.POSTbusy=False + return + + # decision to block in the web interface is confirmed + if authorized and self.path.endswith('/blockconfirm'): + originPathStr=self.path.split('/blockconfirm')[0] + blockerNickname=getNicknameFromActor(originPathStr) + length = int(self.headers['Content-length']) + blockConfirmParams=self.rfile.read(length).decode('utf-8') + if '&submitYes=' in blockConfirmParams: + blockingActor=blockConfirmParams.replace('%3A',':').replace('%2F','/').split('actor=')[1] + if '&' in blockingActor: + blockingActor=blockingActor.split('&')[0] + blockingNickname=getNicknameFromActor(blockingActor) + blockingDomain,blockingPort=getDomainFromActor(blockingActor) + blockingDomainFull=blockingDomain + if blockingPort: + if blockingPort!=80 and blockingPort!=443: + blockingDomainFull=blockingDomain+':'+str(blockingPort) + if blockerNickname==blockingNickname and \ + blockingDomain==self.server.domain and \ + blockingPort==self.server.port: + if self.server.debug: + print('You cannot block yourself!') + else: + if self.server.debug: + print('Adding block by '+blockerNickname+' of '+blockingActor) + addBlock(self.server.baseDir,blockerNickname,self.server.domain, \ + blockingNickname,blockingDomainFull) + self._redirect_headers(originPathStr,cookie) + self.server.POSTbusy=False + return + postState=self._receiveNewPost(authorized,'newpost') if postState!=0: nickname=self.path.split('/users/')[1] diff --git a/webinterface.py b/webinterface.py index 74777c47..50012690 100644 --- a/webinterface.py +++ b/webinterface.py @@ -25,6 +25,7 @@ from session import getJson from auth import createPassword from like import likedByPerson from announce import announcedByPerson +from blocking import isBlocked def htmlEditProfile(baseDir: str,path: str,domain: str,port: int) -> str: """Shows the edit profile screen @@ -640,13 +641,22 @@ def individualPostAsHtml(baseDir: str, \ if isFollowingActor(baseDir,nickname,domain,postJsonObject['actor']): followUnfollowStr='Unfollow' + blockUnblockStr='Block' + # if blocking then show "Unblock" in the dropdown + actorDomainFull=actorDomain + if actorPort: + if actorPort!=80 and actorPort!=443: + actorDomainFull=actorDomain+':'+str(actorPort) + if isBlocked(baseDir,nickname,domain,actorNickname,actorDomainFull): + blockUnblockStr='Unblock' + avatarDropdown= \ ' ' @@ -857,6 +867,66 @@ def htmlUnfollowConfirm(baseDir: str,originPathStr: str,followActor: str,followP followStr+=htmlFooter() return followStr +def htmlBlockConfirm(baseDir: str,originPathStr: str,blockActor: str,blockProfileUrl: str) -> str: + """Asks to confirm a block + """ + blockDomain,port=getDomainFromActor(blockActor) + + if os.path.isfile(baseDir+'/img/block-background.png'): + if not os.path.isfile(baseDir+'/accounts/block-background.png'): + copyfile(baseDir+'/img/block-background.png',baseDir+'/accounts/block-background.png') + + with open(baseDir+'/epicyon-follow.css', 'r') as cssFile: + profileStyle = cssFile.read() + blockStr=htmlHeader(profileStyle) + blockStr+='
' + blockStr+='
' + blockStr+='
' + blockStr+=' ' + blockStr+=' ' + blockStr+='

Block '+getNicknameFromActor(blockActor)+'@'+blockDomain+' ?

' + blockStr+= \ + '
' \ + ' ' \ + ' ' \ + ' ' \ + '
' + blockStr+='
' + blockStr+='
' + blockStr+='
' + blockStr+=htmlFooter() + return blockStr + +def htmlUnblockConfirm(baseDir: str,originPathStr: str,blockActor: str,blockProfileUrl: str) -> str: + """Asks to confirm unblocking an actor + """ + blockDomain,port=getDomainFromActor(blockActor) + + if os.path.isfile(baseDir+'/img/block-background.png'): + if not os.path.isfile(baseDir+'/accounts/block-background.png'): + copyfile(baseDir+'/img/block-background.png',baseDir+'/accounts/block-background.png') + + with open(baseDir+'/epicyon-follow.css', 'r') as cssFile: + profileStyle = cssFile.read() + blockStr=htmlHeader(profileStyle) + blockStr+='
' + blockStr+='
' + blockStr+='
' + blockStr+=' ' + blockStr+=' ' + blockStr+='

Stop blocking '+getNicknameFromActor(blockActor)+'@'+blockDomain+' ?

' + blockStr+= \ + '
' \ + ' ' \ + ' ' \ + ' ' \ + '
' + blockStr+='
' + blockStr+='
' + blockStr+='
' + blockStr+=htmlFooter() + return blockStr + def htmlSearch(baseDir: str,path: str) -> str: """Search called from the timeline icon """