-
Notifications
You must be signed in to change notification settings - Fork 122
/
small_funs.erl
73 lines (60 loc) · 1.89 KB
/
small_funs.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-module(small_funs).
-export([bad/2, good/2]).
bad(UserEmail, Message) ->
User =
case users:find_by_email(UserEmail) of
notfound ->
users:new_with_email(UserEmail);
FoundUser ->
FoundUser
end,
EscapedMessage = message_utils:escape(Message),
CleanMessage = bad_word_checker:clean(EscapedMessage),
db:store_message(User, CleanMessage),
DeviceIds = user:get_devices(User),
lists:foreach(
fun(DeviceId) ->
case devices:get_device(DeviceId) of
notfound -> ok;
Device ->
case device:get_push_info(Device) of
{apns, Token} ->
ApnsMsg = apns:build_message(CleanMessage),
apns:send_msg(Token, ApnsMsg);
{gcm, Token} ->
GcmMsg = gcm:new_message(CleanMessage),
gcm:send_message(Token, GcmMsg);
_ -> ok
end
end
end, DeviceIds).
good(UserEmail, Message) ->
User = find_or_create_user(UserEmail),
CleanMessage = clean_message(Message),
db:store_message(User, CleanMessage),
deliver_message(User, CleanMessage).
find_or_create_user(UserEmail) ->
case users:find_by_email(UserEmail) of
notfound ->
users:new_with_email(UserEmail);
FoundUser ->
FoundUser
end.
clean_message(Message) ->
EscapedMessage = message_utils:escape(Message),
bad_word_checker:clean(EscapedMessage).
deliver_message(User, Message) ->
DeviceIds = user:get_devices(User),
Devices =
[devices:get_device(DeviceId) || DeviceId <- DeviceIds],
lists:foreach(
fun(notfound) -> ok;
(Device) -> send_message(device:get_push_info(Device), Message)
end, Devices).
send_message({apns, Token}, Message) ->
ApnsMsg = apns:build_message(Message),
apns:send_msg(Token, ApnsMsg);
send_message({gcm, Token}, Message) ->
GcmMsg = gcm:new_message(Message),
gcm:send_message(Token, GcmMsg);
send_message(_, _) -> ok.