--- /dev/null
+package Newslash::Eventd::Handler::Submission;
+use Mojo::Base 'Newslash::Eventd::Handler';
+use Data::Dumper;
+
+sub post {
+ my ($self, $item) = @_;
+
+ my $events = $self->app->model('events');
+ my $submission_id = $item->{related_id};
+ my $uid = $item->{source_user_id};
+ my $messages = $self->app->model('messages');
+
+ $self->app->log->debug("event submission posted processing... id: $submission_id, uid: $uid");
+
+ my $targets = $self->app->model('users')->select(seclev => { ge => 100 });
+ if (!@$targets) {
+ $self->inactivate_event($item->{event_id}, -1);
+ return;
+ }
+
+ my $submission = $self->app->model('submissions')->select(submission_id => $submission_id);
+ if (!$submission) {
+ $self->app->log->warn("invalid submission. id: $submission_id") if !$submission;
+ $self->inactivate_event($item->{event_id}, -2);
+ return;
+ }
+
+ for my $target (@$targets) {
+ my $vars = { submission => $submission, };
+ my $rs = $self->create_message(to => $target->{uid},
+ from => $uid,
+ template => "message/submission_post",
+ vars => $vars,
+ code => 6, # code 6 is "new submission"
+ type => "notify");
+ if (defined $rs) {
+ if ($rs) {
+ $self->app->log->debug("message created. id: $item->{event_id}, to: $target->{uid}");
+ }
+ else {
+ $self->app->log->debug("message not created because of user settings. id: $item->{event_id}, to: $target->{uid}");
+ }
+ }
+ else {
+ $self->app->log->debug("message cannot create. id: $item->{event_id}, to: $target->{uid}");
+ }
+ }
+ $self->inactivate_event($item->{event_id}, 0);
+ return;
+}
+
+1;
--- /dev/null
+# -*-Perl-*-
+use Mojo::Base -strict;
+
+use Test::More;
+use Test::Mojo;
+use Newslash::Util::TestMan;
+use Data::Dumper;
+
+use Newslash::Eventd::Handler::Submission;
+
+my $t = Test::Mojo->new('Newslash::Web');
+my ($admin, $user, $submission);
+
+my $test_man = Newslash::Util::TestMan->new($t);
+my $events = $t->app->model('events');
+ok($events, "get Events instance");
+
+my $submission_handler = new Newslash::Eventd::Handler::Submission($t->app);
+ok($submission_handler, "create submission handler instance");
+
+my $web_messages = $t->app->model('web_messages');
+ok($web_messages, "get WebMessages instance");
+my $messages = $t->app->model('messages');
+ok($messages, "get Messages instance");
+
+my $users = $t->app->model('users');
+ok($users, "get Users instance");
+
+my $test_title = "This is test submission foobar";
+
+# prepare for test
+if ($t->app->mode eq 'test') {
+ $admin = $test_man->create_admin("ehsadmin");
+ ok($admin, "create admin user");
+
+ $user = $test_man->create_user("ehstest");
+ ok($user, "create test user");
+
+ # set new submission notification to web
+ ok($users->update_message_config(uid => $admin->{uid},
+ config => [ { code => 6, mode => 1 } ]),
+ "change notification mode for admin");
+
+ $submission = $test_man->create_submission(user => $user,
+ title => $test_title);
+ ok($submission, "create test submission");
+}
+
+subtest 'test submission:post handler' => sub {
+ plan skip_all => "mode is not 'test'" if ($t->app->mode ne 'test');
+
+ # create submission post event
+ $t->app->event_que->emit("submission", "post", $user->{uid}, $submission->{id});
+
+ # get and process event
+ my $event = $events->select(type => "submission",
+ subtype => "post",
+ related_id => $submission->{id},
+ active => 1,
+ order_by => { created_timestamp => "DESC" },
+ limit => 1,
+ );
+ ok($event, "select events");
+ ok($event->[0], "get valid event");
+ if (!$event->[0]) {
+ diag Dumper $event;
+ }
+ else {
+ $event = $event->[0];
+ $submission_handler->post($event);
+ my $msgs = $web_messages->select(uid => $admin->{uid},
+ order_by => { id => "DESC" },
+ limit => 1);
+ ok($msgs, "select web_message");
+ is(@$msgs, 1, "select valid message");
+ like($msgs->[0]->{subject}, qr/$submission->{title}/, "subject check 1");
+ like($msgs->[0]->{subject}, qr/$user->{nickname}/, "subject check 2");
+ like($msgs->[0]->{message}, qr/\/submission\/$submission->{submission_id}/, "message check 1");
+
+ ok($web_messages->hard_delete(id => $msgs->[0]->{id}), "delete message");
+ }
+};
+
+$test_man->cleanup;
+done_testing;