diff --git a/internal/http/graph/messages_resolvers.go b/internal/http/graph/messages_resolvers.go index dbedac2..8f61d5b 100644 --- a/internal/http/graph/messages_resolvers.go +++ b/internal/http/graph/messages_resolvers.go @@ -120,6 +120,11 @@ func (r *queryResolver) GetUserChats(ctx context.Context, userID int) ([]*domain // MessageStream реализация подписки на новые сообщения func (r *subscriptionResolver) MessageStream(ctx context.Context, userID int) (<-chan *domain.Message, error) { + + if userID == 0 { + return nil, fmt.Errorf("user not authenticated") + } + stream, err := r.MessageClient.StreamMessages(ctx, &proto.StreamMessagesRequest{ UserId: int32(userID), }) diff --git a/migrations/0002_initial_schema.down.sql b/migrations/0002_initial_schema.down.sql new file mode 100644 index 0000000..3c75c1a --- /dev/null +++ b/migrations/0002_initial_schema.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS subscription_notifications CASCADE; +DROP TABLE IF EXISTS subscriptions CASCADE; \ No newline at end of file diff --git a/migrations/0002_initial_schema.up.sql b/migrations/0002_initial_schema.up.sql new file mode 100644 index 0000000..150d1b5 --- /dev/null +++ b/migrations/0002_initial_schema.up.sql @@ -0,0 +1,21 @@ +CREATE TABLE subscriptions ( + id SERIAL PRIMARY KEY, + follower_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, -- Кто подписывается + following_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, -- На кого подписываются + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + UNIQUE(follower_id, following_id), -- Уникальная подписка + CHECK (follower_id <> following_id) -- Запрет подписки на самого себя +); + +-- Уведомления о подписках +CREATE TABLE subscription_notifications ( + id SERIAL PRIMARY KEY, + subscription_id INTEGER NOT NULL REFERENCES subscriptions(id) ON DELETE CASCADE, + is_read BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() +); + +-- Индексы для улучшения производительности +CREATE INDEX idx_subscriptions_follower_id ON subscriptions(follower_id); +CREATE INDEX idx_subscriptions_following_id ON subscriptions(following_id); +CREATE INDEX idx_subscription_notifications_subscription_id ON subscription_notifications(subscription_id); \ No newline at end of file