A list of server-side functions for weekend/novice Discourse developers?

Hello friends,

This topic may belong in dev. Apologies if it is misplaced.

I am trying to develop a patch to core that will allow for certain added functionality. I have found the relevant function in the relevant file to make my changes, however I am unsure of how to access information that is in the function.

What I would like is to access the email address of the user who posted the topic that the function is being accessed on.

Here is the code snipet, found in line 148 of lib/email/message_builder.rb:


      if allow_reply_by_email?
        result['X-Discourse-Reply-Key'] = reply_key
        result['Reply-To'] = "test1@test.com"
        result['CC'] = reply_by_email_address
      else
        result['Reply-To'] = from_value
      end

I want to replace test1@test.com with the email address of the author of the topic.

I have found some documentation on API that seems very relevant, but no ā€œlist of functionsā€ as such. I donā€™t believe that what Iā€™m looking for is called a ā€œhookā€, and the list of hooks doesnā€™t appear relevant.

I understand that my question comes out of an incompetence in Rails programming, and I plan on studying Rails more over time. In PHP programming, which I am familiar with, I am able to access this information using some debugging tool like XDebug for PHP where I can ā€˜stopā€™ the code and see what variables are accessible. I havenā€™t figured out how to use such a tool for Rails yet.

Thank you for any suggestions on how to develop.

1 Like

What you want is byebug.

By adding a line with byebug you will stop execution and can see whatā€™s avaliable.

3 Likes

Hi Falco,

OK, byebug was very useful in telling me that I definitely need to know the function.

I did this:

  if allow_reply_by_email?
    result['X-Discourse-Reply-Key'] = reply_key
byebug
    result['Reply-To'] = "test1@test.com"
    result['CC'] = reply_by_email_address

and then typed in var all from sidekiq. This shows all of the variables, and @opts seemed the relevant one. However, it does not contain anything like the senderā€™s email address, though it does contain post id (14). I think the way to pull this is to:

  1. Ask what the user who posted post_id 14
  2. Ask what the email address of that user is
  3. Export it as a variable.

Which brings me back to the original point that I have to figure out what function can do thisā€¦

OK, time to swallow some pride here.

Is it possible that no list of functions exists because Rails is so straightforward and predictable? I tried this and it worked:

(byebug) p = Post.find_by_id 29
(byebug) u = User.find_by_id p.user_id
(byebug) u.email
4 Likes

Hahaha, yes. People love to bash ruby and rails but the api is very straightforward, and focused on developer happiness :heart:.

2 Likes

To complete the thread, here is the solution that I ultimately wrote:

Thanks to all who helped! Hopefully all the functions are this easy to findā€¦

4 Likes

Also, you can just ask for p.user.email instead of having the extra line in the middle.

4 Likes

I fixed this in the patch. Thank you.