@@ -8,8 +8,13 @@ defmodule Algora.Chat do
88 alias Algora.Chat.Thread
99 alias Algora.Repo
1010
11- def broadcast ( message ) do
12- Phoenix.PubSub . broadcast ( Algora.PubSub , "chat:thread:#{ message . thread_id } " , message )
11+ defmodule MessageCreated do
12+ @ moduledoc false
13+ defstruct message: nil , participant: nil
14+ end
15+
16+ def broadcast ( % MessageCreated { } = event ) do
17+ Phoenix.PubSub . broadcast ( Algora.PubSub , "chat:thread:#{ event . message . thread_id } " , event )
1318 end
1419
1520 def subscribe ( thread_id ) do
@@ -23,7 +28,6 @@ defmodule Algora.Chat do
2328 |> Thread . changeset ( % { title: "#{ User . handle ( user_1 ) } <> #{ User . handle ( user_2 ) } " } )
2429 |> Repo . insert ( )
2530
26- # Add participants
2731 for user <- [ user_1 , user_2 ] do
2832 % Participant { }
2933 |> Participant . changeset ( % {
@@ -46,7 +50,7 @@ defmodule Algora.Chat do
4650 |> Repo . insert ( )
4751
4852 participants = Enum . uniq_by ( [ user | admins ] , & & 1 . id )
49- # Add participants
53+
5054 for u <- participants do
5155 % Participant { }
5256 |> Participant . changeset ( % {
@@ -61,20 +65,41 @@ defmodule Algora.Chat do
6165 end )
6266 end
6367
68+ defp ensure_participant ( thread_id , user_id ) do
69+ case Repo . fetch_by ( Participant , thread_id: thread_id , user_id: user_id ) do
70+ { :ok , participant } ->
71+ { :ok , participant }
72+
73+ { :error , _ } ->
74+ % Participant { }
75+ |> Participant . changeset ( % {
76+ thread_id: thread_id ,
77+ user_id: user_id ,
78+ last_read_at: DateTime . utc_now ( )
79+ } )
80+ |> Repo . insert ( )
81+ end
82+ end
83+
84+ defp insert_message ( thread_id , sender_id , content ) do
85+ % Message { }
86+ |> Message . changeset ( % {
87+ thread_id: thread_id ,
88+ sender_id: sender_id ,
89+ content: content
90+ } )
91+ |> Repo . insert ( )
92+ end
93+
6494 def send_message ( thread_id , sender_id , content ) do
65- case % Message { }
66- |> Message . changeset ( % {
67- thread_id: thread_id ,
68- sender_id: sender_id ,
69- content: content
70- } )
71- |> Repo . insert ( ) do
72- { :ok , message } ->
73- message |> Repo . preload ( :sender ) |> broadcast ( )
74- { :ok , message }
75-
76- { :error , changeset } ->
77- { :error , changeset }
95+ with { :ok , participant } <- ensure_participant ( thread_id , sender_id ) ,
96+ { :ok , message } <- insert_message ( thread_id , sender_id , content ) do
97+ broadcast ( % MessageCreated {
98+ message: Repo . preload ( message , :sender ) ,
99+ participant: Repo . preload ( participant , :user )
100+ } )
101+
102+ { :ok , message }
78103 end
79104 end
80105
@@ -107,6 +132,12 @@ defmodule Algora.Chat do
107132 |> Repo . all ( )
108133 end
109134
135+ def list_participants ( thread_id ) do
136+ Participant
137+ |> where ( thread_id: ^ thread_id )
138+ |> Repo . all ( )
139+ end
140+
110141 def mark_as_read ( thread_id , user_id ) do
111142 Participant
112143 |> where ( thread_id: ^ thread_id , user_id: ^ user_id )
@@ -145,4 +176,16 @@ defmodule Algora.Chat do
145176 thread -> { :ok , thread }
146177 end
147178 end
179+
180+ def get_or_create_bounty_thread ( bounty ) do
181+ case Repo . fetch_by ( Thread , bounty_id: bounty . id ) do
182+ { :ok , thread } ->
183+ { :ok , thread }
184+
185+ { :error , _ } ->
186+ % Thread { }
187+ |> Thread . changeset ( % { title: "Contributor chat" , bounty_id: bounty . id } )
188+ |> Repo . insert ( )
189+ end
190+ end
148191end
0 commit comments