Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/compress post #88

Open
wants to merge 2 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions js/stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "webpack"
},
"author": "",
"license": "ISC",
"scripts" : {
"build" : "webpack"
},
"devDependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
Expand All @@ -20,6 +17,7 @@
},
"dependencies": {
"babel-polyfill": "^6.16.0",
"pako": "^1.0.4",
"react": "^15.3.2",
"react-dom": "^15.3.2"
}
Expand Down
10 changes: 10 additions & 0 deletions js/stream/src/components/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import ScrollPosition from '../helper/ScrollPosition'
import Notification from './notification'
import Post from './Post'

import pako from 'pako'

export default class Posts extends Component {
constructor() {
super()
Expand Down Expand Up @@ -89,6 +91,14 @@ export default class Posts extends Component {
addPost(post) {
let rect = this.postsWrapper.getBoundingClientRect()

if (post.compressed) {
let decoded = atob(post.compressed)
let uncompressed = pako.inflate(decoded, {to: 'string'})
let json = JSON.parse(uncompressed)

post = json
}

if (rect.top < 0 || this.state.newPosts.length > 0) {
this.setState({
newPosts: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ function triggerLiveblogPostEvent(LiveblogPost $liveblog_post, $event) {
$client = $this->getClient();
$channel = "liveblog-{$liveblog_post->getLiveblog()->id()}";

$payload = Payload::create($liveblog_post)->getRenderedPayload(TRUE);

// Trigger an event by providing event name and payload.
$response = $client->trigger($channel, $event, Payload::create($liveblog_post)->getRenderedPayload());
$response = $client->trigger($channel, $event, $payload);
if (!$response) {
// Log response if there is an error.
$this->logger->saveLog('error');
Expand Down
26 changes: 25 additions & 1 deletion src/Utility/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ public static function create(LiveblogPost $entity) {
/**
* Gets a rendered payload from the liveblog post entity.
*
* @param bool $compressed
* If the payload should be compressed.
*
* @return array
* The payload array.
*/
public function getRenderedPayload() {
public function getRenderedPayload($compressed = FALSE) {
$entity = $this->entity;

$rendered_entity = $this->entityTypeManager()->getViewBuilder('liveblog_post')->view($entity);
Expand All @@ -67,6 +70,10 @@ public function getRenderedPayload() {
$data['status'] = $entity->status->value;
$data += $output;

if ($compressed) {
$data = $this->compress($data);
}

return $data;
}

Expand Down Expand Up @@ -116,4 +123,21 @@ protected function entityTypeManager() {
return \Drupal::entityTypeManager();
}

/**
* Compresses and encodes a payload.
*
* @param array $data
* The uncompressed payload.
*
* @return array
* The compressed payload.
*/
private function compress(array $data) {
$data = json_encode($data);
$data = gzencode($data, -1, FORCE_DEFLATE);
$data = base64_encode($data);

return ['compressed' => $data];
}

}