package Newslash::Model::Journals;
use Newslash::Model::Base -base;
+use URI::Escape;
use Data::Dumper;
#========================================================================
my $limit = $options->{limit} || 10;
- my $dbh = $self->connect_db;
+ my $dbh = $options->{dbh} || $self->connect_db;
# get journals
my $where_clause = '';
$sth->execute($limit);
my $rs = $sth->fetchall_arrayref({});
- $dbh->disconnect();
+
+ if (!$options->{dbh}) {
+ $dbh->disconnect();
+ }
if (@$rs == 0) {
return [];
=cut
+my $comment_status = ['disabled', 'enabled', 'friends_only', 'friends_fof_only',
+ 'no_foe', 'no_foe_eof' ,'logged_in'];
+
sub create {
my ($self, $params, $user, $extra_params, $opts) = @_;
$self->set_error("no title given", 1);
return;
}
+ # check title exists
+ if (!$params->{article}) {
+ $self->set_error("no journal text given", 1);
+ return;
+ }
# check promotetype
# check discussion
- if (!defined $params->{commentstatus}) {
+ if (!$params->{commentstatus}) {
$self->set_error("no commentstatus given", 1);
return;
}
- my $discussion;
+ if (!grep {/$params->{commentstatus}/} @$comment_status) {
+ $self->set_error("invalid commentstatus given", 1);
+ return;
+ }
+
+ my $dbh = $self->connect_db({AutoCommit => 0,});
+
+ my $discussion_id;
if ($params->{commentstatus} ne 'disabled') {
+ # is discussion enabled, create discussion
# create discussion
#my $rootdir = $gSkin->{rootdir};
#my $did = $slashdb->createDiscussion({
# url => "$rootdir/~" . fixparam($user->{nickname}) . "/journal/$id",
# });
#$journal->set($id, { discussion => $did });
+ my $discussions = $self->new_instance_of('Newslash::Model::Discussions');
+ my $escaped_nickname = uri_escape($user->{nickname});
+ my $discuss_params = {
+ title => $params->{description},
+ url => "http://srad.jp/~$escaped_nickname/journal/$id",
+ topic => $params->{tid} || 2068, # tid=2068 is journal.,
+ kind => 'journal',
+ commentstatus => $params->{commentstatus},
+ uid => $user->{uid},
+ };
+ $discussion_id = $discussions->create($discuss_params, {dbh => $dbh});
+ if (!$discussion_id) {
+ $dbh->rollback;
+ $dbh->disconnect;
+ $self->set_error("discussion create failed", $discussions->last_errorno);
+ return;
+ }
}
# create journal
push @values, $user->{uid};
push @values, $params->{description};
# posttype: 1: PLAINTEXT, 2: HTML, 3: EXTRANS, 4: CODE, 77: FULLHTML
- push @values, $params->{posttype};
- push @values, $params->{discussion};
- push @values, $params->{tid};
+ push @values, $params->{posttype} || 2;
+ push @values, $discussion;
+ push @values, $params->{tid} || 2068; # tid=2068 is journal.
# promotype: publicize: make_submission, publish: post firehose, post: no firehose
push @values, $params->{promotype} || 'publish';
push @values, $user->{srcids}{32};
push @values, $user->{srcids}{24};
+ my $rs = $dbh->do($sql, undef, @values);
+ if (!$rs) {
+ $dbh->rollback;
+ $self->set_error("insert failed", $dbh->{mysql_errorno});
+ $dbh->disconnect;
+ return;
+ }
+ my $journal_id = $dbh->last_insert_id(undef, undef, undef, undef);
+
+ # insert into journals_text
+ my $sql = <<"EOSQL";
+INSERT INTO journals_text
+ (id, article, introtext)
+ VALUES
+ (?, ?, ?)
+EOSQL
+
+ my $rs = $dbh->do($sql, undef, $journal_id, $params->{article}, $params->{article});
+ if (!$rs) {
+ $dbh->rollback;
+ $self->set_error("insert into journals_text failed", $dbh->{mysql_errorno});
+ $dbh->disconnect;
+ return;
+ }
+
+ # create firehose item
+ $rs = $self->createFirehoseItem($journal_id, {dbh => $dbh});
+ if (!$rs) {
+ $dbh->rollback;
+ $dbh->disconnect;
+ return;
+ }
+
+ # update users_journal:
+ # - set latest user's journal id to users_journal.jid
+ # - set latest user's journal date to users_journal.date
+ # - set number of journals to users_journal.count
+ #
+ $sql = <<"EOSQL";
+UPDATE users_journal
+ SET jid = ?,
+ count = count + 1,
+ date = ?
+ WHERE uid = ?
+EOSQL
+ my $rs = $dbh->do($sql, undef, $journal->{id}, $journal->{date}, $journal->{uid});
+ if (!$rs) {
+ $dbh->rollback;
+ $self->set_error("update users_journal failed", $dbh->{mysql_errorno});
+ $dbh->disconnect;
+ return;
+ }
+
+ #my $achievements = getObject('Slash::Achievements');
+ #if ($achievements) {
+ # $achievements->setUserAchievement('journal_posted', $user->{uid});
+ #}
# if url given, create url object. But, current implement has no url form.
#if ($form->{url_id}) {
# $slashdb->addUrlForGlobj($url_id, $globjid);
#}
- # is discussion enabled, create discussion
- #if ($constants->{journal_comments} && $form->{journal_discuss} ne 'disabled') {
- # my $rootdir = $gSkin->{rootdir};
- # my $did = $slashdb->createDiscussion({
- # kind => 'journal',
- # title => $description,
- # topic => $form->{tid},
- # commentstatus => $form->{journal_discuss},
- # url => "$rootdir/~" . fixparam($user->{nickname}) . "/journal/$id",
- # });
- # $journal->set($id, { discussion => $did });
- #}
+ # transfer tag info
+ #getObject("Slash::Tags")->transferTags($fhitem->{globjid}, $journal_globjid);
# call hooks, in current srad.jp, call Slash::Journal::promoteJournal.
# slashHook('journal_save_success', { id => $id });
+ # if promotetype is "publicize", create submission
+ if ($params->{promotype} eq "publicize") {
+ my $submissions = $self->new_instance_of("Newslash::Model::Submissions");
+ my $submit_params = {
+ title => $params->{description},
+ introtext => $params->{article},
+ tid => $params->{tid} || 2068, # tid=2068 is journal.
+ };
+ my $sub_id = $submissions->create($submit_params, $user);
+ if (!$sub_id) {
+ $dbh->rollback;
+ $self->set_error("submission create failed", $submissions->last_errorno);
+ $dbh->disconnect;
+ return;
+ }
+ }
+
# create messages
+ # done
+ $dbh->rollback;
+ $dbh->disconnect;
+ return $journal_id;
}
-sub _fixJournalText {
- my($self, $text, $mode, $user) = @_;
- return unless (length($text) > 0 && $mode);
-
- $user ||= getCurrentUser();
- unless (ref $user) {
- if ($user eq '-1') { # force admin
- $user = { seclev => 100 };
- } else {
- $user = $self->getUser($user);
- }
- }
-
- my $admin = 0;
- # 77 = "full" HTML, 2 = normal HTML
- if ($mode == 77) {
- if (isAdmin($user) || $user->{acl}{journal_admin_tags}) {
- $admin = 1;
- }
- $mode = 2;
- }
-
- local $Slash::Utility::Data::approveTag::admin = 1
- if $admin;
- my $stripped = strip_mode($text, $mode);
-
- my $balanced = balanceTags($stripped, {
- deep_nesting => 1,
- admin => ($admin ? 1 : 0)
- });
-
- return $balanced;
-}
+sub create_firehose_item {
+ my ($self, $journl_id, $article, $options) = @_;
+ my $journal = $self->select(id => $journal_id, $options);
+ return if !$journal;
+ $options ||= {};
+
+ my $introtext = $self->get_intro_text($article);
+ my $bodytext = $self->get_body_text($article);
+
+ # create globjs
+ my $globjs = $self->new_instance_of('Newslash::Model::Globjs');
+ my $globj_id = $globjs->create(journals => $journal->{id}, $options);
+ if (!$globj_id) {
+ $self->set_error($globjs->last_error, $globjs->last_errorno);
+ return;
+ }
+
+ # create item
+ # TODO: sprite_info column don't have default value...
+ my $sql = <<"EOSQL";
+INSERT INTO firehose
+ (uid, globjid, type, createtime, popularity, editorpop, public, attention_needed, primaryskid, tid, srcid,
+ url_id, email, emaildomain, name, ipid, subnetid, sprite_info)
+ VALUES
+ (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
+ ?, ?, ?, ?, ?, ?, "")
+EOSQL
+ my $dbh = $options->{dbh} || $self->connect_db;
+
+ my $publicize = $journal->{promotetype} eq 'publicize';
+ my $publish = $journal->{promotetype} eq 'publish';
+ my $color_lvl = $publicize ? 5 : $publish ? 6 : 7; # post == 7
+ my $editor_lvl = $publicize ? 5 : $publish ? 6 : 8; # post == 8
+
+ my $mods = $self->new_instance_of('Newslash::Model::Moderations');
+ my $popularity = $mods->getEntryPopularityForColorLevel($color_lvl);
+ my $editorpop = $mods->getEntryPopularityForColorLevel($editor_lvl);
+
+ $rs = $dbh->do($sql, undef,
+ $journal->{uid}, #uid
+ $globj_id, #globjid
+ "journal", #type
+ $journal->{date}, #createtime
+ $popularity, #popularity
+ $editorpop, #editorpop
+ "yes", #public
+ "yes", #attention_needed
+ $primaryskid, #primaryskid
+ $journal->{tid}, #tid
+ $journal->{id}, #srcid
+ 0, #url_id
+ "", #email
+ "", #emaildomain
+ "", #name
+ $user->{ipid}, #ipid
+ $user->{subnetid} #subnetid
+ );
+ if (!$rs) {
+ $self->set_error("insert into firehose failed", $dbh->{mysql_errorno});
+ $dbh->disconnect if !$options->{dbh};
+ return;
+ }
+ my fh_id = $dbh->last_insert_id(undef, undef, undef, undef);
+
+ # insert firehose_text
+ my $sql = <<"EOSQL";
+INSERT INTO firehose_text
+ (title, introtext, bodytext, media)
+ VALUES
+ (?, ?, ?, NULL)
+EOSQL
+ $rs = $dbh->do($sql, undef,
+ $journal->{description},
+ $introtext,
+ $bodytext);
+ if (!$rs) {
+ $self->set_error("insert into firehose_text failed", $dbh->{mysql_errorno});
+ $dbh->disconnect if !$options->{dbh};
+ return;
+ }
+ # create tag
+ my $tags_upbote_tagname = "nod";
+ my $tags = $self->new_instance_of("Newslash::Model::Tags");
+ my $tag_id = $tags->set_tag(uid => $journal->{uid},
+ name => $tags_upbote_tagname,
+ globj_id => $globj_id,
+ private => 1,
+ dbh => $dbh);
+ if (!$tag_id) {
+ $self->set_error("insert into tags failed", $tags->last_errorno);
+ $dbh->disconnect if !$options->{dbh};
+ return;
+ }
+ if (!$options->{dbh}) {
+ $dbh->disconnect();
+ }
+
+ return $fh_id;
+}
1;