@@ -5,6 +5,13 @@ class StreamsController < ApplicationController
5
5
6
6
def new
7
7
@stream = current_user . active_stream || Stream . new ( user : current_user )
8
+ if @stream . new_record?
9
+ if old_stream = current_user . streams . order ( created_at : :desc ) . first
10
+ @stream . title = old_stream . title
11
+ @stream . body = old_stream . body
12
+ @stream . tags = old_stream . tags
13
+ end
14
+ end
8
15
if current_user . stream_key . blank?
9
16
current_user . generate_stream_key
10
17
current_user . save!
@@ -81,12 +88,16 @@ def stream_params
81
88
def save_and_redirect
82
89
@stream . published_at ||= Time . now if params [ :publish_stream ]
83
90
@stream . archived_at ||= Time . now if params [ :end_stream ]
91
+ current_user . streams . where ( archived_at : nil ) . update_all ( archived_at : Time . now )
84
92
if @stream . save
85
93
case
86
94
when @stream . archived?
95
+ end_youtube_stream
87
96
flash [ :notice ] = "Your stream has been archived"
88
97
redirect_to live_streams_path
89
98
when @stream . published?
99
+ Rails . logger . info ( "pushing to youtube" )
100
+ stream_to_youtube if @stream . save_recording
90
101
redirect_to profile_stream_path ( current_user . username )
91
102
else
92
103
redirect_to new_stream_path
@@ -95,4 +106,27 @@ def save_and_redirect
95
106
render 'new'
96
107
end
97
108
end
109
+
110
+ def stream_to_youtube
111
+ url = "#{ ENV [ 'QUICKSTREAM_URL' ] } /streams/#{ @stream . user . username } /youtube"
112
+ Excon . put ( url ,
113
+ headers : {
114
+ "Accept" => "application/json" ,
115
+ "Content-Type" => "application/json" } ,
116
+ body : { title : @stream . title , description : @stream . body } . to_json ,
117
+ idempotent : true ,
118
+ tcp_nodelay : true ,
119
+ )
120
+ end
121
+
122
+ def end_youtube_stream
123
+ url = "#{ ENV [ 'QUICKSTREAM_URL' ] } /streams/#{ @stream . user . username } /youtube"
124
+ Excon . delete ( url ,
125
+ headers : {
126
+ "Accept" => "application/json" ,
127
+ "Content-Type" => "application/json" } ,
128
+ idempotent : true ,
129
+ tcp_nodelay : true ,
130
+ )
131
+ end
98
132
end
0 commit comments