Remove mastodon posts on delete and OTP
This commit is contained in:
@@ -122,6 +122,46 @@ class MastodonPlugin(BasePlugin):
|
||||
'reason': str(error),
|
||||
}
|
||||
|
||||
def delete_posts(self, event):
|
||||
config = dict(self.config)
|
||||
user_id = event.get('user_id')
|
||||
if user_id:
|
||||
from backend.app.database import get_connection
|
||||
|
||||
with get_connection() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT config FROM user_plugin_config WHERE user_id = ? AND plugin_name = ?',
|
||||
(user_id, self.name),
|
||||
).fetchone()
|
||||
if row and row['config']:
|
||||
config.update(json.loads(row['config']))
|
||||
|
||||
instance = str(config.get('instance', '')).strip().rstrip('/')
|
||||
if instance and '://' not in instance:
|
||||
instance = f'https://{instance}'
|
||||
access_token = str(config.get('access_token', '')).strip()
|
||||
post_ids = event.get('mastodon_post_ids') or []
|
||||
if not post_ids and event.get('mastodon_post_id'):
|
||||
post_ids = [event['mastodon_post_id']]
|
||||
if not instance or not access_token:
|
||||
return {'status': 'failed', 'plugin': self.name, 'reason': 'Mastodon is not configured'}
|
||||
|
||||
try:
|
||||
for post_id in post_ids:
|
||||
request = Request(
|
||||
f'{instance}/api/v1/statuses/{post_id}',
|
||||
headers={'Authorization': f'Bearer {access_token}', 'User-Agent': 'LinkLog/1.0'},
|
||||
method='DELETE',
|
||||
)
|
||||
with urlopen(request, timeout=5) as response:
|
||||
response.read()
|
||||
return {'status': 'deleted', 'plugin': self.name, 'count': len(post_ids)}
|
||||
except HTTPError as error:
|
||||
response_body = error.read().decode('utf-8', errors='replace')
|
||||
return {'status': 'failed', 'plugin': self.name, 'reason': f'HTTP {error.code}: {response_body[:500]}'}
|
||||
except (URLError, TimeoutError, OSError) as error:
|
||||
return {'status': 'failed', 'plugin': self.name, 'reason': str(error)}
|
||||
|
||||
|
||||
class PluginManager:
|
||||
def __init__(self):
|
||||
@@ -157,5 +197,10 @@ class PluginManager:
|
||||
return {'status': 'skipped', 'plugin': 'mastodon', 'reason': 'disabled'}
|
||||
return plugin.handle_event(event)
|
||||
|
||||
def delete_mastodon_posts(self, event):
|
||||
self.refresh_from_db()
|
||||
plugin = next(plugin for plugin in self.plugins if plugin.name == 'mastodon')
|
||||
return plugin.delete_posts(event)
|
||||
|
||||
|
||||
plugin_manager = PluginManager()
|
||||
|
||||
Reference in New Issue
Block a user