Skip to content
This repository was archived by the owner on Jun 11, 2023. It is now read-only.

Commit 264ce31

Browse files
Merge pull request #406 from renatomassaro/notifications
Add Notification system
2 parents b74ea5a + e4e05f7 commit 264ce31

File tree

122 files changed

+3573
-599
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+3573
-599
lines changed

config/config.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ config :helix,
99
Helix.Entity.Repo,
1010
Helix.Log.Repo,
1111
Helix.Network.Repo,
12+
Helix.Notification.Repo,
1213
Helix.Universe.Repo,
1314
Helix.Process.Repo,
1415
Helix.Server.Repo,

config/notification/config.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use Mix.Config
2+
3+
config :helix, Helix.Notification.Repo,
4+
priv: "priv/repo/notification",
5+
pool_size: 3,
6+
adapter: Ecto.Adapters.Postgres,
7+
username: System.get_env("HELIX_DB_USER") || "postgres",
8+
password: System.get_env("HELIX_DB_PASS") || "postgres",
9+
hostname: System.get_env("HELIX_DB_HOST") || "localhost",
10+
types: HELL.PostgrexTypes

config/notification/dev.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use Mix.Config
2+
3+
prefix = System.get_env("HELIX_DB_PREFIX") || "helix"
4+
5+
config :helix, Helix.Notification.Repo,
6+
database: prefix <> "_dev_notification"

config/notification/prod.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use Mix.Config
2+
3+
prefix = "${HELIX_DB_PREFIX}"
4+
5+
config :helix, Helix.Notification.Repo,
6+
pool_size: "${HELIX_DB_POOL_SIZE}",
7+
username: "${HELIX_DB_USER}",
8+
password: "${HELIX_DB_PASS}",
9+
hostname: "${HELIX_DB_HOST}",
10+
database: prefix <> "_prod_notification"

config/notification/test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use Mix.Config
2+
3+
prefix = System.get_env("HELIX_DB_PREFIX") || "helix"
4+
5+
config :helix, Helix.Notification.Repo,
6+
pool: Ecto.Adapters.SQL.Sandbox,
7+
database: prefix <> "_test_notification",
8+
ownership_timeout: 90_000

events.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@
127127
"Steppable (custom)"
128128
]
129129
},
130-
"Notification": {
130+
"Publication": {
131131
"receives": ["All"],
132-
"emits": ["Notificable (custom)"]
132+
"emits": ["Publishable (custom)"]
133133
}
134134
},
135135
"flows": {
@@ -173,7 +173,7 @@
173173
"File.Install.Processed",
174174
"File.Transfer.Processed"
175175
],
176-
"notificable": [
176+
"publishable": [
177177
"Log.Created",
178178
"Log.Modified",
179179
"Log.Deleted",

graph.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
handlers = events["handlers"]
88
flows = events["flows"]
9-
notificable = events["notificable"]
9+
publishable = events["publishable"]
1010
missions = events["missions"]
1111
process_conclusion = events["process_conclusion"]
1212

13-
def is_notificable(name):
14-
return name in notificable
13+
def is_publishable(name):
14+
return name in publishable
1515

1616
def node_event(g, name):
17-
color = 'lightblue4' if is_notificable(name) else 'lightblue2'
17+
color = 'lightblue4' if is_publishable(name) else 'lightblue2'
1818
g.node(name, shape='box', color=color, style='filled')
1919

2020
def node_handler(g, name):

lib/account/model/account.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule Helix.Account.Model.Account do
88

99
alias Comeonin.Bcrypt
1010
alias Ecto.Changeset
11+
alias Helix.Entity.Model.Entity
1112

1213
@type email :: String.t
1314
@type username :: String.t
@@ -89,6 +90,14 @@ defmodule Helix.Account.Model.Account do
8990
def check_password(account = %__MODULE__{}, pass),
9091
do: Bcrypt.checkpw(pass, account.password)
9192

93+
@spec cast_from_entity(Entity.id) ::
94+
id
95+
@doc """
96+
"Translates" an Entity.id into Account.id
97+
"""
98+
def cast_from_entity(entity_id = %Entity.ID{}),
99+
do: __MODULE__.ID.cast!(to_string(entity_id))
100+
92101
@spec generic_validations(Changeset.t) ::
93102
Changeset.t
94103
defp generic_validations(changeset) do

lib/account/public/index.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule Helix.Account.Public.Index do
66
alias Helix.Entity.Query.Entity, as: EntityQuery
77
alias Helix.Network.Model.Bounce
88
alias Helix.Network.Query.Bounce, as: BounceQuery
9+
alias Helix.Notification.Public.Index.Account, as: AccountNotificationIndex
910
alias Helix.Server.Model.Server
1011
alias Helix.Universe.Bank.Model.BankAccount
1112
alias Helix.Universe.Bank.Query.Bank, as: BankQuery
@@ -18,7 +19,8 @@ defmodule Helix.Account.Public.Index do
1819
inventory: InventoryIndex.index,
1920
bounces: [Bounce.t],
2021
database: DatabaseIndex.index,
21-
bank_accounts: [BankAccount.t]
22+
bank_accounts: [BankAccount.t],
23+
notifications: AccountNotificationIndex.index
2224
}
2325

2426
@type rendered_index ::
@@ -27,7 +29,8 @@ defmodule Helix.Account.Public.Index do
2729
inventory: InventoryIndex.rendered_index,
2830
bounces: [ClientRenderer.rendered_bounce],
2931
database: DatabaseIndex.rendered_index,
30-
bank_accounts: [rendered_bank_account]
32+
bank_accounts: [rendered_bank_account],
33+
notifications: AccountNotificationIndex.rendered_index
3134
}
3235

3336
@typep rendered_bank_account ::
@@ -56,7 +59,8 @@ defmodule Helix.Account.Public.Index do
5659
inventory: InventoryIndex.index(entity),
5760
bounces: bounces,
5861
database: DatabaseIndex.index(entity),
59-
bank_accounts: BankQuery.get_accounts(account_id)
62+
bank_accounts: BankQuery.get_accounts(account_id),
63+
notifications: AccountNotificationIndex.index(account_id)
6064
}
6165
end
6266

@@ -68,7 +72,8 @@ defmodule Helix.Account.Public.Index do
6872
inventory: InventoryIndex.render_index(index.inventory),
6973
bounces: Enum.map(index.bounces, &ClientRenderer.render_bounce/1),
7074
database: DatabaseIndex.render_index(index.database),
71-
bank_accounts: Enum.map(index.bank_accounts, &render_bank_account/1)
75+
bank_accounts: Enum.map(index.bank_accounts, &render_bank_account/1),
76+
notifications: AccountNotificationIndex.render_index(index.notifications)
7277
}
7378
end
7479

lib/account/websocket/channel/account.ex

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import Helix.Websocket.Channel
22

33
channel Helix.Account.Websocket.Channel.Account do
44
@moduledoc """
5-
Channel to notify an user of an action that affects them.
5+
Two-way channel to receive requests and send out publications to an user about
6+
actions that affects them.
67
"""
78

89
alias Helix.Account.Websocket.Channel.Account.Join, as: AccountJoin
@@ -13,6 +14,7 @@ channel Helix.Account.Websocket.Channel.Account do
1314
alias Helix.Network.Websocket.Requests.Bounce.Create, as: BounceCreateRequest
1415
alias Helix.Network.Websocket.Requests.Bounce.Update, as: BounceUpdateRequest
1516
alias Helix.Network.Websocket.Requests.Bounce.Remove, as: BounceRemoveRequest
17+
alias Helix.Notification.Websocket.Requests.Read, as: NotificationReadRequest
1618
alias Helix.Software.Websocket.Requests.Virus.Collect, as: VirusCollectRequest
1719
alias Helix.Story.Websocket.Requests.Email.Reply, as: EmailReplyRequest
1820

@@ -59,7 +61,7 @@ channel Helix.Account.Websocket.Channel.Account do
5961
topic "client.setup", ClientSetupProxyRequest
6062

6163
@doc """
62-
Notifies the backend that `action` has been performed by the player.
64+
Notifies Helix that `action` has been performed by the player.
6365
6466
Params:
6567
*action: Action performed by the player. [0]
@@ -202,10 +204,42 @@ channel Helix.Account.Websocket.Channel.Account do
202204
203205
Input:
204206
+ base errors
205-
206207
"""
207208
topic "bounce.remove", BounceRemoveRequest
208209

210+
@doc """
211+
Marks one (or all) notifications as read.
212+
213+
Params:
214+
- notification_id: Which notification to mark as read. See [1].
215+
- class: Which class the notification belongs to. See [1].
216+
217+
[1] - This request can be used to mark a single notification as read, or all
218+
notifications within a class as read. If the `notification_id` param is
219+
given, we assume only that notification must be marked as read. On the other
220+
hand, if only `class` is given, we assume all notifications of that class
221+
shall be marked as read. Only one of them must be given. If both are given,
222+
we blow up and assume the client did not read this documentation.
223+
224+
Returns: :ok
225+
226+
Events:
227+
- notification_read_event: Emitted when notification(s) are successfully read.
228+
229+
Errors:
230+
231+
Henforcer:
232+
- notification_not_found: The given `notification_id` does not exist.
233+
- notification_not_belongs: Client attempted to read a notification that
234+
belongs to another user.
235+
236+
Input:
237+
- read_the_docs: Client did not read the docs. See [1].
238+
- bad_class: The given class is not valid.
239+
+ base errors
240+
"""
241+
topic "notification.read", NotificationReadRequest
242+
209243
@doc """
210244
Collects money off of active viruses.
211245

0 commit comments

Comments
 (0)