1111use OCA \Files \Event \LoadAdditionalScriptsEvent ;
1212use OCA \Files_Sharing \Event \BeforeTemplateRenderedEvent ;
1313use OCA \Text \Event \LoadEditor ;
14+ use OCA \Text \Exception \DocumentHasUnsavedChangesException ;
1415use OCA \Text \Listeners \AddMissingIndicesListener ;
1516use OCA \Text \Listeners \BeforeAssistantNotificationListener ;
1617use OCA \Text \Listeners \BeforeNodeDeletedListener ;
2526use OCA \Text \Listeners \RegisterTemplateCreatorListener ;
2627use OCA \Text \Middleware \SessionMiddleware ;
2728use OCA \Text \Notification \Notifier ;
29+ use OCA \Text \Service \DocumentService ;
2830use OCA \TpAssistant \Event \BeforeAssistantNotificationEvent ;
2931use OCA \Viewer \Event \LoadViewer ;
3032use OCP \AppFramework \App ;
3739use OCP \Files \Events \Node \BeforeNodeRenamedEvent ;
3840use OCP \Files \Events \Node \BeforeNodeWrittenEvent ;
3941use OCP \Files \Events \Node \NodeCopiedEvent ;
42+ use OCP \Files \File ;
43+ use OCP \Files \NotFoundException ;
4044use OCP \Files \Template \RegisterTemplateCreatorEvent ;
45+ use OCP \Server ;
46+ use OCP \Util ;
4147
4248class Application extends App implements IBootstrap {
4349 public const APP_NAME = 'text ' ;
@@ -63,8 +69,28 @@ public function register(IRegistrationContext $context): void {
6369
6470 $ context ->registerNotifierService (Notifier::class);
6571 $ context ->registerMiddleware (SessionMiddleware::class);
72+
73+ /** @psalm-suppress DeprecatedMethod */
74+ Util::connectHook ('\OCP\Versions ' , 'rollback ' , $ this , 'resetSessionsAfterRestoreFile ' );
6675 }
6776
6877 public function boot (IBootContext $ context ): void {
6978 }
79+
80+ public function resetSessionsAfterRestoreFile (array $ params ): void {
81+ $ node = $ params ['node ' ];
82+ if (!$ node instanceof File) {
83+ return ;
84+ }
85+
86+ $ documentService = Server::get (DocumentService::class);
87+ // Reset document session to avoid manual conflict resolution if there's no unsaved steps
88+ try {
89+ $ documentService ->resetDocument ($ node ->getId ());
90+ } catch (DocumentHasUnsavedChangesException |NotFoundException $ e ) {
91+ // Do not throw during event handling in this is expected to happen
92+ // DocumentHasUnsavedChangesException: A document editing session is likely ongoing, someone can resolve the conflict
93+ // NotFoundException: The event was called oin a file that was just created so a NonExistingFile object is used that has no id yet
94+ }
95+ }
7096}
0 commit comments