|
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\OCH\ChatAPI; |
|
11
|
|
|
use \OCA\Chat\OCH\Db\Conversation; |
|
12
|
|
|
use \OCA\Chat\OCH\Db\MessageMapper; |
|
13
|
|
|
use \OCA\Chat\OCH\Db\ConversationMapper; |
|
14
|
|
|
use \OCA\Chat\OCH\Data\GetUsers; |
|
15
|
|
|
use \OCA\Chat\OCH\Data\Messages; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class StartConv extends ChatAPI { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var $messageMapper \OCA\Chat\OCH\Db\messageMapper |
|
22
|
|
|
*/ |
|
23
|
|
|
private $messageMapper; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var $conversationMapper \OCA\Chat\OCH\Db\ConversationMapper |
|
27
|
|
|
*/ |
|
28
|
|
|
private $conversationMapper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var $invite \OCA\Chat\OCH\Commands\Invite; |
|
32
|
|
|
*/ |
|
33
|
|
|
private $invite; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var $join \OCA\Chat\OCH\Commands\Join; |
|
37
|
|
|
*/ |
|
38
|
|
|
private $join; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var $getUsers \OCA\Chat\OCH\Data\GetUsers |
|
42
|
|
|
*/ |
|
43
|
|
|
private $getUsers; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var $messages \OCA\Chat\OCH\Data\Messages |
|
47
|
|
|
*/ |
|
48
|
|
|
private $messages; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct( |
|
51
|
|
|
MessageMapper $messageMapper, |
|
52
|
|
|
ConversationMapper $conversationMapper, |
|
53
|
|
|
Invite $invite, |
|
54
|
|
|
Join $join, |
|
55
|
|
|
GetUsers $getUsers, |
|
56
|
|
|
Messages $messages |
|
57
|
|
|
){ |
|
58
|
|
|
$this->messageMapper = $messageMapper; |
|
59
|
|
|
$this->conversationMapper = $conversationMapper; |
|
60
|
|
|
$this->invite = $invite; |
|
61
|
|
|
$this->join = $join; |
|
62
|
|
|
$this->getUsers = $getUsers; |
|
63
|
|
|
$this->messages = $messages; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
public function setRequestData(array $requestData) { |
|
68
|
|
|
$this->requestData = $requestData; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function execute(){ |
|
72
|
|
|
|
|
73
|
|
|
// (1) generate a conv id |
|
74
|
|
|
$ids = array(); |
|
75
|
|
|
foreach($this->requestData['user_to_invite'] as $userToInvite){ |
|
76
|
|
|
$ids[] = $userToInvite['id']; |
|
77
|
|
|
} |
|
78
|
|
|
// always add our selve to the array for the conv id |
|
79
|
|
|
$ids[] = $this->requestData['user']['id']; |
|
80
|
|
|
|
|
81
|
|
|
// (2) check if conv id exists |
|
82
|
|
|
|
|
83
|
|
|
if($id = $this->conversationMapper->existsByUsers($ids)){ |
|
84
|
|
|
$id = $id['conv_id']; |
|
85
|
|
|
} else { |
|
86
|
|
|
$id = $this->generateConvId(); |
|
87
|
|
|
|
|
88
|
|
|
// (3) Create the conv |
|
89
|
|
|
$conversation = new Conversation(); |
|
90
|
|
|
$conversation->setConversationId($id); |
|
91
|
|
|
$this->conversationMapper->insert($conversation); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// (5) invite the user_to_invite since we just created the conv |
|
95
|
|
|
// foreach user to invite |
|
96
|
|
|
$requestData = array(); |
|
97
|
|
|
$requestData['conv_id'] = $id; |
|
98
|
|
|
$requestData['user'] = $this->requestData['user']; |
|
99
|
|
|
foreach($this->requestData['user_to_invite'] as $userToInvite){ |
|
100
|
|
|
if($userToInvite['id'] !== $this->requestData['user']['id']){ |
|
101
|
|
|
$requestData['user_to_invite'] = $userToInvite; |
|
102
|
|
|
$this->invite->setRequestData($requestData); |
|
103
|
|
|
$this->invite->execute(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// (4) join the just created conv |
|
108
|
|
|
$this->requestData['conv_id'] = $id; |
|
109
|
|
|
$this->join->setRequestData($this->requestData); |
|
110
|
|
|
$this->join->execute(); |
|
111
|
|
|
|
|
112
|
|
|
// Fetch users in conv |
|
113
|
|
|
$this->getUsers->setRequestData(array("conv_id" => $this->requestData['conv_id'])); |
|
114
|
|
|
$users = $this->getUsers->execute(); |
|
115
|
|
|
$users = $users['users']; |
|
116
|
|
|
|
|
117
|
|
|
// Fetch messages in conv |
|
118
|
|
|
$this->messages->setRequestData(array( |
|
119
|
|
|
"conv_id" => $this->requestData['conv_id'], |
|
120
|
|
|
'user' => $this->requestData['user'] |
|
121
|
|
|
)); |
|
122
|
|
|
$messages = $this->messages->execute(); |
|
123
|
|
|
$messages = $messages['messages']; |
|
124
|
|
|
|
|
125
|
|
|
return array( |
|
126
|
|
|
"conv_id" => $id, |
|
127
|
|
|
"users" => $users, |
|
128
|
|
|
"messages" => $messages |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function generateConvId(){ |
|
133
|
|
|
$id = 'CONV_ID_' . time() . '_' . rand(1, 99); |
|
134
|
|
|
if($this->conversationMapper->existsByConvId($id)){ |
|
135
|
|
|
return $this->generateConvId(); |
|
136
|
|
|
} else { |
|
137
|
|
|
return $id; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|