Reduce indentation

merge-requests/8/head
Bob Mottram 2020-09-01 11:24:58 +01:00
parent c1ae893bc7
commit 20f33a580f
1 changed files with 178 additions and 225 deletions

403
daemon.py
View File

@ -4893,233 +4893,186 @@ class PubServer(BaseHTTPRequestHandler):
"""Shows the replies timeline """Shows the replies timeline
Returns true if the timeline was shown Returns true if the timeline was shown
""" """
if '/statuses/' in path and '/users/' in path: if not ('/statuses/' in path and '/users/' in path):
namedStatus = path.split('/users/')[1] return False
if '/' in namedStatus: namedStatus = path.split('/users/')[1]
postSections = namedStatus.split('/') if '/' not in namedStatus:
if len(postSections) >= 4: return False
if postSections[3].startswith('replies'): postSections = namedStatus.split('/')
nickname = postSections[0] if len(postSections) < 4:
statusNumber = postSections[2] return False
if len(statusNumber) > 10 and \ if not postSections[3].startswith('replies'):
statusNumber.isdigit(): return False
boxname = 'outbox' nickname = postSections[0]
# get the replies file statusNumber = postSections[2]
postDir = \ if not (len(statusNumber) > 10 and statusNumber.isdigit()):
baseDir + '/accounts/' + \ return False
nickname + '@' + domain+'/' + \
boxname
postRepliesFilename = \
postDir + '/' + \
httpPrefix + ':##' + \
domainFull + '#users#' + \
nickname + '#statuses#' + \
statusNumber + '.replies'
if not os.path.isfile(postRepliesFilename):
# There are no replies,
# so show empty collection
contextStr = \
'https://www.w3.org/ns/activitystreams'
firstStr = \
httpPrefix + \
'://' + domainFull + \
'/users/' + nickname + \
'/statuses/' + statusNumber + \
'/replies?page=true'
idStr = \
httpPrefix + \
'://' + domainFull + \
'/users/' + nickname + \
'/statuses/' + statusNumber + \
'/replies'
lastStr = \
httpPrefix + \
'://' + domainFull + \
'/users/' + nickname + \
'/statuses/' + statusNumber + \
'/replies?page=true'
repliesJson = {
'@context': contextStr,
'first': firstStr,
'id': idStr,
'last': lastStr,
'totalItems': 0,
'type': 'OrderedCollection'
}
if self._requestHTTP():
if not self.server.session:
print('DEBUG: ' +
'creating new session ' +
'during get replies')
self.server.session = \
createSession(proxyType)
if not self.server.session:
print('ERROR: GET failed to ' +
'create session ' +
'during get replies')
self._404()
self.server.GETbusy = False
return
recentPostsCache = \
self.server.recentPostsCache
maxRecentPosts = \
self.server.maxRecentPosts
translate = \
self.server.translate
session = \
self.server.session
cachedWebfingers = \
self.server.cachedWebfingers
personCache = \
self.server.personCache
projectVersion = \
self.server.projectVersion
ytDomain = \
self.server.YTReplacementDomain
msg = \
htmlPostReplies(recentPostsCache,
maxRecentPosts,
translate,
baseDir,
session,
cachedWebfingers,
personCache,
nickname,
domain,
port,
repliesJson,
httpPrefix,
projectVersion,
ytDomain)
msg = msg.encode('utf-8')
self._set_headers('text/html',
len(msg),
cookie,
callingDomain)
self._write(msg)
else:
if self._fetchAuthenticated():
msg = \
json.dumps(repliesJson,
ensure_ascii=False)
msg = msg.encode('utf-8')
protocolStr = 'application/json'
self._set_headers(protocolStr,
len(msg), None,
callingDomain)
self._write(msg)
else:
self._404()
self.server.GETbusy = False
return True
else:
# replies exist. Itterate through the
# text file containing message ids
contextStr = \
'https://www.w3.org/ns/activitystreams'
idStr = \
httpPrefix + \
'://' + domainFull + \
'/users/' + nickname + '/statuses/' + \
statusNumber + '?page=true'
partOfStr = \
httpPrefix + \
'://' + domainFull + \
'/users/' + nickname + \
'/statuses/' + statusNumber
repliesJson = {
'@context': contextStr,
'id': idStr,
'orderedItems': [
],
'partOf': partOfStr,
'type': 'OrderedCollectionPage'
}
# populate the items list with replies boxname = 'outbox'
populateRepliesJson(baseDir, # get the replies file
nickname, postDir = \
domain, baseDir + '/accounts/' + nickname + '@' + domain + '/' + boxname
postRepliesFilename, postRepliesFilename = \
authorized, postDir + '/' + \
repliesJson) httpPrefix + ':##' + domainFull + '#users#' + \
nickname + '#statuses#' + statusNumber + '.replies'
if not os.path.isfile(postRepliesFilename):
# There are no replies,
# so show empty collection
contextStr = \
'https://www.w3.org/ns/activitystreams'
firstStr = \
httpPrefix + '://' + domainFull + '/users/' + nickname + \
'/statuses/' + statusNumber + '/replies?page=true'
idStr = \
httpPrefix + '://' + domainFull + '/users/' + nickname + \
'/statuses/' + statusNumber + '/replies'
lastStr = \
httpPrefix + '://' + domainFull + '/users/' + nickname + \
'/statuses/' + statusNumber + '/replies?page=true'
repliesJson = {
'@context': contextStr,
'first': firstStr,
'id': idStr,
'last': lastStr,
'totalItems': 0,
'type': 'OrderedCollection'
}
if self._requestHTTP():
if not self.server.session:
print('DEBUG: creating new session during get replies')
self.server.session = createSession(proxyType)
if not self.server.session:
print('ERROR: GET failed to create session ' +
'during get replies')
self._404()
self.server.GETbusy = False
return
recentPostsCache = self.server.recentPostsCache
maxRecentPosts = self.server.maxRecentPosts
translate = self.server.translate
session = self.server.session
cachedWebfingers = self.server.cachedWebfingers
personCache = self.server.personCache
projectVersion = self.server.projectVersion
ytDomain = self.server.YTReplacementDomain
msg = \
htmlPostReplies(recentPostsCache,
maxRecentPosts,
translate,
baseDir,
session,
cachedWebfingers,
personCache,
nickname,
domain,
port,
repliesJson,
httpPrefix,
projectVersion,
ytDomain)
msg = msg.encode('utf-8')
self._set_headers('text/html', len(msg),
cookie, callingDomain)
self._write(msg)
else:
if self._fetchAuthenticated():
msg = json.dumps(repliesJson, ensure_ascii=False)
msg = msg.encode('utf-8')
protocolStr = 'application/json'
self._set_headers(protocolStr, len(msg), None,
callingDomain)
self._write(msg)
else:
self._404()
self.server.GETbusy = False
return True
else:
# replies exist. Itterate through the
# text file containing message ids
contextStr = 'https://www.w3.org/ns/activitystreams'
idStr = \
httpPrefix + '://' + domainFull + \
'/users/' + nickname + '/statuses/' + \
statusNumber + '?page=true'
partOfStr = \
httpPrefix + '://' + domainFull + \
'/users/' + nickname + '/statuses/' + statusNumber
repliesJson = {
'@context': contextStr,
'id': idStr,
'orderedItems': [
],
'partOf': partOfStr,
'type': 'OrderedCollectionPage'
}
# send the replies json # populate the items list with replies
if self._requestHTTP(): populateRepliesJson(baseDir,
if not self.server.session: nickname,
print('DEBUG: ' + domain,
'creating new session ' + postRepliesFilename,
'during get replies 2') authorized,
self.server.session = \ repliesJson)
createSession(proxyType)
if not self.server.session: # send the replies json
print('ERROR: GET failed to ' + if self._requestHTTP():
'create session ' + if not self.server.session:
'during get replies 2') print('DEBUG: creating new session ' +
self._404() 'during get replies 2')
self.server.GETbusy = False self.server.session = createSession(proxyType)
return if not self.server.session:
recentPostsCache = \ print('ERROR: GET failed to ' +
self.server.recentPostsCache 'create session ' +
maxRecentPosts = \ 'during get replies 2')
self.server.maxRecentPosts self._404()
translate = \ self.server.GETbusy = False
self.server.translate return
session = \ recentPostsCache = self.server.recentPostsCache
self.server.session maxRecentPosts = self.server.maxRecentPosts
cachedWebfingers = \ translate = self.server.translate
self.server.cachedWebfingers session = self.server.session
personCache = \ cachedWebfingers = self.server.cachedWebfingers
self.server.personCache personCache = self.server.personCache
projectVersion = \ projectVersion = self.server.projectVersion
self.server.projectVersion ytDomain = self.server.YTReplacementDomain
ytDomain = \ msg = \
self.server.YTReplacementDomain htmlPostReplies(recentPostsCache,
msg = \ maxRecentPosts,
htmlPostReplies(recentPostsCache, translate,
maxRecentPosts, baseDir,
translate, session,
baseDir, cachedWebfingers,
session, personCache,
cachedWebfingers, nickname,
personCache, domain,
nickname, port,
self.server.domain, repliesJson,
self.server.port, httpPrefix,
repliesJson, projectVersion,
httpPrefix, ytDomain)
projectVersion, msg = msg.encode('utf-8')
ytDomain) self._set_headers('text/html', len(msg),
msg = msg.encode('utf-8') cookie, callingDomain)
self._set_headers('text/html', self._write(msg)
len(msg), self._benchmarkGETtimings(GETstartTime,
cookie, GETtimings,
callingDomain) 'individual post done',
self._write(msg) 'post replies done')
self._benchmarkGETtimings(GETstartTime, else:
GETtimings, if self._fetchAuthenticated():
'indiv' + msg = \
'idual' + json.dumps(repliesJson,
' post done', ensure_ascii=False)
'post ' + msg = msg.encode('utf-8')
'replies ' + protocolStr = 'application/json'
'done') self._set_headers(protocolStr, len(msg),
else: None, callingDomain)
if self._fetchAuthenticated(): self._write(msg)
msg = \ else:
json.dumps(repliesJson, self._404()
ensure_ascii=False) self.server.GETbusy = False
msg = msg.encode('utf-8') return True
protocolStr = 'application/json'
self._set_headers(protocolStr,
len(msg),
None,
callingDomain)
self._write(msg)
else:
self._404()
self.server.GETbusy = False
return True
return False return False
def do_GET(self): def do_GET(self):