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\Attachment; |
12
|
|
|
use \OCA\Chat\OCH\Db\AttachmentMapper; |
13
|
|
|
use \OCA\Chat\OCH\Db\PushMessage; |
14
|
|
|
use \OCA\Chat\OCH\Db\PushMessageMapper; |
15
|
|
|
use \OCA\Chat\OCH\Db\UserMapper; |
16
|
|
|
use \OCP\Share; |
17
|
|
|
|
18
|
|
|
class AttachFile extends ChatAPI { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var $userMapper \OCA\Chat\OCH\Db\UserMapper |
22
|
|
|
*/ |
23
|
|
|
private $userMapper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var $attachmentMapper \OCA\Chat\OCH\Db\AttachmentMapper |
27
|
|
|
*/ |
28
|
|
|
private $attachmentMapper; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var $pushMessageMapper \OCA\Chat\OCH\Db\PushMessageMapper |
32
|
|
|
*/ |
33
|
|
|
private $pushMessageMapper; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
Chat $app, |
37
|
|
|
UserMapper $userMapper, |
38
|
|
|
AttachmentMapper $attachmentMapper, |
39
|
|
|
PushMessageMapper $pushMessageMapper |
40
|
|
|
){ |
41
|
|
|
$this->app = $app; |
|
|
|
|
42
|
|
|
$this->userMapper = $userMapper; |
43
|
|
|
$this->attachmentMapper = $attachmentMapper; |
44
|
|
|
$this->pushMessageMapper = $pushMessageMapper; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* |
48
|
|
|
* @param $requestData['user'] String user id of the client |
49
|
|
|
* @param $requestData['session_id'] String session_id of the client |
50
|
|
|
* @param $requestData['timestamp'] Int timestamp when the command was send |
51
|
|
|
*/ |
52
|
|
|
public function setRequestData(array $requestData){ |
53
|
|
|
$this->requestData = $requestData; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function execute(){ |
57
|
|
|
$paths = $this->requestData['paths']; |
58
|
|
|
$users = $this->userMapper->findUsersInConv($this->requestData['conv_id']); |
59
|
|
|
foreach ($paths as $path) { |
60
|
|
|
$fileId = $this->app->getFileId($path); |
61
|
|
|
$this->insertInDatabase( |
62
|
|
|
$this->app->getUserId(), |
63
|
|
|
$path, |
64
|
|
|
$fileId, |
65
|
|
|
$this->requestData['timestamp'], |
66
|
|
|
$this->requestData['conv_id'] |
67
|
|
|
); |
68
|
|
|
$this->sendPushMessage($path); |
69
|
|
|
} |
70
|
|
|
foreach ($users as $user) { |
71
|
|
|
if ($user !== $this->app->getUserId()) { |
72
|
|
|
foreach ($paths as $path) { |
73
|
|
|
$fileId = $this->app->getFileId($path); |
74
|
|
|
$this->share($fileId, $user); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function sendPushMessage($path){ |
81
|
|
|
$users = $this->userMapper->findSessionsByConversation($this->requestData['conv_id']); |
82
|
|
|
$command = json_encode(array( |
83
|
|
|
'type' => 'file_attached', |
84
|
|
|
'data' => array( |
85
|
|
|
'user' => $this->requestData['user'], |
86
|
|
|
'conv_id' => $this->requestData['conv_id'], |
87
|
|
|
'timestamp' => $this->requestData['timestamp'], |
88
|
|
|
'path' => $path |
89
|
|
|
) |
90
|
|
|
)); |
91
|
|
|
foreach($users as $receiver) { |
92
|
|
|
if($receiver->getUser() !== $this->requestData['user']['id']) { |
93
|
|
|
$pushMessage = new PushMessage(); |
94
|
|
|
$pushMessage->setSender($this->requestData['user']['id']); |
95
|
|
|
$pushMessage->setReceiver($receiver->getUser()); |
96
|
|
|
$pushMessage->setReceiverSessionId($receiver->getSessionId()); |
97
|
|
|
$pushMessage->setCommand($command); |
98
|
|
|
$this->pushMessageMapper->insert($pushMessage); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Inserts the attachment into the DB |
105
|
|
|
* @param $ownerId ownCloud user id |
106
|
|
|
* @param $path path of the file |
107
|
|
|
* @param $fileId |
108
|
|
|
* @param $timestamp |
109
|
|
|
* @param $convId |
110
|
|
|
*/ |
111
|
|
|
private function insertInDatabase($ownerId, $path, $fileId, $timestamp, $convId){ |
112
|
|
|
$attachment = new Attachment(); |
113
|
|
|
$attachment->setOwner($ownerId); |
114
|
|
|
$attachment->setPath($path); |
115
|
|
|
$attachment->setFileId($fileId); |
116
|
|
|
$attachment->setTimestamp($timestamp); |
117
|
|
|
$attachment->setConvId($convId); |
118
|
|
|
$this->attachmentMapper->insertUnique($attachment); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $fileId the fileId of the file |
123
|
|
|
* @param $shareWIth the ownCloud user to share the file with |
124
|
|
|
*/ |
125
|
|
|
private function share($fileId, $shareWIth){ |
126
|
|
|
try { |
127
|
|
|
Share::shareItem('file', $fileId, \OCP\Share::SHARE_TYPE_USER, $shareWIth, \OCP\PERMISSION_ALL); |
128
|
|
|
} Catch (\Exception $e){ |
|
|
|
|
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: