comments / notifications : improve abstraction

This commit is contained in:
isUnknown 2024-11-18 12:00:19 +01:00
parent f467012241
commit 32c026acfe
11 changed files with 92 additions and 65 deletions

View file

@ -13,11 +13,23 @@ Template: document
Comments:
-
page:
uri: projects/miss-dior-blooming-bouquet
title: Miss Dior Blooming Bouquet
file:
uuid: file://s0lNtRA0Z7ybTCWG
location:
page:
uri: >
projects/miss-dior-blooming-bouquet/client-brief
title: Brief client
href: >
/projects/miss-dior-blooming-bouquet?dialog=client-brief
project:
title: Miss Dior Blooming Bouquet
uri: projects/miss-dior-blooming-bouquet
file:
uuid: file://s0lNtRA0Z7ybTCWG
url: file://s0lNtRA0Z7ybTCWG
position:
pageIndex: 1
x: '32.518325137231'
y: '20.46332046332'
replies: [ ]
text: test
author:
@ -25,10 +37,7 @@ Comments:
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
position:
pageIndex: 1
x: '25.96184356358'
y: '22.393822393822'
date: 2024-11-17T12:13:39+01:00
id: m3li0uhb
type: comment
date: 2024-11-18T11:59:06+01:00
id: m3mwxzuo
type: comment
isRead: false

View file

@ -28,6 +28,7 @@ class ProjectPage extends Page {
return [
'label' => $child->title()->value(),
'id' => $child->stepName()->value(),
'slug' => $child->slug(),
'index' => intval($child->stepIndex()->value()),
'modified' => $child->modified('Y-MM-dd'),
'uri' => $uri,
@ -44,13 +45,13 @@ class ProjectPage extends Page {
}
if ($child->pdf()->isNotEmpty()) {
$uri = $child->parent()->uri() . '?dialog=' . $child->stepName()->value();
$uri = $child->parent()->uri() . '?dialog=' . $child->slug();
$files[] = getFileData($child->pdf()->toFile());
}
}
private function handleVirtualSampleStep($child, &$files, &$uri) {
$uri = $child->parent()->uri() . '?dialog=' . $child->stepName()->value();
$uri = $child->parent()->uri() . '?dialog=' . $child->slug();
foreach ($child->views()->toFiles() as $file) {
$files[] = getFileData($file);
}

View file

@ -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' => [

View file

@ -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;
}

View file

@ -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,

View file

@ -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);
}
};