Skip to content

Commit

Permalink
Add settings endpoint for web/push and e-mail notifications #9
Browse files Browse the repository at this point in the history
We are updated space, email and web notification setting

Working flow of space notification setting

1. Pass space id in body data for adding space in space notification.
2. If user is the member of that space only then space is added for the user.
3. Otherwise, it will throw an error.
4. In other word, if user status will be 1(invited) or 2(applicant) for that space ,it will throw an error

Working flow of email notification setting
1. pass the email key name with status (true/false) to update the email notification setting
2.  Check the module is active or not
3. Check the key value is disabled or not (which mean check we can update the value or not)
4. Validate the input values (that is only true,false ,0 and 1 are accepted)
5. Update the email notification setting

Same working flow for web notification also
  • Loading branch information
gaurav-handysolver committed Jun 13, 2022
1 parent 256eff4 commit f733d3d
Show file tree
Hide file tree
Showing 3 changed files with 339 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public static function onRestApiAddRules()
//space
['pattern' => 'space', 'route' => 'smartVillage/space/space/find', 'verb' => 'GET'],
['pattern' => 'space/<spaceId:\d+>', 'route' => 'smartVillage/space/space/view', 'verb' => 'GET'],

//E-mail and Web Notification Setting
['pattern' => 'settings', 'route' => 'smartVillage/setting/notification/save-settings', 'verb' => 'PUT'],
], 'smartVillage');
}
}
292 changes: 292 additions & 0 deletions controllers/notification/NotificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
<?php

namespace humhub\modules\smartVillage\controllers\setting;

use humhub\modules\notification\components\NotificationCategory;
use humhub\modules\notification\models\forms\NotificationSettings;
use humhub\modules\notification\targets\BaseTarget;
use humhub\modules\rest\components\BaseController;
use humhub\modules\space\models\Membership;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use yii;

class NotificationController extends BaseController
{
/**
* @return array|int[]
* Save the setting of notification for space, email and web
*
* Create functions for space,email and web setting
*/
public function actionSaveSettings(){
$user='';
if(!Yii::$app->user->isAdmin()){
$user = User::findOne(Yii::$app->user->id);
}

$keys = Yii::$app->request->getBodyParam('settings',[]);

//save Space setting
$saveSpaceSetting = $this->saveSpaceSetting($keys,$user);
if($saveSpaceSetting['code']==400){
return $saveSpaceSetting;
}

$settings = $this->getSettings($user);

$validInput = array(1,true,0,false);
if(count($keys)>0){
//save Email setting
$saveEmailSetting = $this->saveEmailSetting($keys,$user,$settings,$validInput);
if($saveEmailSetting['code']==400){
return $saveEmailSetting;
}

//Save Web setting
$saveWebSetting = $this->saveWebSetting($keys,$user,$settings,$validInput);
if($saveWebSetting['code']==400){
return $saveWebSetting;
}

return $this->returnSuccess("Setting updated successfully",200);
}else{
return $this->returnError(400,"Data is required");
}

}

/**
* @param $keys
* @param $user
* @return array|int[]
* Save the setting of space
* $keys = data of body
* $user = user data (if the user is admin then $user is null)
*
* working flow
* 1. check the space id is exists or not
* 2. check the user is member of that space or not
* 3. set the space setting using space's guid
*/
public function saveSpaceSetting($keys,$user){
foreach($keys['spaces'] as $spaceId){
$space = Space::find()->where(['id'=>$spaceId])->one();

if(empty($space)){
return $this->returnError(400,$spaceId." Space Id not exists");
}

//Check user is member of that space or not
//check status of user in space_membership status
//If status = 3 (member), status = 2(applicant) and status = 1 (Invite)
$spaceMember = Membership::find()->where(['space_id'=>$spaceId,'user_id'=>$user->id])->one();

if($spaceMember['status']== Membership::STATUS_INVITED || $spaceMember['status']== Membership::STATUS_APPLICANT){
return $this->returnError(400,'You are not member of space : '.$spaceId);
}

Yii::$app->notification->setSpaces($space['guid'], $user);
}
return array("code"=>200);
}

/**
* @param $keys
* @param $user
* @param $settings
* @param $validInput
* @return array|int[]
* Save the setting of email notification
* $keys = data of body
* $user = user data
* $settings = current user's setting status
* $validInput = set of valid input values
*
* working flows
* 1. check the module is activated or not
* 2. check the key's value is disabled or not
* 3. validate the input's values
* 4. set the values of email notification
*/
public function saveEmailSetting($keys,$user,$settings,$validInput){
foreach($keys['email'] as $key => $value){

//Check module is activated or exists or not
$checkModuleStatus = $this->checkModule($key);
if($checkModuleStatus['code']==400){
return $checkModuleStatus;
}

//Check Key is disabled or not
$checkKeyStatus = $this->checkKey($user,$keys);
if($checkKeyStatus['code']==400){
return $checkKeyStatus;
}

//Check value of key is blanked or not
if($value===""){
return $this->returnError(400,$key." value cannot be blanked");
}

//Validate the value
if(!in_array($value,$validInput,true)){
return $this->returnError(400,"Invalid value of ".$key." only 1,0,true or false values are accepted");
}

$key = "notification.".$key;
$settings->set($key, $value);
}
return array("code"=>200);
}

/**
* @param $keys
* @param $user
* @param $settings
* @param $validInput
* @return array|int[]
* Save the setting of web notification
* $keys = data of body
* $user = user data
* $settings = current user's setting status
* $validInput = set of valid input values
*
* working flows
* 1. check the module is activated or not
* 2. check the key's value is disabled or not
* 3. validate the input's values
* 4. set the values of web notification
*/
public function saveWebSetting($keys,$user,$settings,$validInput){
foreach($keys['web'] as $key => $value){

//Check module is activated or exists or not
$checkModuleStatus = $this->checkModule($key);
if($checkModuleStatus['code']==400){
return $checkModuleStatus;
}

//Check Key is disabled or not
$checkKeyStatus = $this->checkKey($user,$keys);
if($checkKeyStatus['code']==400){
return $checkKeyStatus;
}

//Check value of key is blanked or not
if($value===""){
return $this->returnError(400,$key." value cannot be blanked");
}

//Validate the value
if(!in_array($value,$validInput,true)){
return $this->returnError(400,"Invalid value of ".$key." only 1,0,true or false values are accepted");
}

$key = "notification.".$key;
$settings->set($key, $value);
}
return array("code"=>200);
}

/**
* @param $keyName
* @return array|int[]
* check current module is activated or not
* $KeyName = Name of the current key like calendar, mail etc.
*
* working flows
* 1. fetch the all module setting name
* 2. compare with current key name ($keyName)
* 3. If, it is not match then return error message with module name
*/
public function checkModule($keyName){
$setting = new NotificationSettings();
foreach($setting->categories() as $category){
$categoryEmailName = $category->id.'_email';
$categoryWebName = $category->id.'_web';
if($keyName==$categoryEmailName || $keyName==$categoryWebName){
return array("code"=>200);
}
}
//Separate module name from _
$keyName = explode("_",$keyName);
return $this->returnError(400,$keyName[0]. " Module is not present or disable");


}

/**
* @param $user
* @param $keys
* @return array|int[]
* Check the value of key can be updated or not (generally web key name is disable)
* $user = user's data
* $keys = data of body
*
* working flows
* 1. fetch the key name that can be updated and stored in settingKeys variable
* 2. compare with keys of web
* 3. If the key name not exists in settingKeys variable name then we simply return error with key name
*/
public function checkKey($user,$keys){
//Stored the all key that can be updated in settingkeys array
foreach ($this->targets($user) as $target) {
if (!$target->isEditable($user)) {
continue;
}

foreach ($this->categories($user) as $category) {
if ($category->isFixedSetting($target)) {
continue;
}

$settingKeys[] = $target->getSettingKey($category);

}
}

//Check,each value of body can be updated or not
foreach($keys['web'] as $keyName=>$value){
$keyName = "notification.".$keyName;
if(!in_array($keyName,$settingKeys,true)){
return $this->returnError(400,$keyName." value not updated because it might be disabled");
}
}
return array("code"=>200);

}

/**
* @param $user
* @return mixed|object|null
* list of all setting both email and web of the user with key value pair
*/
public function getSettings($user)
{
$module = Yii::$app->getModule('notification');

return ($user) ? $module->settings->user($user) : $module->settings;
}

/**
* @return BaseTarget[] the notification targets enabled for this user (or global)
*/
public function targets($user)
{
$setting = new NotificationSettings();
if (!$setting->_targets) {
$setting->_targets = Yii::$app->notification->getTargets($user);
}

return $setting->_targets;
}

/**
* @return NotificationCategory[] NotificationCategories enabled for this user (or global)
*/
public function categories($user)
{
return Yii::$app->notification->getNotificationCategories($user);
}
}
45 changes: 44 additions & 1 deletion docs/postman/Smart Village API.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "a7d4eddd-ac59-4400-92b3-e8c8ad51ba97",
"_postman_id": "60569f18-7f41-45ed-b2d3-958f5b0afa74",
"name": "Smart Village API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
Expand Down Expand Up @@ -278,6 +278,49 @@
}
]
},
{
"name": "Setting",
"item": [
{
"name": "Notification Setting",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2NTQ1OTk4MTcsImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdFwvaHVtaHViIiwibmJmIjoxNjU0NTk5ODE3LCJ1aWQiOjMxLCJlbWFpbCI6ImdhdXJhdi5oYW5keXNvbHZlckBnbWFpbC5jb20ifQ.ZS0Sg9Y7VostnIsiAj63tRwoR6wTLPCjxdW53i0SnkDiqV210FcFMgfXI1X6NGKLJAQVHfLEze9L6DNyfOIGZw",
"type": "string"
}
]
},
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"settings\":\r\n {\r\n \"spaces\":[\r\n 1\r\n ],\r\n \"email\":{\r\n \"calendar_email\": true,\r\n \"mail_email\": false,\r\n \"mail_conversation_email\": false,\r\n \"comments_email\": false,\r\n \"content_created_email\":false,\r\n \"like_email\":false,\r\n \"space_member_email\":false,\r\n \"followed_email\": false,\r\n \"mentioned_email\": true\r\n },\r\n \"web\":{\r\n \"calendar_web\": true,\r\n \"mail_web\": false,\r\n \"mail_conversation_web\": false,\r\n \"comments_web\": false,\r\n \"content_created_web\":false,\r\n \"like_web\":false,\r\n \"space_member_web\":false,\r\n \"followed_web\": false,\r\n \"mentioned_web\": true\r\n }\r\n }\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{API_URL}}/api/v2/settings",
"host": [
"{{API_URL}}"
],
"path": [
"api",
"v2",
"settings"
]
}
},
"response": []
}
]
},
{
"name": "List Conversations",
"request": {
Expand Down

0 comments on commit f733d3d

Please sign in to comment.