diff --git a/app/api/routes/dashboard.py b/app/api/routes/dashboard.py index ede0590..e25bbd7 100644 --- a/app/api/routes/dashboard.py +++ b/app/api/routes/dashboard.py @@ -73,13 +73,19 @@ async def dashboard_home(request: Request, db: Session = Depends(get_db)): Interaction.responded == False ).order_by(Interaction.interaction_at.desc()).limit(5).all() + # Posts publicados recientemente + recent_published = db.query(Post).filter( + Post.status == "published" + ).order_by(Post.published_at.desc()).limit(5).all() + return templates.TemplateResponse("index.html", { "request": request, "user": user.to_dict(), "stats": stats, "pending_posts": [p.to_dict() for p in pending_posts], "scheduled_posts": [p.to_dict() for p in scheduled_posts], - "recent_interactions": [i.to_dict() for i in recent_interactions] + "recent_interactions": [i.to_dict() for i in recent_interactions], + "recent_published": [p.to_dict() for p in recent_published] }) diff --git a/app/publishers/x_publisher.py b/app/publishers/x_publisher.py index 33c8f71..780ee91 100644 --- a/app/publishers/x_publisher.py +++ b/app/publishers/x_publisher.py @@ -214,30 +214,45 @@ class XPublisher(BasePublisher): return [] async def get_comments(self, post_id: str) -> List[Dict]: - """Obtener respuestas a un tweet.""" + """Obtener respuestas a un tweet (excluyendo las propias).""" if not self.client: return [] try: + # Obtener ID del usuario autenticado para filtrar auto-respuestas + me = self.client.get_me() + my_user_id = str(me.data.id) if me.data else None + # Buscar respuestas al tweet query = f"conversation_id:{post_id}" tweets = self.client.search_recent_tweets( query=query, max_results=50, - tweet_fields=['created_at', 'author_id', 'in_reply_to_user_id'] + tweet_fields=['created_at', 'author_id', 'in_reply_to_user_id'], + user_fields=['username'], + expansions=['author_id'] ) if not tweets.data: return [] + # Crear mapa de usuarios para obtener usernames + users_map = {} + if tweets.includes and 'users' in tweets.includes: + for user in tweets.includes['users']: + users_map[str(user.id)] = user.username + + # Filtrar tweets propios (auto-respuestas del hilo) return [ { "id": str(tweet.id), "text": tweet.text, "author_id": str(tweet.author_id), + "username": users_map.get(str(tweet.author_id), "unknown"), "created_at": tweet.created_at.isoformat() if tweet.created_at else None } for tweet in tweets.data + if str(tweet.author_id) != my_user_id # Excluir tweets propios ] except tweepy.TweepyException: diff --git a/dashboard/templates/index.html b/dashboard/templates/index.html index 1763dd7..202abc8 100644 --- a/dashboard/templates/index.html +++ b/dashboard/templates/index.html @@ -166,6 +166,46 @@ + +
+
+
+

+ + Publicaciones Recientes +

+ Ver todas +
+
+ {% if recent_published %} + {% for post in recent_published %} +
+
+ {% for platform in post.platforms %} + {{ platform }} + {% endfor %} +
+

{{ post.content[:120] }}{% if post.content|length > 120 %}...{% endif %}

+
+ {{ post.published_at[:10] if post.published_at else '-' }} + {% if post.platform_post_ids %} + + Ver en X + + {% endif %} +
+
+ {% endfor %} + {% else %} +
+ 📭 +

No hay publicaciones recientes

+
+ {% endif %} +
+
+
+

Acciones Rápidas

diff --git a/worker/tasks/fetch_interactions.py b/worker/tasks/fetch_interactions.py index 858fe07..fe7d5ab 100644 --- a/worker/tasks/fetch_interactions.py +++ b/worker/tasks/fetch_interactions.py @@ -95,14 +95,25 @@ def fetch_platform_interactions(platform: str): ).first() if not existing: + # Obtener username (X usa 'username', Meta usa 'from.id') + author_username = comment.get("username") + if not author_username: + from_data = comment.get("from", {}) + author_username = from_data.get("id", "unknown") if isinstance(from_data, dict) else "unknown" + + # Obtener nombre del autor + author_name = None + if isinstance(comment.get("from"), dict): + author_name = comment.get("from", {}).get("name") + interaction = Interaction( platform=platform, interaction_type="comment", post_id=post.id, external_id=external_id, external_post_id=platform_id, - author_username=comment.get("username", comment.get("from", {}).get("id", "unknown")), - author_name=comment.get("from", {}).get("name") if isinstance(comment.get("from"), dict) else None, + author_username=author_username, + author_name=author_name, content=comment.get("text", comment.get("message")), interaction_at=datetime.fromisoformat( comment.get("created_at", comment.get("timestamp", comment.get("created_time", datetime.utcnow().isoformat()))).replace("Z", "+00:00")