Skip to content

Commit

Permalink
activity streams fix
Browse files Browse the repository at this point in the history
  • Loading branch information
oktayozel committed Nov 24, 2024
1 parent 976f62d commit c434204
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
24 changes: 16 additions & 8 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,24 @@ def __str__(self):
return f"{self.translation} (Translation of {self.word})"

class ActivityStream(models.Model):
actor = models.ForeignKey(User, on_delete=models.CASCADE, related_name='activities')
verb = models.CharField(max_length=50) # liked, created, followed
object_type = models.CharField(max_length=50) # Quiz, Post
object_id = models.IntegerField() #Quiz ID Post ID
timestamp = models.DateTimeField(default=now) # timestamp
target = models.CharField(max_length=100, null=True, blank=True) # Add this field
actor = models.ForeignKey(
User, on_delete=models.CASCADE, related_name='activities'
)
verb = models.CharField(max_length=50)
object_type = models.CharField(max_length=50)
object_id = models.IntegerField()
timestamp = models.DateTimeField(default=now)
target = models.CharField(max_length=100, null=True, blank=True)
affected_username = models.CharField(
max_length=150, null=True, blank=True
)

def __str__(self):
return f"{self.actor.username} {self.verb} {self.object_type}:{self.object_id} at {self.timestamp}"

affected = f" affecting {self.affected_username}" if self.affected_username else ""
return (
f"{self.actor.username} {self.verb} {self.object_type}:{self.object_id} "
f"at {self.timestamp}{affected}"
)

class Comment(models.Model):
post = models.ForeignKey('Post', on_delete=models.CASCADE, related_name='comments') # Assuming a Post model exists
Expand Down
10 changes: 6 additions & 4 deletions backend/app/views_directory/activity_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from django.contrib.auth.models import User


@api_view(['POST'])
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def activities_for_user_as_object(request):
user = request.user
user = request.user # Logged-in user

activities = ActivityStream.objects.filter(object_type="Profile", object_id=user.id).order_by('-timestamp')
activities = ActivityStream.objects.filter(
affected_username=user.username
).order_by('-timestamp') # Filter where logged-in user is the affected user

activity_data = [
{
Expand All @@ -20,13 +22,13 @@ def activities_for_user_as_object(request):
"object_type": activity.object_type,
"object_id": activity.object_id,
"timestamp": activity.timestamp,
"affected_username": activity.affected_username,
}
for activity in activities
]

return Response({"activities": activity_data}, status=status.HTTP_200_OK)


@api_view(['POST'])
@permission_classes([IsAuthenticated])
def activities_by_user(request):
Expand Down
1 change: 1 addition & 0 deletions backend/app/views_directory/postviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def like_post(request):
verb="liked",
object_type="Post",
object_id=post.id,
affected_username=post.author.username
)

# Include like and bookmark status in the response
Expand Down

0 comments on commit c434204

Please sign in to comment.