Hook, how to add new Property to message

Hi everybody,
I’m using EMQX 5.0.14.
I’m trying to use hook to add a new UserProperty to a message. I’m not very practice with erlang, but the message published doesn’t change.

Here is a little portion of the code:

on_message_publish(Message, _Env) ->
    Username = maps:get(username, Message#message.headers, <<"">>),
    Props = maps:get(properties, Message#message.headers, #{}),
    UserProps = maps:get('User-Property', Props,  []),
    NewUserProps = lists:append(UserProps, [#{<<"publisher_username">> => Username}]),
    NewProps = maps:put('User-Property', NewUserProps, Props),
    Headers = maps:update(properties, NewProps, Message#message.headers),
    NewMessage = emqx_message:set_headers(Headers, Message),
    io:format("New Properties ~p~n  ", [NewProps]),
    io:format("New Headers ~p~n  ", [Headers]),
    io:format("New Publish ~p~n  ~nEnv:~p~n", [emqx_message:to_map(NewMessage), _Env]),
    {ok, NewMessage}.

What’s wrong with my code?

Thanks in advance
Andrea

Hi.
Is the subscriber an MQTT v5 client ?
If yes, could you enable debug level logging and share the logs with us ?

Hello,

The following line looks suspicious:

NewUserProps = lists:append(UserProps, [#{<<"publisher_username">> => Username}]),

It adds an Erlang map to the list of properties, but I think this 'User-Property' should be a proplist, i.e. a list of tuples. Can you try changing it to

NewUserProps = [{<<"publisher_username">>, Username} | UserProps],

?

It works !!!

Thanks a lot for your help

Bye