comments / notifications : improve abstraction
This commit is contained in:
parent
f467012241
commit
32c026acfe
11 changed files with 92 additions and 65 deletions
|
|
@ -9,7 +9,14 @@ return [
|
|||
$json = file_get_contents('php://input');
|
||||
$data = json_decode($json);
|
||||
|
||||
$page = page($data->pageUri);
|
||||
$parsedUrl = parse_url($data->path);
|
||||
$query = $parsedUrl['query'] ?? null;
|
||||
parse_str($query, $queryParams);
|
||||
$stepSlug = $queryParams['dialog'] ?? null;
|
||||
|
||||
$targetPageUri = $stepSlug ? $parsedUrl['path'] . '/' . $stepSlug : $parsedUrl['path'];
|
||||
|
||||
$page = page($targetPageUri);
|
||||
$file = $page->file($data->fileName);
|
||||
$user = kirby()->user($data->userUuid);
|
||||
|
||||
|
|
@ -17,6 +24,7 @@ return [
|
|||
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
||||
|
||||
$data = [
|
||||
'href' => $data->path,
|
||||
'page' => $page,
|
||||
'file' => $file,
|
||||
'position' => [
|
||||
|
|
|
|||
|
|
@ -7,8 +7,9 @@ return [
|
|||
$json = file_get_contents('php://input');
|
||||
$data = json_decode($json);
|
||||
|
||||
$page = page($data->page->uri);
|
||||
$file = $page->file($data->file->uuid);
|
||||
$page = page($data->location->page->uri);
|
||||
$project = page($data->location->project->uri);
|
||||
$file = $page->file($data->location->file->uuid);
|
||||
$isReply = $data->parentId ?? false;
|
||||
|
||||
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
||||
|
|
@ -38,7 +39,7 @@ return [
|
|||
|
||||
echo json_encode(getFileData($newFile));
|
||||
|
||||
kirby()->user()->deleteNotification($page->managers()->toUsers(), $data->id);
|
||||
kirby()->user()->deleteNotification($project->managers()->toUsers(), $data->id);
|
||||
|
||||
exit;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
namespace adrienpayet\comments;
|
||||
|
||||
class BaseComment {
|
||||
protected $page;
|
||||
protected $file;
|
||||
protected $href;
|
||||
protected $location;
|
||||
protected $position;
|
||||
protected $replies;
|
||||
protected $text;
|
||||
|
|
@ -16,6 +16,7 @@ class BaseComment {
|
|||
|
||||
public function __construct($data) {
|
||||
$page = $data['page'];
|
||||
$project = $page->template() == 'project' ? $page : $page->parent();
|
||||
$file = $data['file'];
|
||||
$position = $data['position'];
|
||||
$replies = $data['replies'] ?? [];
|
||||
|
|
@ -25,13 +26,22 @@ class BaseComment {
|
|||
$id = $data['id'];
|
||||
$type = $data['type'] ?? 'comment';
|
||||
|
||||
$this->page = [
|
||||
'uri' => (string) $page->parent()->uri(),
|
||||
'title' => (string) $page->parent()->title(),
|
||||
];
|
||||
$this->file = [
|
||||
$this->location = [
|
||||
'page' => [
|
||||
'uri' => (string) $page->uri(),
|
||||
'title' => (string) $page->title(),
|
||||
],
|
||||
'href' => (string) $data['href'],
|
||||
'project' => [
|
||||
'title' => (string) $project->title(),
|
||||
'uri' => (string) $project->uri(),
|
||||
],
|
||||
'file' => $file ? [
|
||||
'uuid' => (string) $file->uuid(),
|
||||
'url' => (string) $file->uuid()
|
||||
] : false,
|
||||
];
|
||||
|
||||
$this->replies = $replies ?? [];
|
||||
$this->text = $text;
|
||||
$this->author = [
|
||||
|
|
@ -47,12 +57,12 @@ class BaseComment {
|
|||
$this->position = $position;
|
||||
}
|
||||
|
||||
public function page() {
|
||||
return $this->page;
|
||||
public function location() {
|
||||
return $this->location;
|
||||
}
|
||||
|
||||
public function file() {
|
||||
return $this->file;
|
||||
return $this->location['file'];
|
||||
}
|
||||
|
||||
public function replies() {
|
||||
|
|
@ -99,12 +109,11 @@ class BaseComment {
|
|||
|
||||
public function toArray() {
|
||||
return [
|
||||
'page' => $this->page,
|
||||
'file' => $this->file,
|
||||
'location' => $this->location,
|
||||
'position' => $this->position,
|
||||
'replies' => $this->replies,
|
||||
'text' => $this->text,
|
||||
'author' => $this->author,
|
||||
'position' => $this->position,
|
||||
'date' => $this->date,
|
||||
'id' => $this->id,
|
||||
'type' => $this->type,
|
||||
|
|
|
|||
|
|
@ -1,22 +1,23 @@
|
|||
<?php
|
||||
|
||||
return function($notificationId) {
|
||||
$user = kirby()->user();
|
||||
try {
|
||||
$notifications = $user->notifications()->isNotEmpty()
|
||||
? Yaml::decode($user->notifications()->value())
|
||||
: [];
|
||||
|
||||
foreach ($notifications as $key => $notification) {
|
||||
if ($notification['id'] === $notificationId) {
|
||||
unset($notifications[$key]);
|
||||
return function($projectManagers, $notificationId) {
|
||||
foreach ($projectManagers as $projectManager) {
|
||||
try {
|
||||
$notifications = $projectManager->notifications()->isNotEmpty()
|
||||
? Yaml::decode($projectManager->notifications()->value())
|
||||
: [];
|
||||
|
||||
foreach ($notifications as $key => $notification) {
|
||||
if ($notification['id'] === $notificationId) {
|
||||
unset($notifications[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$projectManager->update([
|
||||
'notifications' => Yaml::encode(array_values($notifications))
|
||||
]);
|
||||
} catch (\Throwable $th) {
|
||||
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
|
||||
}
|
||||
|
||||
$user->update([
|
||||
'notifications' => Yaml::encode(array_values($notifications))
|
||||
]);
|
||||
} catch (\Throwable $th) {
|
||||
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue