use DateTime::Format::ISO8601;
has 'last_error';
+has 'app';
+has 'c';
sub register {
my ($self, $app, $conf) = @_;
- $self->attr(app => $app);
- $app->helper(stories => sub { state $stories = $self; });
+ $self->app($app);
+ $app->helper(stories => sub { $self->c(shift); state $stories = $self; });
# default config values
my $cnf = $app->config->{Stories} ||= {};
$cnf->{submission_karma_bonus} = 3 if !defined $cnf->{submission_karma_bonus};
}
+sub related_stories {
+ my ($self, $stoid) = @_;
+ my $stories = $self->app->model('stories');
+
+ return $stories->get_related_items(stoid => $stoid);
+}
+
+sub latest {
+ my ($self, $limit) = @_;
+ my $c = $self->c;
+
+ my $user = $c->stash('user');
+ $limit ||= $c->_get_config("item_per_page", 10);
+ my $show_future = $user->{is_admin} ? 1 : 0;
+ my $show_nonpublic = $user->{is_admin} ? 1 : 0;
+
+ my $stories;
+ if ($user->{is_login}) {
+ $stories = $c->model('stories');
+ }
+ else {
+ # TODO: check expire time
+ $stories = $c->ezcache->model('stories', { expire => 60 });
+ }
+ my $items = $stories->select(hide_future => !$user->{is_admin},
+ public_only => !$user->{is_admin},
+ limit => $limit + 1,
+ order_by => {create_time => 'desc'},
+ );
+
+ return $items;
+}
+
sub create {
my ($self, @params) = @_;
- my $params = {@_};
+ my $params = {@params};
my $err = "";
my $config = $self->app->config->{Stories} || {};
my $stories = $self->model("stories");
# check parameters
# required parameters are: uid, title, introtext
- $err = "no_introtext_given" if !$params->{introtext} && !$params->{intro_text};;
+ my $intro_text = $params->{introtext} || $params->{intro_text};
+ $err = "no_introtext_given" if !$intro_text;
$err = "no_title_given" if !$params->{title};
$err = "no_uid_given" if !$params->{uid};