Skip to content

Show other authors editing the same element #13420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
fbf1c0d
Reveal other authors editing the same element
brandonkelly Jul 12, 2023
40a1909
Entry updated upstream notification
brandonkelly Jul 13, 2023
2d87072
Merge branch '4.5' into feature/dev-1154-show-other-authors-editing-t…
brandonkelly Jul 17, 2023
45dc12c
Tweaks
brandonkelly Jul 17, 2023
71a7549
A11y improvements
brandonkelly Jul 17, 2023
89df320
Custom tooltips
brandonkelly Jul 18, 2023
700484b
Start refactoring dom to use button instead of li
Jul 18, 2023
ab1ca50
Start updating DOM/ARIA
Jul 20, 2023
2d46cd3
Add error to tooltip js when a button element is not used
Jul 20, 2023
294b526
Add class name to toggle tip trigger
Jul 20, 2023
2e78afd
Add new translations to file for new/no authors showing in activity c…
Jul 20, 2023
9f479fb
Remove all live region messages for author updates
Jul 24, 2023
0d821d7
Remove comment
Jul 24, 2023
f466675
Merge branch '4.5' into feature/dev-1154-show-other-authors-editing-t…
brandonkelly Aug 3, 2023
1506cd9
Merge branch 'feature/dev-1154-show-other-authors-editing-the-same-el…
brandonkelly Aug 3, 2023
94c3c0e
Merge branch '4.5' into feature/dev-1154-show-other-authors-editing-t…
brandonkelly Aug 3, 2023
0370e27
Merge branch 'feature/dev-1154-show-other-authors-editing-the-same-el…
brandonkelly Aug 3, 2023
2e17f18
Maintain hovered tooltip visibility on first click
brandonkelly Aug 3, 2023
de722df
Merge branch '4.5' into feature/dev-1154-show-other-authors-editing-t…
brandonkelly Aug 3, 2023
1a8de80
Merge branch 'feature/dev-1154-show-other-authors-editing-the-same-el…
brandonkelly Aug 3, 2023
3f2c50b
A11y improvements
brandonkelly Aug 3, 2023
4d1def8
Add region role to activity container to aid in navigation
Aug 3, 2023
c71dbdf
Hide tooltips if the user is no longer listed
brandonkelly Aug 3, 2023
7a0013b
Merge branch 'a11y/live-region-and-roles' of https://github.com/craft…
brandonkelly Aug 3, 2023
2c867e7
Don't show tooltips on focus
brandonkelly Aug 3, 2023
352dd50
Translate
brandonkelly Aug 3, 2023
635d4f2
Merge pull request #13475 from craftcms/a11y/live-region-and-roles
brandonkelly Aug 4, 2023
950c45f
Merge branch '4.5' into feature/dev-1154-show-other-authors-editing-t…
brandonkelly Aug 4, 2023
f0fafb7
recordActivity() → trackActivity()
brandonkelly Aug 4, 2023
70dc0f2
Typo
brandonkelly Aug 4, 2023
ffcf603
Cleanup
brandonkelly Aug 4, 2023
8269839
GC
brandonkelly Aug 4, 2023
6561c9d
Release notes
brandonkelly Aug 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Release Notes for Craft CMS 4.5 (WIP)

### Content Management
- Entry and category edit pages now show other authors who are currently editing the same element. ([#13420](https://github.com/craftcms/cms/pull/13420))
- Entry and category edit pages now display a notification when the element has been saved by another author. ([#13420](https://github.com/craftcms/cms/pull/13420))
- Table fields can now have a “Row heading” column. ([#13231](https://github.com/craftcms/cms/pull/13231))
- Table fields now have a “Static Rows” setting. ([#13231](https://github.com/craftcms/cms/pull/13231))
- Table fields no longer show a heading row, if all heading values are blank. ([#13231](https://github.com/craftcms/cms/pull/13231))
Expand Down Expand Up @@ -73,6 +75,8 @@
- Added `craft\services\Addresses::$formatter`, which can be used to override the default address formatter. ([#13242](https://github.com/craftcms/cms/pull/13242), [#12615](https://github.com/craftcms/cms/discussions/12615))
- Added `craft\services\Addresses::EVENT_DEFINE_ADDRESS_SUBDIVISIONS`. ([#13361](https://github.com/craftcms/cms/pull/13361))
- Added `craft\services\Addresses::defineAddressSubdivisions()`. ([#13361](https://github.com/craftcms/cms/pull/13361))
- Added `craft\services\Elements::getRecentActivity()`. ([#13420](https://github.com/craftcms/cms/pull/13420))
- Added `craft\services\Elements::trackActivity()`. ([#13420](https://github.com/craftcms/cms/pull/13420))
- Added `craft\services\Structures::ACTION_APPEND`. ([#13429](https://github.com/craftcms/cms/pull/13429))
- Added `craft\services\Structures::ACTION_PLACE_AFTER`. ([#13429](https://github.com/craftcms/cms/pull/13429))
- Added `craft\services\Structures::ACTION_PLACE_BEFORE`. ([#13429](https://github.com/craftcms/cms/pull/13429))
Expand Down Expand Up @@ -103,6 +107,7 @@
- Added `Craft.BaseUploader`. ([#13313](https://github.com/craftcms/cms/pull/13313))
- Added `Craft.createUploader()`. ([#13313](https://github.com/craftcms/cms/pull/13313))
- Added `Craft.registerUploaderClass()`. ([#13313](https://github.com/craftcms/cms/pull/13313))
- Added `Craft.Tooltip`.

### System
- Added support for setting environmental values in a “secrets” PHP file, identified by a `CRAFT_SECRETS_PATH` environment variable. ([#13283](https://github.com/craftcms/cms/pull/13283))
Expand Down
2 changes: 1 addition & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '4.4.16.1',
'schemaVersion' => '4.4.0.4',
'schemaVersion' => '4.5.0.0',
'minVersionRequired' => '3.7.11',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
75 changes: 74 additions & 1 deletion src/controllers/ElementsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use craft\helpers\ElementHelper;
use craft\helpers\Html;
use craft\helpers\UrlHelper;
use craft\models\ElementActivity;
use craft\models\FieldLayoutForm;
use craft\services\Elements;
use craft\web\Controller;
Expand Down Expand Up @@ -425,6 +426,8 @@ public function actionEdit(?ElementInterface $element, ?int $elementId = null):
'siteStatuses' => $siteStatuses,
'siteToken' => (!Craft::$app->getIsLive() || !$element->getSite()->enabled) ? $security->hashData((string)$element->siteId) : null,
'visibleLayoutElements' => $form ? $form->getVisibleElements() : [],
'updatedTimestamp' => $element->dateUpdated->getTimestamp(),
'canonicalUpdatedTimestamp' => $canonical->dateUpdated->getTimestamp(),
]
)
);
Expand Down Expand Up @@ -695,7 +698,11 @@ private function _additionalButtons(
bool $isUnpublishedDraft,
bool $isDraft,
): string {
$components = [];
$components = [
Html::tag('div', options: [
'class' => ['activity-container'],
]),
];

// Preview (View will be added later by JS)
if ($canSave && $previewTargets) {
Expand Down Expand Up @@ -966,6 +973,8 @@ public function actionSave(): ?Response
]));
}

$elementsService->trackActivity($element, ElementActivity::TYPE_SAVE);

// See if the user happens to have a provisional element. If so delete it.
$provisional = $element::find()
->provisionalDrafts()
Expand Down Expand Up @@ -1203,6 +1212,8 @@ public function actionSaveDraft(): ?Response
]));
}

$elementsService->trackActivity($element, ElementActivity::TYPE_SAVE);

$creator = $element->getCreator();

$data = [
Expand Down Expand Up @@ -1275,6 +1286,8 @@ public function actionSaveDraft(): ?Response
'initialDeltaValues' => $view->getInitialDeltaValues(),
'headHtml' => $view->getHeadHtml(),
'bodyHtml' => $view->getBodyHtml(),
'updatedTimestamp' => $element->dateUpdated->getTimestamp(),
'canonicalUpdatedTimestamp' => $element->getCanonical()->dateUpdated->getTimestamp(),
];
}

Expand Down Expand Up @@ -1354,6 +1367,8 @@ public function actionApplyDraft(): ?Response
}
}

$elementsService->trackActivity($canonical, ElementActivity::TYPE_SAVE);

if (!$this->request->getAcceptsJson()) {
// Tell all browser windows about the element save
$session = Craft::$app->getSession();
Expand Down Expand Up @@ -1484,6 +1499,7 @@ public function actionRevert(): Response
}

$canonical = Craft::$app->getRevisions()->revertToRevision($element, $user->id);
Craft::$app->getElements()->trackActivity($canonical, ElementActivity::TYPE_SAVE);

return $this->_asSuccess(Craft::t('app', '{type} reverted to past revision.', [
'type' => $element::displayName(),
Expand Down Expand Up @@ -1522,6 +1538,63 @@ public function actionGetElementHtml(): Response
return $this->asJson(compact('html', 'headHtml'));
}

/**
* Returns any recent activity for an element, and records that the user is viewing the element.
*
* @return Response
* @since 4.5.0
*/
public function actionRecentActivity(): Response
{
$element = $this->_element();

if (!$element || $element->getIsRevision()) {
throw new BadRequestHttpException('No element was identified by the request.');
}

$elementsService = Craft::$app->getElements();
$currentUser = Craft::$app->getUser()->getIdentity();
$activity = $elementsService->getRecentActivity($element, $currentUser->id);
$elementsService->trackActivity($element, ElementActivity::TYPE_VIEW, $currentUser);

return $this->asJson([
'activity' => array_map(function(ElementActivity $record) use ($element) {
$recordIsCanonical = $record->element->getIsCanonical() || $record->element->isProvisionalDraft;
$recordIsCanonicalAndPublished = $recordIsCanonical && !$record->element->getIsUnpublishedDraft();
$isSameOrUpstream = $element->id === $record->element->id || $recordIsCanonical;

if ($isSameOrUpstream) {
$messageParams = [
'user' => $record->user->getName(),
'type' => $recordIsCanonicalAndPublished ? $element::lowerDisplayName() : Craft::t('app', 'draft'),
];
$message = match ($record->type) {
ElementActivity::TYPE_VIEW => Craft::t('app', '{user} is viewing this {type}.', $messageParams),
ElementActivity::TYPE_EDIT, ElementActivity::TYPE_SAVE => Craft::t('app', '{user} is editing this {type}.', $messageParams),
};
} else {
$messageParams = [
'user' => $record->user->getName(),
'type' => $element::lowerDisplayName(),
];
$message = match ($record->type) {
ElementActivity::TYPE_VIEW => Craft::t('app', '{user} is viewing a draft of this {type}.', $messageParams),
ElementActivity::TYPE_EDIT, ElementActivity::TYPE_SAVE => Craft::t('app', '{user} is editing a draft of this {type}.', $messageParams),
};
}

return [
'userId' => $record->user->id,
'userName' => $record->user->getName(),
'userThumb' => $record->user->getThumbHtml(26),
'message' => $message,
];
}, $activity),
'updatedTimestamp' => $element->dateUpdated->getTimestamp(),
'canonicalUpdatedTimestamp' => $element->getCanonical()->dateUpdated->getTimestamp(),
]);
}

/**
* Returns the requested element, populated with any posted attributes.
*
Expand Down
2 changes: 2 additions & 0 deletions src/db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ abstract class Table
public const DEPRECATIONERRORS = '{{%deprecationerrors}}';
/** @since 3.2.0 */
public const DRAFTS = '{{%drafts}}';
/** @since 4.5.0 */
public const ELEMENTACTIVITY = '{{%elementactivity}}';
public const ELEMENTS = '{{%elements}}';
public const ELEMENTS_SITES = '{{%elements_sites}}';
public const RESOURCEPATHS = '{{%resourcepaths}}';
Expand Down
14 changes: 14 additions & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@ public function createTables(): void
'dateLastMerged' => $this->dateTime(),
'saved' => $this->boolean()->notNull()->defaultValue(true),
]);
$this->createTable(Table::ELEMENTACTIVITY, [
'elementId' => $this->integer()->notNull(),
'userId' => $this->integer()->notNull(),
'siteId' => $this->integer()->notNull(),
'draftId' => $this->integer()->null(),
'type' => $this->string()->notNull(),
'timestamp' => $this->dateTime(),
'PRIMARY KEY([[elementId]], [[userId]], [[type]])',
]);
$this->createTable(Table::ELEMENTS, [
'id' => $this->primaryKey(),
'canonicalId' => $this->integer(),
Expand Down Expand Up @@ -815,6 +824,7 @@ public function createIndexes(): void
$this->createIndex(null, Table::DEPRECATIONERRORS, ['key', 'fingerprint'], true);
$this->createIndex(null, Table::DRAFTS, ['creatorId', 'provisional'], false);
$this->createIndex(null, Table::DRAFTS, ['saved'], false);
$this->createIndex(null, Table::ELEMENTACTIVITY, ['elementId', 'timestamp', 'userId'], false);
$this->createIndex(null, Table::ELEMENTS, ['dateDeleted'], false);
$this->createIndex(null, Table::ELEMENTS, ['fieldLayoutId'], false);
$this->createIndex(null, Table::ELEMENTS, ['type'], false);
Expand Down Expand Up @@ -1007,6 +1017,10 @@ public function addForeignKeys(): void
$this->addForeignKey(null, Table::CONTENT, ['siteId'], Table::SITES, ['id'], 'CASCADE', 'CASCADE');
$this->addForeignKey(null, Table::DRAFTS, ['creatorId'], Table::USERS, ['id'], 'SET NULL', null);
$this->addForeignKey(null, Table::DRAFTS, ['canonicalId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['userId'], Table::USERS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['siteId'], Table::SITES, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['draftId'], Table::DRAFTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['canonicalId'], Table::ELEMENTS, ['id'], 'SET NULL');
$this->addForeignKey(null, Table::ELEMENTS, ['draftId'], Table::DRAFTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['revisionId'], Table::REVISIONS, ['id'], 'CASCADE', null);
Expand Down
45 changes: 45 additions & 0 deletions src/migrations/m230710_162700_element_activity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Table;

/**
* m230710_162700_element_activity migration.
*/
class m230710_162700_element_activity extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$this->createTable(Table::ELEMENTACTIVITY, [
'elementId' => $this->integer()->notNull(),
'userId' => $this->integer()->notNull(),
'siteId' => $this->integer()->notNull(),
'draftId' => $this->integer()->null(),
'type' => $this->string()->notNull(),
'timestamp' => $this->dateTime(),
'PRIMARY KEY([[elementId]], [[userId]], [[type]])',
]);

$this->createIndex(null, Table::ELEMENTACTIVITY, ['elementId', 'timestamp', 'userId'], false);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['userId'], Table::USERS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['siteId'], Table::SITES, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['draftId'], Table::DRAFTS, ['id'], 'CASCADE', null);

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m230710_162700_element_activity cannot be reverted.\n";
return false;
}
}
39 changes: 39 additions & 0 deletions src/models/ElementActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\models;

use craft\base\ElementInterface;
use craft\elements\User;
use DateTime;

/**
* Element activity model.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.5.0
*/
class ElementActivity
{
public const TYPE_VIEW = 'view';
public const TYPE_EDIT = 'edit';
public const TYPE_SAVE = 'save';

/**
* @param User $user
* @param ElementInterface $element
* @param self::TYPE_* $type
* @param DateTime $timestamp
*/
public function __construct(
public User $user,
public ElementInterface $element,
public string $type,
public DateTime $timestamp,
) {
}
}
Loading