mirror of https://gitlab.com/bashrc2/epicyon
flake8 style
parent
0f1e754913
commit
fa7c457150
35
blurhash.py
35
blurhash.py
|
@ -31,10 +31,12 @@ Very close port of the original Swift implementation by Dag Ågren.
|
|||
|
||||
import math
|
||||
|
||||
|
||||
# Alphabet for base 83
|
||||
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~"
|
||||
alphabet_values = dict(zip(alphabet, range(len(alphabet))))
|
||||
|
||||
|
||||
def base83_decode(base83_str):
|
||||
"""
|
||||
Decodes a base83 string, as used in blurhash, to an integer.
|
||||
|
@ -44,6 +46,7 @@ def base83_decode(base83_str):
|
|||
value = value * 83 + alphabet_values[base83_char]
|
||||
return value
|
||||
|
||||
|
||||
def base83_encode(value, length):
|
||||
"""
|
||||
Decodes an integer to a base83 string, as used in blurhash.
|
||||
|
@ -60,6 +63,7 @@ def base83_encode(value, length):
|
|||
result += alphabet[int(digit)]
|
||||
return result
|
||||
|
||||
|
||||
def srgb_to_linear(value):
|
||||
"""
|
||||
srgb 0-255 integer to linear 0.0-1.0 floating point conversion.
|
||||
|
@ -69,12 +73,14 @@ def srgb_to_linear(value):
|
|||
return value / 12.92
|
||||
return math.pow((value + 0.055) / 1.055, 2.4)
|
||||
|
||||
|
||||
def sign_pow(value, exp):
|
||||
"""
|
||||
Sign-preserving exponentiation.
|
||||
"""
|
||||
return math.copysign(math.pow(abs(value), exp), value)
|
||||
|
||||
|
||||
def linear_to_srgb(value):
|
||||
"""
|
||||
linear 0.0-1.0 floating point to srgb 0-255 integer conversion.
|
||||
|
@ -84,6 +90,7 @@ def linear_to_srgb(value):
|
|||
return int(value * 12.92 * 255 + 0.5)
|
||||
return int((1.055 * math.pow(value, 1 / 2.4) - 0.055) * 255 + 0.5)
|
||||
|
||||
|
||||
def blurhash_components(blurhash):
|
||||
"""
|
||||
Decodes and returns the number of x and y components in the given blurhash.
|
||||
|
@ -98,6 +105,7 @@ def blurhash_components(blurhash):
|
|||
|
||||
return size_x, size_y
|
||||
|
||||
|
||||
def blurhash_decode(blurhash, width, height, punch=1.0, linear=False):
|
||||
"""
|
||||
Decodes the given blurhash to an image of the specified size.
|
||||
|
@ -155,8 +163,7 @@ def blurhash_decode(blurhash,width,height,punch=1.0,linear=False):
|
|||
|
||||
for j in range(size_y):
|
||||
for i in range(size_x):
|
||||
basis= \
|
||||
math.cos(math.pi*float(x)*float(i)/float(width))* \
|
||||
basis = math.cos(math.pi * float(x) * float(i) / float(width)) * \
|
||||
math.cos(math.pi * float(y) * float(j) / float(height))
|
||||
colour = colours[i + j * size_x]
|
||||
pixel[0] += colour[0] * basis
|
||||
|
@ -173,6 +180,7 @@ def blurhash_decode(blurhash,width,height,punch=1.0,linear=False):
|
|||
pixels.append(pixel_row)
|
||||
return pixels
|
||||
|
||||
|
||||
def blurhash_encode(image, components_x=4, components_y=4, linear=False):
|
||||
"""
|
||||
Calculates the blurhash for an image using the given x and y component counts.
|
||||
|
@ -185,8 +193,10 @@ def blurhash_encode(image,components_x=4,components_y=4,linear=False):
|
|||
useful if you want to encode a version of your image resized to a smaller size (which
|
||||
you should ideally do in linear colour).
|
||||
"""
|
||||
if components_x < 1 or components_x > 9 or components_y < 1 or components_y > 9:
|
||||
raise ValueError("x and y component counts must be between 1 and 9 inclusive.")
|
||||
if components_x < 1 or components_x > 9 or \
|
||||
components_y < 1 or components_y > 9:
|
||||
raise ValueError("x and y component counts must be " +
|
||||
"between 1 and 9 inclusive.")
|
||||
height = float(len(image))
|
||||
width = float(len(image[0]))
|
||||
|
||||
|
@ -214,8 +224,7 @@ def blurhash_encode(image,components_x=4,components_y=4,linear=False):
|
|||
component = [0.0, 0.0, 0.0]
|
||||
for y in range(int(height)):
|
||||
for x in range(int(width)):
|
||||
basis= \
|
||||
norm_factor * math.cos(math.pi * float(i) * float(x) / width) * \
|
||||
basis = norm_factor * math.cos(math.pi * float(i) * float(x) / width) * \
|
||||
math.cos(math.pi * float(j) * float(y) / height)
|
||||
component[0] += basis * image_linear[y][x][0]
|
||||
component[1] += basis * image_linear[y][x][1]
|
||||
|
@ -227,20 +236,18 @@ def blurhash_encode(image,components_x=4,components_y=4,linear=False):
|
|||
components.append(component)
|
||||
|
||||
if not (i==0 and j==0):
|
||||
max_ac_component= \
|
||||
max(max_ac_component,abs(component[0]), \
|
||||
max_ac_component = max(max_ac_component,abs(component[0]),
|
||||
abs(component[1]),abs(component[2]))
|
||||
|
||||
# Encode components
|
||||
dc_value= \
|
||||
(linear_to_srgb(components[0][0]) << 16)+ \
|
||||
dc_value = (linear_to_srgb(components[0][0]) << 16) + \
|
||||
(linear_to_srgb(components[0][1]) << 8) + \
|
||||
linear_to_srgb(components[0][2])
|
||||
|
||||
quant_max_ac_component= \
|
||||
int(max(0, min(82, math.floor(max_ac_component * 166 - 0.5))))
|
||||
ac_component_norm_factor= \
|
||||
float(quant_max_ac_component+1) / 166.0
|
||||
quant_max_ac_component = int(max(0, min(82,
|
||||
math.floor(max_ac_component *
|
||||
166 - 0.5))))
|
||||
ac_component_norm_factor = float(quant_max_ac_component + 1) / 166.0
|
||||
|
||||
ac_values = []
|
||||
for r, g, b in components[1:]:
|
||||
|
|
346
bookmarks.py
346
bookmarks.py
|
@ -7,8 +7,6 @@ __email__="bob@freedombone.net"
|
|||
__status__ = "Production"
|
||||
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
from pprint import pprint
|
||||
from utils import removePostFromCache
|
||||
from utils import urlPermitted
|
||||
|
@ -24,8 +22,10 @@ from webfinger import webfingerHandle
|
|||
from auth import createBasicAuthHeader
|
||||
from posts import getPersonBox
|
||||
|
||||
def undoBookmarksCollectionEntry(recentPostsCache: {}, \
|
||||
baseDir: str,postFilename: str,objectUrl: str, \
|
||||
|
||||
def undoBookmarksCollectionEntry(recentPostsCache: {},
|
||||
baseDir: str, postFilename: str,
|
||||
objectUrl: str,
|
||||
actor: str, domain: str, debug: bool) -> None:
|
||||
"""Undoes a bookmark for a particular actor
|
||||
"""
|
||||
|
@ -33,10 +33,11 @@ def undoBookmarksCollectionEntry(recentPostsCache: {}, \
|
|||
if not postJsonObject:
|
||||
return
|
||||
|
||||
# remove any cached version of this post so that the bookmark icon is changed
|
||||
# remove any cached version of this post so that the
|
||||
# bookmark icon is changed
|
||||
nickname = getNicknameFromActor(actor)
|
||||
cachedPostFilename= \
|
||||
getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
|
||||
cachedPostFilename = getCachedPostFilename(baseDir, nickname,
|
||||
domain, postJsonObject)
|
||||
if cachedPostFilename:
|
||||
if os.path.isfile(cachedPostFilename):
|
||||
os.remove(cachedPostFilename)
|
||||
|
@ -68,7 +69,8 @@ def undoBookmarksCollectionEntry(recentPostsCache: {}, \
|
|||
if bookmarkItem['actor'] == actor:
|
||||
if debug:
|
||||
print('DEBUG: bookmark was removed for ' + actor)
|
||||
postJsonObject['object']['bookmarks']['items'].remove(bookmarkItem)
|
||||
bmIt = bookmarkItem
|
||||
postJsonObject['object']['bookmarks']['items'].remove(bmIt)
|
||||
itemFound = True
|
||||
break
|
||||
|
||||
|
@ -80,13 +82,13 @@ def undoBookmarksCollectionEntry(recentPostsCache: {}, \
|
|||
print('DEBUG: bookmarks was removed from post')
|
||||
del postJsonObject['object']['bookmarks']
|
||||
else:
|
||||
postJsonObject['object']['bookmarks']['totalItems']= \
|
||||
len(postJsonObject['bookmarks']['items'])
|
||||
bmItLen = len(postJsonObject['object']['bookmarks']['items'])
|
||||
postJsonObject['object']['bookmarks']['totalItems'] = bmItLen
|
||||
saveJson(postJsonObject, postFilename)
|
||||
|
||||
# remove from the index
|
||||
bookmarksIndexFilename= \
|
||||
baseDir+'/accounts/'+nickname+'@'+domain+'/bookmarks.index'
|
||||
bookmarksIndexFilename = baseDir + '/accounts/' + \
|
||||
nickname + '@' + domain + '/bookmarks.index'
|
||||
if not os.path.isfile(bookmarksIndexFilename):
|
||||
return
|
||||
if '/' in postFilename:
|
||||
|
@ -106,6 +108,7 @@ def undoBookmarksCollectionEntry(recentPostsCache: {}, \
|
|||
bookmarksIndexFile.write(indexStr)
|
||||
bookmarksIndexFile.close()
|
||||
|
||||
|
||||
def bookmarkedByPerson(postJsonObject: {}, nickname: str, domain: str) -> bool:
|
||||
"""Returns True if the given post is bookmarked by the given person
|
||||
"""
|
||||
|
@ -117,6 +120,7 @@ def bookmarkedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool:
|
|||
return True
|
||||
return False
|
||||
|
||||
|
||||
def noOfBookmarks(postJsonObject: {}) -> int:
|
||||
"""Returns the number of bookmarks ona given post
|
||||
"""
|
||||
|
@ -133,18 +137,20 @@ def noOfBookmarks(postJsonObject: {}) -> int:
|
|||
postJsonObject['object']['bookmarks']['totalItems'] = 0
|
||||
return len(postJsonObject['object']['bookmarks']['items'])
|
||||
|
||||
def updateBookmarksCollection(recentPostsCache: {}, \
|
||||
baseDir: str,postFilename: str, \
|
||||
objectUrl: str, \
|
||||
|
||||
def updateBookmarksCollection(recentPostsCache: {},
|
||||
baseDir: str, postFilename: str,
|
||||
objectUrl: str,
|
||||
actor: str, domain: str, debug: bool) -> None:
|
||||
"""Updates the bookmarks collection within a post
|
||||
"""
|
||||
postJsonObject = loadJson(postFilename)
|
||||
if postJsonObject:
|
||||
# remove any cached version of this post so that the bookmark icon is changed
|
||||
# remove any cached version of this post so that the
|
||||
# bookmark icon is changed
|
||||
nickname = getNicknameFromActor(actor)
|
||||
cachedPostFilename= \
|
||||
getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
|
||||
cachedPostFilename = getCachedPostFilename(baseDir, nickname,
|
||||
domain, postJsonObject)
|
||||
if cachedPostFilename:
|
||||
if os.path.isfile(cachedPostFilename):
|
||||
os.remove(cachedPostFilename)
|
||||
|
@ -182,9 +188,10 @@ def updateBookmarksCollection(recentPostsCache: {}, \
|
|||
'type': 'Bookmark',
|
||||
'actor': actor
|
||||
}
|
||||
postJsonObject['object']['bookmarks']['items'].append(newBookmark)
|
||||
postJsonObject['object']['bookmarks']['totalItems']= \
|
||||
len(postJsonObject['object']['bookmarks']['items'])
|
||||
nb = newBookmark
|
||||
bmIt = len(postJsonObject['object']['bookmarks']['items'])
|
||||
postJsonObject['object']['bookmarks']['items'].append(nb)
|
||||
postJsonObject['object']['bookmarks']['totalItems'] = bmIt
|
||||
|
||||
if debug:
|
||||
print('DEBUG: saving post with bookmarks added')
|
||||
|
@ -193,20 +200,20 @@ def updateBookmarksCollection(recentPostsCache: {}, \
|
|||
saveJson(postJsonObject, postFilename)
|
||||
|
||||
# prepend to the index
|
||||
bookmarksIndexFilename= \
|
||||
baseDir+'/accounts/'+nickname+'@'+domain+'/bookmarks.index'
|
||||
bookmarksIndexFilename = baseDir + '/accounts/' + \
|
||||
nickname + '@' + domain + '/bookmarks.index'
|
||||
bookmarkIndex = postFilename.split('/')[-1]
|
||||
if os.path.isfile(bookmarksIndexFilename):
|
||||
if bookmarkIndex not in open(bookmarksIndexFilename).read():
|
||||
try:
|
||||
with open(bookmarksIndexFilename, 'r+') as bookmarksIndexFile:
|
||||
content=bookmarksIndexFile.read()
|
||||
bookmarksIndexFile.seek(0, 0)
|
||||
bookmarksIndexFile.write(bookmarkIndex+'\n'+content)
|
||||
with open(bookmarksIndexFilename, 'r+') as bmIndexFile:
|
||||
content = bmIndexFile.read()
|
||||
bmIndexFile.seek(0, 0)
|
||||
bmIndexFile.write(bookmarkIndex + '\n' + content)
|
||||
if debug:
|
||||
print('DEBUG: bookmark added to index')
|
||||
except Exception as e:
|
||||
print('WARN: Failed to write entry to bookmarks index '+ \
|
||||
print('WARN: Failed to write entry to bookmarks index ' +
|
||||
bookmarksIndexFilename + ' ' + str(e))
|
||||
else:
|
||||
bookmarksIndexFile = open(bookmarksIndexFilename, 'w')
|
||||
|
@ -214,14 +221,15 @@ def updateBookmarksCollection(recentPostsCache: {}, \
|
|||
bookmarksIndexFile.write(bookmarkIndex + '\n')
|
||||
bookmarksIndexFile.close()
|
||||
|
||||
def bookmark(recentPostsCache: {}, \
|
||||
session,baseDir: str,federationList: [], \
|
||||
nickname: str,domain: str,port: int, \
|
||||
ccList: [],httpPrefix: str, \
|
||||
objectUrl: str,actorBookmarked: str, \
|
||||
clientToServer: bool, \
|
||||
sendThreads: [],postLog: [], \
|
||||
personCache: {},cachedWebfingers: {}, \
|
||||
|
||||
def bookmark(recentPostsCache: {},
|
||||
session, baseDir: str, federationList: [],
|
||||
nickname: str, domain: str, port: int,
|
||||
ccList: [], httpPrefix: str,
|
||||
objectUrl: str, actorBookmarked: str,
|
||||
clientToServer: bool,
|
||||
sendThreads: [], postLog: [],
|
||||
personCache: {}, cachedWebfingers: {},
|
||||
debug: bool, projectVersion: str) -> {}:
|
||||
"""Creates a bookmark
|
||||
actor is the person doing the bookmarking
|
||||
|
@ -237,10 +245,6 @@ def bookmark(recentPostsCache: {}, \
|
|||
if ':' not in domain:
|
||||
fullDomain = domain + ':' + str(port)
|
||||
|
||||
bookmarkTo=[]
|
||||
if '/statuses/' in objectUrl:
|
||||
bookmarkTo=[objectUrl.split('/statuses/')[0]]
|
||||
|
||||
newBookmarkJson = {
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
'type': 'Bookmark',
|
||||
|
@ -256,16 +260,16 @@ def bookmark(recentPostsCache: {}, \
|
|||
bookmarkedPostDomain = None
|
||||
bookmarkedPostPort = None
|
||||
if actorBookmarked:
|
||||
bookmarkedPostNickname=getNicknameFromActor(actorBookmarked)
|
||||
bookmarkedPostDomain,bookmarkedPostPort= \
|
||||
getDomainFromActor(actorBookmarked)
|
||||
acBm = actorBookmarked
|
||||
bookmarkedPostNickname = getNicknameFromActor(acBm)
|
||||
bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(acBm)
|
||||
else:
|
||||
if '/users/' in objectUrl or \
|
||||
'/channel/' in objectUrl or \
|
||||
'/profile/' in objectUrl:
|
||||
bookmarkedPostNickname=getNicknameFromActor(objectUrl)
|
||||
bookmarkedPostDomain,bookmarkedPostPort= \
|
||||
getDomainFromActor(objectUrl)
|
||||
ou = objectUrl
|
||||
bookmarkedPostNickname = getNicknameFromActor(ou)
|
||||
bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(ou)
|
||||
|
||||
if bookmarkedPostNickname:
|
||||
postFilename = locatePost(baseDir, nickname, domain, objectUrl)
|
||||
|
@ -276,29 +280,31 @@ def bookmark(recentPostsCache: {}, \
|
|||
print('DEBUG: bookmark objectUrl: ' + objectUrl)
|
||||
return None
|
||||
|
||||
updateBookmarksCollection(recentPostsCache, \
|
||||
baseDir,postFilename,objectUrl, \
|
||||
updateBookmarksCollection(recentPostsCache,
|
||||
baseDir, postFilename, objectUrl,
|
||||
newBookmarkJson['actor'], domain, debug)
|
||||
|
||||
sendSignedJson(newBookmarkJson,session,baseDir, \
|
||||
nickname,domain,port, \
|
||||
bookmarkedPostNickname, \
|
||||
bookmarkedPostDomain,bookmarkedPostPort, \
|
||||
'https://www.w3.org/ns/activitystreams#Public', \
|
||||
httpPrefix,True,clientToServer,federationList, \
|
||||
sendThreads,postLog,cachedWebfingers,personCache, \
|
||||
sendSignedJson(newBookmarkJson, session, baseDir,
|
||||
nickname, domain, port,
|
||||
bookmarkedPostNickname,
|
||||
bookmarkedPostDomain, bookmarkedPostPort,
|
||||
'https://www.w3.org/ns/activitystreams#Public',
|
||||
httpPrefix, True, clientToServer, federationList,
|
||||
sendThreads, postLog, cachedWebfingers, personCache,
|
||||
debug, projectVersion)
|
||||
|
||||
return newBookmarkJson
|
||||
|
||||
def bookmarkPost(recentPostsCache: {}, \
|
||||
session,baseDir: str,federationList: [], \
|
||||
nickname: str,domain: str,port: int,httpPrefix: str, \
|
||||
bookmarkNickname: str,bookmarkedomain: str,bookmarkPort: int, \
|
||||
ccList: [], \
|
||||
bookmarkStatusNumber: int,clientToServer: bool, \
|
||||
sendThreads: [],postLog: [], \
|
||||
personCache: {},cachedWebfingers: {}, \
|
||||
|
||||
def bookmarkPost(recentPostsCache: {},
|
||||
session, baseDir: str, federationList: [],
|
||||
nickname: str, domain: str, port: int, httpPrefix: str,
|
||||
bookmarkNickname: str, bookmarkedomain: str,
|
||||
bookmarkPort: int,
|
||||
ccList: [],
|
||||
bookmarkStatusNumber: int, clientToServer: bool,
|
||||
sendThreads: [], postLog: [],
|
||||
personCache: {}, cachedWebfingers: {},
|
||||
debug: bool, projectVersion: str) -> {}:
|
||||
"""Bookmarks a given status post. This is only used by unit tests
|
||||
"""
|
||||
|
@ -308,32 +314,26 @@ def bookmarkPost(recentPostsCache: {}, \
|
|||
if ':' not in bookmarkedomain:
|
||||
bookmarkedomain = bookmarkedomain + ':' + str(bookmarkPort)
|
||||
|
||||
actorBookmarked= \
|
||||
httpPrefix + '://'+bookmarkedomain+'/users/'+bookmarkNickname
|
||||
actorBookmarked = httpPrefix + '://' + bookmarkedomain + \
|
||||
'/users/' + bookmarkNickname
|
||||
objectUrl = actorBookmarked + '/statuses/' + str(bookmarkStatusNumber)
|
||||
|
||||
ccUrl=httpPrefix+'://'+bookmarkedomain+'/users/'+bookmarkNickname
|
||||
if bookmarkPort:
|
||||
if bookmarkPort!=80 and bookmarkPort!=443:
|
||||
if ':' not in bookmarkedomain:
|
||||
ccUrl= \
|
||||
httpPrefix+'://'+bookmarkedomain+':'+ \
|
||||
str(bookmarkPort)+'/users/'+bookmarkNickname
|
||||
|
||||
return bookmark(recentPostsCache, \
|
||||
session,baseDir,federationList,nickname,domain,port, \
|
||||
ccList,httpPrefix,objectUrl,actorBookmarked,clientToServer, \
|
||||
sendThreads,postLog,personCache,cachedWebfingers, \
|
||||
return bookmark(recentPostsCache,
|
||||
session, baseDir, federationList, nickname, domain, port,
|
||||
ccList, httpPrefix, objectUrl, actorBookmarked,
|
||||
clientToServer,
|
||||
sendThreads, postLog, personCache, cachedWebfingers,
|
||||
debug, projectVersion)
|
||||
|
||||
def undoBookmark(recentPostsCache: {}, \
|
||||
session,baseDir: str,federationList: [], \
|
||||
nickname: str,domain: str,port: int, \
|
||||
ccList: [],httpPrefix: str, \
|
||||
objectUrl: str,actorBookmarked: str, \
|
||||
clientToServer: bool, \
|
||||
sendThreads: [],postLog: [], \
|
||||
personCache: {},cachedWebfingers: {}, \
|
||||
|
||||
def undoBookmark(recentPostsCache: {},
|
||||
session, baseDir: str, federationList: [],
|
||||
nickname: str, domain: str, port: int,
|
||||
ccList: [], httpPrefix: str,
|
||||
objectUrl: str, actorBookmarked: str,
|
||||
clientToServer: bool,
|
||||
sendThreads: [], postLog: [],
|
||||
personCache: {}, cachedWebfingers: {},
|
||||
debug: bool, projectVersion: str) -> {}:
|
||||
"""Removes a bookmark
|
||||
actor is the person doing the bookmarking
|
||||
|
@ -349,10 +349,6 @@ def undoBookmark(recentPostsCache: {}, \
|
|||
if ':' not in domain:
|
||||
fullDomain = domain + ':' + str(port)
|
||||
|
||||
bookmarkTo=[]
|
||||
if '/statuses/' in objectUrl:
|
||||
bookmarkTo=[objectUrl.split('/statuses/')[0]]
|
||||
|
||||
newUndoBookmarkJson = {
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
'type': 'Undo',
|
||||
|
@ -373,45 +369,48 @@ def undoBookmark(recentPostsCache: {}, \
|
|||
bookmarkedPostDomain = None
|
||||
bookmarkedPostPort = None
|
||||
if actorBookmarked:
|
||||
bookmarkedPostNickname=getNicknameFromActor(actorBookmarked)
|
||||
bookmarkedPostDomain,bookmarkedPostPort= \
|
||||
getDomainFromActor(actorBookmarked)
|
||||
acBm = actorBookmarked
|
||||
bookmarkedPostNickname = getNicknameFromActor(acBm)
|
||||
bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(acBm)
|
||||
else:
|
||||
if '/users/' in objectUrl or \
|
||||
'/channel/' in objectUrl or \
|
||||
'/profile/' in objectUrl:
|
||||
bookmarkedPostNickname=getNicknameFromActor(objectUrl)
|
||||
bookmarkedPostDomain,bookmarkedPostPort= \
|
||||
getDomainFromActor(objectUrl)
|
||||
ou = objectUrl
|
||||
bookmarkedPostNickname = getNicknameFromActor(ou)
|
||||
bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(ou)
|
||||
|
||||
if bookmarkedPostNickname:
|
||||
postFilename = locatePost(baseDir, nickname, domain, objectUrl)
|
||||
if not postFilename:
|
||||
return None
|
||||
|
||||
undoBookmarksCollectionEntry(recentPostsCache, \
|
||||
baseDir,postFilename,objectUrl, \
|
||||
newBookmarkJson['actor'],domain,debug)
|
||||
undoBookmarksCollectionEntry(recentPostsCache,
|
||||
baseDir, postFilename, objectUrl,
|
||||
newUndoBookmarkJson['actor'],
|
||||
domain, debug)
|
||||
|
||||
sendSignedJson(newUndoBookmarkJson,session,baseDir, \
|
||||
nickname,domain,port, \
|
||||
bookmarkedPostNickname,bookmarkedPostDomain,bookmarkedPostPort, \
|
||||
'https://www.w3.org/ns/activitystreams#Public', \
|
||||
httpPrefix,True,clientToServer,federationList, \
|
||||
sendThreads,postLog,cachedWebfingers,personCache, \
|
||||
sendSignedJson(newUndoBookmarkJson, session, baseDir,
|
||||
nickname, domain, port,
|
||||
bookmarkedPostNickname, bookmarkedPostDomain,
|
||||
bookmarkedPostPort,
|
||||
'https://www.w3.org/ns/activitystreams#Public',
|
||||
httpPrefix, True, clientToServer, federationList,
|
||||
sendThreads, postLog, cachedWebfingers, personCache,
|
||||
debug, projectVersion)
|
||||
else:
|
||||
return None
|
||||
|
||||
return newUndoBookmarkJson
|
||||
|
||||
def undoBookmarkPost(session,baseDir: str,federationList: [], \
|
||||
nickname: str,domain: str,port: int,httpPrefix: str, \
|
||||
bookmarkNickname: str,bookmarkedomain: str, \
|
||||
bookmarkPort: int,ccList: [], \
|
||||
bookmarkStatusNumber: int,clientToServer: bool, \
|
||||
sendThreads: [],postLog: [], \
|
||||
personCache: {},cachedWebfingers: {}, \
|
||||
|
||||
def undoBookmarkPost(session, baseDir: str, federationList: [],
|
||||
nickname: str, domain: str, port: int, httpPrefix: str,
|
||||
bookmarkNickname: str, bookmarkedomain: str,
|
||||
bookmarkPort: int, ccList: [],
|
||||
bookmarkStatusNumber: int, clientToServer: bool,
|
||||
sendThreads: [], postLog: [],
|
||||
personCache: {}, cachedWebfingers: {},
|
||||
debug: bool) -> {}:
|
||||
"""Removes a bookmarked post
|
||||
"""
|
||||
|
@ -421,27 +420,22 @@ def undoBookmarkPost(session,baseDir: str,federationList: [], \
|
|||
if ':' not in bookmarkedomain:
|
||||
bookmarkedomain = bookmarkedomain + ':' + str(bookmarkPort)
|
||||
|
||||
objectUrl= \
|
||||
httpPrefix+'://'+bookmarkedomain+'/users/'+bookmarkNickname+ \
|
||||
objectUrl = httpPrefix + '://' + bookmarkedomain + \
|
||||
'/users/' + bookmarkNickname + \
|
||||
'/statuses/' + str(bookmarkStatusNumber)
|
||||
|
||||
ccUrl=httpPrefix+'://'+bookmarkedomain+'/users/'+bookmarkNickname
|
||||
if bookmarkPort:
|
||||
if bookmarkPort!=80 and bookmarkPort!=443:
|
||||
if ':' not in bookmarkedomain:
|
||||
ccUrl= \
|
||||
httpPrefix+'://'+bookmarkedomain+':'+ \
|
||||
str(bookmarkPort)+'/users/'+bookmarkNickname
|
||||
return undoBookmark(session, baseDir, federationList,
|
||||
nickname, domain, port,
|
||||
ccList, httpPrefix, objectUrl, clientToServer,
|
||||
sendThreads, postLog, personCache,
|
||||
cachedWebfingers, debug)
|
||||
|
||||
return undoBookmark(session,baseDir,federationList,nickname,domain,port, \
|
||||
ccList,httpPrefix,objectUrl,clientToServer, \
|
||||
sendThreads,postLog,personCache,cachedWebfingers,debug)
|
||||
|
||||
def sendBookmarkViaServer(baseDir: str,session, \
|
||||
def sendBookmarkViaServer(baseDir: str, session,
|
||||
fromNickname: str, password: str,
|
||||
fromDomain: str,fromPort: int, \
|
||||
httpPrefix: str,bookmarkUrl: str, \
|
||||
cachedWebfingers: {},personCache: {}, \
|
||||
fromDomain: str, fromPort: int,
|
||||
httpPrefix: str, bookmarkUrl: str,
|
||||
cachedWebfingers: {}, personCache: {},
|
||||
debug: bool, projectVersion: str) -> {}:
|
||||
"""Creates a bookmark via c2s
|
||||
"""
|
||||
|
@ -455,12 +449,6 @@ def sendBookmarkViaServer(baseDir: str,session, \
|
|||
if ':' not in fromDomain:
|
||||
fromDomainFull = fromDomain + ':' + str(fromPort)
|
||||
|
||||
toUrl=['https://www.w3.org/ns/activitystreams#Public']
|
||||
ccUrl=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
|
||||
|
||||
if '/statuses/' in bookmarkUrl:
|
||||
toUrl=[bookmarkUrl.split('/statuses/')[0]]
|
||||
|
||||
newBookmarkJson = {
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
'type': 'Bookmark',
|
||||
|
@ -471,7 +459,8 @@ def sendBookmarkViaServer(baseDir: str,session, \
|
|||
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
|
||||
|
||||
# lookup the inbox for the To handle
|
||||
wfRequest=webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
|
||||
wfRequest = webfingerHandle(session, handle, httpPrefix,
|
||||
cachedWebfingers,
|
||||
fromDomain, projectVersion)
|
||||
if not wfRequest:
|
||||
if debug:
|
||||
|
@ -481,9 +470,11 @@ def sendBookmarkViaServer(baseDir: str,session, \
|
|||
postToBox = 'outbox'
|
||||
|
||||
# get the actor inbox for the To handle
|
||||
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
|
||||
getPersonBox(baseDir,session,wfRequest,personCache, \
|
||||
projectVersion,httpPrefix,fromNickname, \
|
||||
(inboxUrl, pubKeyId, pubKey,
|
||||
fromPersonId, sharedInbox,
|
||||
capabilityAcquisition, avatarUrl,
|
||||
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
|
||||
projectVersion, httpPrefix, fromNickname,
|
||||
fromDomain, postToBox)
|
||||
|
||||
if not inboxUrl:
|
||||
|
@ -498,27 +489,28 @@ def sendBookmarkViaServer(baseDir: str,session, \
|
|||
authHeader = createBasicAuthHeader(fromNickname, password)
|
||||
|
||||
headers = {
|
||||
'host': fromDomain, \
|
||||
'Content-type': 'application/json', \
|
||||
'host': fromDomain,
|
||||
'Content-type': 'application/json',
|
||||
'Authorization': authHeader
|
||||
}
|
||||
postResult= \
|
||||
postJson(session,newBookmarkJson,[],inboxUrl,headers,"inbox:write")
|
||||
#if not postResult:
|
||||
# if debug:
|
||||
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
|
||||
# return 5
|
||||
postResult = postJson(session, newBookmarkJson, [],
|
||||
inboxUrl, headers, "inbox:write")
|
||||
if not postResult:
|
||||
if debug:
|
||||
print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
|
||||
return 5
|
||||
|
||||
if debug:
|
||||
print('DEBUG: c2s POST bookmark success')
|
||||
|
||||
return newBookmarkJson
|
||||
|
||||
def sendUndoBookmarkViaServer(baseDir: str,session, \
|
||||
fromNickname: str,password: str, \
|
||||
fromDomain: str,fromPort: int, \
|
||||
httpPrefix: str,bookmarkUrl: str, \
|
||||
cachedWebfingers: {},personCache: {}, \
|
||||
|
||||
def sendUndoBookmarkViaServer(baseDir: str, session,
|
||||
fromNickname: str, password: str,
|
||||
fromDomain: str, fromPort: int,
|
||||
httpPrefix: str, bookmarkUrl: str,
|
||||
cachedWebfingers: {}, personCache: {},
|
||||
debug: bool, projectVersion: str) -> {}:
|
||||
"""Undo a bookmark via c2s
|
||||
"""
|
||||
|
@ -532,12 +524,6 @@ def sendUndoBookmarkViaServer(baseDir: str,session, \
|
|||
if ':' not in fromDomain:
|
||||
fromDomainFull = fromDomain + ':' + str(fromPort)
|
||||
|
||||
toUrl=['https://www.w3.org/ns/activitystreams#Public']
|
||||
ccUrl=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
|
||||
|
||||
if '/statuses/' in bookmarkUrl:
|
||||
toUrl=[bookmarkUrl.split('/statuses/')[0]]
|
||||
|
||||
newUndoBookmarkJson = {
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
'type': 'Undo',
|
||||
|
@ -552,7 +538,7 @@ def sendUndoBookmarkViaServer(baseDir: str,session, \
|
|||
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
|
||||
|
||||
# lookup the inbox for the To handle
|
||||
wfRequest=webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
|
||||
wfRequest = webfingerHandle(session, handle, httpPrefix, cachedWebfingers,
|
||||
fromDomain, projectVersion)
|
||||
if not wfRequest:
|
||||
if debug:
|
||||
|
@ -562,9 +548,11 @@ def sendUndoBookmarkViaServer(baseDir: str,session, \
|
|||
postToBox = 'outbox'
|
||||
|
||||
# get the actor inbox for the To handle
|
||||
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
|
||||
getPersonBox(baseDir,session,wfRequest,personCache, \
|
||||
projectVersion,httpPrefix,fromNickname, \
|
||||
(inboxUrl, pubKeyId, pubKey,
|
||||
fromPersonId, sharedInbox,
|
||||
capabilityAcquisition, avatarUrl,
|
||||
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
|
||||
projectVersion, httpPrefix, fromNickname,
|
||||
fromDomain, postToBox)
|
||||
|
||||
if not inboxUrl:
|
||||
|
@ -579,25 +567,26 @@ def sendUndoBookmarkViaServer(baseDir: str,session, \
|
|||
authHeader = createBasicAuthHeader(fromNickname, password)
|
||||
|
||||
headers = {
|
||||
'host': fromDomain, \
|
||||
'Content-type': 'application/json', \
|
||||
'host': fromDomain,
|
||||
'Content-type': 'application/json',
|
||||
'Authorization': authHeader
|
||||
}
|
||||
postResult= \
|
||||
postJson(session,newUndoBookmarkJson,[],inboxUrl,headers,"inbox:write")
|
||||
#if not postResult:
|
||||
# if debug:
|
||||
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
|
||||
# return 5
|
||||
postResult = postJson(session, newUndoBookmarkJson, [],
|
||||
inboxUrl, headers, "inbox:write")
|
||||
if not postResult:
|
||||
if debug:
|
||||
print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
|
||||
return 5
|
||||
|
||||
if debug:
|
||||
print('DEBUG: c2s POST undo bookmark success')
|
||||
|
||||
return newUndoBookmarkJson
|
||||
|
||||
def outboxBookmark(recentPostsCache: {}, \
|
||||
baseDir: str,httpPrefix: str, \
|
||||
nickname: str,domain: str,port: int, \
|
||||
|
||||
def outboxBookmark(recentPostsCache: {},
|
||||
baseDir: str, httpPrefix: str,
|
||||
nickname: str, domain: str, port: int,
|
||||
messageJson: {}, debug: bool) -> None:
|
||||
""" When a bookmark request is received by the outbox from c2s
|
||||
"""
|
||||
|
@ -638,15 +627,16 @@ def outboxBookmark(recentPostsCache: {}, \
|
|||
print('DEBUG: c2s bookmark post not found in inbox or outbox')
|
||||
print(messageId)
|
||||
return True
|
||||
updateBookmarksCollection(recentPostsCache, \
|
||||
baseDir,postFilename,messageId, \
|
||||
updateBookmarksCollection(recentPostsCache,
|
||||
baseDir, postFilename, messageId,
|
||||
messageJson['actor'], domain, debug)
|
||||
if debug:
|
||||
print('DEBUG: post bookmarked via c2s - ' + postFilename)
|
||||
|
||||
def outboxUndoBookmark(recentPostsCache: {}, \
|
||||
baseDir: str,httpPrefix: str, \
|
||||
nickname: str,domain: str,port: int, \
|
||||
|
||||
def outboxUndoBookmark(recentPostsCache: {},
|
||||
baseDir: str, httpPrefix: str,
|
||||
nickname: str, domain: str, port: int,
|
||||
messageJson: {}, debug: bool) -> None:
|
||||
""" When an undo bookmark request is received by the outbox from c2s
|
||||
"""
|
||||
|
@ -697,8 +687,8 @@ def outboxUndoBookmark(recentPostsCache: {}, \
|
|||
print('DEBUG: c2s undo bookmark post not found in inbox or outbox')
|
||||
print(messageId)
|
||||
return True
|
||||
undoBookmarksCollectionEntry(recentPostsCache, \
|
||||
baseDir,postFilename,messageId, \
|
||||
undoBookmarksCollectionEntry(recentPostsCache,
|
||||
baseDir, postFilename, messageId,
|
||||
messageJson['actor'], domain, debug)
|
||||
if debug:
|
||||
print('DEBUG: post undo bookmarked via c2s - ' + postFilename)
|
||||
|
|
Loading…
Reference in New Issue