This commit is contained in:
@@ -2,12 +2,15 @@
|
||||
## SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import json
|
||||
import logging
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from backend.app.plugins.base import BasePlugin
|
||||
|
||||
DEFAULT_POST_PREFIX = 'From my #LinkLog: '
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DefaultFrontendPlugin(BasePlugin):
|
||||
@@ -50,6 +53,10 @@ class MastodonPlugin(BasePlugin):
|
||||
instance = f'https://{instance}'
|
||||
access_token = str(config.get('access_token', '')).strip()
|
||||
if not instance or not access_token:
|
||||
logger.debug(
|
||||
'Mastodon post skipped: instance_configured=%s token_configured=%s user_id=%s',
|
||||
bool(instance), bool(access_token), user_id,
|
||||
)
|
||||
return {'status': 'skipped', 'plugin': self.name, 'reason': 'not_configured'}
|
||||
|
||||
status_parts = [event.get('title') or event.get('url', '')]
|
||||
@@ -63,25 +70,51 @@ class MastodonPlugin(BasePlugin):
|
||||
if event.get('tags'):
|
||||
status = f'{status} {" ".join(event["tags"])}'
|
||||
status_parts.append(status)
|
||||
post_body = '\n'.join(status_parts)
|
||||
endpoint = f'{instance}/api/v1/statuses'
|
||||
logger.debug(
|
||||
'Posting link to Mastodon: endpoint=%s user_id=%s event_id=%s body_length=%d',
|
||||
endpoint, user_id, event.get('id'), len(post_body),
|
||||
)
|
||||
|
||||
try:
|
||||
request = Request(
|
||||
f'{instance}/api/v1/statuses',
|
||||
data=json.dumps({'status': '\n'.join(status_parts)}).encode('utf-8'),
|
||||
endpoint,
|
||||
data=urlencode({'status': post_body}).encode('utf-8'),
|
||||
headers={
|
||||
'Authorization': f'Bearer {access_token}',
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Accept': 'application/json',
|
||||
'User-Agent': 'LinkLog/1.0',
|
||||
},
|
||||
method='POST',
|
||||
)
|
||||
with urlopen(request, timeout=5) as response:
|
||||
response_data = json.loads(response.read().decode('utf-8'))
|
||||
response_body = response.read().decode('utf-8')
|
||||
logger.debug(
|
||||
'Mastodon post response: endpoint=%s status=%s body_length=%d',
|
||||
endpoint, response.status, len(response_body),
|
||||
)
|
||||
response_data = json.loads(response_body)
|
||||
logger.info('Mastodon post succeeded: instance=%s user_id=%s post_id=%s', instance, user_id, response_data.get('id'))
|
||||
return {
|
||||
'status': 'posted',
|
||||
'plugin': self.name,
|
||||
'post_id': response_data.get('id'),
|
||||
}
|
||||
except (HTTPError, URLError, TimeoutError, OSError, ValueError) as error:
|
||||
except HTTPError as error:
|
||||
response_body = error.read().decode('utf-8', errors='replace')
|
||||
logger.warning(
|
||||
'Mastodon post failed: endpoint=%s user_id=%s status=%s response=%s',
|
||||
endpoint, user_id, error.code, response_body[:500],
|
||||
)
|
||||
return {
|
||||
'status': 'failed',
|
||||
'plugin': self.name,
|
||||
'reason': f'HTTP {error.code}: {response_body[:500]}',
|
||||
}
|
||||
except (URLError, TimeoutError, OSError, ValueError) as error:
|
||||
logger.exception('Mastodon post failed: endpoint=%s user_id=%s error=%s', endpoint, user_id, error)
|
||||
return {
|
||||
'status': 'failed',
|
||||
'plugin': self.name,
|
||||
@@ -111,7 +144,9 @@ class PluginManager:
|
||||
results = []
|
||||
for plugin in self.plugins:
|
||||
if plugin.enabled:
|
||||
results.append(plugin.handle_event(event))
|
||||
result = plugin.handle_event(event)
|
||||
results.append(result)
|
||||
logger.debug('Plugin dispatch result: plugin=%s event_id=%s result=%s', plugin.name, event.get('id'), result)
|
||||
return results
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user