Initial commit: Aurora Moments self-hosted photo/video sharing platform
Django/DRF + PostgreSQL/Redis/Celery backend with accounts, library, moments, and notifications apps; React/TypeScript/Vite frontend with Instagram-style sidebar nav, feed, profiles, nested comment replies, notifications, and photo/video upload with ffmpeg-generated posters. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Notification
|
||||
|
||||
|
||||
@admin.register(Notification)
|
||||
class NotificationAdmin(admin.ModelAdmin):
|
||||
list_display = ["id", "recipient", "actor", "verb", "moment", "is_read", "created_at"]
|
||||
list_filter = ["verb", "is_read"]
|
||||
@@ -0,0 +1,10 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class NotificationsConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.notifications"
|
||||
label = "notifications"
|
||||
|
||||
def ready(self):
|
||||
from . import signals # noqa: F401
|
||||
@@ -0,0 +1,35 @@
|
||||
# Generated by Django 5.0.9 on 2026-08-09 21:28
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('moments', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Notification',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('verb', models.CharField(choices=[('like', 'Like'), ('comment', 'Comment'), ('reply', 'Reply')], max_length=10)),
|
||||
('is_read', models.BooleanField(default=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('actor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to=settings.AUTH_USER_MODEL)),
|
||||
('comment', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to='moments.comment')),
|
||||
('moment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to='moments.moment')),
|
||||
('recipient', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'ordering': ['-created_at'],
|
||||
'indexes': [models.Index(fields=['recipient', 'is_read'], name='notificatio_recipie_4e3567_idx')],
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,31 @@
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Notification(models.Model):
|
||||
"""Tells `recipient` that `actor` did something (`verb`) involving
|
||||
`moment` (and, for comment/reply verbs, the specific `comment`).
|
||||
Created by signal handlers in signals.py — never directly by API
|
||||
consumers."""
|
||||
|
||||
class Verb(models.TextChoices):
|
||||
LIKE = "like", "Like"
|
||||
COMMENT = "comment", "Comment"
|
||||
REPLY = "reply", "Reply"
|
||||
|
||||
recipient = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="notifications", on_delete=models.CASCADE)
|
||||
actor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="+", on_delete=models.CASCADE)
|
||||
verb = models.CharField(max_length=10, choices=Verb.choices)
|
||||
moment = models.ForeignKey("moments.Moment", related_name="notifications", on_delete=models.CASCADE)
|
||||
comment = models.ForeignKey(
|
||||
"moments.Comment", null=True, blank=True, related_name="notifications", on_delete=models.CASCADE
|
||||
)
|
||||
is_read = models.BooleanField(default=False)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at"]
|
||||
indexes = [models.Index(fields=["recipient", "is_read"])]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.actor} {self.verb} -> {self.recipient}"
|
||||
@@ -0,0 +1,16 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.accounts.serializers import UserSerializer
|
||||
|
||||
from .models import Notification
|
||||
|
||||
|
||||
class NotificationSerializer(serializers.ModelSerializer):
|
||||
actor = UserSerializer(read_only=True)
|
||||
moment_id = serializers.IntegerField(read_only=True)
|
||||
comment_id = serializers.IntegerField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Notification
|
||||
fields = ["id", "actor", "verb", "moment_id", "comment_id", "is_read", "created_at"]
|
||||
read_only_fields = fields
|
||||
@@ -0,0 +1,49 @@
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
from apps.moments.models import Comment, Reaction
|
||||
|
||||
from .models import Notification
|
||||
|
||||
|
||||
@receiver(post_save, sender=Reaction)
|
||||
def notify_on_reaction(sender, instance, created, **kwargs):
|
||||
if not created:
|
||||
return
|
||||
moment = instance.moment
|
||||
if instance.user_id == moment.owner_id:
|
||||
return
|
||||
Notification.objects.create(
|
||||
recipient_id=moment.owner_id,
|
||||
actor=instance.user,
|
||||
verb=Notification.Verb.LIKE,
|
||||
moment=moment,
|
||||
)
|
||||
|
||||
|
||||
@receiver(post_save, sender=Comment)
|
||||
def notify_on_comment(sender, instance, created, **kwargs):
|
||||
if not created:
|
||||
return
|
||||
moment = instance.moment
|
||||
|
||||
if instance.parent_id is None:
|
||||
if instance.author_id != moment.owner_id:
|
||||
Notification.objects.create(
|
||||
recipient_id=moment.owner_id,
|
||||
actor=instance.author,
|
||||
verb=Notification.Verb.COMMENT,
|
||||
moment=moment,
|
||||
comment=instance,
|
||||
)
|
||||
return
|
||||
|
||||
parent_author_id = instance.parent.author_id
|
||||
if instance.author_id != parent_author_id:
|
||||
Notification.objects.create(
|
||||
recipient_id=parent_author_id,
|
||||
actor=instance.author,
|
||||
verb=Notification.Verb.REPLY,
|
||||
moment=moment,
|
||||
comment=instance,
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from .views import NotificationViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("", NotificationViewSet, basename="notification")
|
||||
|
||||
urlpatterns = router.urls
|
||||
@@ -0,0 +1,24 @@
|
||||
from rest_framework import mixins, permissions, viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
|
||||
from .models import Notification
|
||||
from .serializers import NotificationSerializer
|
||||
|
||||
|
||||
class NotificationViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
|
||||
serializer_class = NotificationSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Notification.objects.filter(recipient=self.request.user).select_related("actor", "moment")
|
||||
|
||||
@action(detail=False, methods=["get"], url_path="unread-count")
|
||||
def unread_count(self, request):
|
||||
count = self.get_queryset().filter(is_read=False).count()
|
||||
return Response({"unread_count": count})
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="mark-read")
|
||||
def mark_read(self, request):
|
||||
self.get_queryset().filter(is_read=False).update(is_read=True)
|
||||
return Response(status=204)
|
||||
Reference in New Issue
Block a user