@@ -8,6 +8,7 @@ defmodule AlgoraWeb.BountyLive do
88 alias Algora.Bounties
99 alias Algora.Bounties.Bounty
1010 alias Algora.Bounties.LineItem
11+ alias Algora.Chat
1112 alias Algora.Repo
1213 alias Algora.Util
1314 alias Algora.Workspace
@@ -95,6 +96,14 @@ defmodule AlgoraWeb.BountyLive do
9596
9697 exclusive_changeset = ExclusiveBountyForm . changeset ( % ExclusiveBountyForm { } , % { } )
9798
99+ { :ok , thread } = Chat . get_or_create_bounty_thread ( bounty )
100+ messages = thread . id |> Chat . list_messages ( ) |> Repo . preload ( :sender )
101+ participants = thread . id |> Chat . list_participants ( ) |> Repo . preload ( :user )
102+
103+ if connected? ( socket ) do
104+ Chat . subscribe ( thread . id )
105+ end
106+
98107 { :ok ,
99108 socket
100109 |> assign ( :page_title , bounty . ticket . title )
@@ -107,7 +116,9 @@ defmodule AlgoraWeb.BountyLive do
107116 |> assign ( :show_exclusive_modal , false )
108117 |> assign ( :selected_context , nil )
109118 |> assign ( :line_items , [ ] )
110- |> assign ( :messages , [ ] )
119+ |> assign ( :thread , thread )
120+ |> assign ( :messages , messages )
121+ |> assign ( :participants , participants )
111122 |> assign ( :reward_form , to_form ( reward_changeset ) )
112123 |> assign ( :exclusive_form , to_form ( exclusive_changeset ) )
113124 |> assign_exclusives ( bounty . shared_with ) }
@@ -118,34 +129,76 @@ defmodule AlgoraWeb.BountyLive do
118129 { :noreply , socket }
119130 end
120131
132+ @ impl true
121133 def handle_params ( % { "context" => context_id } , _url , socket ) do
122134 { :noreply , socket |> assign_selected_context ( context_id ) |> assign_line_items ( ) }
123135 end
124136
137+ @ impl true
125138 def handle_params ( _params , _url , socket ) do
126139 { :noreply , socket }
127140 end
128141
142+ @ impl true
143+ def handle_info ( % Chat.MessageCreated { message: message , participant: participant } , socket ) do
144+ socket =
145+ if message . id in Enum . map ( socket . assigns . messages , & & 1 . id ) do
146+ socket
147+ else
148+ Phoenix.Component . update ( socket , :messages , & ( & 1 ++ [ message ] ) )
149+ end
150+
151+ socket =
152+ if participant . id in Enum . map ( socket . assigns . participants , & & 1 . id ) do
153+ socket
154+ else
155+ Phoenix.Component . update ( socket , :participants , & ( & 1 ++ [ participant ] ) )
156+ end
157+
158+ { :noreply , socket }
159+ end
160+
161+ @ impl true
162+ def handle_event ( "send_message" , % { "message" => content } , socket ) do
163+ { :ok , message } =
164+ Chat . send_message (
165+ socket . assigns . thread . id ,
166+ socket . assigns . current_user . id ,
167+ content
168+ )
169+
170+ message = Repo . preload ( message , :sender )
171+
172+ { :noreply ,
173+ socket
174+ |> update ( :messages , & ( & 1 ++ [ message ] ) )
175+ |> push_event ( "clear-input" , % { selector: "#message-input" } ) }
176+ end
177+
129178 @ impl true
130179 def handle_event ( "reward" , _params , socket ) do
131180 { :noreply , assign ( socket , :show_reward_modal , true ) }
132181 end
133182
183+ @ impl true
134184 def handle_event ( "exclusive" , _params , socket ) do
135185 { :noreply , assign ( socket , :show_exclusive_modal , true ) }
136186 end
137187
188+ @ impl true
138189 def handle_event ( "close_drawer" , _params , socket ) do
139190 { :noreply , close_drawers ( socket ) }
140191 end
141192
193+ @ impl true
142194 def handle_event ( "validate_reward" , % { "reward_bounty_form" => params } , socket ) do
143195 { :noreply ,
144196 socket
145197 |> assign ( :reward_form , to_form ( RewardBountyForm . changeset ( % RewardBountyForm { } , params ) ) )
146198 |> assign_line_items ( ) }
147199 end
148200
201+ @ impl true
149202 def handle_event ( "pay_with_stripe" , % { "reward_bounty_form" => params } , socket ) do
150203 changeset = RewardBountyForm . changeset ( % RewardBountyForm { } , params )
151204
@@ -165,13 +218,15 @@ defmodule AlgoraWeb.BountyLive do
165218 end
166219 end
167220
221+ @ impl true
168222 def handle_event ( "validate_exclusive" , % { "exclusive_bounty_form" => params } , socket ) do
169223 { :noreply ,
170224 socket
171225 |> assign ( :exclusive_form , to_form ( ExclusiveBountyForm . changeset ( % ExclusiveBountyForm { } , params ) ) )
172226 |> assign_line_items ( ) }
173227 end
174228
229+ @ impl true
175230 def handle_event ( "share_exclusive" , % { "exclusive_bounty_form" => params } , socket ) do
176231 changeset = ExclusiveBountyForm . changeset ( % ExclusiveBountyForm { } , params )
177232 bounty = socket . assigns . bounty
@@ -420,11 +475,11 @@ defmodule AlgoraWeb.BountyLive do
420475 Contributor chat
421476 </ h2 >
422477 < div class = "relative flex -space-x-2 " >
423- <%= for user <- @ exclusives do %>
478+ <%= for participant <- @ participants do %>
424479 < . avatar >
425- < . avatar_image src = { user . avatar_url } alt = "Developer avatar " />
480+ < . avatar_image src = { participant . user . avatar_url } alt = "Developer avatar " />
426481 < . avatar_fallback >
427- { Algora.Util . initials ( @ bounty . owner . name ) }
482+ { Algora.Util . initials ( participant . user . name ) }
428483 </ . avatar_fallback >
429484 </ . avatar >
430485 <% end %>
0 commit comments