2020-04-03 16:59:12 +00:00
|
|
|
__filename__ = "metadata.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2021-01-26 10:07:42 +00:00
|
|
|
__version__ = "1.2.0"
|
2020-04-03 16:59:12 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
|
|
|
__email__ = "bob@freedombone.net"
|
|
|
|
__status__ = "Production"
|
2021-06-15 15:08:12 +00:00
|
|
|
__module_group__ = "Metadata"
|
2019-11-13 10:32:12 +00:00
|
|
|
|
2019-11-13 12:45:41 +00:00
|
|
|
import os
|
2021-06-07 09:10:52 +00:00
|
|
|
from utils import isAccountDir
|
2019-11-23 10:08:00 +00:00
|
|
|
from utils import loadJson
|
2019-11-13 14:02:45 +00:00
|
|
|
from utils import noOfAccounts
|
2019-11-13 15:19:43 +00:00
|
|
|
from utils import noOfActiveAccountsMonthly
|
2019-11-13 10:32:12 +00:00
|
|
|
|
2020-04-03 16:59:12 +00:00
|
|
|
|
2021-06-05 09:22:35 +00:00
|
|
|
def _getStatusCount(baseDir: str) -> int:
|
|
|
|
"""Get the total number of posts
|
|
|
|
"""
|
|
|
|
statusCtr = 0
|
|
|
|
accountsDir = baseDir + '/accounts'
|
|
|
|
for subdir, dirs, files in os.walk(accountsDir):
|
|
|
|
for acct in dirs:
|
2021-06-07 09:10:52 +00:00
|
|
|
if not isAccountDir(acct):
|
2021-06-05 09:22:35 +00:00
|
|
|
continue
|
|
|
|
acctDir = os.path.join(accountsDir, acct + '/outbox')
|
|
|
|
for subdir2, dirs2, files2 in os.walk(acctDir):
|
|
|
|
statusCtr += len(files2)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
return statusCtr
|
|
|
|
|
|
|
|
|
2021-05-04 09:39:15 +00:00
|
|
|
def metaDataNodeInfo(baseDir: str,
|
|
|
|
aboutUrl: str,
|
|
|
|
termsOfServiceUrl: str,
|
|
|
|
registration: bool, version: str,
|
2021-05-03 10:05:05 +00:00
|
|
|
showAccounts: bool) -> {}:
|
2019-11-13 10:32:12 +00:00
|
|
|
""" /nodeinfo/2.0 endpoint
|
2021-05-03 10:05:05 +00:00
|
|
|
Also see https://socialhub.activitypub.rocks/t/
|
|
|
|
fep-f1d5-nodeinfo-in-fediverse-software/1190/4
|
|
|
|
|
|
|
|
Note that there are security considerations with this. If an adversary
|
|
|
|
sees a lot of accounts and "local" posts then the instance may be
|
|
|
|
considered a higher priority target.
|
|
|
|
Also exposure of the version number and number of accounts could be
|
|
|
|
sensitive
|
2019-11-13 10:32:12 +00:00
|
|
|
"""
|
2021-05-03 10:05:05 +00:00
|
|
|
if showAccounts:
|
|
|
|
activeAccounts = noOfAccounts(baseDir)
|
|
|
|
activeAccountsMonthly = noOfActiveAccountsMonthly(baseDir, 1)
|
|
|
|
activeAccountsHalfYear = noOfActiveAccountsMonthly(baseDir, 6)
|
2021-06-05 09:22:35 +00:00
|
|
|
localPosts = _getStatusCount(baseDir)
|
2021-05-03 10:05:05 +00:00
|
|
|
else:
|
|
|
|
activeAccounts = 1
|
|
|
|
activeAccountsMonthly = 1
|
|
|
|
activeAccountsHalfYear = 1
|
2021-06-05 09:22:35 +00:00
|
|
|
localPosts = 1
|
2021-05-03 10:05:05 +00:00
|
|
|
|
2020-04-03 16:59:12 +00:00
|
|
|
nodeinfo = {
|
2019-11-13 10:32:12 +00:00
|
|
|
'openRegistrations': registration,
|
|
|
|
'protocols': ['activitypub'],
|
|
|
|
'software': {
|
|
|
|
'name': 'epicyon',
|
|
|
|
'version': version
|
|
|
|
},
|
2021-05-04 09:39:15 +00:00
|
|
|
'documents': {
|
|
|
|
'about': aboutUrl,
|
|
|
|
'terms': termsOfServiceUrl
|
|
|
|
},
|
2019-11-13 10:32:12 +00:00
|
|
|
'usage': {
|
2021-06-05 09:22:35 +00:00
|
|
|
'localPosts': localPosts,
|
2019-11-13 10:32:12 +00:00
|
|
|
'users': {
|
2019-11-13 15:19:43 +00:00
|
|
|
'activeHalfyear': activeAccountsHalfYear,
|
|
|
|
'activeMonth': activeAccountsMonthly,
|
2019-11-13 14:02:45 +00:00
|
|
|
'total': activeAccounts
|
2019-11-13 10:32:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'version': '2.0'
|
|
|
|
}
|
|
|
|
return nodeinfo
|
2019-11-13 12:45:41 +00:00
|
|
|
|
2020-04-03 16:59:12 +00:00
|
|
|
|
2021-06-05 09:22:35 +00:00
|
|
|
def metaDataInstance(showAccounts: bool,
|
|
|
|
instanceTitle: str,
|
2020-04-03 16:59:12 +00:00
|
|
|
instanceDescriptionShort: str,
|
|
|
|
instanceDescription: str,
|
|
|
|
httpPrefix: str, baseDir: str,
|
|
|
|
adminNickname: str, domain: str, domainFull: str,
|
|
|
|
registration: bool, systemLanguage: str,
|
2019-11-13 12:45:41 +00:00
|
|
|
version: str) -> {}:
|
|
|
|
""" /api/v1/instance endpoint
|
|
|
|
"""
|
2020-04-03 16:59:12 +00:00
|
|
|
adminActorFilename = \
|
|
|
|
baseDir + '/accounts/' + adminNickname + '@' + domain + '.json'
|
2019-11-13 12:45:41 +00:00
|
|
|
if not os.path.isfile(adminActorFilename):
|
|
|
|
return {}
|
|
|
|
|
2020-04-03 16:59:12 +00:00
|
|
|
adminActor = loadJson(adminActorFilename, 0)
|
2019-11-13 12:45:41 +00:00
|
|
|
if not adminActor:
|
2019-11-23 10:08:00 +00:00
|
|
|
print('WARN: json load exception metaDataInstance')
|
2019-11-13 12:45:41 +00:00
|
|
|
return {}
|
|
|
|
|
2020-04-03 16:59:12 +00:00
|
|
|
isBot = False
|
|
|
|
if adminActor['type'] != 'Person':
|
|
|
|
isBot = True
|
|
|
|
|
|
|
|
url = \
|
|
|
|
httpPrefix + '://' + domainFull + '/@' + \
|
|
|
|
adminActor['preferredUsername']
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2021-06-05 09:22:35 +00:00
|
|
|
if showAccounts:
|
|
|
|
activeAccounts = noOfAccounts(baseDir)
|
|
|
|
localPosts = _getStatusCount(baseDir)
|
|
|
|
else:
|
|
|
|
activeAccounts = 1
|
|
|
|
localPosts = 1
|
|
|
|
|
2020-04-03 16:59:12 +00:00
|
|
|
instance = {
|
2019-11-13 12:55:37 +00:00
|
|
|
'approval_required': False,
|
2020-03-22 20:36:19 +00:00
|
|
|
'contact_account': {
|
|
|
|
'acct': adminActor['preferredUsername'],
|
|
|
|
'avatar': adminActor['icon']['url'],
|
|
|
|
'avatar_static': adminActor['icon']['url'],
|
|
|
|
'bot': isBot,
|
|
|
|
'display_name': adminActor['name'],
|
|
|
|
'header': adminActor['image']['url'],
|
|
|
|
'header_static': adminActor['image']['url'],
|
|
|
|
'locked': adminActor['manuallyApprovesFollowers'],
|
2021-05-04 10:17:06 +00:00
|
|
|
'note': '<p>Admin of ' + domain + '</p>',
|
2020-04-03 16:59:12 +00:00
|
|
|
'url': url,
|
2020-03-22 20:36:19 +00:00
|
|
|
'username': adminActor['preferredUsername']
|
2019-11-13 12:45:41 +00:00
|
|
|
},
|
|
|
|
'description': instanceDescription,
|
|
|
|
'languages': [systemLanguage],
|
|
|
|
'registrations': registration,
|
|
|
|
'short_description': instanceDescriptionShort,
|
|
|
|
'stats': {
|
2021-06-05 09:22:35 +00:00
|
|
|
'domain_count': 1,
|
|
|
|
'status_count': localPosts,
|
|
|
|
'user_count': activeAccounts
|
2019-11-13 12:45:41 +00:00
|
|
|
},
|
2021-05-04 10:17:06 +00:00
|
|
|
'thumbnail': httpPrefix + '://' + domainFull + '/login.png',
|
2019-11-13 12:45:41 +00:00
|
|
|
'title': instanceTitle,
|
|
|
|
'uri': domainFull,
|
|
|
|
'urls': {},
|
|
|
|
'version': version
|
|
|
|
}
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2019-11-13 12:45:41 +00:00
|
|
|
return instance
|
2021-05-27 22:08:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def metadataCustomEmoji(baseDir: str,
|
|
|
|
httpPrefix: str, domainFull: str) -> {}:
|
|
|
|
"""Returns the custom emoji
|
|
|
|
Endpoint /api/v1/custom_emojis
|
|
|
|
See https://docs.joinmastodon.org/methods/instance/custom_emojis
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
emojisUrl = httpPrefix + '://' + domainFull + '/emoji'
|
|
|
|
for subdir, dirs, files in os.walk(baseDir + '/emoji'):
|
|
|
|
for f in files:
|
2021-05-27 22:19:52 +00:00
|
|
|
if len(f) < 3:
|
|
|
|
continue
|
|
|
|
if f[0].isdigit() or f[1].isdigit():
|
2021-05-27 22:08:49 +00:00
|
|
|
continue
|
|
|
|
if not f.endswith('.png'):
|
|
|
|
continue
|
|
|
|
url = os.path.join(emojisUrl, f)
|
|
|
|
result.append({
|
|
|
|
"shortcode": f.replace('.png', ''),
|
|
|
|
"url": url,
|
|
|
|
"static_url": url,
|
|
|
|
"visible_in_picker": True
|
|
|
|
})
|
|
|
|
break
|
|
|
|
return result
|