Format mastodon output

This commit is contained in:
2026-08-26 09:51:28 +02:00
parent 22add753fe
commit 2e5610555e
5 changed files with 47 additions and 11 deletions
+9 -8
View File
@@ -59,18 +59,19 @@ class MastodonPlugin(BasePlugin):
)
return {'status': 'skipped', 'plugin': self.name, 'reason': 'not_configured'}
status_parts = [event.get('title') or event.get('url', '')]
if event.get('comment'):
status_parts.append(event['comment'])
post_prefix = config.get('post_prefix')
if post_prefix is None and config.get('hashtag'):
post_prefix = f'#{str(config["hashtag"]).strip().lstrip("#")} '
post_prefix = str(post_prefix if post_prefix is not None else DEFAULT_POST_PREFIX)
status = f'{post_prefix}{event.get("url", "")}'.strip()
post_prefix = str(post_prefix if post_prefix is not None else DEFAULT_POST_PREFIX).strip()
title = str(event.get('title') or '').strip()
status_parts = [f'{post_prefix} {title}'.strip()]
if event.get('comment'):
status_parts.append(event['comment'])
if title:
status_parts.append(f'from: {event.get("url", "")}')
if event.get('tags'):
status = f'{status} {" ".join(event["tags"])}'
status_parts.append(status)
post_body = '\n'.join(status_parts)
status_parts.append(' '.join(event['tags']))
post_body = '\n\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',
+23 -2
View File
@@ -6,7 +6,7 @@ import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import parse_qs
from uuid import uuid4
from unittest.mock import patch
from unittest.mock import MagicMock, patch
from fastapi.testclient import TestClient
@@ -445,13 +445,34 @@ def test_link_submission_posts_to_enabled_mastodon_plugin():
assert received['path'] == '/api/v1/statuses'
assert received['authorization'] == 'Bearer test-token'
assert received['content_type'] == 'application/x-www-form-urlencoded'
assert received['body'] == {'status': ['A useful page\nWorth sharing\nFrom my #LinkLog: https://example.com/useful #python #web']}
assert received['body'] == {'status': ['From my #LinkLog: A useful page\n\nWorth sharing\n\nfrom: https://example.com/useful\n\n#python #web']}
finally:
server.shutdown()
thread.join()
server.server_close()
def test_mastodon_post_without_title_omits_source_line():
from backend.app.services.plugin_manager import MastodonPlugin
response = MagicMock()
response.__enter__.return_value.read.return_value = b'{"id":"status-2"}'
plugin = MastodonPlugin()
plugin.initialize({'instance': 'https://mastodon.example', 'access_token': 'test-token'})
with patch('backend.app.services.plugin_manager.urlopen', return_value=response) as open_url:
result = plugin.handle_event({
'url': 'https://example.com/useful',
'title': '',
'comment': 'A comment',
'tags': ['#tag'],
})
body = parse_qs(open_url.call_args.args[0].data.decode())['status'][0]
assert result['status'] == 'posted'
assert body == 'From my #LinkLog:\n\nA comment\n\n#tag'
def test_plugin_config_can_be_saved_for_mastodon():
headers = login_headers()
assert client.put('/api/user/plugins/mastodon', headers=headers, json={