epicyon/like.py

179 lines
6.7 KiB
Python
Raw Normal View History

2019-07-02 11:13:45 +00:00
__filename__ = "like.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "0.0.1"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import json
import commentjson
2019-07-11 12:29:31 +00:00
from pprint import pprint
2019-07-02 11:13:45 +00:00
from utils import urlPermitted
2019-07-10 09:47:07 +00:00
from utils import getNicknameFromActor
from utils import getDomainFromActor
2019-07-11 12:29:31 +00:00
from utils import locatePost
from posts import sendSignedJson
2019-07-11 22:22:27 +00:00
def undoLikesCollectionEntry(postFilename: str,objectUrl: str, actor: str,debug: bool) -> None:
"""Undoes a like for a particular actor
"""
with open(postFilename, 'r') as fp:
postJson=commentjson.load(fp)
if not postJson.get('object'):
if debug:
pprint(postJson)
print('DEBUG: post '+objectUrl+' has no object')
return
if not postJson['object'].get('likes'):
return
if not postJson['object']['likes'].get('items'):
return
totalItems=0
if postJson['object']['likes'].get('totalItems'):
totalItems=postJson['object']['likes']['totalItems']
itemFound=False
for likeItem in postJson['object']['likes']['items']:
if likeItem.get('actor'):
if likeItem['actor']==actor:
if debug:
print('DEBUG: like was removed for '+actor)
postJson['object']['likes']['items'].remove(likeItem)
itemFound=True
break
if itemFound:
if totalItems==1:
if debug:
print('DEBUG: likes was removed from post')
postJson['object'].remove(postJson['object']['likes'])
else:
postJson['object']['likes']['totalItems']=len(postJson['likes']['items'])
with open(postFilename, 'w') as fp:
commentjson.dump(postJson, fp, indent=4, sort_keys=True)
2019-07-11 12:29:31 +00:00
def updateLikesCollection(postFilename: str,objectUrl: str, actor: str,debug: bool) -> None:
"""Updates the likes collection within a post
"""
with open(postFilename, 'r') as fp:
postJson=commentjson.load(fp)
if not postJson.get('object'):
if debug:
pprint(postJson)
print('DEBUG: post '+objectUrl+' has no object')
return
if not objectUrl.endswith('/likes'):
objectUrl=objectUrl+'/likes'
if not postJson['object'].get('likes'):
if debug:
print('DEBUG: Adding initial likes to '+objectUrl)
likesJson = {
'id': objectUrl,
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'Like',
'actor': actor
}]
}
postJson['object']['likes']=likesJson
else:
if postJson['object']['likes'].get('items'):
for likeItem in postJson['likes']['items']:
if likeItem.get('actor'):
if likeItem['actor']==actor:
return
newLike={
'type': 'Like',
'actor': actor
}
postJson['object']['likes']['items'].append(newLike)
postJson['object']['likes']['totalItems']=len(postJson['likes']['items'])
else:
if debug:
print('DEBUG: likes section of post has no items list')
if debug:
print('DEBUG: saving post with likes added')
with open(postFilename, 'w') as fp:
commentjson.dump(postJson, fp, indent=4, sort_keys=True)
2019-07-02 11:13:45 +00:00
2019-07-10 09:47:07 +00:00
def like(session,baseDir: str,federationList: [],nickname: str,domain: str,port: int, \
2019-07-11 12:29:31 +00:00
ccList: [],httpPrefix: str,objectUrl: str,clientToServer: bool, \
sendThreads: [],postLog: [],personCache: {},cachedWebfingers: {}, \
debug: bool) -> {}:
2019-07-02 11:13:45 +00:00
"""Creates a like
2019-07-10 09:47:07 +00:00
ccUrl might be a specific person whose post was liked
2019-07-02 11:13:45 +00:00
objectUrl is typically the url of the message, corresponding to url or atomUri in createPostBase
"""
2019-07-09 14:20:23 +00:00
if not urlPermitted(objectUrl,federationList,"inbox:write"):
2019-07-02 11:13:45 +00:00
return None
2019-07-11 12:29:31 +00:00
fullDomain=domain
2019-07-02 11:13:45 +00:00
if port!=80 and port!=443:
2019-07-11 12:29:31 +00:00
if ':' not in domain:
fullDomain=domain+':'+str(port)
2019-07-02 11:13:45 +00:00
2019-07-10 09:47:07 +00:00
newLikeJson = {
2019-07-02 11:13:45 +00:00
'type': 'Like',
2019-07-11 12:29:31 +00:00
'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname,
2019-07-02 11:13:45 +00:00
'object': objectUrl,
2019-07-11 12:29:31 +00:00
'to': [httpPrefix+'://'+fullDomain+'/users/'+nickname+'/followers'],
2019-07-02 18:38:51 +00:00
'cc': []
2019-07-02 11:13:45 +00:00
}
2019-07-11 12:29:31 +00:00
if ccList:
if len(ccList)>0:
newLikeJson['cc']=ccList
2019-07-10 09:47:07 +00:00
# Extract the domain and nickname from a statuses link
likedPostNickname=None
likedPostDomain=None
likedPostPort=None
if '/users/' in objectUrl:
likedPostNickname=getNicknameFromActor(objectUrl)
likedPostDomain,likedPostPort=getDomainFromActor(objectUrl)
if likedPostNickname:
2019-07-11 12:29:31 +00:00
postFilename=locatePost(baseDir,nickname,domain,objectUrl)
if not postFilename:
return None
updateLikesCollection(postFilename,objectUrl,newLikeJson['actor'],debug)
sendSignedJson(newLikeJson,session,baseDir, \
2019-07-10 09:47:07 +00:00
nickname,domain,port, \
likedPostNickname,likedPostDomain,likedPostPort, \
'https://www.w3.org/ns/activitystreams#Public', \
httpPrefix,True,clientToServer,federationList, \
sendThreads,postLog,cachedWebfingers,personCache,debug)
return newLikeJson
def likePost(session,baseDir: str,federationList: [], \
2019-07-11 12:29:31 +00:00
nickname: str,domain: str,port: int,httpPrefix: str, \
likeNickname: str,likeDomain: str,likePort: int, \
ccList: [], \
likeStatusNumber: int,clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
debug: bool) -> {}:
2019-07-02 14:02:58 +00:00
"""Likes a given status post
"""
likeDomain=likeDomain
if likePort!=80 and likePort!=443:
likeDomain=likeDomain+':'+str(likePort)
2019-07-10 09:47:07 +00:00
objectUrl = \
httpPrefix + '://'+likeDomain+'/users/'+likeNickname+ \
'/statuses/'+str(likeStatusNumber)
2019-07-02 14:02:58 +00:00
2019-07-11 12:29:31 +00:00
if likePort!=80 and likePort!=443:
ccUrl=httpPrefix+'://'+likeDomain+':'+str(likePort)+'/users/'+likeNickname
else:
ccUrl=httpPrefix+'://'+likeDomain+'/users/'+likeNickname
2019-07-10 09:47:07 +00:00
return like(session,baseDir,federationList,nickname,domain,port, \
2019-07-11 12:29:31 +00:00
ccList,httpPrefix,objectUrl,clientToServer, \
sendThreads,postLog,personCache,cachedWebfingers,debug)
2019-07-10 12:40:31 +00:00