|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014, Tobia De Koninck hey--at--ledfan.be |
|
4
|
|
|
* This file is licensed under the AGPL version 3 or later. |
|
5
|
|
|
* See the COPYING file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace OCA\Chat\OCH\Commands; |
|
9
|
|
|
|
|
10
|
|
|
use \OCA\Chat\Controller\OCH\ApiController; |
|
11
|
|
|
use \OCA\Chat\OCH\ChatAPI; |
|
12
|
|
|
use \OCA\Chat\OCH\Db\User; |
|
13
|
|
|
use \OCA\Chat\OCH\Db\UserMapper; |
|
14
|
|
|
use \OCA\Chat\OCH\Db\PushMessageMapper; |
|
15
|
|
|
use \OCA\Chat\OCH\Exceptions\RequestDataInvalid; |
|
16
|
|
|
use \OCA\Chat\OCH\Db\InitConv; |
|
17
|
|
|
use \OCA\Chat\OCH\Data\GetUsers; |
|
18
|
|
|
use \OCA\Chat\App\Chat; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class Join extends ChatAPI { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var $pushMessageMapper \OCA\Chat\OCH\Db\PushMessageMapper |
|
25
|
|
|
*/ |
|
26
|
|
|
private $pushMessageMapper; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var $getUsers \OCA\Chat\OCH\Data\GetUsers |
|
30
|
|
|
*/ |
|
31
|
|
|
private $getUsers; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var $userMapper \OCA\Chat\OCH\Db\UserMapper |
|
35
|
|
|
*/ |
|
36
|
|
|
private $userMapper; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
PushMessageMapper $pushMessageMapper, |
|
40
|
|
|
GetUsers $getUsers, |
|
41
|
|
|
UserMapper $userMapper |
|
42
|
|
|
){ |
|
43
|
|
|
$this->pushMessageMapper = $pushMessageMapper; |
|
44
|
|
|
$this->getUsers = $getUsers; |
|
45
|
|
|
$this->userMapper = $userMapper; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
public function setRequestData(array $requestData){ |
|
50
|
|
|
if(empty($requestData['conv_id'])){ |
|
51
|
|
|
throw new RequestDataInvalid(ApiController::NO_CONV_ID); |
|
52
|
|
|
} |
|
53
|
|
|
$this->requestData = $requestData; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function execute(){ |
|
57
|
|
|
|
|
58
|
|
|
// Add the user to the conversation |
|
59
|
|
|
$user = new User(); |
|
60
|
|
|
$user->setConversationId($this->requestData['conv_id']); |
|
61
|
|
|
$user->setJoined(time()); |
|
62
|
|
|
$user->setUser($this->requestData['user']['id']); |
|
63
|
|
|
$this->userMapper->insertUnique($user); |
|
64
|
|
|
|
|
65
|
|
|
$this->getUsers->setRequestData(array("conv_id" => $this->requestData['conv_id'])); |
|
66
|
|
|
$users = $this->getUsers->execute(); |
|
67
|
|
|
$users = $users['users']; |
|
68
|
|
|
if(count($users) > 2){ |
|
69
|
|
|
// we are in a group conv this mean we have to let the other users now we joined it |
|
70
|
|
|
$command = json_encode(array( |
|
71
|
|
|
"type" => "joined", |
|
72
|
|
|
"data" => array( |
|
73
|
|
|
"conv_id" => $this->requestData['conv_id'], |
|
74
|
|
|
"users" => $users |
|
75
|
|
|
) |
|
76
|
|
|
)); |
|
77
|
|
|
$this->pushMessageMapper->createForAllUsersInConv( |
|
78
|
|
|
$this->requestData['user']['id'], |
|
79
|
|
|
$this->requestData['conv_id'], |
|
80
|
|
|
$command |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->getUsers->setRequestData(array("conv_id" => $this->requestData['conv_id'])); |
|
85
|
|
|
$users = $this->getUsers->execute(); |
|
86
|
|
|
|
|
87
|
|
|
return $users; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|