From 38a68dbd3d97f47695efd73fc9b70b2ce8fcabda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20M=2E=20Lage?= Date: Thu, 14 Nov 2024 18:11:12 +0100 Subject: [PATCH] WIP get and show timeline For now the user is hardcoded in the url, don't want to need an auth at this stage of the development. --- src/blueskydj/urls.py | 3 +- src/core/templates/core/user_timeline.html | 7 +++ src/core/urls.py | 7 +++ src/core/views.py | 59 +++++++++++++++++++++- src/requirements.txt | 1 + 5 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/core/templates/core/user_timeline.html create mode 100644 src/core/urls.py diff --git a/src/blueskydj/urls.py b/src/blueskydj/urls.py index 80e3ecc..94c563c 100644 --- a/src/blueskydj/urls.py +++ b/src/blueskydj/urls.py @@ -15,8 +15,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), + path('', include('core.urls')), ] diff --git a/src/core/templates/core/user_timeline.html b/src/core/templates/core/user_timeline.html new file mode 100644 index 0000000..c9adc10 --- /dev/null +++ b/src/core/templates/core/user_timeline.html @@ -0,0 +1,7 @@ +{% for publication in publications %} +
+

{{ publication.author_name }} ({{ publication.author_handle }}) {{ publication.published_at }}

+

{{ publication.content }}

+ View post +
+{% endfor %} diff --git a/src/core/urls.py b/src/core/urls.py new file mode 100644 index 0000000..45e3b58 --- /dev/null +++ b/src/core/urls.py @@ -0,0 +1,7 @@ + +from django.urls import path +from . import views + +urlpatterns = [ + path('timeline//', views.user_timeline, name='user_timeline'), +] diff --git a/src/core/views.py b/src/core/views.py index 91ea44a..4574267 100644 --- a/src/core/views.py +++ b/src/core/views.py @@ -1,3 +1,60 @@ +import requests from django.shortcuts import render +from django.http import JsonResponse +from .models import PublicationCache, User +from django.conf import settings +from django.db import IntegrityError -# Create your views here. +def user_timeline(request, user_id): + try: + user = User.objects.get(id=user_id) + headers = {'Authorization': f"Bearer {user.access_token}"} + timeline_url = f"{user.pds_url}/xrpc/app.bsky.feed.getTimeline" + + # Realizamos la solicitud al PDS + response = requests.get(timeline_url, headers=headers) + + if response.status_code != 200: + return JsonResponse({"error": "Failed to retrieve timeline"}, status=500) + + feed_data = response.json() + + posts = feed_data.get('feed', []) + for post_data in posts: + post = post_data.get('post', {}) + record = post.get('record', {}) + author = post.get('author', {}) + text = record.get('text', '') + created_at = record.get('createdAt', '') + post_id = post.get('uri', '') + + if post_id: + publication, created = PublicationCache.objects.get_or_create( + id=post_id, + user=user, + defaults={ + 'content': text, + 'author': author.get('handle', ''), + 'author_name': author.get('displayName', ''), + 'published_at': created_at, + 'uri': post.get('uri', ''), + 'cid': post.get('cid', ''), + } + ) + + if not created: + publication.content = text + publication.author = author.get('handle', '') + publication.author_name = author.get('displayName', '') + publication.published_at = created_at + publication.uri = post.get('uri', '') + publication.cid = post.get('cid', '') + publication.save() + + publications = PublicationCache.objects.filter(user=user).order_by('-published_at') + return render(request, 'core/user_timeline.html', {'publications': publications}) + + except User.DoesNotExist: + return JsonResponse({"error": "User not found"}, status=404) + except IntegrityError as e: + return JsonResponse({"error": str(e)}, status=500) diff --git a/src/requirements.txt b/src/requirements.txt index d6206c2..eb1fd3e 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -1,3 +1,4 @@ Django==5.1.3 python-decouple==3.8 mysqlclient==2.2.6 +requests==2.32.3