Fill in post for user?

(Peter N Lewis) #1

I looked at the API, but I don’t think it will do what I want (for one thing it requires the user to create an API key which isn’t going to be practical).

What I want to do is have a way for my Mac application (Keyboard Maestro) to allow users to post content (from the application) to the forum. So the user selects a chunk of content, and clicks a button, and the forum opens up with the New Topic fields filled in (category, subject, content, including an uploaded file). The user can then edit the post as they desire before posting it.

Ideally, if the user is not logged in, they would be prompted to login/create an account first, but I could accept that they simply have to be logged in first.

Is there any way to accomplish something like this?

Using the Discourse API to post with uploaded files?
(Jeff Atwood) #2

Aren’t you referring to the URL that pre-fills a topic, title, etc? @techapj worked on this, and it now exists.

(Peter N Lewis) #3

Yep, that is close to what I am after. It would be good if it survived the login process if the user is not yet logged in, but it doesn’t currently

I don’t see how it can be extended to support a file (or image) upload though, so that may be a problem.

Can images/files be uploaded separately via the API? I (obviously) can’t store the master key in the application, but I could probably do something where the application uploaded the file to a web service I define, which in turn uploads it to the forum, and then have the topic body reference that uploaded file. A bit ugly, but perhaps doable. Does the API allow uploading files?

(Jeff Atwood) #4

Any way we can get this to survive login, @techapj?

(Arpit Jalan) #5

Fixed via:

(Peter N Lewis) #6

This patch is not working for me.

I applied the patch, and then ran rake assets:precompile but it is not working - is there something else I need to do? I believe it is failing because the shouldRedirectToUrl cookie is set, but the destination_url cookie is never set because you don’t call redirect_to_login_if_required (which sets the destination_url cookie), instead you do self.replaceWith(‘login’);.

Note also in looking at redirect_to_login_if_required, it refuses to set the destination_url if the original URL matches “uploads”.

  cookies[:destination_url] = request.original_url unless request.original_url =~ /uploads/

Does the “original_url” ivar include the query parameters? If so, this would fail if the new_topic URL included a message containing “uploads” (which in my case is highly likely since it will include uploaded files and user text).

(Peter N Lewis) #7

BTW, I am on version 1.3.2, with just this patch (and the API synchronous patch) added, so if there is an interaction with other changes since 1.3.2, that might be why its not working).

(Arpit Jalan) #8

I just updated the code, use this patch instead:

(Peter N Lewis) #9

It works more, but it seems to lose all the parameters.

It opens the new topic, but it does not fill in any of the fields. Its like it has lost all the query string part of the URL, though I don’t know why.

(Peter N Lewis) #10

If I’m following Discourse’s code correctly, the relevant routine is “enter” in static_controller.rb which does this:

    if uri.path.present? &&
       ( || == &&
       uri.path !~ /\./

      destination = uri.path

Presumably uri.path gets just the path of the uri, and drops the query string, which would exactly match what I see - I get redirected to newtopic, but with nothing filled in.

(Arpit Jalan) #11

That code was updated in my initial fix:

So I believe you just need to add line # 75 in your source file.

(Peter N Lewis) #12

Yep, sorry, that got dropped when I undid the previous patch.

Excellent, that all works perfectly now, including the upload of the images/files.

I’ll try to post a quick demo of how awesome this now is.

(Peter N Lewis) #13

OK, I can’t upload videos to this site, but here is a short video showing just how cool this integration is, combined with the synchronous uploading files (thanks @zogstrip!) from Using the Discourse API to post with uploaded files?. This link probably wont last forever, but I’ll keep it alive for a fair while.

(Kane York) #14

There’s a website where you can upload videos and they’ll keep it around for a while :wink: