Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / lib / SGN / Controller / AJAX / Contact.pm
blob2e2014678e71aa1a0e3fb70baa9c1453279e3f54
2 =head1 NAME
4 SGN::Controller::AJAX::Contact - a REST controller class to provide the
5 functions for posting the contact form as an issue on github
7 =head1 DESCRIPTION
9 When the contact form is submitted it is posted as an issue to github
11 =head1 AUTHOR
13 Nicolas Morales <nm529@cornell.edu>
15 =cut
17 package SGN::Controller::AJAX::Contact;
19 use Moose;
20 use Data::Dumper;
21 use LWP::UserAgent;
22 use JSON;
24 BEGIN { extends 'Catalyst::Controller::REST' }
26 __PACKAGE__->config(
27 default => 'application/json',
28 stash_key => 'rest',
29 map => { 'application/json' => 'JSON' },
32 sub submit_contact_form : Path('/ajax/contact/submit') : ActionClass('REST') { }
34 sub submit_contact_form_POST : Args(0) {
35 my $self = shift;
36 my $c = shift;
37 my $title = $c->req->param('title');
38 my $body = $c->req->param('body');
39 my $security_answer = $c->req->param('security_answer');
40 my $security_attempt = $c->req->param('security_attempt');
42 if (!$security_attempt || !$security_answer || $security_answer ne $security_attempt){
43 $c->stash->{rest} = {error => "You must be a clever bot"};
44 $c->detach;
47 my $github_access_token = $c->config->{github_access_token};
48 my $website_name = $c->config->{project_name};
49 my $ua = LWP::UserAgent->new;
51 my $server_endpoint = "https://api.github.com/repos/solgenomics/contactform/issues";
52 my $req = HTTP::Request->new(POST => $server_endpoint);
53 $req->header('content-type' => 'application/json', 'Authorization' => "token $github_access_token");
55 $body .= "\n\nSent from website: $website_name";
56 $body .= "\n\nPlease remember to include the contact person's email in any replies which are directed to them. Please include the github.reply email address as a recipient in all messages, so that they are logged with the open ticket.";
57 my $post_data = { "title"=>$title, "body"=> $body, "labels"=>[$website_name] };
58 $req->content( encode_json $post_data);
60 my $resp = $ua->request($req);
61 if ($resp->is_success) {
62 my $message = $resp->decoded_content;
63 my $message_hash = decode_json $message;
64 #print STDERR Dumper $message_hash;
65 if ($message_hash->{id}){
66 $c->stash->{rest} = {success => 1};
67 } else {
68 $c->stash->{rest} = {error => 'The message was not posted to github correctly. Please try again.'};
70 } else {
71 $c->stash->{rest} = {error => "There was an error submitting the message. Please try again."};