Skip to content

Commit

Permalink
Merge pull request #27 from watzenare/improve-send-request
Browse files Browse the repository at this point in the history
Improve send request
  • Loading branch information
watzenare committed May 30, 2015
2 parents 4725be7 + f9361d7 commit 919e79c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
26 changes: 13 additions & 13 deletions PushApi/Controllers/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class LogController extends Controller
{
const MAX_DELAY = 3600;

private $androidUsers = array();
private $iosUsers = array();
private $message = '';
Expand Down Expand Up @@ -64,15 +66,15 @@ public function sendMessage()
// If delay is set, notification will be send after the delay time.
// Delay must be in seconds and a message can't be delayed more than 1 hour.
if (isset($this->requestParams['delay'])) {
if ($this->requestParams['delay'] <= 3600) {
if ($this->requestParams['delay'] <= self::MAX_DELAY) {
$this->delay = Date("Y-m-d h:i:s a", time() + $this->requestParams['delay']);
} else {
throw new PushApiException(PushApiException::INVALID_OPTION, "Max delay value 3600 (1 hour)");
}
}

// Search if preference exist and if true, it gets all the users that have set preferences.
$theme = Theme::with('preferences.user')->where('name', $this->theme)->first();
// Search if preference exist
$theme = Theme::where('name', $this->theme)->first();
if (!$theme) {
throw new PushApiException(PushApiException::NOT_FOUND, "Theme doesn't exist");
}
Expand Down Expand Up @@ -132,23 +134,21 @@ private function unicastChecker($theme, $log)
// If no results found there's no problem to send the notification
}

$user = false;
// Searching user into theme preferences (if the user exist we don't need to find him into database)
foreach ($theme->preferences->toArray() as $key => $preferenceUser) {
if ($preferenceUser['user']['id'] == $userId) {
$user = $preferenceUser['user'];
$preference = decbin($preferenceUser['option']);
}
}
// Searching if the user has set preferences for that theme in order to get the option
$user = Preference::with('User')->where('theme_id', $theme->id)->where('user_id', $userId)->first();

// If we don't find the user, the default preference is to send him through all devices
// If we don't find the user, the default preference is to send through all devices
if (!$user) {
try {
$user = User::findOrFail($userId);
} catch (ModelNotFoundException $e) {
throw new PushApiException(PushApiException::NOT_FOUND);
}
$preference = decbin(Preference::ALL_RANGES);
} else {
$user = $user->toArray();
$preference = $user['option'];
$user = $user['user'];
}

$this->preQueuingDecider(
Expand Down Expand Up @@ -233,7 +233,7 @@ private function broadcastChecker($theme, $log)
// Search if the user has set broadcast preferences
$preference = User::findOrFail($user['id'])
->preferences()
->where('theme_id', $theme['id'])
->where('theme_id', $theme->id)
->first();
// If user has set, it is used that option but if not set, default is all devices
if (isset($preference)) {
Expand Down
2 changes: 1 addition & 1 deletion PushApi/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function setUser()
$email = $this->requestParams['email'];

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new PushApiException(PushApiException::NO_DATA);
throw new PushApiException(PushApiException::INVALID_DATA);
}

// Checking if user already exists
Expand Down
4 changes: 2 additions & 2 deletions PushApi/System/pushdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(80) NOT NULL,
`android_id` varchar(100) NOT NULL DEFAULT '0',
`ios_id` varchar(100) NOT NULL DEFAULT '0',
`android_id` varchar(255) NOT NULL DEFAULT '0',
`ios_id` varchar(255) NOT NULL DEFAULT '0',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

The PushApi is a server side project using PHP. It provides a way to notify users of different kind of events. There is the possibility to send notifications using unicast (target user), multicast (interested group) or broadcast (all users).

> It is being tested in a real system and it is obtaining the goals for which Pushapi was designed for.
- It is being tested handling a database of more than 400k users.
- It is sending an average of 50k daily mails.
- The same values than before with android smartphones.


## Index

Expand Down

0 comments on commit 919e79c

Please sign in to comment.