peppy

peppy

Need help with making an Enum/Map function to output a certain way

I’m a little newbish with the way enum and logic works with Elixir, so I need a little help here. I have a simple script that sends out a notification and Phoenix receives the following map example:

%{"action" => "newMessage", "userList" => [1,45,98], "muted" => [0,0,1], "optionalData" => ""}

What I need to do is broadcast a separate “newMessage” event to each of the three users in the userList array. I also sent a muted array, which is the user’s corresponding status for muting notifications. In the example above, only user 98 has the muted status selected.

  def notification(conn, %{"action" => _action, "userlist" => userlist, "muted" => muted, "optionalData" => _data} = payloadIn) do
    payloadOut = Map.take(payloadIn, ["action", "optionalData"])
    Enum.each(userlist, & AppWeb.Endpoint.broadcast("user:" <> &1, "newMessage", %{"payload" => payloadOut, "muted" => muted[Enum.find_index(userlist, fn x -> x == &1 end)]))
    json(conn, %{})
  end

My intended broadcasts would be something like:

Sent to user 1: %{"payload" => payloadOut, "muted" => 0}
Sent to user 45: %{"payload" => payloadOut, "muted" => 0}
Sent to user 98: %{"payload" => payloadOut, "muted" => 1}

payloadOut will be the same for each user, it will just be:

%{"action" => "newMessage", "optionalData" => data}

I don’t want to send the full userList and muted array back to users to protect the privacy of the users, from eachother, keep their muted status private, who the notifications are going out to, etc. This is why I need to send the muted status of each individual user and then the client-side javascript will handle it.

I’m getting tripped up on this part: muted[Enum.find_index(userlist, fn x -> x == &1 end)] , I get the following error, I think because array selection of muted[0] (the muted status for the first user) doesn’t work in Elixir the way it does in Javascript or PHP:

(ArgumentError) the Access calls for keywords expect the key to be an atom, got: 0

Since I’m sort of newish on Elixir programming, I was wondering if maybe there is a better way or some functions that I don’t know that might handle the logic of problem more efficiently.

Thanks

Marked As Solved

emadb

emadb

Probably the Enum.zip/2 can help:

You can do something like this:

def notification(conn, %{"action" => _action, "userlist" => userlist, "muted" => muted, "optionalData" => _data} = payloadIn) do

Enum.zip(userlist, muted)
|> Enum.map(fn {u, m} ->  
   AppWeb.Endpoint.broadcast("user:" <> u, "newMessage", %{"payload" => payloadOut, "muted" => m})
end)

Consider this a starting point, I don’t understand completely what you need.

Also Liked

peppy

peppy

Wow, that’s pretty amazing. The more I’m able to learn how to use these functions correctly and when to use them, the more I’m realizing how powerful and elegant Elixir really is. Great stuff, Thanks!

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement