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\Exceptions\RequestDataInvalid; |
15
|
|
|
use \OCA\Chat\Controller\OCH\ApiController; |
16
|
|
|
|
17
|
|
|
class RemoveFile extends ChatAPI { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var $pushMessageMapper \OCA\Chat\OCH\Db\PushMessageMapper |
21
|
|
|
*/ |
22
|
|
|
private $pushMessageMapper; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var $attachmentMapper \OCA\Chat\OCH\Db\AttachmentMapper |
26
|
|
|
*/ |
27
|
|
|
private $attachmentMapper; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var $userMapper \OCA\Chat\OCH\Db\UserMapper |
31
|
|
|
*/ |
32
|
|
|
private $userMapper; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
Chat $app, |
36
|
|
|
PushMessageMapper $pushMessageMapper, |
37
|
|
|
AttachmentMapper $attachmentMapper, |
38
|
|
|
UserMapper $userMapper |
39
|
|
|
){ |
40
|
|
|
$this->app = $app; |
|
|
|
|
41
|
|
|
$this->pushMessageMapper = $pushMessageMapper; |
42
|
|
|
$this->attachmentMapper = $attachmentMapper; |
43
|
|
|
$this->userMapper = $userMapper; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/* |
49
|
|
|
* @param $requestData['user'] String user id of the client |
50
|
|
|
* @param $requestData['session_id'] String session_id of the client |
51
|
|
|
* @param $requestData['timestamp'] Int timestamp when the command was send |
52
|
|
|
*/ |
53
|
|
|
public function setRequestData(array $requestData){ |
54
|
|
|
$this->requestData = $requestData; |
55
|
|
|
$attachment = $this->attachmentMapper->findByPathAndConvId($this->requestData['path'], $this->requestData['conv_id']); |
56
|
|
|
if ($attachment->getOwner() !== $this->app->getUserId()){ |
57
|
|
|
throw new RequestDataInvalid(ApiController::NOT_OWNER_OF_FILE); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function execute(){ |
62
|
|
|
|
63
|
|
|
$fileId = $this->app->getFileId($this->requestData['path']); |
64
|
|
|
$file = new Attachment(); |
65
|
|
|
$file->setConvId($this->requestData['conv_id']); |
66
|
|
|
$file->setFileId($fileId); |
67
|
|
|
$this->attachmentMapper->deleteByConvAndFileID($file); |
68
|
|
|
$this->sendPushMessage($this->requestData['path']); |
69
|
|
|
$users = $this->userMapper->findUsersInConv($this->requestData['conv_id']); |
70
|
|
|
foreach ($users as $user) { |
71
|
|
|
if ($user !== $this->app->getUserId()) { |
72
|
|
|
$this->unShare($fileId, $user); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $fileId the fileId of the file |
79
|
|
|
* @param $shareWIth the ownCloud user to share the file with |
80
|
|
|
*/ |
81
|
|
|
private function unShare($fileId, $shareWIth){ |
82
|
|
|
try { |
83
|
|
|
\OCP\Share::unshare('file', $fileId, \OCP\Share::SHARE_TYPE_USER, $shareWIth); |
84
|
|
|
} Catch (\Exception $e){ |
|
|
|
|
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function sendPushMessage($path){ |
90
|
|
|
$users = $this->userMapper->findSessionsByConversation($this->requestData['conv_id']); |
91
|
|
|
$command = json_encode(array( |
92
|
|
|
'type' => 'file_removed', |
93
|
|
|
'data' => array( |
94
|
|
|
'user' => $this->requestData['user'], |
95
|
|
|
'conv_id' => $this->requestData['conv_id'], |
96
|
|
|
'timestamp' => $this->requestData['timestamp'], |
97
|
|
|
'path' => $path |
98
|
|
|
) |
99
|
|
|
)); |
100
|
|
|
foreach($users as $receiver) { |
101
|
|
|
if($receiver->getUser() !== $this->requestData['user']['id']) { |
102
|
|
|
$pushMessage = new PushMessage(); |
103
|
|
|
$pushMessage->setSender($this->requestData['user']['id']); |
104
|
|
|
$pushMessage->setReceiver($receiver->getUser()); |
105
|
|
|
$pushMessage->setReceiverSessionId($receiver->getSessionId()); |
106
|
|
|
$pushMessage->setCommand($command); |
107
|
|
|
$this->pushMessageMapper->insert($pushMessage); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
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: