Teams API

Added Teams API to create, delete and update chat teams.
create , accept and decline team invitations.
post and list team messages (push notifications)

Change-Id: I2d8784e1a505c1988cb5c1b3d7011f943306c7c7
This commit is contained in:
Sebastian Marcet 2016-12-22 15:15:50 -03:00
parent 81c9b6a3c8
commit 1323dc3a38
45 changed files with 3684 additions and 130 deletions

View File

@ -0,0 +1,70 @@
<?php namespace App\Console\Commands;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Illuminate\Console\Command;
use services\model\IChatTeamService;
/**
* Class ChatTeamMessagesSender
* @package App\Console\Commands
*/
final class ChatTeamMessagesSender extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'teams:message-sender';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'teams:message-sender {bach_size?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send out Teams Chat Messages';
/**
* @var IChatTeamService
*/
private $service;
/**
* ChatTeamMessagesSender constructor.
* @param IChatTeamService $service
*/
public function __construct(IChatTeamService $service)
{
parent::__construct();
$this->service = $service;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$bach_size = $this->argument('bach_size');
if(is_null($bach_size)) $bach_size = 100;
$res = $this->service->sendMessages($bach_size);
$this->info(sprintf("total messages sent %s", $res));
}
}

View File

@ -17,8 +17,6 @@ use libs\utils\ICacheService;
use models\summit\ISummitRepository;
use ModelSerializers\SerializerRegistry;
use services\model\ISummitService;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Support\Facades\Config;
/**
@ -42,6 +40,12 @@ final class SummitJsonGenerator extends Command {
*/
private $cache_service;
/**
* SummitJsonGenerator constructor.
* @param ISummitRepository $repository
* @param ISummitService $service
* @param ICacheService $cache_service
*/
public function __construct(
ISummitRepository $repository,
ISummitService $service,

View File

@ -14,6 +14,7 @@ class Kernel extends ConsoleKernel
*/
protected $commands = [
\App\Console\Commands\SummitJsonGenerator::class,
\App\Console\Commands\ChatTeamMessagesSender::class,
];
/**
@ -24,9 +25,15 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// current
//Current
$schedule->command('summit:json-generator')->everyTenMinutes()->withoutOverlapping();
//austin
//Austin
$schedule->command('summit:json-generator 6')->everyTenMinutes()->withoutOverlapping();
//BCN
$schedule->command('summit:json-generator 7')->everyTenMinutes()->withoutOverlapping();
//Boston
$schedule->command('summit:json-generator 22')->everyTenMinutes()->withoutOverlapping();
// teams messages
$schedule->command('teams:message-sender 100')->everyMinute()->withoutOverlapping();
}
}

View File

@ -0,0 +1,123 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\main\IMemberRepository;
use models\oauth2\IResourceServerContext;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use utils\Filter;
use utils\FilterParser;
use utils\FilterParserException;
use utils\OrderParser;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Log;
use utils\PagingInfo;
/**
* Class OAuth2MembersApiController
* @package App\Http\Controllers
*/
final class OAuth2MembersApiController extends OAuth2ProtectedController
{
/**
* OAuth2MembersApiController constructor.
* @param IMemberRepository $member_repository
* @param IResourceServerContext $resource_server_context
*/
public function __construct
(
IMemberRepository $member_repository,
IResourceServerContext $resource_server_context
) {
parent::__construct($resource_server_context);
$this->repository = $member_repository;
}
public function getMembers(){
$values = Input::all();
$rules = array
(
'page' => 'integer|min:1',
'per_page' => 'required_with:page|integer|min:5|max:100',
);
try {
$validation = Validator::make($values, $rules);
if ($validation->fails()) {
$ex = new ValidationException();
throw $ex->setMessages($validation->messages()->toArray());
}
// default values
$page = 1;
$per_page = 5;
if (Input::has('page')) {
$page = intval(Input::get('page'));
$per_page = intval(Input::get('per_page'));
}
$filter = null;
if (Input::has('filter')) {
$filter = FilterParser::parse(Input::get('filter'), array
(
'irc' => ['=@', '=='],
'twitter' => ['=@', '=='],
'first_name' => ['=@', '=='],
'last_name' => ['=@', '=='],
));
}
$order = null;
if (Input::has('order'))
{
$order = OrderParser::parse(Input::get('order'), array
(
'first_name',
'last_name',
'id',
));
}
if(is_null($filter)) $filter = new Filter();
$data = $this->repository->getAllByPage(new PagingInfo($page, $per_page), $filter, $order);
return $this->ok($data->toArray(Request::input('expand', '')));
}
catch (EntityNotFoundException $ex1) {
Log::warning($ex1);
return $this->error404();
}
catch (ValidationException $ex2) {
Log::warning($ex2);
return $this->error412($ex2->getMessages());
}
catch(FilterParserException $ex3){
Log::warning($ex3);
return $this->error412($ex3->getMessages());
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -0,0 +1,137 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Illuminate\Support\Facades\Log;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\main\IChatTeamInvitationRepository;
use models\oauth2\IResourceServerContext;
use ModelSerializers\SerializerRegistry;
use services\model\IChatTeamService;
use utils\PagingResponse;
/**
* Class OAuth2TeamInvitationsApiController
* @package App\Http\Controllers
*/
final class OAuth2TeamInvitationsApiController extends OAuth2ProtectedController
{
/**
* @var IChatTeamService
*/
private $service;
/**
* OAuth2TeamInvitationsApiController constructor.
* @param IChatTeamService $service
* @param IChatTeamInvitationRepository $repository
* @param IResourceServerContext $resource_server_context
*/
public function __construct
(
IChatTeamService $service,
IChatTeamInvitationRepository $repository,
IResourceServerContext $resource_server_context
)
{
parent::__construct($resource_server_context);
$this->repository = $repository;
$this->service = $service;
}
public function getMyInvitations(){
try {
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$invitations = $this->repository->getInvitationsByInvitee($current_member_id);
$response = new PagingResponse
(
count($invitations),
count($invitations),
1,
1,
$invitations
);
return $this->ok($response->toArray($expand = Input::get('expand','')));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $invitation_id
* @return mixed
*/
public function acceptInvitation($invitation_id){
try {
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$team_member = $this->service->acceptInvitation($invitation_id, $current_member_id);
return $this->created(SerializerRegistry::getInstance()->getSerializer($team_member)->serialize($expand = ''));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $invitation_id
* @return mixed
*/
public function declineInvitation($invitation_id){
try {
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$this->service->declineInvitation($invitation_id, $current_member_id);
return $this->deleted();
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -0,0 +1,540 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use libs\utils\HTMLCleaner;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\main\IChatTeamPushNotificationMessageRepository;
use models\main\IChatTeamRepository;
use models\main\IMemberRepository;
use models\main\PushNotificationMessagePriority;
use models\oauth2\IResourceServerContext;
use ModelSerializers\SerializerRegistry;
use services\model\IChatTeamService;
use utils\Filter;
use utils\FilterParser;
use utils\OrderParser;
use utils\PagingInfo;
use utils\PagingResponse;
/**
* Class OAuth2TeamsApiController
* @package App\Http\Controllers
*/
final class OAuth2TeamsApiController extends OAuth2ProtectedController
{
/**
* @var IChatTeamService
*/
private $service;
/**
* @var IMemberRepository
*/
private $member_repository;
/**
* @var IChatTeamPushNotificationMessageRepository
*/
private $message_repository;
/**
* OAuth2TeamsApiController constructor.
* @param IChatTeamService $service
* @param IMemberRepository $member_repository
* @param IChatTeamPushNotificationMessageRepository $message_repository
* @param IChatTeamRepository $repository
* @param IResourceServerContext $resource_server_context
*/
public function __construct
(
IChatTeamService $service,
IMemberRepository $member_repository,
IChatTeamPushNotificationMessageRepository $message_repository,
IChatTeamRepository $repository,
IResourceServerContext $resource_server_context
)
{
parent::__construct($resource_server_context);
$this->service = $service;
$this->repository = $repository;
$this->message_repository = $message_repository;
$this->member_repository = $member_repository;
}
/**
* @return mixed
*/
public function getMyTeams(){
try {
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$current_member = $this->member_repository->getById($current_member_id);
if (is_null($current_member)) return $this->error404();
$teams = $this->repository->getTeamsByMember($current_member);
$response = new PagingResponse
(
count($teams),
count($teams),
1,
1,
$teams
);
return $this->ok($response->toArray($expand = Input::get('expand','')));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $team_id
* @return mixed
*/
public function getMyTeam($team_id){
try {
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$current_member = $this->member_repository->getById($current_member_id);
if (is_null($current_member)) return $this->error403();
$team = $this->repository->getById($team_id);
if(is_null($team)) throw new EntityNotFoundException();
if(!$team->isMember($current_member))
throw new EntityNotFoundException();
return $this->ok(SerializerRegistry::getInstance()->getSerializer($team)->serialize($expand = Input::get('expand','')));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @return mixed
*/
public function addTeam(){
try {
if(!Request::isJson())
return $this->error403();
$data = Input::json();
$rules = array
(
'name' => 'required|string|max:255',
'description' => 'required|sometimes|string',
);
// Creates a Validator instance and validates the data.
$validation = Validator::make($data->all(), $rules);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412
(
$messages
);
}
$fields = array
(
'name',
'description',
);
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$current_member = $this->member_repository->getById($current_member_id);
if (is_null($current_member)) return $this->error404();
$team = $this->service->addTeam(HTMLCleaner::cleanData($data->all(), $fields), $current_member);
return $this->created(SerializerRegistry::getInstance()->getSerializer($team)->serialize($expand = 'owner,members,member'));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $team_id
* @return mixed
*/
public function deleteTeam($team_id){
try {
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$this->service->deleteTeam($team_id);
return $this->deleted();
}
catch (ValidationException $ex1)
{
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $team_id
* @return mixed
*/
public function updateTeam($team_id){
try {
if(!Request::isJson())
return $this->error403();
$data = Input::json();
$rules = array
(
'name' => 'required|string|max:255',
'description' => 'required|sometimes|string',
);
// Creates a Validator instance and validates the data.
$validation = Validator::make($data->all(), $rules);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412
(
$messages
);
}
$fields = array
(
'name',
'description',
);
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$team = $this->service->updateTeam(HTMLCleaner::cleanData($data->all(), $fields), $team_id);
return $this->updated(SerializerRegistry::getInstance()->getSerializer($team)->serialize($expand = 'owner,members,member'));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $team_id
* @return mixed
*/
public function getMyTeamMessages($team_id){
$values = Input::all();
$rules = array
(
'page' => 'integer|min:1',
'per_page' => 'required_with:page|integer|min:5|max:100',
);
try {
$validation = Validator::make($values, $rules);
if ($validation->fails()) {
$ex = new ValidationException();
throw $ex->setMessages($validation->messages()->toArray());
}
// default values
$page = 1;
$per_page = 5;
if (Input::has('page')) {
$page = intval(Input::get('page'));
$per_page = intval(Input::get('per_page'));
}
$filter = null;
if (Input::has('filter')) {
$filter = FilterParser::parse(Input::get('filter'), array
(
'owner_id' => ['=='],
'sent_date' => ['>', '<', '<=', '>=', '=='],
));
}
$order = null;
if (Input::has('order'))
{
$order = OrderParser::parse(Input::get('order'), array
(
'sent_date',
'id',
));
}
if(is_null($filter)) $filter = new Filter();
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$current_member = $this->member_repository->getById($current_member_id);
if (is_null($current_member)) return $this->error403();
$team = $this->repository->getById($team_id);
if(is_null($team)) throw new EntityNotFoundException();
if(!$team->isMember($current_member))
throw new EntityNotFoundException();
$data = $this->message_repository->getAllSentByTeamPaginated
(
$team_id,
new PagingInfo($page, $per_page),
$filter,
$order
);
return $this->ok($data->toArray(Request::input('expand', '')));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $team_id
* @return mixed
*/
public function postTeamMessage($team_id){
try {
if(!Request::isJson())
return $this->error403();
$data = Input::json();
$rules = array
(
'body' => 'required|string',
'priority' => 'required|sometimes|string|chat_message_priority',
);
$values = $data->all();
// Creates a Validator instance and validates the data.
$validation = Validator::make($values, $rules);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412
(
$messages
);
}
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
if(!isset($values['priority']))
$values['priority'] = PushNotificationMessagePriority::Normal;
$message = $this->service->postMessage($team_id, $values);
return $this->created(SerializerRegistry::getInstance()->getSerializer($message)->serialize($expand = 'team,owner'));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $team_id
* @param $member_id
* @return mixed
*/
public function addMember2MyTeam($team_id, $member_id){
try {
if(!Request::isJson())
return $this->error403();
$data = Input::json();
$rules = array
(
'permission' => 'required|string|team_permission',
);
$values = $data->all();
// Creates a Validator instance and validates the data.
$validation = Validator::make($values, $rules);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412
(
$messages
);
}
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$invitation = $this->service->addMember2Team($team_id, $member_id, $values['permission']);
return $this->created(SerializerRegistry::getInstance()->getSerializer($invitation)->serialize($expand = 'team,inviter,invitee'));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $team_id
* @param $member_id
* @return mixed
*/
public function removedMemberFromMyTeam($team_id, $member_id){
try {
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) return $this->error403();
$this->service->removeMemberFromTeam($team_id, $member_id);
return $this->deleted();
}
catch (ValidationException $ex1)
{
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -46,7 +46,6 @@ final class OAuth2SummitMembersApiController extends OAuth2ProtectedController
$this->repository = $member_repository;
}
public function getMyMember($summit_id){
$summit = SummitFinderStrategyFactory::build($this->summit_repository)->find($summit_id);

View File

@ -44,6 +44,47 @@ Route::group(array(
});
// members
Route::group(['prefix'=>'members'], function(){
Route::get('', 'OAuth2MembersApiController@getMembers');
Route::group(['prefix'=>'me'], function(){
// invitations
Route::group(['prefix'=>'team-invitations'], function(){
Route::get('', 'OAuth2TeamInvitationsApiController@getMyInvitations');
Route::group(['prefix'=>'{invitation_id}'], function() {
Route::patch('', 'OAuth2TeamInvitationsApiController@acceptInvitation');
Route::delete('', 'OAuth2TeamInvitationsApiController@declineInvitation');
});
});
});
});
// teams
Route::group(['prefix'=>'teams'], function(){
Route::get('', 'OAuth2TeamsApiController@getMyTeams');
Route::post('', 'OAuth2TeamsApiController@addTeam');
Route::group(['prefix' => '{team_id}'], function () {
Route::get('', 'OAuth2TeamsApiController@getMyTeam');
Route::put('', 'OAuth2TeamsApiController@updateTeam');
Route::delete('', 'OAuth2TeamsApiController@deleteTeam');
Route::group(array('prefix' => 'messages'), function () {
Route::get('', 'OAuth2TeamsApiController@getMyTeamMessages');
Route::post('', 'OAuth2TeamsApiController@postTeamMessage');
});
Route::group(array('prefix' => 'members'), function () {
Route::group(['prefix' => '{member_id}'], function () {
Route::post('', 'OAuth2TeamsApiController@addMember2MyTeam');
Route::delete('', 'OAuth2TeamsApiController@removedMemberFromMyTeam');
});
});
});
});
// summits
Route::group(array('prefix' => 'summits'), function () {

View File

@ -0,0 +1,79 @@
<?php namespace ModelSerializers\ChatTeams;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\main\ChatTeamInvitation;
use ModelSerializers\SerializerRegistry;
use ModelSerializers\SilverStripeSerializer;
/**
* Class ChatTeamInvitationSerializer
* @package ModelSerializers\ChatTeams
*/
final class ChatTeamInvitationSerializer extends SilverStripeSerializer
{
protected static $array_mappings = array
(
'TeamId' => 'team_id:json_int',
'InviteeId' => 'invitee_id:json_int',
'InviterId' => 'inviter_id:json_int',
'Permission' => 'permission:json_string',
'IsAccepted' => 'is_accepted:json_boolean',
);
/**
* @param null $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = array(), array $relations = array(), array $params = array() )
{
$invitation = $this->object;
if(! $invitation instanceof ChatTeamInvitation) return [];
$values = parent::serialize($expand, $fields, $relations, $params);
if (!empty($expand)) {
$expand = explode(',', $expand);
foreach ($expand as $relation) {
switch (trim($relation)) {
case 'inviter':{
if(isset($values['inviter_id']))
{
unset($values['inviter_id']);
$values['inviter'] = SerializerRegistry::getInstance()->getSerializer($invitation->getInviter())->serialize();
}
}
break;
case 'invitee':{
if(isset($values['invitee_id']))
{
unset($values['invitee_id']);
$values['invitee'] = SerializerRegistry::getInstance()->getSerializer($invitation->getInvitee())->serialize();
}
}
break;
case 'team':{
if(isset($values['team_id']))
{
unset($values['team_id']);
$values['team'] = SerializerRegistry::getInstance()->getSerializer($invitation->getTeam())->serialize();
}
}
break;
}
}
}
return $values;
}
}

View File

@ -0,0 +1,63 @@
<?php namespace ModelSerializers\ChatTeams;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\main\ChatTeamMember;
use ModelSerializers\SerializerRegistry;
use ModelSerializers\SilverStripeSerializer;
/**
* Class ChatTeamMemberSerializer
* @package ModelSerializers\ChatTeams
*/
final class ChatTeamMemberSerializer extends SilverStripeSerializer
{
protected static $array_mappings = array
(
'TeamId' => 'team_id:json_int',
'MemberId' => 'member_id:json_int',
'Permission' => 'permission:json_string',
);
/**
* @param null $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = array(), array $relations = array(), array $params = array() )
{
$team_member = $this->object;
if(! $team_member instanceof ChatTeamMember) return [];
$values = parent::serialize($expand, $fields, $relations, $params);
if (!empty($expand)) {
$expand = explode(',', $expand);
foreach ($expand as $relation) {
switch (trim($relation)) {
case 'member':{
if(isset($values['member_id']))
{
unset($values['member_id']);
$values['member'] = SerializerRegistry::getInstance()->getSerializer($team_member->getMember())->serialize();
}
}
break;
}
}
}
return $values;
}
}

View File

@ -0,0 +1,75 @@
<?php namespace ModelSerializers\ChatTeams;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\main\ChatTeamPushNotificationMessage;
use ModelSerializers\SerializerRegistry;
use ModelSerializers\SilverStripeSerializer;
/**
* Class ChatTeamPushNotificationMessageSerializer
* @package ModelSerializers\ChatTeams
*/
final class ChatTeamPushNotificationMessageSerializer extends SilverStripeSerializer
{
protected static $array_mappings = array
(
'TeamId' => 'team_id:json_int',
'OwnerId' => 'owner_id:json_int',
'Priority' => 'priority:json_string',
'Message' => 'body:json_string',
);
/**
* @param null $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = array(), array $relations = array(), array $params = array() )
{
$message = $this->object;
if(! $message instanceof ChatTeamPushNotificationMessage) return [];
$values = parent::serialize($expand, $fields, $relations, $params);
if($message->isSent()){
$values['sent_date'] = $message->getSentDate()->getTimestamp();
}
if (!empty($expand)) {
$expand = explode(',', $expand);
foreach ($expand as $relation) {
switch (trim($relation)) {
case 'owner':{
if(isset($values['owner_id']))
{
unset($values['owner_id']);
$values['owner'] = SerializerRegistry::getInstance()->getSerializer($message->getOwner())->serialize();
}
}
break;
case 'team':{
if(isset($values['team_id']))
{
unset($values['team_id']);
$values['team'] = SerializerRegistry::getInstance()->getSerializer($message->getTeam())->serialize();
}
}
break;
}
}
}
return $values;
}
}

View File

@ -0,0 +1,70 @@
<?php namespace ModelSerializers\ChatTeams;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\main\ChatTeam;
use ModelSerializers\SerializerRegistry;
use ModelSerializers\SilverStripeSerializer;
/**
* Class ChatTeamSerializer
*/
final class ChatTeamSerializer extends SilverStripeSerializer
{
protected static $array_mappings = array
(
'Name' => 'name:json_string',
'Description' => 'description:json_string',
'OwnerId' => 'owner_id:json_int',
);
/**
* @param null $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = array(), array $relations = array(), array $params = array() )
{
$team = $this->object;
if(! $team instanceof ChatTeam) return [];
$values = parent::serialize($expand, $fields, $relations, $params);
$members = [];
foreach($team->getMembers() as $member){
$members[] = SerializerRegistry::getInstance()->getSerializer($member)->serialize($expand);
}
$values['members'] = $members;
if (!empty($expand)) {
$expand = explode(',', $expand);
foreach ($expand as $relation) {
switch (trim($relation)) {
case 'owner':{
if(isset($values['owner_id']))
{
unset($values['owner_id']);
$values['owner'] = SerializerRegistry::getInstance()->getSerializer($team->getOwner())->serialize();
}
}
break;
}
}
}
return $values;
}
}

View File

@ -1,5 +1,12 @@
<?php namespace ModelSerializers;
use Libs\ModelSerializers\IModelSerializer;
use models\main\ChatTeam;
use models\main\ChatTeamMember;
use models\main\ChatTeamPushNotificationMessage;
use ModelSerializers\ChatTeams\ChatTeamInvitationSerializer;
use ModelSerializers\ChatTeams\ChatTeamMemberSerializer;
use ModelSerializers\ChatTeams\ChatTeamPushNotificationMessageSerializer;
use ModelSerializers\ChatTeams\ChatTeamSerializer;
use ModelSerializers\Locations\SummitAirportSerializer;
use ModelSerializers\Locations\SummitExternalLocationSerializer;
use ModelSerializers\Locations\SummitHotelSerializer;
@ -84,6 +91,12 @@ final class SerializerRegistry
// push notification
$this->registry['SummitPushNotification'] = SummitPushNotificationSerializer::class;
// teams
$this->registry['ChatTeam'] = ChatTeamSerializer::class;
$this->registry['ChatTeamMember'] = ChatTeamMemberSerializer::class;
$this->registry['ChatTeamInvitation'] = ChatTeamInvitationSerializer::class;
$this->registry['ChatTeamPushNotificationMessage'] = ChatTeamPushNotificationMessageSerializer::class;
}
/**

View File

@ -0,0 +1,285 @@
<?php namespace models\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use models\utils\SilverstripeBaseModel;
/**
* @ORM\Entity
* @ORM\Table(name="ChatTeam")
* @ORM\Entity(repositoryClass="repositories\main\DoctrineChatTeamRepository")
* Class ChatTeam
* @package models\main
*/
class ChatTeam extends SilverstripeBaseModel
{
public function __construct()
{
parent::__construct();
$this->members = new ArrayCollection();
$this->messages = new ArrayCollection();
$this->invitations = new ArrayCollection();
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* @return ChatTeamMember[]
*/
public function getMembers()
{
return $this->members;
}
/**
* @param ChatTeamMember[] $members
*/
public function setMembers($members)
{
$this->members = $members;
}
/**
* @ORM\Column(name="Name", type="string")
* @var string
*/
private $name;
/**
* @ORM\Column(name="Description", type="string")
* @var string
*/
private $description;
/**
* @ORM\OneToMany(targetEntity="ChatTeamMember", mappedBy="team", cascade={"persist"}, orphanRemoval=true)
* @var ChatTeamMember[]
*/
private $members;
/**
* @ORM\ManyToOne(targetEntity="models\main\Member")
* @ORM\JoinColumn(name="OwnerID", referencedColumnName="ID")
* @var Member
*/
private $owner;
/**
* @ORM\OneToMany(targetEntity="ChatTeamPushNotificationMessage", mappedBy="team", cascade={"persist"}, orphanRemoval=true)
* @var ChatTeamPushNotificationMessage[]
*/
private $messages;
/**
* @ORM\OneToMany(targetEntity="ChatTeamInvitation", mappedBy="team", cascade={"persist"}, orphanRemoval=true)
* @var ChatTeamInvitation[]
*/
private $invitations;
/**
* @return Member
*/
public function getOwner()
{
return $this->owner;
}
/**
* @return int
*/
public function getOwnerId(){
try{
return $this->owner->getId();
}
catch (\Exception $ex){
return 0;
}
}
/**
* @param mixed $owner
*/
public function setOwner($owner)
{
$this->owner = $owner;
}
/**
* @return ChatTeamPushNotificationMessage[]
*/
public function getMessages()
{
return $this->messages;
}
/**
* @return ChatTeamInvitation[]
*/
public function getInvitations()
{
return $this->invitations;
}
/**
* @param ChatTeamInvitation $invitation
*/
public function addInvitation(ChatTeamInvitation $invitation){
$this->invitations->add($invitation);
$invitation->setTeam($this);
}
/**
* @param ChatTeamPushNotificationMessage $message
*/
public function addMessage(ChatTeamPushNotificationMessage $message){
$this->messages->add($message);
$message->setTeam($this);
}
/**
* @param ChatTeamMember $team_member
*/
public function addMember(ChatTeamMember $team_member){
$this->members->add($team_member);
$team_member->setTeam($this);
}
/**
* @param Member $inviter
* @param Member $invitee
* @param string $permission
* @return ChatTeamInvitation
*/
public function createInvitation(Member $inviter, Member $invitee, $permission = ChatTeamPermission::Read){
$invitation = new ChatTeamInvitation();
$invitation->setTeam($this);
$invitation->setInviter($inviter);
$invitation->setInvitee($invitee);
$invitation->setPermission($permission);
return $invitation;
}
public function createMember(Member $member, $permission = ChatTeamPermission::Read){
$team_member = new ChatTeamMember();
$team_member->setTeam($this);
$team_member->setPermission($permission);
$team_member->setMember($member);
return $team_member;
}
/**
* @param Member $owner
* @param string $body
* @param string $priority
* @return ChatTeamPushNotificationMessage
*/
public function createMessage(Member $owner, $body, $priority = PushNotificationMessagePriority::Normal){
$message = new ChatTeamPushNotificationMessage();
$message->setTeam($this);
$message->setOwner($owner);
$message->setMessage($body);
$message->setPriority($priority);
return $message;
}
/**
* @param Member $member
* @return bool
*/
public function isMember(Member $member){
$res = $this->members->filter(function ($e) use($member){
return $e->getMember()->getId() == $member->getId();
});
return $res->count() > 0;
}
/**
* @param Member $member
* @return bool
*/
public function canPostMessages(Member $member){
$res = $this->members->filter(function ($e) use($member){
return $e->getMember()->getId() == $member->getId();
});
if($res->count() == 0) return false;
$team_member = $res->first();
return $team_member->canPostMessages();
}
/**
* @param Member $member
* @return bool
*/
public function isOwner(Member $member){
return $this->getOwnerId() == $member->getId();
}
/**
* @param Member $member
* @return bool
*/
public function isAdmin(Member $member){
$res = $this->members->filter(function ($e) use($member){
return $e->getMember()->getId() == $member->getId() && $e->isAdmin();
});
return $res->count() > 0;
}
/**
* @param Member $member
*/
public function removeMember(Member $member){
$res = $this->members->filter(function ($e) use($member){
return $e->getMember()->getId() == $member->getId();
});
if($res->count() == 0) return;
$team_member = $res->first();
$this->members->removeElement($team_member);
$team_member->setTeam(null);
}
}

View File

@ -0,0 +1,210 @@
<?php namespace models\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\utils\SilverstripeBaseModel;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="ChatTeamInvitation")
* @ORM\Entity(repositoryClass="repositories\main\DoctrineChatTeamInvitationRepository")
* Class ChatTeamInvitation
* @package models\main
*/
class ChatTeamInvitation extends SilverstripeBaseModel
{
public function __construct()
{
parent::__construct();
$this->is_accepted = false;
}
/**
* @ORM\Column(name="Permission", type="string")
* @var string
*/
private $permission;
/**
* @return string
*/
public function getPermission()
{
return $this->permission;
}
/**
* @param string $permission
*/
public function setPermission($permission)
{
$this->permission = $permission;
}
/**
* @return ChatTeam
*/
public function getTeam()
{
return $this->team;
}
/**
* @return int
*/
public function getTeamId(){
try{
return $this->team->getId();
}
catch (\Exception $ex){
return 0;
}
}
/**
* @param ChatTeam $team
*/
public function setTeam($team)
{
$this->team = $team;
}
/**
* @return Member
*/
public function getInvitee()
{
return $this->invitee;
}
/**
* @return int
*/
public function getInviteeId(){
try{
return $this->invitee->getId();
}
catch (\Exception $ex){
return 0;
}
}
/**
* @param Member $invitee
*/
public function setInvitee($invitee)
{
$this->invitee = $invitee;
}
/**
* @return Member
*/
public function getInviter()
{
return $this->inviter;
}
/**
* @return int
*/
public function getInviterId(){
try{
return $this->inviter->getId();
}
catch (\Exception $ex){
return 0;
}
}
/**
* @param Member $inviter
*/
public function setInviter($inviter)
{
$this->inviter = $inviter;
}
/**
* @ORM\Column(name="Accepted", type="boolean")
* @var bool
*/
private $is_accepted;
/**
* @ORM\Column(name="AcceptedDate", type="datetime")
* @var \DateTime
*/
private $accepted_date;
/**
* @return bool
*/
public function getIsAccepted()
{
return $this->is_accepted;
}
/**
* @return bool
*/
public function isAccepted(){
return $this->getIsAccepted();
}
public function accept()
{
$this->is_accepted = true;
$now = new \DateTime('now', new \DateTimeZone(SilverstripeBaseModel::DefaultTimeZone));
$this->accepted_date = $now;
}
/**
* @return \DateTime
*/
public function getAcceptedDate()
{
return $this->accepted_date;
}
/**
* @param \DateTime $accepted_date
*/
public function setAcceptedDate($accepted_date)
{
$this->accepted_date = $accepted_date;
}
/**
* @ORM\ManyToOne(targetEntity="models\main\ChatTeam")
* @ORM\JoinColumn(name="TeamID", referencedColumnName="ID")
* @var ChatTeam
*/
private $team;
/**
* @ORM\ManyToOne(targetEntity="models\main\Member")
* @ORM\JoinColumn(name="InviteeID", referencedColumnName="ID")
* @var Member
*/
private $invitee;
/**
* @ORM\ManyToOne(targetEntity="models\main\Member")
* @ORM\JoinColumn(name="InviterID", referencedColumnName="ID")
* @var Member
*/
private $inviter;
}

View File

@ -0,0 +1,145 @@
<?php namespace models\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="ChatTeam_Members")
* Class ChatTeamMember
* @package models\main
*/
class ChatTeamMember
{
/**
* @return Member
*/
public function getMember()
{
return $this->member;
}
/**
* @param Member $member
*/
public function setMember($member)
{
$this->member = $member;
}
/**
* @return ChatTeam
*/
public function getTeam()
{
return $this->team;
}
/**
* @param ChatTeam $team
*/
public function setTeam($team)
{
$this->team = $team;
}
/**
* @return string
*/
public function getPermission()
{
return $this->permission;
}
/**
* @return bool
*/
public function isAdmin(){
return $this->permission == ChatTeamPermission::Admin;
}
/**
* @return bool
*/
public function canPostMessages(){
return $this->isAdmin() || $this->permission == ChatTeamPermission::Write;
}
/**
* @return bool
*/
public function canDeleteMembers(){
return $this->isAdmin();
}
/**
* @param mixed $permission
*/
public function setPermission($permission)
{
$this->permission = $permission;
}
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="ID", type="integer", unique=true, nullable=false)
*/
private $id;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
public function getMemberId(){
try{
return $this->member->getId();
}
catch (\Exception $ex){
return 0;
}
}
public function getTeamId(){
try{
return $this->team->getId();
}
catch (\Exception $ex){
return 0;
}
}
/**
* @ORM\ManyToOne(targetEntity="models\main\Member")
* @ORM\JoinColumn(name="MemberID", referencedColumnName="ID")
* @var Member
*/
private $member;
/**
* @ORM\ManyToOne(targetEntity="models\main\ChatTeam")
* @ORM\JoinColumn(name="ChatTeamID", referencedColumnName="ID")
* @var ChatTeam
*/
private $team;
/**
* @ORM\Column(name="Permission", type="string")
* @var string
*/
private $permission;
}

View File

@ -0,0 +1,24 @@
<?php namespace models\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* Class ChatTeamPermission
* @package models\main
*/
final class ChatTeamPermission
{
const Read = 'READ';
const Write = 'WRITE';
const Admin = 'ADMIN';
}

View File

@ -0,0 +1,59 @@
<?php namespace models\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="ChatTeamPushNotificationMessage")
* @ORM\Entity(repositoryClass="repositories\main\DoctrineChatTeamPushNotificationMessageRepository")
* Class ChatTeamPushNotificationMessage
* @package models\summit
*/
class ChatTeamPushNotificationMessage extends PushNotificationMessage
{
const PushType = 'TEAM_MESSAGE';
/**
* @return ChatTeam
*/
public function getTeam()
{
return $this->team;
}
/**
* @return int
*/
public function getTeamId(){
try{
return $this->team->getId();
}
catch (\Exception $ex){
return 0;
}
}
/**
* @param ChatTeam $team
*/
public function setTeam($team)
{
$this->team = $team;
}
/**
* @ORM\ManyToOne(targetEntity="models\main\ChatTeam")
* @ORM\JoinColumn(name="ChatTeamID", referencedColumnName="ID")
* @var ChatTeam
*/
private $team;
}

View File

@ -0,0 +1,176 @@
<?php namespace models\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Doctrine\ORM\Mapping AS ORM;
use models\utils\SilverstripeBaseModel;
/**
* @ORM\Entity
* @ORM\Table(name="PushNotificationMessage")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="ClassName", type="string")
* @ORM\DiscriminatorMap({"PushNotificationMessage" = "PushNotificationMessage", "SummitPushNotification" = "models\summit\SummitPushNotification", "ChatTeamPushNotificationMessage" = "ChatTeamPushNotificationMessage"})
* Class PushNotificationMessage
* @package models\main
*/
class PushNotificationMessage extends SilverstripeBaseModel
{
public function __construct()
{
parent::__construct();
$this->is_sent = false;
}
/**
* @ORM\Column(name="Message", type="string")
* @var string
*/
protected $message;
/**
* @return string
*/
public function getPriority()
{
return $this->priority;
}
/**
* @param string $priority
*/
public function setPriority($priority)
{
$this->priority = $priority;
}
/**
* @ORM\Column(name="Priority", type="string")
* @var string
*/
protected $priority;
/**
* @ORM\Column(name="SentDate", type="datetime")
* @var \DateTime
*/
protected $sent_date;
/**
* @ORM\Column(name="IsSent", type="boolean")
* @var bool
*/
protected $is_sent;
/**
* @ORM\ManyToOne(targetEntity="models\main\Member")
* @ORM\JoinColumn(name="OwnerID", referencedColumnName="ID")
* @var Member
*/
protected $owner;
/**
* @return int
*/
public function getOwnerId(){
try{
return $this->owner->getId();
}
catch (\Exception $ex){
return 0;
}
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @return $this
*/
public function markSent(){
$this->is_sent = true;
$now = new \DateTime('now', new \DateTimeZone(SilverstripeBaseModel::DefaultTimeZone));
$this->sent_date = $now;
return $this;
}
/**
* @param string $message
*/
public function setMessage($message)
{
$this->message = $message;
}
/**
* @return \DateTime
*/
public function getSentDate()
{
return $this->sent_date;
}
/**
* @param \DateTime $sent_date
*/
public function setSentDate($sent_date)
{
$this->sent_date = $sent_date;
}
/**
* @return boolean
*/
public function isSent()
{
return $this->is_sent;
}
/**
* @return boolean
*/
public function getIsSent()
{
return $this->isSent();
}
/**
* @param boolean $is_sent
*/
public function setIsSent($is_sent)
{
$this->is_sent = $is_sent;
}
/**
* @return Member
*/
public function getOwner()
{
return $this->owner;
}
/**
* @param Member $owner
*/
public function setOwner($owner)
{
$this->owner = $owner;
}
}

View File

@ -0,0 +1,23 @@
<?php namespace models\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* Class PushNotificationMessagePriority
* @package models\main
*/
final class PushNotificationMessagePriority
{
const Normal = 'NORMAL';
const High = 'HIGH';
}

View File

@ -0,0 +1,26 @@
<?php namespace models\main;
/*
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\utils\IBaseRepository;
/**
* Interface IChatTeamInvitationRepository
* @package models\main
*/
interface IChatTeamInvitationRepository extends IBaseRepository
{
/**
* @param int $invitee_id
* @return ChatTeamInvitation[]
*/
function getInvitationsByInvitee($invitee_id);
}

View File

@ -0,0 +1,50 @@
<?php namespace models\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\utils\IBaseRepository;
use utils\Filter;
use utils\Order;
use utils\PagingInfo;
use utils\PagingResponse;
/**
* Interface IChatTeamPushNotificationMessageRepository
* @package models\main
*/
interface IChatTeamPushNotificationMessageRepository extends IBaseRepository
{
/**
* @param int $team_id
* @param PagingInfo $paging_info
* @param Filter|null $filter
* @param Order|null $order
* @return PagingResponse
*/
function getAllSentByTeamPaginated
(
$team_id,
PagingInfo $paging_info,
Filter $filter = null,
Order $order = null
);
/**
* @param int $team_id
* @param PagingInfo $paging_info
* @return PagingResponse
*/
function getAllNotSentByTeamPaginated
(
$team_id,
PagingInfo $paging_info
);
}

View File

@ -12,19 +12,22 @@
* limitations under the License.
**/
use Doctrine\ORM\Mapping as ORM;
use models\utils\SilverstripeBaseModel;
use models\utils\IBaseRepository;
/**
* @ORM\Entity
* @ORM\Table(name="CustomDataObject")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="ClassName", type="string")
* @ORM\DiscriminatorMap({"CustomDataObject" = "CustomDataObject", "SummitPushNotification" = "models\summit\SummitPushNotification"})
* Class CustomDataObject
* Interface IChatTeamRepository
* @package models\main
*/
abstract class CustomDataObject extends SilverstripeBaseModel
interface IChatTeamRepository extends IBaseRepository
{
/**
* @param Member $member
* @return ChatTeam[]
*/
function getTeamsByMember(Member $member);
/**
* @return int[]
*/
function getAllTeamsIdsWithPendingMessages2Sent();
}

View File

@ -14,6 +14,10 @@
**/
use models\utils\IBaseRepository;
use utils\Filter;
use utils\Order;
use utils\PagingInfo;
use utils\PagingResponse;
/**
* Interface IMemberRepository
@ -26,4 +30,12 @@ interface IMemberRepository extends IBaseRepository
* @return Member
*/
public function getByEmail($email);
/**
* @param PagingInfo $paging_info
* @param Filter|null $filter
* @param Order|null $order
* @return PagingResponse
*/
public function getAllByPage(PagingInfo $paging_info, Filter $filter = null, Order $order = null);
}

View File

@ -199,8 +199,6 @@ class SummitEvent extends SilverstripeBaseModel
*/
protected $rsvp_template_id;
/**
* @return DateTime
*/

View File

@ -14,9 +14,8 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use models\main\CustomDataObject;
use models\main\Group;
use models\main\Member;
use models\main\PushNotificationMessage;
/**
* Class SummitPushNotificationChannel
@ -47,6 +46,7 @@ final class SummitPushNotificationChannel {
return in_array($channel, self::getPublicChannels());
}
}
/**
* @ORM\Entity
* @ORM\Table(name="SummitPushNotification")
@ -54,7 +54,7 @@ final class SummitPushNotificationChannel {
* Class SummitPushNotification
* @package models\summit
*/
class SummitPushNotification extends CustomDataObject
class SummitPushNotification extends PushNotificationMessage
{
use SummitOwned;
@ -80,78 +80,6 @@ class SummitPushNotification extends CustomDataObject
$this->channel = $channel;
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @param string $message
*/
public function setMessage($message)
{
$this->message = $message;
}
/**
* @return \DateTime
*/
public function getSentDate()
{
return $this->sent_date;
}
/**
* @param \DateTime $sent_date
*/
public function setSentDate($sent_date)
{
$this->sent_date = $sent_date;
}
/**
* @return boolean
*/
public function isSent()
{
return $this->is_sent;
}
/**
* @return boolean
*/
public function getIsSent()
{
return $this->isSent();
}
/**
* @param boolean $is_sent
*/
public function setIsSent($is_sent)
{
$this->is_sent = $is_sent;
}
/**
* @return Member
*/
public function getOwner()
{
return $this->owner;
}
/**
* @param Member $owner
*/
public function setOwner($owner)
{
$this->owner = $owner;
}
/**
* @return SummitEvent
*/
@ -200,31 +128,6 @@ class SummitPushNotification extends CustomDataObject
$this->recipients = $recipients;
}
/**
* @ORM\Column(name="Message", type="string")
* @var string
*/
private $message;
/**
* @ORM\Column(name="SentDate", type="datetime")
* @var \DateTime
*/
private $sent_date;
/**
* @ORM\Column(name="IsSent", type="boolean")
* @var bool
*/
private $is_sent;
/**
* @ORM\ManyToOne(targetEntity="models\main\Member")
* @ORM\JoinColumn(name="OwnerID", referencedColumnName="ID")
* @var Member
*/
private $owner;
/**
* @ORM\ManyToOne(targetEntity="models\summit\SummitEvent")
* @ORM\JoinColumn(name="EventID", referencedColumnName="ID")

View File

@ -16,7 +16,7 @@ use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Registry;
use LaravelDoctrine\ORM\Facades\Registry;
/***
* @ORM\MappedSuperclass
@ -90,7 +90,6 @@ class SilverstripeBaseModel implements IEntity
public function __construct()
{
$now = new \DateTime('now', new \DateTimeZone(self::DefaultTimeZone));
$this->created = $now;
$this->last_edited = $now;
@ -116,7 +115,6 @@ class SilverstripeBaseModel implements IEntity
* @return mixed
*/
protected function prepareRawSQL($sql){
return Registry::getManager(self::EntityManager)->getConnection()->prepare($sql);
}

View File

@ -5,6 +5,8 @@ use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use models\main\ChatTeamPermission;
use models\main\PushNotificationMessagePriority;
use Monolog\Handler\NativeMailerHandler;
/**
@ -70,6 +72,22 @@ class AppServiceProvider extends ServiceProvider
}
return true;
});
Validator::extend('team_permission', function($attribute, $value, $parameters, $validator)
{
$validator->addReplacer('team_permission', function($message, $attribute, $rule, $parameters) use ($validator) {
return sprintf("%s should be a valid permission value (ADMIN, WRITE, READ)", $attribute);
});
return in_array($value, [ChatTeamPermission::Read, ChatTeamPermission::Write, ChatTeamPermission::Admin]);
});
Validator::extend('chat_message_priority', function($attribute, $value, $parameters, $validator)
{
$validator->addReplacer('chat_message_priority', function($message, $attribute, $rule, $parameters) use ($validator) {
return sprintf("%s should be a valid message priority value (NORMAL, HIGH)", $attribute);
});
return in_array($value, [ PushNotificationMessagePriority::Normal, PushNotificationMessagePriority::High]);
});
}
/**

View File

@ -0,0 +1,40 @@
<?php namespace repositories\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\main\ChatTeamInvitation;
use models\main\IChatTeamInvitationRepository;
use repositories\SilverStripeDoctrineRepository;
/**
* Class DoctrineChatTeamInvitationRepository
* @package repositories\main
*/
final class DoctrineChatTeamInvitationRepository
extends SilverStripeDoctrineRepository
implements IChatTeamInvitationRepository
{
/**
* @param int $invitee_id
* @return ChatTeamInvitation[]
*/
function getInvitationsByInvitee($invitee_id)
{
return $this->getEntityManager()
->createQueryBuilder()
->select("i")
->from(\models\main\ChatTeamInvitation::class, "i")
->innerJoin('i.invitee', 'iv')
->innerJoin('iv.id', 'm', Join::WITH, "m.id = :member_id")
->setParameter('member_id', $invitee_id)->getQuery()->getResult();
}
}

View File

@ -0,0 +1,137 @@
<?php namespace repositories\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Doctrine\ORM\Tools\Pagination\Paginator;
use models\main\IChatTeamPushNotificationMessageRepository;
use repositories\SilverStripeDoctrineRepository;
use utils\DoctrineJoinFilterMapping;
use utils\Filter;
use utils\Order;
use utils\PagingInfo;
use utils\PagingResponse;
/**
* Class DoctrineChatTeamPushNotificationMessageRepository
* @package repositories\main
*/
final class DoctrineChatTeamPushNotificationMessageRepository
extends SilverStripeDoctrineRepository
implements IChatTeamPushNotificationMessageRepository
{
/**
* @param int $team_id
* @param PagingInfo $paging_info
* @param Filter|null $filter
* @param Order|null $order
* @return PagingResponse
*/
function getAllSentByTeamPaginated($team_id, PagingInfo $paging_info, Filter $filter = null, Order $order = null)
{
$query = $this->getEntityManager()->createQueryBuilder()
->select("m")
->from(\models\main\ChatTeamPushNotificationMessage::class, "m")
->join('m.team', 't')
->where('m.is_sent = 1')
->andWhere('t.id = :team_id')
->setParameter('team_id', $team_id);
if(!is_null($filter)){
$filter->apply2Query($query, array
(
'sent_date' => 'm.sent_date:datetime_epoch',
'owner_id' => new DoctrineJoinFilterMapping
(
'm.owner',
'mb',
"mb.id :operator :value"
),
));
}
if (!is_null($order)) {
$order->apply2Query($query, array
(
'sent_date' => 'm.sent_date',
'id' => 'm.id',
));
} else {
//default order
$query = $query->addOrderBy("m.sent_date",'ASC');
$query = $query->addOrderBy("m.id", 'ASC');
}
$query =
$query
->setFirstResult($paging_info->getOffset())
->setMaxResults($paging_info->getPerPage());
$paginator = new Paginator($query, $fetchJoinCollection = true);
$total = $paginator->count();
$data = array();
foreach($paginator as $entity)
array_push($data, $entity);
return new PagingResponse
(
$total,
$paging_info->getPerPage(),
$paging_info->getCurrentPage(),
$paging_info->getLastPage($total),
$data
);
}
/**
* @param int $team_id
* @param PagingInfo $paging_info
* @return PagingResponse
*/
function getAllNotSentByTeamPaginated
(
$team_id,
PagingInfo $paging_info
)
{
$query = $this->getEntityManager()->createQueryBuilder()
->select("m")
->from(\models\main\ChatTeamPushNotificationMessage::class, "m")
->join('m.team', 't')
->where('m.is_sent = 0')
->andWhere('t.id = :team_id')
->setParameter('team_id', $team_id);
$query =
$query
->setFirstResult($paging_info->getOffset())
->setMaxResults($paging_info->getPerPage());
$paginator = new Paginator($query, $fetchJoinCollection = true);
$total = $paginator->count();
$data = array();
foreach($paginator as $entity)
array_push($data, $entity);
return new PagingResponse
(
$total,
$paging_info->getPerPage(),
$paging_info->getCurrentPage(),
$paging_info->getLastPage($total),
$data
);
}
}

View File

@ -0,0 +1,53 @@
<?php namespace repositories\main;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\main\ChatTeam;
use models\main\IChatTeamRepository;
use models\main\Member;
use repositories\SilverStripeDoctrineRepository;
use Doctrine\ORM\Query\Expr\Join;
/**
* Class DoctrineChatTeamRepository
* @package repositories\main
*/
final class DoctrineChatTeamRepository extends SilverStripeDoctrineRepository implements IChatTeamRepository
{
/**
* @param Member $member
* @return ChatTeam[]
*/
function getTeamsByMember(Member $member)
{
return $this->getEntityManager()
->createQueryBuilder()
->select("t")
->from(\models\main\ChatTeam::class, "t")
->innerJoin('t.members', 'tm')
->innerJoin('tm.member', 'm', Join::WITH, "m.id = :member_id")
->setParameter('member_id', $member->getId())->getQuery()->getResult();
}
/**
* @return int[]
*/
function getAllTeamsIdsWithPendingMessages2Sent()
{
$result = $this
->getEntityManager()
->createQuery("select t.id from \models\main\ChatTeam t join t.messages m where exists (select m2.id from \models\main\ChatTeamPushNotificationMessage m2 where m2.id = m.id and m2.is_sent = 0 )")
->getScalarResult();
$ids = array_map('current', $result);
return $ids;
}
}

View File

@ -1,5 +1,4 @@
<?php namespace repositories;
/**
* Copyright 2015 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,16 +11,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
use LaravelDoctrine\ORM\Facades\EntityManager;
/**
* Class RepositoriesProvider
* @package repositories
*/
class RepositoriesProvider extends ServiceProvider
final class RepositoriesProvider extends ServiceProvider
{
protected $defer = false;
@ -111,5 +108,23 @@ class RepositoriesProvider extends ServiceProvider
function(){
return EntityManager::getRepository(\models\main\Tag::class);
});
App::singleton(
'models\main\IChatTeamRepository',
function(){
return EntityManager::getRepository(\models\main\ChatTeam::class);
});
App::singleton(
'models\main\IChatTeamInvitationRepository',
function(){
return EntityManager::getRepository(\models\main\ChatTeamInvitation::class);
});
App::singleton(
'models\main\IChatTeamPushNotificationMessageRepository',
function(){
return EntityManager::getRepository(\models\main\ChatTeamPushNotificationMessage::class);
});
}
}

View File

@ -12,9 +12,14 @@
* limitations under the License.
**/
use Doctrine\ORM\Tools\Pagination\Paginator;
use models\main\IMemberRepository;
use models\main\Member;
use repositories\SilverStripeDoctrineRepository;
use utils\Filter;
use utils\Order;
use utils\PagingInfo;
use utils\PagingResponse;
/**
* Class DoctrineMemberRepository
@ -31,4 +36,62 @@ final class DoctrineMemberRepository extends SilverStripeDoctrineRepository impl
{
// TODO: Implement getByEmail() method.
}
/**
* @param PagingInfo $paging_info
* @param Filter|null $filter
* @param Order|null $order
* @return PagingResponse
*/
public function getAllByPage(PagingInfo $paging_info, Filter $filter = null, Order $order = null)
{
$query = $this->getEntityManager()->createQueryBuilder()
->select("m")
->from(\models\main\Member::class, "m");
if(!is_null($filter)){
$filter->apply2Query($query, array
(
'irc' => 'm.irc_handle:json_string',
'twitter' => 'm.twitter_handle:json_string',
'first_name' => 'm.first_name:json_string',
'last_name' => 'm.last_name:json_string',
));
}
if (!is_null($order)) {
$order->apply2Query($query, array
(
'id' => 'm.id',
'first_name' => 'm.first_name',
'last_name' => 'm.last_name',
));
} else {
//default order
$query = $query->addOrderBy("m.first_name",'ASC');
$query = $query->addOrderBy("m.last_name", 'ASC');
}
$query= $query
->setFirstResult($paging_info->getOffset())
->setMaxResults($paging_info->getPerPage());
$paginator = new Paginator($query, $fetchJoinCollection = true);
$total = $paginator->count();
$data = array();
foreach($paginator as $entity)
array_push($data, $entity);
return new PagingResponse
(
$total,
$paging_info->getPerPage(),
$paging_info->getCurrentPage(),
$paging_info->getLastPage($total),
$data
);
}
}

View File

@ -0,0 +1,77 @@
<?php namespace services\apis;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use GuzzleHttp\Client;
use models\main\PushNotificationMessagePriority;
use Illuminate\Support\Facades\Log;
/**
* Class FireBaseGCMApi
* @package services\apis
*/
final class FireBaseGCMApi implements IPushNotificationApi
{
const BaseUrl = 'https://fcm.googleapis.com';
/**
* @var string
*/
private $api_server_key;
public function __construct($api_server_key)
{
$this->api_server_key = $api_server_key;
}
/**
* https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
* @param string $to
* @param array $data
* @param string $priority
* @param null|int $ttl
* @return bool
*/
function sendPush($to, array $data, $priority = PushNotificationMessagePriority::Normal, $ttl = null)
{
$endpoint = self::BaseUrl.'/fcm/send';
$client = new Client();
$res = true;
try {
foreach ($to as $recipient) {
$message = [
'to' => '/topics/' . $recipient,
'data' => $data,
];
$response = $client->post($endpoint, [
'headers' => [
'Authorization' => sprintf('key=%s', $this->api_server_key),
'Content-Type' => 'application/json'
],
'body' => json_encode($message)
]);
if ($response->getStatusCode() !== 200) $res = $res && false;
}
return $res;
}
catch (\GuzzleHttp\Exception\ClientException $ex1){
Log::error($ex1->getMessage());
return false;
}
catch(\Exception $ex){
Log::error($ex->getMessage());
return false;
}
}
}

View File

@ -0,0 +1,29 @@
<?php namespace services\apis;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\main\PushNotificationMessagePriority;
/**
* Interface IPushNotificationApi
* @package services\apis
*/
interface IPushNotificationApi
{
/**
* @param string $to
* @param array $data
* @param string $priority
* @param null|int $ttl
* @return bool
*/
function sendPush($to, array $data, $priority = PushNotificationMessagePriority::Normal, $ttl = null);
}

View File

@ -0,0 +1,377 @@
<?php namespace services\model;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use libs\utils\ITransactionService;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\main\ChatTeam;
use models\main\ChatTeamInvitation;
use models\main\ChatTeamMember;
use models\main\ChatTeamPermission;
use models\main\ChatTeamPushNotificationMessage;
use models\main\IChatTeamInvitationRepository;
use models\main\IChatTeamPushNotificationMessageRepository;
use models\main\IChatTeamRepository;
use models\main\IMemberRepository;
use models\main\Member;
use models\oauth2\IResourceServerContext;
use services\apis\IPushNotificationApi;
use utils\PagingInfo;
/**
* Class ChatTeamService
* @package services\model
*/
final class ChatTeamService implements IChatTeamService
{
/**
* @var ITransactionService
*/
private $tx_service;
/**
* @var IChatTeamRepository
*/
private $repository;
/**
* @var IMemberRepository
*/
private $member_repository;
/**
* @var IChatTeamInvitationRepository
*/
private $invitation_repository;
/**
* @var IChatTeamPushNotificationMessageRepository
*/
private $chat_message_repository;
/**
* @var IResourceServerContext
*/
private $resource_server_context;
/**
* @var IPushNotificationApi
*/
private $push_sender_service;
public function __construct
(
IMemberRepository $member_repository,
IChatTeamInvitationRepository $invitation_repository,
IChatTeamPushNotificationMessageRepository $chat_message_repository,
IChatTeamRepository $repository,
IResourceServerContext $resource_server_context,
IPushNotificationApi $push_sender_service,
ITransactionService $tx_service
)
{
$this->invitation_repository = $invitation_repository;
$this->chat_message_repository = $chat_message_repository;
$this->repository = $repository;
$this->member_repository = $member_repository;
$this->resource_server_context = $resource_server_context;
$this->push_sender_service = $push_sender_service;
$this->tx_service = $tx_service;
}
/**
* @param array $data
* @param Member $owner
* @return ChatTeam
*/
function addTeam(array $data, Member $owner)
{
return $this->tx_service->transaction(function () use($data, $owner){
$team = new ChatTeam();
$team->setName($data['name']);
if(isset($data['description']))
$team->setDescription($data['description']);
$team->setOwner($owner);
$team_member = $team->createMember($owner, ChatTeamPermission::Admin);
$team->addMember($team_member);
$this->repository->add($team);
return $team;
});
}
/**
* @param array $data
* @param int $team_id
* @return ChatTeam
*/
function updateTeam(array $data, $team_id){
return $this->tx_service->transaction(function () use($data, $team_id){
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) throw new EntityNotFoundException();
$current_member = $this->member_repository->getById($current_member_id);
if (is_null($current_member)) throw new EntityNotFoundException();
$team = $this->repository->getById($team_id);
if(is_null($team)) throw new EntityNotFoundException();
if(!$team->isAdmin($current_member))
throw new EntityNotFoundException();
$team->setName($data['name']);
if(isset($data['description']))
$team->setDescription($data['description']);
$this->repository->add($team);
return $team;
});
}
/**
* @param int $team_id
* @return void
* @throws EntityNotFoundException
*/
function deleteTeam($team_id)
{
$this->tx_service->transaction(function() use($team_id){
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) throw new EntityNotFoundException();
$current_member = $this->member_repository->getById($current_member_id);
if (is_null($current_member)) throw new EntityNotFoundException();
$team = $this->repository->getById($team_id);
if(is_null($team)) throw new EntityNotFoundException();
if(!$team->isAdmin($current_member))
throw new EntityNotFoundException();
$this->repository->delete($team);
});
}
/**
* @param int $team_id
* @param int $invitee_id
* @param string $permission
* @throws EntityNotFoundException
* @throws ValidationException
* @return ChatTeamInvitation
*/
function addMember2Team($team_id, $invitee_id, $permission = ChatTeamPermission::Read)
{
return $this->tx_service->transaction(function() use($team_id, $invitee_id, $permission){
$team = $this->repository->getById($team_id);
if(is_null($team)) throw new EntityNotFoundException();
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) throw new EntityNotFoundException();
$inviter = $this->member_repository->getById($current_member_id);
if (is_null($inviter)) throw new EntityNotFoundException();
$invitee = $this->member_repository->getById($invitee_id);
if(is_null($invitee))
throw new EntityNotFoundException();
if(!$team->isAdmin($inviter))
throw new EntityNotFoundException();
if($team->isMember($invitee))
throw new ValidationException(sprintf('member id %s already is a member of team id %s', $invitee_id, $team_id));
$invitation = $team->createInvitation($inviter, $invitee, $permission);
$team->addInvitation($invitation);
$this->repository->add($team);
return $invitation;
});
}
/**
* @param int $invitation_id
* @param int $invitee_id
* @return ChatTeamMember
* @throws EntityNotFoundException
* @throws ValidationException
*/
function acceptInvitation($invitation_id, $invitee_id)
{
return $this->tx_service->transaction(function() use($invitation_id, $invitee_id){
$invitee = $this->member_repository->getById($invitee_id);
if(is_null($invitee))
throw new EntityNotFoundException();
$invitation = $this->invitation_repository->getById($invitation_id);
if(is_null($invitation))
throw new EntityNotFoundException();
if($invitation->getInviteeId() != $invitee_id)
throw new EntityNotFoundException();
if($invitation->isAccepted())
throw new ValidationException(sprintf('invitation id %s is already accepted!', $invitee_id));
$invitation->accept();
$team = $invitation->getTeam();
if($team->isMember($invitee))
throw new ValidationException(sprintf('invitee id %s is already member of team id %s!', $invitee_id, $team->getId()));
$team_member = $team->createMember($invitee, $invitation->getPermission());
$team->addMember($team_member);
return $team_member;
});
}
/**
* @param int $invitation_id
* @param int $invitee_id
* @return void
* @throws EntityNotFoundException
* @throws ValidationException
*/
function declineInvitation($invitation_id, $invitee_id)
{
$this->tx_service->transaction(function() use($invitation_id, $invitee_id){
$invitee = $this->member_repository->getById($invitee_id);
if(is_null($invitee))
throw new EntityNotFoundException();
$invitation = $this->invitation_repository->getById($invitation_id);
if(is_null($invitation))
throw new EntityNotFoundException();
if($invitation->getInviteeId() != $invitee_id)
throw new EntityNotFoundException();
if($invitation->isAccepted())
throw new ValidationException(sprintf('invitation id %s is already accepted!', $invitee_id));
$this->invitation_repository->delete($invitation);
});
}
/**
* @param int $team_id
* @param int $member_id
* @return void
* @throws EntityNotFoundException
* @throws ValidationException
*/
function removeMemberFromTeam($team_id, $member_id)
{
$this->tx_service->transaction(function() use($member_id, $team_id){
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) throw new EntityNotFoundException();
$current_member = $this->member_repository->getById($current_member_id);
if (is_null($current_member)) throw new EntityNotFoundException();
$team_member = $this->member_repository->getById($member_id);
if (is_null($team_member)) throw new EntityNotFoundException();
$team = $this->repository->getById($team_id);
if(is_null($team)) throw new EntityNotFoundException();
if(!$team->isAdmin($current_member))
throw new EntityNotFoundException();
if(!$team->isMember($team_member))
throw new ValidationException(sprintf('member id %s is not a member of team id %s', $member_id, $team_id));
$team->removeMember($team_member);
});
}
/**
* @param int $team_id
* @param array $values
* @return ChatTeamPushNotificationMessage
* @throws EntityNotFoundException
* @throws ValidationException
*/
function postMessage($team_id, array $values)
{
return $this->tx_service->transaction(function() use($team_id, $values){
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id)) throw new EntityNotFoundException();
$current_member = $this->member_repository->getById($current_member_id);
if (is_null($current_member)) throw new EntityNotFoundException();
$team = $this->repository->getById($team_id);
if(is_null($team)) throw new EntityNotFoundException();
if(!$team->canPostMessages($current_member))
throw new ValidationException(sprintf('you do not have permissions to post messages to team id %s', $team_id));
$message = $team->createMessage($current_member, $values['body'], $values['priority']);
$team->addMessage($message) ;
return $message;
});
}
/**
* @param int $batch_size
* @return int
*/
function sendMessages($batch_size = 1000)
{
return $this->tx_service->transaction(function() use($batch_size){
$teams_ids = $this->repository->getAllTeamsIdsWithPendingMessages2Sent();
$qty = 0;
foreach($teams_ids as $team_id) {
$messages = $this->chat_message_repository->getAllNotSentByTeamPaginated
(
$team_id,
new PagingInfo(1, $batch_size)
);
foreach ($messages->getItems() as $message){
$data = [
'id' => intval($message->getId()),
'type' => ChatTeamPushNotificationMessage::PushType,
'body' => $message->getMessage(),
'from_id' => intval($message->getOwner()->getId()),
'from_first_name' => $message->getOwner()->getFirstName(),
'from_last_name' => $message->getOwner()->getLastName(),
'created_at' => intval($message->getCreated()->getTimestamp()),
];
$this->push_sender_service->sendPush([sprintf('team_%s', $team_id)], $data);
$message->markSent();
++$qty;
}
}
return $qty;
});
}
}

View File

@ -0,0 +1,102 @@
<?php namespace services\model;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\main\ChatTeamInvitation;
use models\main\ChatTeam;
use models\main\ChatTeamMember;
use models\main\ChatTeamPermission;
use models\main\ChatTeamPushNotificationMessage;
use models\main\Member;
/**
* Interface IChatTeamService
* @package services\model
*/
interface IChatTeamService
{
/**
* @param array $data
* @param Member $owner
* @return ChatTeam
*/
function addTeam(array $data, Member $owner);
/**
* @param array $data
* @param int $team_id
* @return ChatTeam
*/
function updateTeam(array $data, $team_id);
/**
* @param int $team_id
* @return void
* @throws EntityNotFoundException
*/
function deleteTeam($team_id);
/**
* @param int $team_id
* @param int $invitee_id
* @param string $permission
* @throws EntityNotFoundException
* @throws ValidationException
* @return ChatTeamInvitation
*/
function addMember2Team($team_id, $invitee_id, $permission = ChatTeamPermission::Read);
/**
* @param int $invitation_id
* @param int $invitee_id
* @return ChatTeamMember
* @throws EntityNotFoundException
* @throws ValidationException
*/
function acceptInvitation($invitation_id, $invitee_id);
/**
* @param int $invitation_id
* @param int $invitee_id
* @return void
* @throws EntityNotFoundException
* @throws ValidationException
*/
function declineInvitation($invitation_id, $invitee_id);
/**
* @param int $team_id
* @param int $member_id
* @return void
* @throws EntityNotFoundException
* @throws ValidationException
*/
function removeMemberFromTeam($team_id, $member_id);
/**
* @param int $team_id
* @param array $values
* @return ChatTeamPushNotificationMessage
* @throws EntityNotFoundException
* @throws ValidationException
*/
function postMessage($team_id, array $values);
/**
* @param int $batch_size
* @return int
*/
function sendMessages($batch_size = 1000);
}

View File

@ -1,5 +1,4 @@
<?php namespace services;
/**
* Copyright 2015 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,12 +11,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App;
use Config;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use services\apis\EventbriteAPI;
use services\apis\FireBaseGCMApi;
/***
* Class ServicesProvider
* @package services
@ -38,10 +36,15 @@ class ServicesProvider extends ServiceProvider
});
App::singleton('services\model\ISummitService', 'services\model\SummitService');
App::singleton('services\model\IPresentationService', 'services\model\PresentationService');
App::singleton('services\model\IChatTeamService', 'services\model\ChatTeamService');
App::singleton('services\apis\IEventbriteAPI', function(){
$api = new EventbriteAPI();
$api->setCredentials(array('token' => Config::get("server.eventbrite_oauth2_personal_token", null)));
return $api;
});
App::singleton('services\apis\IPushNotificationApi', function(){
$api = new FireBaseGCMApi(Config::get("server.firebase_gcm_server_key", null));
return $api;
});
}
}

View File

@ -21,4 +21,5 @@ return array
'assets_base_url' => env('ASSETS_BASE_URL', null),
'response_cache_lifetime' => env('API_RESPONSE_CACHE_LIFETIME', 300),
'eventbrite_oauth2_personal_token' => env('EVENTBRITE_OAUTH2_PERSONAL_TOKEN', ''),
'firebase_gcm_server_key' => env('FIREBASE_GCM_SERVER_KEY', ''),
);

View File

@ -27,10 +27,13 @@ class ApiEndpointsSeeder extends Seeder
{
DB::table('endpoint_api_scopes')->delete();
DB::table('api_endpoints')->delete();
$this->seedPublicCloudsEndpoints();
$this->seedPrivateCloudsEndpoints();
$this->seedConsultantsEndpoints();
$this->seedSummitEndpoints();
$this->seedMemberEndpoints();
$this->seedTeamEndpoints();
}
/**
@ -465,4 +468,98 @@ class ApiEndpointsSeeder extends Seeder
}
private function seedMemberEndpoints(){
$current_realm = Config::get('app.url');
$this->seedApiEndpoints('members', [
// members
array(
'name' => 'get-members',
'route' => '/api/v1/members',
'http_method' => 'GET',
'scopes' => [sprintf('%s/members/read', $current_realm)],
)
]
);
}
private function seedTeamEndpoints(){
$current_realm = Config::get('app.url');
$this->seedApiEndpoints('teams', [
array(
'name' => 'add-team',
'route' => '/api/v1/teams',
'http_method' => 'POST',
'scopes' => [sprintf('%s/teams/write', $current_realm)],
),
array(
'name' => 'delete-team',
'route' => '/api/v1/teams/{team_id}',
'http_method' => 'DELETE',
'scopes' => [sprintf('%s/teams/write', $current_realm)],
),
array(
'name' => 'get-teams',
'route' => '/api/v1/teams',
'http_method' => 'GET',
'scopes' => [sprintf('%s/teams/read', $current_realm)],
),
array(
'name' => 'get-team',
'route' => '/api/v1/teams/{team_id}',
'http_method' => 'GET',
'scopes' => [sprintf('%s/teams/read', $current_realm)],
),
array(
'name' => 'post-message-2-team',
'route' => '/api/v1/teams/{team_id}/messages',
'http_method' => 'POST',
'scopes' => [sprintf('%s/teams/write', $current_realm)],
),
array(
'name' => 'get-messages-from-team',
'route' => '/api/v1/teams/{team_id}/messages',
'http_method' => 'GET',
'scopes' => [sprintf('%s/teams/read', $current_realm)],
),
array(
'name' => 'add-member-2-team',
'route' => '/api/v1/teams/{team_id}/members/{member_id}',
'http_method' => 'POST',
'scopes' => [sprintf('%s/teams/write', $current_realm)],
),
array(
'name' => 'remove-member-from-team',
'route' => '/api/v1/teams/{team_id}/members/{member_id}',
'http_method' => 'DELETE',
'scopes' => [sprintf('%s/teams/write', $current_realm)],
),
]
);
$this->seedApiEndpoints('members', [
array(
'name' => 'get-invitations',
'route' => '/api/v1/members/me/team-invitations',
'http_method' => 'GET',
'scopes' => [sprintf('%s/teams/read', $current_realm)],
),
array(
'name' => 'accept-invitation',
'route' => '/api/v1/members/me/team-invitations/{invitation_id}',
'http_method' => 'PATCH',
'scopes' => [sprintf('%s/teams/write', $current_realm)],
),
array(
'name' => 'decline-invitation',
'route' => '/api/v1/members/me/team-invitations/{invitation_id}',
'http_method' => 'DELETE',
'scopes' => [sprintf('%s/teams/write', $current_realm)],
),
]
);
}
}

View File

@ -33,6 +33,8 @@ final class ApiScopesSeeder extends Seeder
$this->seedPrivateCloudScopes();
$this->seedConsultantScopes();
$this->seedSummitScopes();
$this->seedMembersScopes();
$this->seedTeamsScopes();
}
private function seedPublicCloudScopes()
@ -163,4 +165,61 @@ final class ApiScopesSeeder extends Seeder
}
private function seedMembersScopes(){
$current_realm = Config::get('app.url');
$api = EntityManager::getRepository(\App\Models\ResourceServer\Api::class)->findOneBy(['name' => 'members']);
$scopes = [
array(
'name' => sprintf('%s/members/read', $current_realm),
'short_description' => 'Get Members Data',
'description' => 'Grants read only access for Members Data',
),
];
foreach ($scopes as $scope_info) {
$scope = new ApiScope();
$scope->setName($scope_info['name']);
$scope->setShortDescription($scope_info['short_description']);
$scope->setDescription($scope_info['description']);
$scope->setActive(true);
$scope->setDefault(false);
$scope->setApi($api);
EntityManager::persist($scope);
}
EntityManager::flush();
}
private function seedTeamsScopes(){
$current_realm = Config::get('app.url');
$api = EntityManager::getRepository(\App\Models\ResourceServer\Api::class)->findOneBy(['name' => 'teams']);
$scopes = [
array(
'name' => sprintf('%s/teams/read', $current_realm),
'short_description' => 'Get Teams Data',
'description' => 'Grants read only access for Teams Data',
),
array(
'name' => sprintf('%s/teams/write', $current_realm),
'short_description' => 'Write Teams Data',
'description' => 'Grants write access for Teams Data',
),
];
foreach ($scopes as $scope_info) {
$scope = new ApiScope();
$scope->setName($scope_info['name']);
$scope->setShortDescription($scope_info['short_description']);
$scope->setDescription($scope_info['description']);
$scope->setActive(true);
$scope->setDefault(false);
$scope->setApi($api);
EntityManager::persist($scope);
}
EntityManager::flush();
}
}

View File

@ -68,5 +68,27 @@ final class ApiSeeder extends Seeder
EntityManager::flush();
// members
$api = new Api();
$api->setName('members');
$api->setActive(true);
$api->setDescription('Members API');
EntityManager::persist($api);
EntityManager::flush();
// teams
$api = new Api();
$api->setName('teams');
$api->setActive(true);
$api->setDescription('Teams API');
EntityManager::persist($api);
EntityManager::flush();
}
}

View File

@ -0,0 +1,277 @@
<?php
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
final class OAuth2ChatTeamApiTest extends ProtectedApiTest
{
public function testAddTeam()
{
$params = [];
$data = [
'name' => 'team test #1',
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"POST",
"OAuth2TeamsApiController@addTeam",
$params,
array(),
array(),
array(),
$headers,
json_encode($data)
);
$content = $response->getContent();
$team = json_decode($content);
$this->assertTrue(!is_null($team));
$this->assertResponseStatus(201);
return $team;
}
public function testDeleteMyTeam(){
$team = $this->testAddTeam();
$params = ['team_id' => $team->id];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
];
$response = $this->action(
"DELETE",
"OAuth2TeamsApiController@deleteTeam",
$params,
array(),
array(),
array(),
$headers
);
$this->assertResponseStatus(204);
}
public function testGetMyTeams(){
$params = ['expand' => 'owner, member'];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
];
$response = $this->action(
"GET",
"OAuth2TeamsApiController@getMyTeams",
$params,
array(),
array(),
array(),
$headers
);
$content = $response->getContent();
$teams = json_decode($content);
$this->assertTrue(!is_null($teams));
$this->assertResponseStatus(200);
}
public function testGetMyTeam($team_id = null, $expected_http_response = 200){
if($team_id == null) {
$team = $this->testAddTeam();
$team_id = $team->id;
}
$params = ['team_id' => $team_id];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
];
$response = $this->action(
"GET",
"OAuth2TeamsApiController@getMyTeam",
$params,
array(),
array(),
array(),
$headers
);
$content = $response->getContent();
$team = json_decode($content);
$this->assertTrue(!is_null($team));
$this->assertResponseStatus($expected_http_response);
}
public function testAddMemberToTeam(){
$team = $this->testAddTeam();
$params = [
'member_id' => 11624,
'team_id' => $team->id,
];
$data = [
'permission' => \models\main\ChatTeamPermission::Read,
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"POST",
"OAuth2TeamsApiController@addMember2MyTeam",
$params,
array(),
array(),
array(),
$headers,
json_encode($data)
);
$content = $response->getContent();
$invitation = json_decode($content);
$this->assertTrue(!is_null($invitation));
$this->assertResponseStatus(201);
return $invitation;
}
public function testAcceptInvitation(){
$invitation = $this->testAddMemberToTeam();
$params = [
'invitation_id' => $invitation->id,
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
];
$response = $this->action(
"PATCH",
"OAuth2TeamInvitationsApiController@acceptInvitation",
$params,
array(),
array(),
array(),
$headers
);
$content = $response->getContent();
$team_member = json_decode($content);
$this->assertTrue(!is_null($team_member));
$this->assertResponseStatus(201);
return $team_member;
}
public function testPostMessage($team = null){
if(is_null($team))
$team = $this->testAddTeam();
$params = [
'team_id' => $team->id,
];
$data = [
'body' => 'test message',
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"POST",
"OAuth2TeamsApiController@postTeamMessage",
$params,
array(),
array(),
array(),
$headers,
json_encode($data)
);
$content = $response->getContent();
$message = json_decode($content);
$this->assertTrue(!is_null($message));
$this->assertResponseStatus(201);
return $message;
}
public function testGetMessagesFromTeam(){
$team = $this->testAddTeam();
$message = $this->testPostMessage($team);
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
];
$params = [
'team_id' => $team->id,
'expand' => 'team,owner',
];
$response = $this->action(
"GET",
"OAuth2TeamsApiController@getMyTeamMessages",
$params,
array(),
array(),
array(),
$headers
);
$content = $response->getContent();
$messages = json_decode($content);
$this->assertTrue(!is_null($messages));
$this->assertResponseStatus(200);
return $messages;
}
public function testRemoveMemberFromTeam(){
$team = $this->testAddTeam();
$params = [
'team_id' => $team->id,
'member_id' => $team->owner->id,
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
];
$response = $this->action(
"DELETE",
"OAuth2TeamsApiController@removedMemberFromMyTeam",
$params,
array(),
array(),
array(),
$headers
);
$this->assertResponseStatus(204);
// try to get team again, we are not longer members , so will return 404
$this->testGetMyTeam($team->id, 404);
}
}

View File

@ -0,0 +1,45 @@
<?php
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
final class OAuth2MembersApiTest extends ProtectedApiTest
{
public function testGetMembers()
{
$params = [
//AND FILTER
'filter' => 'first_name=@Seba',
'filter' => 'last_name=@Marcet',
'order' => '+first_name,-last_name'
];
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2MembersApiController@getMembers",
$params,
array(),
array(),
array(),
$headers
);
$content = $response->getContent();
$members = json_decode($content);
$this->assertTrue(!is_null($members));
$this->assertResponseStatus(200);
}
}

View File

@ -48,6 +48,9 @@ class AccessTokenServiceStub implements IAccessTokenService
$url . '/summits/write-videos',
$url . '/me/read',
$url . '/summits/read-notifications',
$url . '/members/read',
$url . '/teams/read',
$url . '/teams/write',
);
return AccessToken::createFromParams('123456789', implode(' ', $scopes), '1', $realm, '1','13867', 3600, 'WEB_APPLICATION', '', '');
@ -83,6 +86,9 @@ class AccessTokenServiceStub2 implements IAccessTokenService
$url . '/summits/write-videos',
$url . '/me/read',
$url . '/summits/read-notifications',
$url . '/members/read',
$url . '/teams/read',
$url . '/teams/write',
);
return AccessToken::createFromParams('123456789', implode(' ', $scopes), '1', $realm, null,null, 3600, 'SERVICE', '', '');