module Youtube # Aggiorna privacy o elimina un VOD YouTube collegato a un replay. class VideoLifecycleService class Error < StandardError; end def initialize(recording) @recording = recording @team = recording.team @session = recording.stream_session end def update_privacy!(privacy_status:) video_id = @recording.youtube_video_id return if video_id.blank? || video_id.to_s.start_with?("mock_") privacy = map_privacy(privacy_status) client = authorized_client video = Google::Apis::YoutubeV3::Video.new( id: video_id, status: Google::Apis::YoutubeV3::VideoStatus.new( privacy_status: privacy, self_declared_made_for_kids: false ) ) client.update_video("status", video) merge_youtube_meta!("privacy_status" => privacy) true rescue Google::Apis::Error => e raise Error, e.message end def delete! video_id = @recording.youtube_video_id return if video_id.blank? || video_id.to_s.start_with?("mock_") client = authorized_client client.delete_video(video_id) true rescue Google::Apis::Error => e raise Error, e.message end private def map_privacy(status) status.to_s == "public" ? "public" : "unlisted" end def merge_youtube_meta!(attrs) meta = @recording.metadata.is_a?(Hash) ? @recording.metadata.dup : {} publish = meta.fetch("youtube_publish", {}).merge(attrs) @recording.update!(metadata: meta.merge("youtube_publish" => publish)) end def youtube_channel @session&.platform == "youtube" ? "team" : nil end def authorized_client credential = CredentialResolver.new(@team, channel: youtube_channel).resolve raise Error, "Credenziali YouTube non disponibili" if credential.blank? OauthRefresh.new(credential).apply!(Google::Apis::YoutubeV3::YouTubeService.new) end end end