Skip to content

Commit d64bab9

Browse files
authored
Merge pull request #1295 from code-corps/1286-conversation-models
1286 conversation models
2 parents 8fe691f + 30c9b07 commit d64bab9

File tree

6 files changed

+273
-1
lines changed

6 files changed

+273
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule CodeCorps.Conversation do
2+
@moduledoc ~S"""
3+
A header of a `CodeCorps.Message` thread, depicting a start of a conversation
4+
with a specific `CodeCorps.User`
5+
6+
When a project sends a `CodeCorps.Message` to one or more users, a
7+
`CodeCorps.Conversation` needs to be created for each of those users, so
8+
separate conversations can be held with different users starting from the same
9+
original `CodeCorps.Message`
10+
11+
Once replies start coming in, a `CodeCorps.ConversationPart` is created for
12+
each of those replies.
13+
"""
14+
use CodeCorps.Model
15+
16+
@type t :: %__MODULE__{}
17+
18+
schema "conversations" do
19+
field :status, :string, null: false, default: "open"
20+
field :read_at, :utc_datetime, null: true
21+
22+
belongs_to :message, CodeCorps.Message
23+
belongs_to :user, CodeCorps.User
24+
25+
has_many :conversation_parts, CodeCorps.ConversationPart
26+
27+
timestamps()
28+
end
29+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule CodeCorps.ConversationPart do
2+
@moduledoc ~S"""
3+
An individual "line of conversation" in a `CodeCorps.Conversation` thread,
4+
depicting a reply to the `CodeCorps.Conversation` by any of the two sides.
5+
6+
When a project sends a `CodeCorps.Message` to one or more users, a
7+
`CodeCorps.Conversation` needs to be created for each of those users, so
8+
separate conversations can be held with different users starting from the same
9+
original `CodeCorps.Message`
10+
11+
Once replies start coming in, a `CodeCorps.ConversationPart` is created for
12+
each of those replies, regardless of which side is making them.
13+
"""
14+
15+
use CodeCorps.Model
16+
17+
@type t :: %__MODULE__{}
18+
19+
schema "conversation_parts" do
20+
field :body, :string, null: false
21+
field :read_at, :utc_datetime, null: true
22+
23+
belongs_to :author, CodeCorps.User
24+
belongs_to :conversation, CodeCorps.Conversation
25+
26+
timestamps()
27+
end
28+
end

lib/code_corps/model/message.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ defmodule CodeCorps.Message do
2525
belongs_to :author, CodeCorps.User
2626
belongs_to :project, CodeCorps.Project
2727

28+
has_many :conversations, CodeCorps.Conversation
29+
2830
timestamps()
2931
end
3032

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule CodeCorps.Repo.Migrations.AddConversationModels do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:conversations) do
6+
add :status, :string, null: false, default: "open"
7+
add :read_at, :utc_datetime, null: true
8+
add :message_id, references(:messages)
9+
add :user_id, references(:users)
10+
11+
timestamps()
12+
end
13+
14+
create index(:conversations, [:status])
15+
create index(:conversations, [:message_id])
16+
create index(:conversations, [:user_id])
17+
18+
create table(:conversation_parts) do
19+
add :body, :text, null: false
20+
add :read_at, :utc_datetime, null: true
21+
add :author_id, references(:users)
22+
add :conversation_id, references(:conversations)
23+
24+
timestamps()
25+
end
26+
27+
create index(:conversation_parts, [:author_id])
28+
create index(:conversation_parts, [:conversation_id])
29+
end
30+
end

priv/repo/structure.sql

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,74 @@ CREATE SEQUENCE comments_id_seq
155155
ALTER SEQUENCE comments_id_seq OWNED BY comments.id;
156156

157157

158+
--
159+
-- Name: conversation_parts; Type: TABLE; Schema: public; Owner: -
160+
--
161+
162+
CREATE TABLE conversation_parts (
163+
id bigint NOT NULL,
164+
body text NOT NULL,
165+
read_at timestamp without time zone,
166+
author_id bigint,
167+
conversation_id bigint,
168+
inserted_at timestamp without time zone NOT NULL,
169+
updated_at timestamp without time zone NOT NULL
170+
);
171+
172+
173+
--
174+
-- Name: conversation_parts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
175+
--
176+
177+
CREATE SEQUENCE conversation_parts_id_seq
178+
START WITH 1
179+
INCREMENT BY 1
180+
NO MINVALUE
181+
NO MAXVALUE
182+
CACHE 1;
183+
184+
185+
--
186+
-- Name: conversation_parts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
187+
--
188+
189+
ALTER SEQUENCE conversation_parts_id_seq OWNED BY conversation_parts.id;
190+
191+
192+
--
193+
-- Name: conversations; Type: TABLE; Schema: public; Owner: -
194+
--
195+
196+
CREATE TABLE conversations (
197+
id bigint NOT NULL,
198+
status character varying(255) DEFAULT 'open'::character varying NOT NULL,
199+
read_at timestamp without time zone,
200+
message_id bigint,
201+
user_id bigint,
202+
inserted_at timestamp without time zone NOT NULL,
203+
updated_at timestamp without time zone NOT NULL
204+
);
205+
206+
207+
--
208+
-- Name: conversations_id_seq; Type: SEQUENCE; Schema: public; Owner: -
209+
--
210+
211+
CREATE SEQUENCE conversations_id_seq
212+
START WITH 1
213+
INCREMENT BY 1
214+
NO MINVALUE
215+
NO MAXVALUE
216+
CACHE 1;
217+
218+
219+
--
220+
-- Name: conversations_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
221+
--
222+
223+
ALTER SEQUENCE conversations_id_seq OWNED BY conversations.id;
224+
225+
158226
--
159227
-- Name: donation_goals; Type: TABLE; Schema: public; Owner: -
160228
--
@@ -1826,6 +1894,20 @@ ALTER TABLE ONLY categories ALTER COLUMN id SET DEFAULT nextval('categories_id_s
18261894
ALTER TABLE ONLY comments ALTER COLUMN id SET DEFAULT nextval('comments_id_seq'::regclass);
18271895

18281896

1897+
--
1898+
-- Name: conversation_parts id; Type: DEFAULT; Schema: public; Owner: -
1899+
--
1900+
1901+
ALTER TABLE ONLY conversation_parts ALTER COLUMN id SET DEFAULT nextval('conversation_parts_id_seq'::regclass);
1902+
1903+
1904+
--
1905+
-- Name: conversations id; Type: DEFAULT; Schema: public; Owner: -
1906+
--
1907+
1908+
ALTER TABLE ONLY conversations ALTER COLUMN id SET DEFAULT nextval('conversations_id_seq'::regclass);
1909+
1910+
18291911
--
18301912
-- Name: donation_goals id; Type: DEFAULT; Schema: public; Owner: -
18311913
--
@@ -2144,6 +2226,22 @@ ALTER TABLE ONLY comments
21442226
ADD CONSTRAINT comments_pkey PRIMARY KEY (id);
21452227

21462228

2229+
--
2230+
-- Name: conversation_parts conversation_parts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
2231+
--
2232+
2233+
ALTER TABLE ONLY conversation_parts
2234+
ADD CONSTRAINT conversation_parts_pkey PRIMARY KEY (id);
2235+
2236+
2237+
--
2238+
-- Name: conversations conversations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
2239+
--
2240+
2241+
ALTER TABLE ONLY conversations
2242+
ADD CONSTRAINT conversations_pkey PRIMARY KEY (id);
2243+
2244+
21472245
--
21482246
-- Name: donation_goals donation_goals_pkey; Type: CONSTRAINT; Schema: public; Owner: -
21492247
--
@@ -2475,6 +2573,41 @@ CREATE INDEX comments_task_id_index ON comments USING btree (task_id);
24752573
CREATE INDEX comments_user_id_index ON comments USING btree (user_id);
24762574

24772575

2576+
--
2577+
-- Name: conversation_parts_author_id_index; Type: INDEX; Schema: public; Owner: -
2578+
--
2579+
2580+
CREATE INDEX conversation_parts_author_id_index ON conversation_parts USING btree (author_id);
2581+
2582+
2583+
--
2584+
-- Name: conversation_parts_conversation_id_index; Type: INDEX; Schema: public; Owner: -
2585+
--
2586+
2587+
CREATE INDEX conversation_parts_conversation_id_index ON conversation_parts USING btree (conversation_id);
2588+
2589+
2590+
--
2591+
-- Name: conversations_message_id_index; Type: INDEX; Schema: public; Owner: -
2592+
--
2593+
2594+
CREATE INDEX conversations_message_id_index ON conversations USING btree (message_id);
2595+
2596+
2597+
--
2598+
-- Name: conversations_status_index; Type: INDEX; Schema: public; Owner: -
2599+
--
2600+
2601+
CREATE INDEX conversations_status_index ON conversations USING btree (status);
2602+
2603+
2604+
--
2605+
-- Name: conversations_user_id_index; Type: INDEX; Schema: public; Owner: -
2606+
--
2607+
2608+
CREATE INDEX conversations_user_id_index ON conversations USING btree (user_id);
2609+
2610+
24782611
--
24792612
-- Name: donation_goals_current_unique_to_project; Type: INDEX; Schema: public; Owner: -
24802613
--
@@ -3445,6 +3578,38 @@ ALTER TABLE ONLY comments
34453578
ADD CONSTRAINT comments_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id);
34463579

34473580

3581+
--
3582+
-- Name: conversation_parts conversation_parts_author_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
3583+
--
3584+
3585+
ALTER TABLE ONLY conversation_parts
3586+
ADD CONSTRAINT conversation_parts_author_id_fkey FOREIGN KEY (author_id) REFERENCES users(id);
3587+
3588+
3589+
--
3590+
-- Name: conversation_parts conversation_parts_conversation_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
3591+
--
3592+
3593+
ALTER TABLE ONLY conversation_parts
3594+
ADD CONSTRAINT conversation_parts_conversation_id_fkey FOREIGN KEY (conversation_id) REFERENCES conversations(id);
3595+
3596+
3597+
--
3598+
-- Name: conversations conversations_message_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
3599+
--
3600+
3601+
ALTER TABLE ONLY conversations
3602+
ADD CONSTRAINT conversations_message_id_fkey FOREIGN KEY (message_id) REFERENCES messages(id);
3603+
3604+
3605+
--
3606+
-- Name: conversations conversations_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
3607+
--
3608+
3609+
ALTER TABLE ONLY conversations
3610+
ADD CONSTRAINT conversations_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id);
3611+
3612+
34483613
--
34493614
-- Name: donation_goals donation_goals_project_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
34503615
--
@@ -3993,5 +4158,5 @@ ALTER TABLE ONLY users
39934158
-- PostgreSQL database dump complete
39944159
--
39954160

3996-
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646), (20170927100300), (20170928234412), (20171003134956), (20171003225853), (20171006063358), (20171006161407), (20171012215106), (20171012221231), (20171016125229), (20171016125516), (20171016223356), (20171016235656), (20171017235433), (20171019191035), (20171025184225), (20171026010933), (20171027061833), (20171028011642), (20171028173508), (20171030182857), (20171031232023), (20171031234356), (20171101023309), (20171104013543), (20171106045740), (20171106050209), (20171106103153), (20171106200036), (20171109231538), (20171110001134), (20171114010851), (20171114033357), (20171114225214), (20171114225713), (20171114232534), (20171115201624), (20171115225358), (20171119004204), (20171121075226), (20171121144138), (20171123065902), (20171127215847), (20171201073818), (20171205161052);
4161+
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646), (20170927100300), (20170928234412), (20171003134956), (20171003225853), (20171006063358), (20171006161407), (20171012215106), (20171012221231), (20171016125229), (20171016125516), (20171016223356), (20171016235656), (20171017235433), (20171019191035), (20171025184225), (20171026010933), (20171027061833), (20171028011642), (20171028173508), (20171030182857), (20171031232023), (20171031234356), (20171101023309), (20171104013543), (20171106045740), (20171106050209), (20171106103153), (20171106200036), (20171109231538), (20171110001134), (20171114010851), (20171114033357), (20171114225214), (20171114225713), (20171114232534), (20171115201624), (20171115225358), (20171119004204), (20171121075226), (20171121144138), (20171123065902), (20171127215847), (20171201073818), (20171205161052), (20171213062707);
39974162

test/support/factories.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ defmodule CodeCorps.Factories do
2323
}
2424
end
2525

26+
def conversation_factory do
27+
%CodeCorps.Conversation{
28+
status: "open",
29+
read_at: nil,
30+
message: build(:message),
31+
user: build(:user)
32+
}
33+
end
34+
35+
def conversation_part_factory do
36+
%CodeCorps.ConversationPart{
37+
body: sequence(:body, &"Reply to conversation #{&1}"),
38+
read_at: nil,
39+
author: build(:user),
40+
conversation: build(:conversation)
41+
}
42+
end
43+
2644
def donation_goal_factory do
2745
%CodeCorps.DonationGoal{
2846
amount: 100,

0 commit comments

Comments
 (0)