Skip to content

Commit 5dae01d

Browse files
authored
Move unpacking nadia update struct to the separate function, add case… (#34)
* Move unpacking nadia update struct to the separate function, add case with edited_message instead of message * Ignore edited messages * Fix dumb matching mistake
1 parent f6eb370 commit 5dae01d

File tree

7 files changed

+87
-24
lines changed

7 files changed

+87
-24
lines changed

lib/mauricio/acceptor.ex

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ defmodule Mauricio.Acceptor do
3535
|> List.wrap()
3636
|> Nadia.Parser.parse_result("getUpdates")
3737
|> hd()
38-
|> call_process_update()
38+
|> unpack_update_struct()
39+
|> CatChat.process_update(:sync)
3940

4041
{200, [], ""}
4142
end
@@ -48,20 +49,31 @@ defmodule Mauricio.Acceptor do
4849
{404, [], "Our princess is in another castle..."}
4950
end
5051

51-
def call_process_update(%NadiaUpdate{
52-
message: %NadiaMessage{
53-
chat: %NadiaChat{id: chat_id},
54-
date: date,
55-
from: from,
56-
message_id: message_id,
57-
text: text
58-
},
59-
update_id: update_id
60-
}, mode \\ :sync) do
61-
CatChat.process_update(%{
62-
message: %{chat: %{id: chat_id}, date: date, from: from, message_id: message_id, text: text},
52+
@spec unpack_update_struct(NadiaUpdate.t()) :: Acceptor.unpacked_nadia_message()
53+
def unpack_update_struct(%NadiaUpdate{message: message, update_id: update_id}) when not is_nil(message) do
54+
%NadiaMessage{
55+
chat: %NadiaChat{id: chat_id},
56+
date: date,
57+
from: from,
58+
message_id: message_id,
59+
text: text
60+
} = message
61+
62+
%{
63+
message: %{
64+
chat: %{id: chat_id},
65+
date: date,
66+
from: from,
67+
message_id: message_id,
68+
text: text
69+
},
6370
update_id: update_id
64-
}, mode)
71+
}
72+
end
73+
74+
def unpack_update_struct(update) do
75+
Logger.warn(inspect(update))
76+
%{message: nil}
6577
end
6678

6779
@impl :elli_handler

lib/mauricio/catchat.ex

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ defmodule Mauricio.CatChat do
100100
{:noreply, catsup_pid}
101101
end
102102

103-
def handle_update(%{message: %{chat: %{id: chat_id}, text: text} = message}, mode)
104-
when not is_nil(message) do
103+
def handle_update(%{message: message}, mode) when not is_nil(message) do
104+
%{chat: %{id: chat_id}, text: text} = message
105105
Logger.log(:info, "Message from #{chat_id} with text #{text}")
106106

107107
chat_pid = CatSup.get_chat(chat_id)
@@ -142,10 +142,7 @@ defmodule Mauricio.CatChat do
142142
end
143143
end
144144

145-
def handle_update(update, _mode) do
146-
Logger.warn(inspect(update))
147-
:ok
148-
end
145+
def handle_update(_, _), do: :ok
149146

150147
def handle_cast({:process_update, update}, catsup_pid) do
151148
handle_update(update, :async)
@@ -161,7 +158,7 @@ defmodule Mauricio.CatChat do
161158
def process_update(update, :async),
162159
do: GenServer.cast(__MODULE__, {:process_update, update})
163160

164-
def process_update(%{message: message} = update, :sync) do
161+
def process_update(%{message: message} = update, :sync) do
165162
case GenServer.call(__MODULE__, {:process_update, update}) do
166163
:ok ->
167164
:ok

lib/mauricio/catchat/chat/interaction.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ defmodule Mauricio.CatChat.Chat.Interaction do
22
require Logger
33

44
alias Nadia.Model.User, as: NadiaUser
5-
65
alias Mauricio.CatChat.{Member, Cat}
76
alias Mauricio.Text
87

lib/mauricio/poller.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule Mauricio.Poller do
44

55
alias Nadia.Model.Update, as: NadiaUpdate
66
alias Mauricio.Acceptor
7+
alias Mauricio.CatChat
78

89
def start_link(_arg) do
910
Logger.log(:info, "Started poller")
@@ -41,7 +42,7 @@ defmodule Mauricio.Poller do
4142

4243
def process_message(%NadiaUpdate{update_id: update_id} = update) do
4344
Logger.log(:info, inspect(update))
44-
Acceptor.call_process_update(update, :async)
45+
Acceptor.unpack_update_struct(update) |> CatChat.process_update(:async)
4546
update_id
4647
end
4748
end

test/acceptor_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule MauricioTest.Acceptor do
33

44
alias Mauricio.Acceptor
55
alias Mauricio.CatChat.Chats
6+
alias MauricioTest.Helpers
67

78
setup_all do
89
children = [{Mauricio.Acceptor, [port: 4001]}]
@@ -101,4 +102,27 @@ defmodule MauricioTest.Acceptor do
101102
workers: 0
102103
}
103104
end
105+
106+
describe "unpack_update_struct" do
107+
test "correctly unpacks new message struct" do
108+
new_message = Helpers.update_with_text(1, "new message")
109+
110+
assert %{
111+
message: %{
112+
chat: %{id: _},
113+
date: _,
114+
from: %{first_name: _, id: _, last_name: _, username: _},
115+
message_id: _,
116+
text: _
117+
},
118+
update_id: _
119+
} = Acceptor.unpack_update_struct(new_message)
120+
end
121+
122+
test "ignores edited message" do
123+
edited_message = Helpers.edited_message(1, "edit")
124+
125+
assert %{message: nil} = Acceptor.unpack_update_struct(edited_message)
126+
end
127+
end
104128
end

test/cat_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ defmodule MauricioTest.CatTest do
33
use PropCheck
44

55
alias Nadia.Model.User, as: NadiaUser
6-
76
alias Mauricio.CatChat.{Cat, Member}
87
alias Mauricio.CatChat.Cat.State.{Awake, Away, WantCare}
98
alias Mauricio.Storage.{Serializable, Decoder}

test/test_helper.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ defmodule MauricioTest.Helpers do
7171
}
7272
end
7373

74+
def edited_message(chat_id, user_id \\ nil, text) do
75+
user_id = user_id || chat_id
76+
77+
%NadiaUpdate{
78+
callback_query: nil,
79+
channel_post: nil,
80+
chosen_inline_result: nil,
81+
edited_message: %{
82+
chat: %{
83+
id: chat_id,
84+
title: "Chat title",
85+
type: "private"
86+
},
87+
date: 1_578_194_118,
88+
edit_date: 1_578_194_119,
89+
from: %{
90+
first_name: "name",
91+
id: user_id,
92+
is_bot: false,
93+
last_name: "name",
94+
username: "username"
95+
},
96+
message_id: 123,
97+
text: text
98+
},
99+
inline_query: nil,
100+
message: nil,
101+
update_id: 123_123_123
102+
}
103+
end
104+
74105
def chat(chat_id) do
75106
%NadiaChat{
76107
first_name: "Yaropolk",

0 commit comments

Comments
 (0)