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


(Tarek Loubani) #1

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.


(Rafael dos Santos Silva) #2

What you want is byebug.

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


(Tarek Loubani) #3

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…


(Tarek Loubani) #4

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

(Rafael dos Santos Silva) #5

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


(Tarek Loubani) #6

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…


(Kane York) #7

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


(Tarek Loubani) #8

I fixed this in the patch. Thank you.