Skip to content

Commit

Permalink
Merged pull request #1 from clayhinson/master.
Browse files Browse the repository at this point in the history
unread message sync
  • Loading branch information
funkatron committed Apr 26, 2011
2 parents 2a8a96a + 3106f5b commit e5bc971
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 1 deletion.
111 changes: 111 additions & 0 deletions custom/Action/Unreadsync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/**
* Action Unreadsync
*
* Sync unread message status
*
* @link http://getfrapi.com
* @author Frapi <frapi@getfrapi.com>
* @link /sync
*/
class Action_Unreadsync extends Frapi_Action implements Frapi_Action_Interface
{

/**
* Required parameters
*
* @var An array of required parameters.
*/
protected $requiredParams = array('timeline', 'replies', 'messages', 'key', 'service', 'userid', 'client');

/**
* The data container to use in toArray()
*
* @var A container of data to fill and return in toArray()
*/
private $data = array();

/**
* To Array
*
* This method returns the value found in the database
* into an associative array.
*
* @return array
*/
public function toArray()
{
return $this->data;
}

/**
* Default Call Method
*
* This method is called when no specific request handler has been found
*
* @return array
*/
public function executeAction()
{
throw new Frapi_Error('NOT_IMPLEMENTED');
}

/**
* Get Request Handler
*
* This method is called when a request is a GET
*
* @return array
*/
public function executeGet()
{
$this->requiredParams = array('service', 'userid');
$valid = $this->hasRequiredParameters($this->requiredParams);
if ($valid instanceof Frapi_Error) {
return $valid;
}

$service = $this->getParam('service', self::TYPE_STRING);
$userid = $this->getParam('userid', self::TYPE_STRING);

$sm = new SpazUnreadSync();
$sync = $sm->retrieve($service, $userid);

if(!$sync) {
throw new Frapi_Error('ERROR_RETRIEVING_SYNC');
}

return $sync;
}

/**
* Post Request Handler
*
*
*
* @return array
*/
public function executePost()
{
$valid = $this->hasRequiredParameters($this->requiredParams);
if ($valid instanceof Frapi_Error) {
return $valid;
}
$syncParams = array();
foreach($this->requiredParams as $param) {
$syncParams[$param] = $this->getParam($param, self::TYPE_STRING);
}

$sm = new SpazUnreadSync();
$sync = $sm->sync($syncParams);

if (!$sync) {
throw new Frapi_Error('ERROR_SAVING_SYNC');
}

return $this->toArray();
}

}

1 change: 1 addition & 0 deletions custom/AllFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
// Use the constant CUSTOM_MODEL to access the custom model directory
require CUSTOM_MODEL . '/Spaz/Url.php';
require CUSTOM_MODEL . '/Spaz/Avatar.php';
require CUSTOM_MODEL . '/Spaz/UnreadSync.php';

require dirname(__FILE__) . DIRECTORY_SEPARATOR.'helpers.php';
39 changes: 38 additions & 1 deletion custom/Config/actions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<name>Avatar</name>
<enabled>1</enabled>
<public>1</public>
<description></description>
<route>/avatar/:service/:userid</route>
<hash>6b3886e90fdc939f0d4525409995ece59b7fffe5</hash>
<parameters>
Expand All @@ -81,5 +80,43 @@
</parameter>
</parameters>
</action>
<action>
<name>Unreadsync</name>
<enabled>1</enabled>
<public>1</public>
<description>Sync unread message status</description>
<route>/sync</route>
<hash>d31eba41d3c31653ae1b26d18e8a10dd7d266253</hash>
<parameters>
<parameter>
<name>timeline</name>
<required>1</required>
</parameter>
<parameter>
<name>replies</name>
<required>1</required>
</parameter>
<parameter>
<name>key</name>
<required>1</required>
</parameter>
<parameter>
<name>service</name>
<required>1</required>
</parameter>
<parameter>
<name>userid</name>
<required>1</required>
</parameter>
<parameter>
<name>messages</name>
<required>1</required>
</parameter>
<parameter>
<name>client</name>
<required>1</required>
</parameter>
</parameters>
</action>
</actions>
</frapi-config>
22 changes: 22 additions & 0 deletions custom/Model/Spaz/UnreadSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class SpazUnreadSync
{
function __construct()
{
$this->cache = Frapi_Cache::getInstance('apc');
}

public function sync($params)
{
$ck = "sync_".$params['service']."_".$params['userid'];
return $this->cache->add($ck, $params);
}

public function retrieve($service, $userid)
{
$ck = "sync_{$service}_{$userid}";
$sync = $this->cache->get($ck);
return $sync;
}
}

0 comments on commit e5bc971

Please sign in to comment.