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
|
|
|
|
from utils import urlPermitted
|
2019-07-10 09:47:07 +00:00
|
|
|
from utils import getNicknameFromActor
|
|
|
|
from utils import getDomainFromActor
|
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, \
|
|
|
|
ccUrl: str,httpPrefix: str,objectUrl: str,clientToServer: bool, \
|
|
|
|
sendThreads: [],postLog: [],personCache: {},cachedWebfingers: {}) -> {}:
|
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
|
|
|
|
|
|
|
|
if port!=80 and port!=443:
|
|
|
|
domain=domain+':'+str(port)
|
|
|
|
|
2019-07-10 09:47:07 +00:00
|
|
|
newLikeJson = {
|
2019-07-02 11:13:45 +00:00
|
|
|
'type': 'Like',
|
2019-07-03 19:00:03 +00:00
|
|
|
'actor': httpPrefix+'://'+domain+'/users/'+nickname,
|
2019-07-02 11:13:45 +00:00
|
|
|
'object': objectUrl,
|
2019-07-10 09:47:07 +00:00
|
|
|
'to': [httpPrefix+'://'+domain+'/users/'+nickname+'/followers'],
|
2019-07-02 18:38:51 +00:00
|
|
|
'cc': []
|
2019-07-02 11:13:45 +00:00
|
|
|
}
|
|
|
|
if ccUrl:
|
|
|
|
if len(ccUrl)>0:
|
2019-07-10 09:47:07 +00:00
|
|
|
newLikeJson['cc']=ccUrl
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
sendSignedJson(newlikeJson,session,baseDir, \
|
|
|
|
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: [], \
|
|
|
|
nickname: str, domain: str, port: int, httpPrefix: str, \
|
|
|
|
likeNickname: str, likeDomain: str, likePort: int, \
|
|
|
|
likeHttps: bool, likeStatusNumber: int, \
|
|
|
|
clientToServer: bool,sendThreads: [],postLog: [], \
|
|
|
|
personCache: {},cachedWebfingers: {}) -> {}:
|
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-10 09:47:07 +00:00
|
|
|
return like(session,baseDir,federationList,nickname,domain,port, \
|
|
|
|
ccUrl,httpPrefix,objectUrl,clientToServer, \
|
|
|
|
sendThreads,postLog,personCache,cachedWebfingers)
|
2019-07-10 12:40:31 +00:00
|
|
|
|
|
|
|
def updateLikesCollection(postFilename: str,objectUrl: str, actor: str) -> None:
|
|
|
|
"""Updates the likes collection within a post
|
|
|
|
"""
|
|
|
|
with open(postFilename, 'r') as fp:
|
|
|
|
postJson=commentjson.load(fp)
|
|
|
|
if not objectUrl.endswith('/likes'):
|
|
|
|
objectUrl=objectUrl+'/likes'
|
|
|
|
if not postJson.get('likes'):
|
|
|
|
likesJson = {
|
|
|
|
'id': objectUrl,
|
|
|
|
'type': 'Collection',
|
|
|
|
"totalItems": 1,
|
2019-07-10 18:00:14 +00:00
|
|
|
'items': [{
|
|
|
|
'type': 'Like',
|
|
|
|
'actor': actor
|
|
|
|
|
|
|
|
}]
|
2019-07-10 12:40:31 +00:00
|
|
|
}
|
|
|
|
postJson['likes']=likesJson
|
|
|
|
else:
|
|
|
|
if postJson['likes'].get('items'):
|
2019-07-10 18:00:14 +00:00
|
|
|
for likeItem in postJson['likes']['items']:
|
|
|
|
if likeItem['actor']==actor:
|
|
|
|
return
|
|
|
|
newLike={
|
|
|
|
'type': 'Like',
|
|
|
|
'actor': actor
|
|
|
|
}
|
|
|
|
postJson['likes']['items'].append(newLike)
|
2019-07-10 12:40:31 +00:00
|
|
|
postJson['likes']['totalItems']=len(postJson['likes']['items'])
|
|
|
|
with open(postFilename, 'w') as fp:
|
|
|
|
commentjson.dump(postJson, fp, indent=4, sort_keys=True)
|