1010use OCA \Text \Event \RegisterContextEvent ;
1111use OCP \EventDispatcher \IEventDispatcher ;
1212use OCP \Files \NotFoundException ;
13+ use OCP \Files \NotPermittedException ;
14+ use OCP \IUserSession ;
1315use Psr \Log \LoggerInterface ;
16+ use Psr \Container \ContainerInterface ;
1417
1518class ContextManager {
1619 /** @var array<string, callable> */
1720 private array $ contexts = [];
1821 public function __construct (
22+ private readonly ContainerInterface $ c ,
1923 private readonly IEventDispatcher $ eventDispatcher ,
2024 private readonly LoggerInterface $ logger ,
25+ private readonly IUserSession $ userSession ,
2126 ) {
2227 }
2328
@@ -33,24 +38,30 @@ private function getContexts(): array {
3338 return $ this ->contexts ;
3439 }
3540
36- public function registerContext (string $ type , callable $ createContext ): void {
41+ public function registerContext (string $ type , string $ factoryClassName ): void {
3742 $ this ->logger ->debug ('Registering context for type " ' . $ type . '". ' );
3843 if (array_key_exists ($ type , $ this ->contexts )) {
3944 $ this ->logger ->warning ('Context of type " ' . $ type . '" was already registered! ' );
4045 return ;
4146 }
42- $ this ->contexts [$ type ] = $ createContext ;
47+ $ this ->contexts [$ type ] = $ factoryClassName ;
4348 }
4449
4550 public function getContext (string $ type , int $ id , ?string $ shareToken ): IContext {
46- $ createContext = $ this ->getContexts ()[$ type ];
47- if (! is_callable ( $ createContext ) ) {
51+ $ factoryClassName = $ this ->getContexts ()[$ type ];
52+ if ($ factoryClassName === null ) {
4853 throw new NotFoundException ('Context of type " ' . $ type . '" was not registered! ' );
4954 }
50- $ context = $ createContext ($ id , $ type , $ shareToken );
51- if (!$ context instanceof IContext) {
52- throw new NotFoundException ('Failed to create context of type ' . $ type . '! ' );
55+ $ factory = $ this ->c ->get ($ factoryClassName );
56+
57+ if ($ shareToken === null ) {
58+ $ user = $ this ->userSession ->getUser ();
59+ if ($ user === null ) {
60+ throw new NotPermittedException ();
61+ }
62+ return $ factory ->buildForUser ($ user , $ id );
63+ } else {
64+ return $ factory ->buildForShare ($ shareToken , $ id );
5365 }
54- return $ context ;
5566 }
5667}
0 commit comments