Get user active stream
Kullanıcının aktif yayınını getirme
Get User Active Stream API Dökümantasyonu
Genel Bakış
Bu API endpoint'i, kullanıcının profilinde göstermek için aktif canlı yayınını getirir. Bir kullanıcının aynı anda sadece 1 aktif canlı yayını olması beklenir. Eğer birden fazla aktif yayın bulunursa, backend loglarında uyarı kaydedilir ve en son başlayan yayın döndürülür.
Not: Tüm yanıtlar BaseResponseDto<T> formatında döner. Interceptor tarafından otomatik olarak bu formata dönüştürülür.
Response Formatı
Tüm başarılı yanıtlar aşağıdaki formatta döner:
{
"isSuccess": true,
"statusCode": 200,
"data": { ... },
"errors": [],
"timestamp": "2024-01-01T12:00:00.000Z"
}Hata yanıtları da aynı formatta döner, ancak isSuccess: false ve errors dizisinde hata mesajları bulunur.
Endpoint'ler
1. Mevcut Kullanıcının Aktif Stream'ini Getir
Endpoint: GET /live-stream/my-active-stream
Apidog ID: 24696491
Açıklama: İstek atan kullanıcının aktif canlı yayınını getirir.
Authentication: Optional JWT Bearer Token
Request Headers:
Authorization: Bearer {jwt_token} // optional
Content-Type: application/jsonResponse:
Başarılı Yanıt (200 OK):
{
"isSuccess": true,
"statusCode": 200,
"data": {
"id": "507f1f77bcf86cd799439011",
"title": "Canlı Yayın Başlığı",
"channelName": "canli-yayin-basligi-abc123",
"creator": {
"_id": "507f1f77bcf86cd799439012",
"username": "kullanıcı_adi",
"name": "Ad",
"surname": "Soyad",
"profilePhoto": {
"_id": "507f1f77bcf86cd799439013",
"url": "https://example.com/photo.jpg",
"type": "image",
"createdAt": "2024-01-01T00:00:00.000Z",
"updatedAt": "2024-01-01T00:00:00.000Z"
}
},
"broadcasters": [
{
"user": "507f1f77bcf86cd799439012",
"role": "host",
"uid": 123456,
"userSummary": {
"_id": "507f1f77bcf86cd799439012",
"username": "kullanıcı_adi",
"name": "Ad",
"surname": "Soyad",
"profilePhoto": { ... }
}
}
],
"guests": [],
"thumbnailUrl": "https://example.com/thumbnail.jpg",
"recording": true,
"recordingUrl": null,
"status": "active",
"accessType": "free",
"price": 0,
"startedAt": "2024-01-01T12:00:00.000Z",
"endedAt": null,
"createdAt": "2024-01-01T12:00:00.000Z",
"updatedAt": "2024-01-01T12:00:00.000Z"
},
"errors": [],
"timestamp": "2024-01-01T12:00:00.000Z"
}Aktif Stream Bulunamadı (404 Not Found):
{
"isSuccess": false,
"statusCode": 404,
"data": null,
"errors": ["Kullanıcının aktif canlı yayını bulunamadı"],
"timestamp": "2024-01-01T12:00:00.000Z"
}Hata Yanıtları:
401 Unauthorized:
{
"isSuccess": false,
"statusCode": 401,
"data": null,
"errors": ["Unauthorized"],
"timestamp": "2024-01-01T12:00:00.000Z"
}Not: Bu hata sadece istemci JWT gönderdiginde ve token gecersiz veya suresi dolmus oldugunda döner.
500 Internal Server Error:
{
"isSuccess": false,
"statusCode": 500,
"data": null,
"errors": ["Internal server error"],
"timestamp": "2024-01-01T12:00:00.000Z"
}2. Belirli Bir Kullanıcının Aktif Stream'ini Getir
Endpoint: GET /live-stream/user/:userId/active-stream
Apidog ID: 24696492
Açıklama: Belirli bir kullanıcının aktif canlı yayınını getirir. Profil sayfalarında kullanılır.
Authentication: JWT Bearer Token gerekli
Path Parameters:
userId(string, required): Kullanıcı kimliği (MongoDB ObjectId formatında)
Request Headers:
Authorization: Bearer {jwt_token}
Content-Type: application/jsonResponse:
Başarılı Yanıt (200 OK):
{
"isSuccess": true,
"statusCode": 200,
"data": {
"id": "507f1f77bcf86cd799439011",
"title": "Canlı Yayın Başlığı",
"channelName": "canli-yayin-basligi-abc123",
"creator": { ... },
"broadcasters": [ ... ],
"guests": [ ... ],
"thumbnailUrl": "https://example.com/thumbnail.jpg",
"recording": true,
"recordingUrl": null,
"status": "active",
"accessType": "free",
"price": 0,
"startedAt": "2024-01-01T12:00:00.000Z",
"endedAt": null,
"createdAt": "2024-01-01T12:00:00.000Z",
"updatedAt": "2024-01-01T12:00:00.000Z"
},
"errors": [],
"timestamp": "2024-01-01T12:00:00.000Z"
}Aktif Stream Bulunamadı (404 Not Found):
{
"isSuccess": false,
"statusCode": 404,
"data": null,
"errors": ["Kullanıcının aktif canlı yayını bulunamadı"],
"timestamp": "2024-01-01T12:00:00.000Z"
}Hata Yanıtları:
400 Bad Request (Geçersiz userId formatı):
{
"isSuccess": false,
"statusCode": 400,
"data": null,
"errors": ["Invalid userId format"],
"timestamp": "2024-01-01T12:00:00.000Z"
}401 Unauthorized:
{
"isSuccess": false,
"statusCode": 401,
"data": null,
"errors": ["Unauthorized"],
"timestamp": "2024-01-01T12:00:00.000Z"
}500 Internal Server Error:
{
"isSuccess": false,
"statusCode": 500,
"data": null,
"errors": ["Internal server error"],
"timestamp": "2024-01-01T12:00:00.000Z"
}Kullanım Senaryoları
Senaryo 1: Kullanıcı Kendi Profilini Görüntülüyor
Kullanıcı kendi profil sayfasındayken aktif yayınını görmek için:
const response = await fetch('https://api.allmine.win/live-stream/my-active-stream', {
method: 'GET',
headers: {
Authorization: `Bearer ${jwtToken}`,
'Content-Type': 'application/json',
},
});
const result = await response.json();
if (result.isSuccess && result.data) {
// Aktif yayın var, UI'da göster
console.log('Aktif yayın:', result.data.title);
} else if (response.status === 404) {
// Aktif yayın yok
console.log('Aktif yayın bulunamadı');
} else {
// Hata durumu
console.error('Hata:', result.errors);
}Senaryo 2: Başka Bir Kullanıcının Profilini Görüntülüyor
Kullanıcı başka birinin profil sayfasındayken o kullanıcının aktif yayınını görmek için:
const userId = '507f1f77bcf86cd799439012';
const response = await fetch(`https://api.allmine.win/live-stream/user/${userId}/active-stream`, {
method: 'GET',
headers: {
Authorization: `Bearer ${jwtToken}`,
'Content-Type': 'application/json',
},
});
const result = await response.json();
if (result.isSuccess && result.data) {
// Aktif yayın var, profil sayfasında göster
displayActiveStream(result.data);
} else if (response.status === 404) {
// Aktif yayın yok, profil sayfasında gösterme
hideActiveStreamIndicator();
} else {
// Hata durumu
console.error('Hata:', result.errors);
}Önemli Notlar
1. Birden Fazla Aktif Stream Durumu
Normalde bir kullanıcının aynı anda sadece 1 aktif canlı yayını olması gerekir. Ancak sistemde bir hata veya edge case durumunda birden fazla aktif stream bulunabilir. Bu durumda:
- Backend loglarında WARN seviyesinde uyarı kaydedilir
- Log formatı:
⚠️ User {userId} has {count} active streams. This should not happen! Stream IDs: {id1}, {id2}, ... - API yine de yanıt döner ve en son başlayan stream döndürülür (startedAt'e göre sıralanmış)
2. Stream Durumu
Sadece status: "active" olan stream'ler döndürülür. ended veya scheduled durumundaki stream'ler döndürülmez.
3. 404 Not Found Response
Eğer kullanıcının aktif stream'i yoksa, API 404 Not Found döner. Bu durumda:
isSuccess: falsestatusCode: 404data: nullerrors: ["Kullanıcının aktif canlı yayını bulunamadı"]
Frontend'de 404 durumunu kontrol ederek aktif stream göstergesi gösterilmemelidir.
4. Response DTO Yapısı
data alanında LiveStreamResponseDto formatında stream bilgileri bulunur:
- id: Stream'in benzersiz kimliği
- title: Yayın başlığı
- channelName: Agora kanal adı
- creator: Yayını başlatan kullanıcı bilgileri (populated)
- broadcasters: Yayıncılar listesi (host ve guest'ler)
- guests: Misafir kullanıcılar listesi
- thumbnailUrl: Yayın thumbnail URL'i
- recording: Kayıt alınıp alınmadığı
- recordingUrl: Kayıt URL'i (varsa)
- status: Stream durumu (her zaman "active" olacak)
- accessType: Erişim tipi ("free" veya "paid")
- price: Fiyat (ücretli yayınlar için)
- startedAt: Yayının başlama zamanı
- endedAt: Yayının bitiş zamanı (aktif stream'lerde null)
- createdAt: Oluşturulma zamanı
- updatedAt: Güncellenme zamanı
Örnek Kullanım (React Native)
import { useState, useEffect } from 'react';
interface BaseResponse<T> {
isSuccess: boolean;
statusCode: number;
data: T | null;
errors: string[];
timestamp: string;
}
interface LiveStreamResponse {
id: string;
title: string;
channelName: string;
creator: {
_id: string;
username?: string;
name: string | null;
surname: string | null;
profilePhoto: {
_id: string;
url: string;
type: string;
} | null;
};
status: string;
startedAt: string;
// ... diğer alanlar
}
const useUserActiveStream = (userId?: string) => {
const [activeStream, setActiveStream] = useState<LiveStreamResponse | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const fetchActiveStream = async () => {
try {
setLoading(true);
setError(null);
const endpoint = userId
? `/live-stream/user/${userId}/active-stream`
: '/live-stream/my-active-stream';
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${await getAuthToken()}`,
'Content-Type': 'application/json',
},
});
const result: BaseResponse<LiveStreamResponse> = await response.json();
if (result.isSuccess && result.data) {
setActiveStream(result.data);
} else if (response.status === 404) {
// Aktif stream yok, bu normal bir durum
setActiveStream(null);
} else {
// Hata durumu
setError(result.errors.join(', ') || 'Bilinmeyen hata');
setActiveStream(null);
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Bilinmeyen hata');
setActiveStream(null);
} finally {
setLoading(false);
}
};
fetchActiveStream();
}, [userId]);
return { activeStream, loading, error };
};
// Kullanım
const ProfileScreen = ({ userId }: { userId?: string }) => {
const { activeStream, loading, error } = useUserActiveStream(userId);
if (loading) {
return <ActivityIndicator />;
}
if (error) {
return <Text>Hata: {error}</Text>;
}
return (
<View>
{activeStream ? (
<View>
<Text>🔴 Canlı Yayında</Text>
<Text>{activeStream.title}</Text>
<Text>Başladı: {new Date(activeStream.startedAt).toLocaleString()}</Text>
</View>
) : (
<Text>Şu anda canlı yayında değil</Text>
)}
</View>
);
};Hata Yönetimi
Frontend'de Hata Yönetimi
try {
const response = await fetch('/live-stream/my-active-stream', {
headers: {
Authorization: `Bearer ${token}`,
},
});
const result: BaseResponse<LiveStreamResponse> = await response.json();
if (result.isSuccess && result.data) {
// Aktif stream var
console.log('Aktif stream:', result.data);
} else if (response.status === 404) {
// Aktif stream yok - bu normal bir durum
console.log('Aktif stream bulunamadı');
} else if (response.status === 401) {
// Token geçersiz, yeniden giriş yap
await refreshToken();
// İsteği tekrar dene
} else if (response.status === 500) {
// Sunucu hatası
console.error('Sunucu hatası:', result.errors);
} else {
// Diğer hatalar
console.error('Hata:', result.errors);
}
} catch (error) {
console.error('Aktif stream getirme hatası:', error);
}Özet
- ✅ Kullanıcının aktif canlı yayınını getirir
- ✅ İki endpoint: kendi stream'i ve başka kullanıcının stream'i
- ✅ JWT authentication gerektirir
- ✅ Tüm yanıtlar
BaseResponseDto<T>formatında döner - ✅ Aktif stream yoksa
404 Not Founddöner - ✅ Birden fazla aktif stream durumunda log kaydedilir
- ✅ En son başlayan stream döndürülür
- ✅ Profil sayfalarında kullanım için optimize edilmiştir