feat: implement shared-album multi-user photo album
Backend (Go/Gin/pgx): env config, pgx pool with retries, Garage S3 client (path-style, region garage), embedded golang-migrate migrations, JWT auth with refresh rotation, albums CRUD, members/roles/invites with no-escalation matrix, photo upload + async imaging/goexif WebP variants, cursor pagination, presigned URLs, likes/comments, SSE hub (album + personal), notifications, share links. Frontend (SvelteKit 2 + Svelte 5 + Tailwind v4): API wrapper with 401 refresh, auth store, all screens, Uploader/PhotoGrid/Lightbox/Comments, SSE client with reconnect + REST catch-up, three themes + system via data-theme. Deploy without Docker: Makefile builds the frontend into backend/static and embeds it (go:embed) into a single statically linked binary; systemd unit and nginx SSE example included. Use gen2brain/webp (-tags nodynamic) instead of cgo kolesa-team/go-webp to keep CGO_ENABLED=0 static builds; design.md/proposal.md updated accordingly.
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package members
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"shared-album/internal/auth"
|
||||
"shared-album/internal/httpx"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
svc *Service
|
||||
}
|
||||
|
||||
func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} }
|
||||
|
||||
func (h *Handler) List(c *gin.Context) {
|
||||
list, err := h.svc.List(c.Request.Context(), c.Param("id"), auth.UserID(c))
|
||||
if err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"members": list})
|
||||
}
|
||||
|
||||
type inviteRequest struct {
|
||||
Role string `json:"role" binding:"required"`
|
||||
ExpiresIn string `json:"expires_in"`
|
||||
MaxUses int `json:"max_uses"`
|
||||
}
|
||||
|
||||
func (h *Handler) CreateInvite(c *gin.Context) {
|
||||
var req inviteRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.RespondError(c, httpx.BadRequest("invalid_request", "role is required"))
|
||||
return
|
||||
}
|
||||
var ttl time.Duration
|
||||
if req.ExpiresIn != "" {
|
||||
d, err := time.ParseDuration(req.ExpiresIn)
|
||||
if err != nil {
|
||||
httpx.RespondError(c, httpx.BadRequest("invalid_expires_in", "invalid expires_in duration"))
|
||||
return
|
||||
}
|
||||
ttl = d
|
||||
}
|
||||
inv, err := h.svc.CreateInvite(c.Request.Context(), c.Param("id"), auth.UserID(c), CreateInviteInput{
|
||||
Role: req.Role,
|
||||
TTL: ttl,
|
||||
MaxUses: req.MaxUses,
|
||||
})
|
||||
if err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"invite": inv})
|
||||
}
|
||||
|
||||
func (h *Handler) AcceptInvite(c *gin.Context) {
|
||||
inv, err := h.svc.Accept(c.Request.Context(), c.Param("token"), auth.UserID(c))
|
||||
if err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"album_id": inv.AlbumID, "role": inv.Role})
|
||||
}
|
||||
|
||||
func (h *Handler) InviteInfo(c *gin.Context) {
|
||||
inv, err := h.svc.InviteInfo(c.Request.Context(), c.Param("token"))
|
||||
if err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"invite": gin.H{
|
||||
"album_id": inv.AlbumID,
|
||||
"role": inv.Role,
|
||||
"expires_at": inv.ExpiresAt,
|
||||
}})
|
||||
}
|
||||
|
||||
type roleRequest struct {
|
||||
Role string `json:"role" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *Handler) ChangeRole(c *gin.Context) {
|
||||
var req roleRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.RespondError(c, httpx.BadRequest("invalid_request", "role is required"))
|
||||
return
|
||||
}
|
||||
if err := h.svc.ChangeRole(c.Request.Context(), c.Param("id"), auth.UserID(c), c.Param("user_id"), req.Role); err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *Handler) Remove(c *gin.Context) {
|
||||
if err := h.svc.Remove(c.Request.Context(), c.Param("id"), auth.UserID(c), c.Param("user_id")); err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package members
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"shared-album/internal/auth"
|
||||
"shared-album/internal/httpx"
|
||||
"shared-album/internal/models"
|
||||
"shared-album/internal/roles"
|
||||
"shared-album/internal/sse"
|
||||
)
|
||||
|
||||
type AlbumReader interface {
|
||||
OwnerID(ctx context.Context, albumID string) (string, error)
|
||||
}
|
||||
|
||||
type Notifier interface {
|
||||
Create(ctx context.Context, userID, typ string, payload map[string]any) error
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
store *Store
|
||||
albums AlbumReader
|
||||
hub *sse.Hub
|
||||
notify Notifier
|
||||
}
|
||||
|
||||
func NewService(store *Store, albums AlbumReader, hub *sse.Hub, notify Notifier) *Service {
|
||||
return &Service{store: store, albums: albums, hub: hub, notify: notify}
|
||||
}
|
||||
|
||||
func (s *Service) Role(ctx context.Context, albumID, userID string) (string, error) {
|
||||
return s.store.Role(ctx, albumID, userID)
|
||||
}
|
||||
|
||||
func (s *Service) actorRole(ctx context.Context, albumID, actorID string) (string, error) {
|
||||
ownerID, err := s.albums.OwnerID(ctx, albumID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ownerID == actorID {
|
||||
return roles.Owner, nil
|
||||
}
|
||||
role, err := s.store.Role(ctx, albumID, actorID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if role == "" {
|
||||
return "", httpx.NotFound("album_not_found", "album not found")
|
||||
}
|
||||
return role, nil
|
||||
}
|
||||
|
||||
type CreateInviteInput struct {
|
||||
Role string
|
||||
TTL time.Duration
|
||||
MaxUses int
|
||||
}
|
||||
|
||||
func (s *Service) CreateInvite(ctx context.Context, albumID, actorID string, in CreateInviteInput) (*models.Invite, error) {
|
||||
role, err := s.actorRole(ctx, albumID, actorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !roles.CanInvite(role, in.Role) {
|
||||
return nil, httpx.Forbidden("forbidden", "you cannot invite with this role")
|
||||
}
|
||||
if in.TTL <= 0 {
|
||||
in.TTL = 7 * 24 * time.Hour
|
||||
}
|
||||
if in.MaxUses <= 0 {
|
||||
in.MaxUses = 1
|
||||
}
|
||||
token, err := randomToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inv := &models.Invite{
|
||||
ID: auth.NewID(),
|
||||
AlbumID: albumID,
|
||||
Token: token,
|
||||
Role: in.Role,
|
||||
CreatedBy: actorID,
|
||||
ExpiresAt: time.Now().Add(in.TTL),
|
||||
MaxUses: in.MaxUses,
|
||||
}
|
||||
if err := s.store.CreateInvite(ctx, inv); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return inv, nil
|
||||
}
|
||||
|
||||
func (s *Service) Accept(ctx context.Context, token, userID string) (*models.Invite, error) {
|
||||
inv, err := s.store.GetInviteByToken(ctx, token)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return nil, httpx.NotFound("invite_not_found", "invite not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if time.Now().After(inv.ExpiresAt) {
|
||||
return nil, httpx.BadRequest("invite_expired", "invite has expired")
|
||||
}
|
||||
if inv.Uses >= inv.MaxUses {
|
||||
return nil, httpx.BadRequest("invite_exhausted", "invite has no remaining uses")
|
||||
}
|
||||
if err := s.store.Add(ctx, inv.AlbumID, userID, inv.Role, &inv.CreatedBy); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.store.IncrementUses(ctx, inv.ID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.hub.Publish(inv.AlbumID, "member.joined", map[string]string{"album_id": inv.AlbumID, "user_id": userID, "role": inv.Role})
|
||||
if s.notify != nil {
|
||||
ownerID, err := s.albums.OwnerID(ctx, inv.AlbumID)
|
||||
if err == nil && ownerID != userID {
|
||||
_ = s.notify.Create(ctx, ownerID, "member.joined", map[string]any{"album_id": inv.AlbumID, "user_id": userID, "role": inv.Role})
|
||||
}
|
||||
}
|
||||
return inv, nil
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, albumID, actorID string) ([]models.AlbumMember, error) {
|
||||
if _, err := s.actorRole(ctx, albumID, actorID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.store.List(ctx, albumID)
|
||||
}
|
||||
|
||||
func (s *Service) InviteInfo(ctx context.Context, token string) (*models.Invite, error) {
|
||||
inv, err := s.store.GetInviteByToken(ctx, token)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return nil, httpx.NotFound("invite_not_found", "invite not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if time.Now().After(inv.ExpiresAt) || inv.Uses >= inv.MaxUses {
|
||||
return nil, httpx.BadRequest("invite_inactive", "invite is expired or exhausted")
|
||||
}
|
||||
return inv, nil
|
||||
}
|
||||
|
||||
func (s *Service) ChangeRole(ctx context.Context, albumID, actorID, targetID, newRole string) error {
|
||||
actor, err := s.actorRole(ctx, albumID, actorID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := s.store.Role(ctx, albumID, targetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current == "" {
|
||||
if ownerID, oerr := s.albums.OwnerID(ctx, albumID); oerr == nil && ownerID == targetID {
|
||||
return httpx.Forbidden("forbidden", "cannot change the owner's role")
|
||||
}
|
||||
return httpx.NotFound("member_not_found", "member not found")
|
||||
}
|
||||
if !roles.CanChangeRole(actor, current, newRole) {
|
||||
return httpx.Forbidden("forbidden", "you cannot assign this role")
|
||||
}
|
||||
if err := s.store.UpdateRole(ctx, albumID, targetID, newRole); err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return httpx.NotFound("member_not_found", "member not found")
|
||||
}
|
||||
return err
|
||||
}
|
||||
s.hub.Publish(albumID, "member.updated", map[string]string{"album_id": albumID, "user_id": targetID, "role": newRole})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) Remove(ctx context.Context, albumID, actorID, targetID string) error {
|
||||
actor, err := s.actorRole(ctx, albumID, actorID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
targetRole, err := s.store.Role(ctx, albumID, targetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if targetRole == "" {
|
||||
if ownerID, oerr := s.albums.OwnerID(ctx, albumID); oerr == nil && ownerID == targetID {
|
||||
return httpx.Forbidden("forbidden", "cannot remove the album owner")
|
||||
}
|
||||
return httpx.NotFound("member_not_found", "member not found")
|
||||
}
|
||||
if !roles.CanRemoveMember(actor, targetRole) {
|
||||
return httpx.Forbidden("forbidden", "you cannot remove this member")
|
||||
}
|
||||
if err := s.store.Remove(ctx, albumID, targetID); err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return httpx.NotFound("member_not_found", "member not found")
|
||||
}
|
||||
return err
|
||||
}
|
||||
s.hub.Publish(albumID, "member.left", map[string]string{"album_id": albumID, "user_id": targetID})
|
||||
return nil
|
||||
}
|
||||
|
||||
func randomToken() (string, error) {
|
||||
raw, _, err := auth.NewRefreshToken()
|
||||
return raw, err
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package members
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"shared-album/internal/models"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("not found")
|
||||
|
||||
type Store struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewStore(pool *pgxpool.Pool) *Store { return &Store{pool: pool} }
|
||||
|
||||
func (s *Store) Role(ctx context.Context, albumID, userID string) (string, error) {
|
||||
var role string
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT role FROM album_members WHERE album_id = $1 AND user_id = $2`,
|
||||
albumID, userID).Scan(&role)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
return role, nil
|
||||
}
|
||||
|
||||
func (s *Store) List(ctx context.Context, albumID string) ([]models.AlbumMember, error) {
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`SELECT m.album_id, m.user_id, m.role, m.invited_by, m.joined_at,
|
||||
u.id, u.email, u.name, COALESCE(u.avatar_url, '')
|
||||
FROM album_members m
|
||||
JOIN users u ON u.id = m.user_id
|
||||
WHERE m.album_id = $1
|
||||
ORDER BY m.joined_at`, albumID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := []models.AlbumMember{}
|
||||
for rows.Next() {
|
||||
var m models.AlbumMember
|
||||
if err := rows.Scan(&m.AlbumID, &m.UserID, &m.Role, &m.InvitedBy, &m.JoinedAt,
|
||||
&m.User.ID, &m.User.Email, &m.User.Name, &m.User.AvatarURL); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) Add(ctx context.Context, albumID, userID, role string, invitedBy *string) error {
|
||||
_, err := s.pool.Exec(ctx,
|
||||
`INSERT INTO album_members (album_id, user_id, role, invited_by)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (album_id, user_id) DO UPDATE SET role = EXCLUDED.role`,
|
||||
albumID, userID, role, invitedBy)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) UpdateRole(ctx context.Context, albumID, userID, role string) error {
|
||||
tag, err := s.pool.Exec(ctx,
|
||||
`UPDATE album_members SET role = $3 WHERE album_id = $1 AND user_id = $2`,
|
||||
albumID, userID, role)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Remove(ctx context.Context, albumID, userID string) error {
|
||||
tag, err := s.pool.Exec(ctx,
|
||||
`DELETE FROM album_members WHERE album_id = $1 AND user_id = $2`, albumID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) CreateInvite(ctx context.Context, inv *models.Invite) error {
|
||||
return s.pool.QueryRow(ctx,
|
||||
`INSERT INTO invites (id, album_id, token, role, created_by, expires_at, max_uses)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING created_at`,
|
||||
inv.ID, inv.AlbumID, inv.Token, inv.Role, inv.CreatedBy, inv.ExpiresAt, inv.MaxUses,
|
||||
).Scan(&inv.CreatedAt)
|
||||
}
|
||||
|
||||
func (s *Store) GetInviteByToken(ctx context.Context, token string) (*models.Invite, error) {
|
||||
var inv models.Invite
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT id, album_id, token, role, created_by, expires_at, max_uses, uses, created_at
|
||||
FROM invites WHERE token = $1`, token).
|
||||
Scan(&inv.ID, &inv.AlbumID, &inv.Token, &inv.Role, &inv.CreatedBy, &inv.ExpiresAt, &inv.MaxUses, &inv.Uses, &inv.CreatedAt)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &inv, nil
|
||||
}
|
||||
|
||||
func (s *Store) IncrementUses(ctx context.Context, id string) error {
|
||||
_, err := s.pool.Exec(ctx, `UPDATE invites SET uses = uses + 1 WHERE id = $1`, id)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user