sojorn/post_repository_fixed.go
Patrick Britton 3c4680bdd7 Initial commit: Complete threaded conversation system with inline replies
**Major Features Added:**
- **Inline Reply System**: Replace compose screen with inline reply boxes
- **Thread Navigation**: Parent/child navigation with jump functionality
- **Chain Flow UI**: Reply counts, expand/collapse animations, visual hierarchy
- **Enhanced Animations**: Smooth transitions, hover effects, micro-interactions

 **Frontend Changes:**
- **ThreadedCommentWidget**: Complete rewrite with animations and navigation
- **ThreadNode Model**: Added parent references and descendant counting
- **ThreadedConversationScreen**: Integrated navigation handlers
- **PostDetailScreen**: Replaced with threaded conversation view
- **ComposeScreen**: Added reply indicators and context
- **PostActions**: Fixed visibility checks for chain buttons

 **Backend Changes:**
- **API Route**: Added /posts/:id/thread endpoint
- **Post Repository**: Include allow_chain and visibility fields in feed
- **Thread Handler**: Support for fetching post chains

 **UI/UX Improvements:**
- **Reply Context**: Clear indication when replying to specific posts
- **Character Counting**: 500 character limit with live counter
- **Visual Hierarchy**: Depth-based indentation and styling
- **Smooth Animations**: SizeTransition, FadeTransition, hover states
- **Chain Navigation**: Parent/child buttons with visual feedback

 **Technical Enhancements:**
- **Animation Controllers**: Proper lifecycle management
- **State Management**: Clean separation of concerns
- **Navigation Callbacks**: Reusable navigation system
- **Error Handling**: Graceful fallbacks and user feedback

This creates a Reddit-style threaded conversation experience with smooth
animations, inline replies, and intuitive navigation between posts in a chain.
2026-01-30 07:40:19 -06:00

123 lines
4.4 KiB
Go

package repository
import (
"context"
"fmt"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/patbritton/sojorn-backend/internal/models"
)
type PostRepository struct {
pool *pgxpool.Pool
}
func NewPostRepository(pool *pgxpool.Pool) *PostRepository {
return &PostRepository{pool: pool}
}
func (r *PostRepository) CreatePost(ctx context.Context, post *models.Post) error {
// Calculate confidence score if it's a beacon
if post.IsBeacon {
var harmonyScore int
err := r.pool.QueryRow(ctx, "SELECT harmony_score FROM public.trust_state WHERE user_id = $1", post.AuthorID).Scan(&harmonyScore)
if err == nil {
// Logic: confidence = harmony_score / 100.0 (legacy parity)
post.Confidence = float64(harmonyScore) / 100.0
} else {
post.Confidence = 0.5 // Default fallback
}
}
query := `
INSERT INTO public.posts (
author_id, category_id, body, tone_label, cis_score, image_url, video_url, thumbnail_url, duration_ms,
body_format, background_id, tags, location, is_beacon, beacon_type, confidence_score, is_active_beacon,
allow_chain, chain_parent_id, expires_at, status, visibility
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22)
RETURNING id, created_at, edited_at
`
err := r.pool.QueryRow(
ctx, query,
post.AuthorID, post.CategoryID, post.Body, post.ToneLabel, post.CISScore,
post.ImageURL, post.VideoURL, post.ThumbnailURL, post.DurationMS,
post.BodyFormat, post.BackgroundID, post.Tags, post.Location,
post.IsBeacon, post.BeaconType, post.Confidence, post.IsActiveBeacon,
post.AllowChain, post.ChainParentID, post.ExpiresAt, post.Status, post.Visibility,
).Scan(&post.ID, &post.CreatedAt, &post.EditedAt)
if err != nil {
return fmt.Errorf("failed to create post: %w", err)
}
return nil
}
func (r *PostRepository) GetFeed(ctx context.Context, userID string, categorySlug string, hasVideo bool, limit int, offset int) ([]models.Post, error) {
query := `
SELECT
p.id, p.author_id, p.category_id, p.body,
COALESCE(p.image_url, ''),
CASE
WHEN COALESCE(p.video_url, '') <> '' THEN p.video_url
WHEN COALESCE(p.image_url, '') ILIKE '%.mp4' THEN p.image_url
ELSE ''
END AS resolved_video_url,
COALESCE(NULLIF(p.thumbnail_url, ''), p.image_url, '') AS resolved_thumbnail_url,
COALESCE(p.duration_ms, 0),
COALESCE(p.tags, ARRAY[]::text[]),
p.created_at,
pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url,
COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count,
CASE WHEN ($4::text) != '' THEN EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = NULLIF($4::text, '')::uuid) ELSE FALSE END as is_liked,
p.allow_chain, p.visibility
FROM public.posts p
JOIN public.profiles pr ON p.author_id = pr.id
LEFT JOIN public.post_metrics m ON p.id = m.post_id
LEFT JOIN public.categories c ON p.category_id = c.id
WHERE p.deleted_at IS NULL AND p.status = 'active'
AND (
p.author_id = NULLIF($4::text, '')::uuid -- My own posts
OR pr.is_private = FALSE -- Public profiles
OR EXISTS (
SELECT 1 FROM public.follows f
WHERE f.follower_id = NULLIF($4::text, '')::uuid AND f.following_id = p.author_id AND f.status = 'accepted'
)
)
AND ($3 = FALSE OR (COALESCE(p.video_url, '') <> '' OR (COALESCE(p.image_url, '') ILIKE '%.mp4')))
AND ($5 = '' OR c.slug = $5)
ORDER BY p.created_at DESC
LIMIT $1 OFFSET $2
`
rows, err := r.pool.Query(ctx, query, limit, offset, hasVideo, userID, categorySlug)
if err != nil {
return nil, err
}
defer rows.Close()
posts := []models.Post{}
for rows.Next() {
var p models.Post
err := rows.Scan(
&p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt,
&p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL,
&p.LikeCount, &p.CommentCount, &p.IsLiked,
&p.AllowChain, &p.Visibility,
)
if err != nil {
return nil, err
}
p.Author = &models.AuthorProfile{
ID: p.AuthorID,
Handle: p.AuthorHandle,
DisplayName: p.AuthorDisplayName,
AvatarURL: p.AuthorAvatarURL,
}
posts = append(posts, p)
}
return posts, nil
}
// Rest of the file would continue with other methods...
// This is just the GetFeed method with the fix applied