#68 - create comment create working (notification to adapt)

This commit is contained in:
isUnknown 2024-12-18 16:26:55 +01:00
parent 3d4ddc12fc
commit cf83edc1e6
11 changed files with 106 additions and 158 deletions

View file

@ -6,112 +6,56 @@ Comments:
uri: >
projects/miss-dior-blooming-bouquet/proposal
title: Offre commerciale
href: '/projects/miss-dior-blooming-bouquet?dialog=proposal&fileIndex=0'
project:
title: Miss Dior Blooming Bouquet
uri: projects/miss-dior-blooming-bouquet
dialogUri: '/projects/miss-dior-blooming-bouquet?dialog=proposal&fileIndex=0'
file:
uuid: file://3vTh1tMFeFM2JxaN
url: file://3vTh1tMFeFM2JxaN
position:
pageIndex: 1
x: "60.110185093015"
y: "44.594594594595"
replies: [ ]
url: >
http://localhost:8888/media/pages/projects/miss-dior-blooming-bouquet/proposal/788ddebfe3-1731941917/des-textos-revelent-comment-bfm-sest-mise-au-service-de-sarkozy-le-boss.pdf
text: test
author:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-11-28T16:30:56+01:00
id: m41h238q
date: 2024-12-18T16:20:04+01:00
id: 6dbb6d56-a6b7-467b-8341-3d8112a73684
position:
pageIndex: 1
x: "73.3181571571"
y: "27.272727272727"
type: comment
isRead: false
replies: [ ]
-
location:
page:
uri: >
projects/miss-dior-blooming-bouquet/proposal
title: Offre commerciale
href: '/projects/miss-dior-blooming-bouquet?dialog=proposal&fileIndex=0'
project:
title: Miss Dior Blooming Bouquet
uri: projects/miss-dior-blooming-bouquet
dialogUri: '/projects/miss-dior-blooming-bouquet?dialog=proposal&fileIndex=0'
file:
uuid: file://3vTh1tMFeFM2JxaN
url: file://3vTh1tMFeFM2JxaN
url: >
http://localhost:8888/media/pages/projects/miss-dior-blooming-bouquet/proposal/788ddebfe3-1731941917/des-textos-revelent-comment-bfm-sest-mise-au-service-de-sarkozy-le-boss.pdf
text: est
author:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-12-18T16:23:42+01:00
id: 5b2d93f9-fc44-4a69-955a-bf282fba8966
position:
pageIndex: 1
x: "26.098441098196"
y: "64.864864864865"
replies: [ ]
text: Nouveau commentaire
author:
name: Utilisateur Dior
email: utilisateur@dior.com
uuid: user://HfuumN8s
role: client
date: 2024-12-04T15:00:15+01:00
id: m49ygks2
x: '43.56742057598'
y: '65.340909090909'
type: comment
isRead: false
-
location:
page:
uri: >
projects/miss-dior-blooming-bouquet/proposal
title: Offre commerciale
href: '/projects/miss-dior-blooming-bouquet?dialog=proposal&fileIndex=0'
project:
title: Miss Dior Blooming Bouquet
uri: projects/miss-dior-blooming-bouquet
file:
uuid: file://3vTh1tMFeFM2JxaN
url: file://3vTh1tMFeFM2JxaN
position:
pageIndex: 1
x: "64.481172808783"
y: "76.447876447876"
replies: [ ]
text: encore un commentaire
author:
name: Utilisateur Dior
email: utilisateur@dior.com
uuid: user://HfuumN8s
role: client
date: 2024-12-04T15:03:53+01:00
id: m49yl8zc
type: comment
isRead: false
-
location:
page:
uri: >
projects/miss-dior-blooming-bouquet/proposal
title: Offre commerciale
href: '/projects/miss-dior-blooming-bouquet?dialog=proposal&fileIndex=0'
project:
title: Miss Dior Blooming Bouquet
uri: projects/miss-dior-blooming-bouquet
file:
uuid: file://3vTh1tMFeFM2JxaN
url: file://3vTh1tMFeFM2JxaN
position:
pageIndex: 1
x: '29.786457814876'
y: '83.397683397683'
replies: [ ]
text: et encore un
author:
name: Utilisateur Dior
email: utilisateur@dior.com
uuid: user://HfuumN8s
role: client
date: 2024-12-04T15:05:10+01:00
id: m49ymwuw
type: comment
isRead: false
----

View file

@ -1,13 +1,14 @@
<?php
namespace adrienpayet\D2P\data\location;
use Kirby;
class FileDetails
{
public string $uuid;
public string $url;
public function __construct(array $data)
public function __construct(Kirby\Cms\File $data)
{
$this->uuid = (string) $data->uuid();
$this->url = (string) $data->url();

View file

@ -3,7 +3,14 @@
F::loadClasses([
'adrienpayet\\comments\\BaseComment' => __DIR__ . '/src/BaseComment.php',
'adrienpayet\\comments\\Comment' => __DIR__ . '/src/Comment.php',
'adrienpayet\\comments\\Reply' => __DIR__ . '/src/Reply.php'
'adrienpayet\\comments\\Reply' => __DIR__ . '/src/Reply.php',
'adrienpayet\\D2P\\data\\Position' => __DIR__ . '/../classes/Position.php',
'adrienpayet\\D2P\data\Author' => __DIR__ . '/../classes/Author.php',
'adrienpayet\\D2P\\data\\location\\Location' => __DIR__ . '/../classes/location/Location.php',
'adrienpayet\\D2P\\data\\location\\PageDetails' => __DIR__ . '/../classes/location/PageDetails.php',
'adrienpayet\\D2P\\data\\location\\ProjectDetails' => __DIR__ . '/../classes/location/ProjectDetails.php',
'adrienpayet\\D2P\\data\\location\\FileDetails' => __DIR__ . '/../classes/location/FileDetails.php',
]);
Kirby::plugin('adrienpayet/kirby4-comments', [

View file

@ -9,49 +9,53 @@ return [
$json = file_get_contents('php://input');
$data = json_decode($json);
$parsedUrl = parse_url($data->path);
$parsedUrl = parse_url($data->dialogUri);
$query = $parsedUrl['query'] ?? null;
parse_str($query, $queryParams);
$stepSlug = $queryParams['dialog'] ?? null;
$targetPageUri = $stepSlug ? $parsedUrl['path'] . '/' . $stepSlug : $parsedUrl['path'];
$project = page($parsedUrl['path']);
$page = page($targetPageUri);
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
$user = kirby()->user($data->userUuid);
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
$data = [
'href' => $data->path,
'page' => $page,
'file' => $file,
'position' => [
'pageIndex' => $data->position->pageIndex,
'x' => $data->position->x,
'y' => $data->position->y
$commentData = [
"location" => [
"page" => $page,
"project" => $project,
"dialogUri" => $data->dialogUri,
"file" => $file
],
'replies' => [],
'text' => $data->text,
'author' => $user,
'date' => (string) $data->date,
'id' => $data->id,
'type' => 'comment'
"position" => [
"pageIndex" => $data->position->pageIndex,
"x" => $data->position->x,
"y" => $data->position->y
],
"date" => (string) $data->date,
"text" => $data->text,
"author" => kirby()->user(),
"id" => Str::uuid(),
"type" => "comment",
];
$newComment = new Comment($data);
$user->sendNotification($project, $commentData);
$newComment = new Comment($commentData);
$comments[] = $newComment->toArray();
$newFile = $file->update([
'comments' => $comments
]);
]);
echo json_encode(getFileData($newFile));
try {
$user->sendNotification($page->parent(), $newComment->toArray());
$user->sendNotification($project, $commentData);
} catch (\Throwable $th) {
echo json_encode([
"error" => $th->getMessage() . " in " . $th->getFile() . " line " . $th->getLine(),

View file

@ -1,40 +1,32 @@
<?php
namespace adrienpayet\comments;
use adrienpayet\D2P\data\location\Location;
use adrienpayet\D2P\data\Author;
class BaseComment {
protected $href;
protected $location;
protected $position;
protected $replies;
protected $text;
protected $author;
protected $date;
protected $id;
protected $type;
protected $isRead;
protected string $type;
protected Location $location;
protected string $text;
protected Author $author;
protected string $date;
protected string $id;
public function __construct($data) {
$this->type = $data["type"];
$this->location = new Location($data["location"]);
$this->text = $data["text"];
$this->author = new Author($data["author"]);
$this->date = $data["date"];
$this->id = $data["id"];
$this->isRead = "false";
}
public function toArray() {
return [
'location' => $this->location,
'position' => $this->position,
'replies' => $this->replies,
'text' => $this->text,
'author' => $this->author,
'date' => $this->date,
'id' => $this->id,
'type' => $this->type,
'isRead' => $this->isRead
"location" => $this->location->toArray(),
"text" => $this->text,
"author" => $this->author->toArray(),
"date" => $this->date,
"id" => $this->id,
];
}
}

View file

@ -1,23 +1,26 @@
<?php
namespace adrienpayet\comments;
use adrienpayet\D2P\data\Position;
class Comment extends BaseComment
{
protected $position;
protected ?Position $position = null;
protected string $type = "comment";
protected array $replies = [];
public function __construct($data) {
parent::__construct($data);
$this->position = $data['position'] ?? null;
}
public function position() {
return $this->position;
if (isset($data["position"])) {
$this->position = new Position($data['position']);
}
}
public function toArray() {
$array = parent::toArray();
$array['position'] = $this->position;
$array['type'] = $this->type;
$array['replies'] = $this->replies;
return $array;
}

View file

@ -12,7 +12,7 @@ class Notification
protected Author $author;
protected string $date;
protected string $id;
protected string $isRead;
protected string $isRead = "false";
protected ?Position $position = null;
@ -23,11 +23,6 @@ class Notification
$this->author = new Author($data["author"]);
$this->date = $data["date"];
$this->id = $data["id"];
$this->isRead = "false";
if ($data["type"] === "comment") {
$this->position = new Position($data["position"]);
}
}
public function toArray() {
@ -40,9 +35,6 @@ class Notification
"id" => $this->id,
"isRead" => $this->isRead,
];
if ($this->type === "comment") {
$array["position"] = $this->position->toArray();
}
return $array;
}

View file

@ -16,27 +16,26 @@ use adrienpayet\notifications\Notification;
*/
return function ($project, $notificationData) {
$recipients = $project->managers()->without($this);
$recipients = $project->managers()->without($this);
if ($recipients->isEmpty()) return;
if ($recipients->isEmpty()) return;
$notificationData['isRead'] = false;
$newNotification = new Notification($notificationData);
foreach ($recipients as $otherUser) {
try {
$notifications = $otherUser->notifications()->isNotEmpty()
? Yaml::decode($otherUser->notifications()->value())
: [];
$newNotification = new Notification($notificationData);
$notifications[] = $newNotification->toArray();
foreach ($recipients as $otherUser) {
try {
$notifications = $otherUser->notifications()->isNotEmpty()
? Yaml::decode($otherUser->notifications()->value())
: [];
$notifications[] = $newNotification->toArray();
$otherUser->update([
'notifications' => $notifications
]);
} catch (\Throwable $th) {
error_log("Notification error for user " . $otherUser->email() . ": " . $th->getMessage());
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
}
$otherUser->update([
'notifications' => $notifications
]);
} catch (\Throwable $th) {
error_log("Notification error for user " . $otherUser->email() . ": " . $th->getMessage());
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
}
};