From e40c002a6dfedf87e77cd284fa8089b4fbb53c06 Mon Sep 17 00:00:00 2001 From: Chad Little Date: Sun, 20 Aug 2017 15:05:28 -0700 Subject: [PATCH] Add a basic view count to Phame Summary: This adds a very very basic view count to Phame, so bloggers can get some idea which posts are more popular than others. Anything more than this I think should be Facts or Google Analytics. Test Plan: Write a new post, see post count. Reload page, post count goes up. Archive post, post count stays the same. Reviewers: epriestley Reviewed By: epriestley Subscribers: Korvin Differential Revision: https://secure.phabricator.com/D18446 --- .../20170820.phame.01.post.views.sql | 2 ++ .../20170820.phame.02.post.views.sql | 2 ++ src/__phutil_library_map__.php | 2 ++ .../post/PhamePostViewController.php | 24 +++++++++++++++++++ .../phame/editor/PhamePostEditor.php | 6 +++++ src/applications/phame/storage/PhamePost.php | 5 +++- .../xaction/PhamePostViewsTransaction.php | 18 ++++++++++++++ 7 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 resources/sql/autopatches/20170820.phame.01.post.views.sql create mode 100644 resources/sql/autopatches/20170820.phame.02.post.views.sql create mode 100644 src/applications/phame/xaction/PhamePostViewsTransaction.php diff --git a/resources/sql/autopatches/20170820.phame.01.post.views.sql b/resources/sql/autopatches/20170820.phame.01.post.views.sql new file mode 100644 index 0000000000..f5f72294f6 --- /dev/null +++ b/resources/sql/autopatches/20170820.phame.01.post.views.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_phame.phame_post + ADD views INTEGER NOT NULL; diff --git a/resources/sql/autopatches/20170820.phame.02.post.views.sql b/resources/sql/autopatches/20170820.phame.02.post.views.sql new file mode 100644 index 0000000000..00b9b29203 --- /dev/null +++ b/resources/sql/autopatches/20170820.phame.02.post.views.sql @@ -0,0 +1,2 @@ +UPDATE {$NAMESPACE}_phame.phame_post + SET views = 0; diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 5d8d7c3543..070f56d5c8 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -4417,6 +4417,7 @@ phutil_register_library_map(array( 'PhamePostTransactionQuery' => 'applications/phame/query/PhamePostTransactionQuery.php', 'PhamePostTransactionType' => 'applications/phame/xaction/PhamePostTransactionType.php', 'PhamePostViewController' => 'applications/phame/controller/post/PhamePostViewController.php', + 'PhamePostViewsTransaction' => 'applications/phame/xaction/PhamePostViewsTransaction.php', 'PhamePostVisibilityTransaction' => 'applications/phame/xaction/PhamePostVisibilityTransaction.php', 'PhameSchemaSpec' => 'applications/phame/storage/PhameSchemaSpec.php', 'PhameSite' => 'applications/phame/site/PhameSite.php', @@ -10041,6 +10042,7 @@ phutil_register_library_map(array( 'PhamePostTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 'PhamePostTransactionType' => 'PhabricatorModularTransactionType', 'PhamePostViewController' => 'PhameLiveController', + 'PhamePostViewsTransaction' => 'PhamePostTransactionType', 'PhamePostVisibilityTransaction' => 'PhamePostTransactionType', 'PhameSchemaSpec' => 'PhabricatorConfigSchemaSpec', 'PhameSite' => 'PhabricatorSite', diff --git a/src/applications/phame/controller/post/PhamePostViewController.php b/src/applications/phame/controller/post/PhamePostViewController.php index 63adedb7ae..7dbaddc17a 100644 --- a/src/applications/phame/controller/post/PhamePostViewController.php +++ b/src/applications/phame/controller/post/PhamePostViewController.php @@ -18,6 +18,25 @@ final class PhamePostViewController $is_live = $this->getIsLive(); $is_external = $this->getIsExternal(); + // Register a blog "view" count + // + if (!$post->isDraft() && !$post->isArchived()) { + $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); + $xactions = array(); + $xactions[] = id(new PhamePostTransaction()) + ->setTransactionType(PhamePostViewsTransaction::TRANSACTIONTYPE) + ->setNewValue(null); + + $editor = id(new PhamePostEditor()) + ->setActor($viewer) + ->setContentSourceFromRequest($request) + ->setContinueOnMissingFields(true) + ->setContinueOnNoEffect(true); + + $editor->applyTransactions($post, $xactions); + unset($unguarded); + } + $header = id(new PHUIHeaderView()) ->addClass('phame-header-bar') ->setUser($viewer); @@ -151,6 +170,11 @@ final class PhamePostViewController ->setUser($viewer) ->setObject($post); + $views = id(new PhutilNumber($post->getViews())); + $properties->addProperty( + pht('Views'), + pht('%s', $views)); + $is_live = $this->getIsLive(); $is_external = $this->getIsExternal(); $next_view = new PhameNextPostView(); diff --git a/src/applications/phame/editor/PhamePostEditor.php b/src/applications/phame/editor/PhamePostEditor.php index 488d7a4938..845538dc12 100644 --- a/src/applications/phame/editor/PhamePostEditor.php +++ b/src/applications/phame/editor/PhamePostEditor.php @@ -41,6 +41,12 @@ final class PhamePostEditor if ($object->isDraft() || $object->isArchived()) { return false; } + foreach ($xactions as $xaction) { + switch ($xaction->getTransactionType()) { + case PhamePostViewsTransaction::TRANSACTIONTYPE: + return false; + } + } return true; } diff --git a/src/applications/phame/storage/PhamePost.php b/src/applications/phame/storage/PhamePost.php index a9525e0be7..c3ec4d4608 100644 --- a/src/applications/phame/storage/PhamePost.php +++ b/src/applications/phame/storage/PhamePost.php @@ -22,6 +22,7 @@ final class PhamePost extends PhameDAO protected $phameTitle; protected $body; protected $visibility; + protected $views; protected $configData; protected $datePublished; protected $blogPHID; @@ -40,7 +41,8 @@ final class PhamePost extends PhameDAO ->setBlogPHID($blog->getPHID()) ->attachBlog($blog) ->setDatePublished(PhabricatorTime::getNow()) - ->setVisibility(PhameConstants::VISIBILITY_PUBLISHED); + ->setVisibility(PhameConstants::VISIBILITY_PUBLISHED) + ->setViews(0); return $post; } @@ -128,6 +130,7 @@ final class PhamePost extends PhameDAO 'subtitle' => 'text64', 'phameTitle' => 'sort64?', 'visibility' => 'uint32', + 'views' => 'uint32', 'mailKey' => 'bytes20', 'headerImagePHID' => 'phid?', diff --git a/src/applications/phame/xaction/PhamePostViewsTransaction.php b/src/applications/phame/xaction/PhamePostViewsTransaction.php new file mode 100644 index 0000000000..2260a1d752 --- /dev/null +++ b/src/applications/phame/xaction/PhamePostViewsTransaction.php @@ -0,0 +1,18 @@ +getViews(); + } + + public function applyInternalEffects($object, $value) { + $views = $object->getViews(); + $views++; + $object->setViews($views); + } + +}