Skip to content
This repository has been archived by the owner on Jul 15, 2020. It is now read-only.

Commit

Permalink
Count reactions instead of likes
Browse files Browse the repository at this point in the history
  • Loading branch information
petk committed May 22, 2017
1 parent c4e3598 commit f1983b2
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 155 deletions.
27 changes: 5 additions & 22 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ All notable changes to this project will be documented in this file. This projec
[keeps a CHANGELOG](http://keepachangelog.com/) and adheres to
[Semantic Versioning](http://semver.org/).

## [UNREALEASED] 2017-01-01

## [UNREALEASED] 2016-
* ...

### Added
## [0.7.0] - 2017-05-22

* Parameters configuration generated when installing dependencies with Composer
* Documentation
Expand All @@ -16,56 +17,40 @@ All notable changes to this project will be documented in this file. This projec
* Report date range set via command options
* Translations for multiple languages
* Symfony Dependency Injection
* ...

### Fixed

* Timezones now works correctly for current user and the Graph API's UTC format.
* Most shared topic
* Comments have reactions instead of likes

## [0.6.0] - 2016-09-05

### Added

* Project update
* Template updates
* Topic points based on reactions
* Access token removed from configuration
* ROT13 for offensive words


## [0.5.0] - 2016-06-06

### Added

* Better points calculation for more explanatory comments and replies.


## [0.4.0] - 2016-05-23

### Added

* Points calculation for images and animated images shares.
* More detailed configuration descriptions and FAQ.
* Configuration refactoring.
* Symfony Expression Language component
* Points calculation improvements.
* Bonus points for topics from group staff.


## [0.3.0] - 2016-05-16

### Added

* Dependency injection container
* Configuration overriding
* Offensive speech detection
* Comments merging


## [0.2.0] - 2016-05-09

### Added

* Coding style fixed
* Twig template engine
* Code quality improvements
Expand All @@ -74,6 +59,4 @@ All notable changes to this project will be documented in this file. This projec

## [0.1.0] - 2016-05-02

### Added

* Initial repository structure
5 changes: 3 additions & 2 deletions app/config/points.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ points:
# Points for reactions on topic
topic_reactions: '(topic.getReactionsCount() >= 100) ? 15 : ceil(topic.getReactionsCount() / 10)'

# Points for number of likes on comment or reply
likes: '(likes > 100) ? 11 : ceil(likes / 10)'
# Points for number of reactions on comment or reply
comment_reactions: '(comment.getReactionsCount() > 100) ? 15 : ceil(comment.getReactionsCount() / 10)'

# Points for topics with only image or animated image (such as bumped images from
# album topics and images without much description).
Expand All @@ -33,6 +33,7 @@ points:
- ['github.io', 10]
- ['php.net', 20]
- ['php.earth', 20]
- ['wwphp-fb.github.io', 20]
- ['packagist.org', 10]
- ['getcomposer.org', 10]
- ['phptherightway.com', 10]
Expand Down
9 changes: 2 additions & 7 deletions src/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getFeed()
$pagesCount = 0;
$startDate = $this->config->getParameter('start_datetime');
$endDate = $this->config->getParameter('end_datetime');
$response = $this->fb->get('/'.$this->config->getParameter('group_id').'/feed?fields=comments.limit(200).summary(1){like_count,comment_count,from,created_time,message,can_comment,comments.limit(200).summary(1){like_count,comment_count,from,created_time,message}},reactions.limit(0).summary(1),from,created_time,updated_time,message,type,attachments{type},shares&include_hidden=true&limit=50&since='.$startDate->getTimestamp().'&until='.$endDate->getTimestamp());
$response = $this->fb->get('/'.$this->config->getParameter('group_id').'/feed?fields=comments.limit(200).summary(1){comment_count,from,created_time,message,can_comment,reactions.limit(0).summary(1),comments.limit(200).summary(1){comment_count,from,created_time,message,reactions.limit(0).summary(1)}},reactions.limit(0).summary(1),from,created_time,updated_time,message,type,attachments{type},shares&include_hidden=true&limit=50&since='.$startDate->getTimestamp().'&until='.$endDate->getTimestamp());

$feedEdge = $response->getGraphEdge();

Expand All @@ -80,13 +80,8 @@ public function getFeed()
++$pagesCount;
$this->progress->setMessage('Fetching feed from API page '.$pagesCount.' and with the topic updated '.$feedEdge[0]->getField('updated_time')->format('Y-m-d H:i:s'));
$this->progress->advance();

foreach ($feedEdge as $topic) {
$topicArray = $topic->asArray();
$topicArray['commentsCount'] = $topic->getField('comments')->getMetaData()['summary']['total_count'];
$topicArray['reactionsCount'] = $topic->getField('reactions')->getMetaData()['summary']['total_count'];
$topicArray['canComment'] = $topic->getField('comments')->getMetaData()['summary']['can_comment'];
$this->feed[] = $topicArray;
$this->feed[] = $topic;
}
} while ($feedEdge = $this->fb->next($feedEdge));
}
Expand Down
136 changes: 67 additions & 69 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,19 @@ private function mapFeed($feed)
{
$startDate = $this->config->getParameter('start_datetime');
$endDate = $this->config->getParameter('end_datetime');

foreach ($feed as $topic) {
if ($topic['created_time'] >= $startDate && $topic['created_time'] <= $endDate) {
if ($topic->getField('created_time') >= $startDate && $topic->getField('created_time') <= $endDate) {
$this->mapTopic($topic);
}

if (isset($topic['comments'])) {
foreach ($topic['comments'] as $comment) {
if ($comment['created_time'] >= $startDate && $comment['created_time'] <= $endDate) {
$this->mapComment($comment);
}
foreach($topic->getField('comments', []) as $i => $comment) {
if ($comment->getField('created_time') >= $startDate && $comment->getField('created_time') <= $endDate) {
$this->mapComment($comment);
}

if (isset($comment['comments'])) {
foreach ($comment['comments'] as $reply) {
if ($reply['created_time'] >= $startDate && $reply['created_time'] <= $endDate) {
$this->mapReply($reply);
}
}
foreach($comment->getField('comments', []) as $j=>$reply) {
if ($reply->getField('created_time') >= $startDate && $reply->getField('created_time') <= $endDate) {
$this->mapReply($reply);
}
}
}
Expand All @@ -122,112 +117,115 @@ private function mapFeed($feed)
/**
* Maps topic data from API feed to Topic object.
*
* @param array $topic
* @param array $data
*
* @return Topic
*
* @throws \Exception
*/
private function mapTopic($topic)
private function mapTopic($data)
{
$newTopic = new Topic();
$commentsCount = $topic['commentsCount'];
if (array_key_exists('comments', $topic)) {
foreach ($topic['comments'] as $comment) {
if (isset($comment['comment_count'])) {
$commentsCount += $comment['comment_count'];
}
}
}
$newTopic->setCommentsCount($commentsCount);
$newTopic->setId($topic['id']);
$newTopic->setCreatedTime($topic['created_time']);
if (array_key_exists('message', $topic)) {
$newTopic->setMessage($topic['message']);
$topic = new Topic();

// Count comments and replies
$commentsCount = $data->getField('comments')->getMetaData()['summary']['total_count'];
foreach ($data->getField('comments', []) as $comment) {
$commentsCount += $comment->getField('comment_count', 0);
}
$newTopic->setReactionsCount($topic['reactionsCount']);
$newTopic->setCanComment($topic['canComment']);
$newTopic->setType($topic['type']);
if ($newTopic->getType() == 'link' && isset($topic['attachments'][0]['type']) && $topic['attachments'][0]['type'] == 'animated_image_share') {
$newTopic->setType('animated_image_share');
$topic->setCommentsCount($commentsCount);

$topic->setId($data->getField('id'));
$topic->setCreatedTime($data->getField('created_time'));
$topic->setMessage($data->getField('message'));

$topic->setReactionsCount($data->getField('reactions')->getMetaData()['summary']['total_count']);
$topic->setCanComment($data->getField('comments')->getMetaData()['summary']['can_comment']);
$topic->setType($data->getField('type'));

$dataArray = $data->asArray();

if ($topic->getType() == 'link' && isset($dataArray['attachments'][0]['type']) && $dataArray['attachments'][0]['type'] == 'animated_image_share') {
$topic->setType('animated_image_share');
}

if (array_key_exists('from', $topic)) {
$user = $this->mapUser($topic['from']);
$user->addTopic($newTopic);
$newTopic->setUser($user);
if (array_key_exists('from', $dataArray)) {
$user = $this->mapUser($dataArray['from']);
$user->addTopic($topic);
$topic->setUser($user);
}

if (array_key_exists('shares', $topic)) {
$newTopic->setSharesCount($topic['shares']['count']);
if (array_key_exists('shares', $dataArray)) {
$topic->setSharesCount($dataArray['shares']['count']);
}

// Add topic to collection
$this->topics->add($newTopic, $newTopic->getId());
$this->topics->add($topic, $topic->getId());

// Log topic
$log = $newTopic->getId()."\t";
$log .= ' Reactions: '.$newTopic->getReactionsCount()."\t";
$log .= ' Comments: '.$newTopic->getCommentsCount()."\n";
$log = $topic->getId()."\t";
$log .= ' Reactions: '.$topic->getReactionsCount()."\t";
$log .= ' Comments: '.$topic->getCommentsCount()."\n";
$this->log->logTopic($log);

return $newTopic;
return $topic;
}

/**
* Maps comment data from API feed to Comment object.
*
* @param array $comment
* @param array $data
*
* @return Comment
*
* @throws \Exception
*/
private function mapComment($comment)
private function mapComment($data)
{
$newComment = new Comment();
$newComment->setId($comment['id']);
$newComment->setMessage($comment['message']);
$newComment->setLikesCount($comment['like_count']);
$comment = new Comment();
$comment->setId($data->getField('id'));
$comment->setMessage($data->getField('message'));
$comment->setReactionsCount($data->getField('reactions')->getMetaData()['summary']['total_count']);

if (array_key_exists('from', $comment)) {
$user = $this->mapUser($comment['from']);
$user->addComment($newComment);
$dataArray = $data->asArray();
if (array_key_exists('from', $dataArray)) {
$user = $this->mapUser($dataArray['from']);
$user->addComment($comment);

$newComment->setUser($user);
$comment->setUser($user);
}

$this->comments->add($newComment, $newComment->getId());
$this->comments->add($comment, $comment->getId());

return $newComment;
return $comment;
}

/**
* Map reply data from API feed to Reply object.
*
* @param array $reply
* @param array $data
*
* @return Reply
*
* @throws \Exception
*/
private function mapReply($reply)
private function mapReply($data)
{
$newReply = new Reply();
$newReply->setId($reply['id']);
$newReply->setMessage($reply['message']);
$newReply->setLikesCount($reply['like_count']);
$reply = new Reply();
$reply->setId($data->getField('id'));
$reply->setMessage($data->getField('message'));
$reply->setReactionsCount($data->getField('reactions')->getMetaData()['summary']['total_count']);

if (array_key_exists('from', $reply)) {
$user = $this->mapUser($reply['from']);
$user->addReply($newReply);
$dataArray = $data->asArray();
if (array_key_exists('from', $dataArray)) {
$user = $this->mapUser($dataArray['from']);
$user->addReply($reply);

$newReply->setUser($user);
$reply->setUser($user);
}

$this->replies->add($newReply, $newReply->getId());
$this->replies->add($reply, $reply->getId());

return $newReply;
return $reply;
}

/**
Expand Down
Loading

0 comments on commit f1983b2

Please sign in to comment.