Skip to content

Instantly share code, notes, and snippets.

@cafreamoroso
Created June 2, 2012 16:21
Show Gist options
  • Save cafreamoroso/2859040 to your computer and use it in GitHub Desktop.
Save cafreamoroso/2859040 to your computer and use it in GitHub Desktop.
ahitrol<?
class PushWebservice extends WebService {
const publish_key = 'pub-97f307a9-9106-4f70-9669-1f39baebaf4b';
const subscribe_key = 'sub-e7711140-84c5-11e1-9cee-cb71e91be0d6';
public $user;
public $user_id;
public $message_id;
public $notification_id;
private $channel;
private $message;
private $notification;
function __construct() {
$this->initDatabase();
$this->initActiveRecord();
$this->setRequestData();
if (isset($this->message_id)) {
$this->setMessage();
$this->setUserById($this->message->user_id);
$this->initRealtimeChannel();
$this->pushMessage();
}
if (isset($this->notification_id)) {
$this->setNotification();
$this->setUserById($this->notification->user_id);
$this->initRealtimeChannel();
$this->pushNotification();
}
}
private function setRequestData() {
if (isset($_REQUEST['message_id'])) {
$this->message_id = $_REQUEST['message_id'];
}
if (isset($_REQUEST['notification_id'])) {
$this->notification_id = $_REQUEST['notification_id'];
}
if (isset($_REQUEST['history']) && isset($_REQUEST['user_id'])) {
$this->user_id = $_REQUEST['user_id'];
}
}
private function setUserById($id) {
$user = User::find_by_id($id);
if (!empty($user)) {
$this->user = $user;
}
else {
exit('Need an user to push data to');
}
}
private function setMessage() {
$message = UserMessage::find_by_id($this->message_id);
if (!empty($message)) {
$this->message = $message;
}
else {
exit('Need a message to push');
}
}
private function setNotification() {
$notification = UserNotification::find_by_id($this->notification_id);
if (!empty($notification)) {
$this->notification = $notification;
}
else {
exit('Need a notification to push');
}
}
private function processPushRequest() {
if (isset($this->message)) {
$this->pushMessage();
}
if (isset($this->notification)) {
$this->pushNotification();
}
}
private function pushMessage() {
$alias = AccountAlias::find_by_account_id($this->user->account_id);
$alias = $alias->alias;
$data = array();
$data['id'] = $this->message->id;
$data['t'] = '1';
$data['b'] = $this->message->body;
$data['been_read'] = $this->message->been_read;
$data['url'] = "http://my.flightbackpack.com/".$alias."/message/".$this->message->id;
if ($this->message->from_app) {
$data['app_id'] = $this->message->app_id;
if (!empty($this->message->app->medium_icon_url)) {
$data['i'] = "http://my.flightbackpack.com/public_uploads/app_icons/".$this->message->app->medium_icon_url;
}
else {
$data['i'] = "http://my.flightbackpack.com/public/images/apps/unknown75x75.png";
}
}
if ($this->message->from_system) {
$data['system_message'] = '1';
$data['i'] = "http://my.flightbackpack.com/public/images/system_message_avatar.png";
}
print_r($data);
echo "size of msg in kb".mb_strlen(serialize($data), 'latin1')/1024;
/* $this->pubnubPublish($data); */ //jst in case
$this->realtimePublish($this->user->user_secret, $data);
}
private function pushNotification() {
$data = array();
$data['id'] = $this->notification->id;
$data['t'] = '2';
$data['b'] = $this->notification->body;
$data['been_read'] = $this->notification->been_read;
if ($this->notification->from_app) {
$data['app_id'] = $this->notification->app_id;
if (!empty($this->notification->app->medium_icon_url)) {
$data['i'] = "http://my.flightbackpack.com/public_uploads/app_icons/".$this->notification->app->medium_icon_url;
}
else {
$data['i'] = "http://my.flightbackpack.com/public/images/apps/unknown75x75.png";
}
}
if ($this->notification->from_system) {
$data['system_message'] = '1';
$data['i'] = "http://my.flightbackpack.com/public/images/system_message_avatar.png";
}
if (isset($this->notification->url)) {
$data['url'] = $this->notification->url;
}
print_r($data);
echo "size of msg in kb".mb_strlen(serialize($data), 'latin1')/1024;
/* $this->pubnubPublish($data); */
$this->realtimePublish($this->user->user_secret, $data);
}
private function initPubnubChannel() {
$this->channel = new Pubnub( PushWebservice::publish_key, PushWebservice::subscribe_key );
}
private function pubnubPublish($data) {
$this->channel->publish(array(
'channel' => $this->user->user_secret,
'message' => $data
));
}
private function initRealtimeChannel ($host = '127.0.0.1') {
$this->channel = new Redis();
$this->channel->connect($host);
}
private function realtimePublish ($channels, $data) {
if (!is_array($channels)) {
$channels = array($channels);
}
$message = array(
'channels' => $channels,
'data' => $data
);
$this->channel->publish('juggernaut', json_encode($message));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment