1010use Exception ;
1111use OC \App \CompareVersion ;
1212use OCA \DAV \CardDAV \CardDavBackend ;
13- use OCA \CloudFederationAPI \Db \FederatedInviteMapper ;
1413use OCA \Contacts \AppInfo \Application ;
14+ use OCA \Contacts \Db \FederatedInvite ;
15+ use OCA \Contacts \Db \FederatedInviteMapper ;
16+ use OCA \Contacts \Service \FederatedInvitesService ;
1517use OCA \Contacts \Service \GroupSharingService ;
1618use OCA \Contacts \Service \SocialApiService ;
1719use OCA \FederatedFileSharing \AddressHandler ;
1820use OCP \App \IAppManager ;
1921use OCP \AppFramework \Http ;
2022use OCP \AppFramework \Http \DataResponse ;
2123use OCP \AppFramework \Http \TemplateResponse ;
24+ use OCP \AppFramework \Utility \ITimeFactory ;
2225use OCP \Contacts \IManager ;
2326use OCP \Http \Client \IClientService ;
2427use OCP \IConfig ;
3033use OCP \IUserSession ;
3134use OCP \L10N \IFactory ;
3235use Psr \Log \LoggerInterface ;
36+ use Sabre \DAV \UUIDUtil ;
3337
3438/**
3539 * Controller for federated invites related routes.
@@ -43,24 +47,27 @@ public function __construct(
4347 IRequest $ request ,
4448 private AddressHandler $ addressHandler ,
4549 private CardDavBackend $ cardDavBackend ,
50+ private FederatedInviteMapper $ federatedInviteMapper ,
51+ private FederatedInvitesService $ federatedInvitesService ,
52+ private IAppManager $ appManager ,
4653 private IClientService $ httpClient ,
4754 private IConfig $ config ,
4855 private IInitialStateService $ initialStateService ,
4956 private IFactory $ languageFactory ,
5057 private IManager $ contactsManager ,
5158 private IUserSession $ userSession ,
5259 private SocialApiService $ socialApiService ,
53- private IAppManager $ appManager ,
60+ private ITimeFactory $ timeFactory ,
5461 private CompareVersion $ compareVersion ,
5562 private GroupSharingService $ groupSharingService ,
5663 private IL10N $ il10 ,
5764 private IURLGenerator $ urlGenerator ,
5865 private IUserManager $ userManager ,
59- private FederatedInviteMapper $ federatedInviteMapper ,
6066 private LoggerInterface $ logger ,
6167 ) {
6268 parent ::__construct (
6369 $ request ,
70+ $ federatedInvitesService ,
6471 $ config ,
6572 $ initialStateService ,
6673 $ languageFactory ,
@@ -73,6 +80,28 @@ public function __construct(
7380 );
7481 }
7582
83+ /**
84+ * @NoAdminRequired
85+ * @NoCSRFRequired
86+ *
87+ * Returns all open (not yet accepted) invites.
88+ *
89+ * @return DataResponse
90+ */
91+ public function getInvites (): DataResponse {
92+ $ _invites = $ this ->federatedInviteMapper ->findOpenInvitesByUiddd ($ this ->userSession ->getUser ()->getUID ());
93+ $ invites = [];
94+ foreach ($ _invites as $ invite ) {
95+ if ($ invite instanceof FederatedInvite) {
96+ array_push (
97+ $ invites ,
98+ $ invite ->jsonSerialize ()
99+ );
100+ }
101+ }
102+ return new DataResponse ($ invites , Http::STATUS_OK );
103+ }
104+
76105 /**
77106 * @NoAdminRequired
78107 * @NoCSRFRequired
@@ -82,9 +111,7 @@ public function __construct(
82111 * @param string $token
83112 * @param string $provider
84113 */
85- public function inviteAcceptDialog (string $ token = "" , string $ provider = "" ): TemplateResponse
86- {
87- $ this ->logger ->debug (" - FederatedInvitesController inviteAcceptDialog method: setting initial state ( $ token, $ provider) and returning PageController index " , ['app ' => Application::APP_ID ]);
114+ public function inviteAcceptDialog (string $ token = "" , string $ provider = "" ): TemplateResponse {
88115 $ this ->initialStateService ->provideInitialState (Application::APP_ID , 'inviteToken ' , $ token );
89116 $ this ->initialStateService ->provideInitialState (Application::APP_ID , 'inviteProvider ' , $ provider );
90117 // TODO read from config
@@ -99,11 +126,29 @@ public function inviteAcceptDialog(string $token = "", string $provider = ""): T
99126 *
100127 * Creates an invitation to exchange contact info for the user with the specified uid.
101128 *
102- * @return DataResponse
129+ * @param string $email the recipient email to send the invitation to
130+ * @param string $message the optional message to send with the invitation
131+ * @return DataResponse with data signature ['token' | 'error'] - the token of the invitation or an error message in case of error
103132 */
104- public function createInvite (string $ uid = '' ): DataResponse
105- {
106- return new DataResponse (['message ' => "Route not implemented " ], Http::STATUS_NOT_IMPLEMENTED );
133+ public function createInvite (string $ email = null , string $ message = null ): DataResponse {
134+ if (!isset ($ email )) {
135+ return new DataResponse (['error ' => 'Recipient email is required ' ], Http::STATUS_BAD_REQUEST );
136+ }
137+ $ invite = new FederatedInvite ();
138+ $ invite ->setUserId ($ this ->userSession ->getUser ()->getUID ());
139+ $ token = UUIDUtil::getUUID ();
140+ $ invite ->setToken ($ token );
141+ $ invite ->setCreatedAt ($ this ->timeFactory ->getTime ());
142+ // TODO get expiration period from config
143+ // take 30 days
144+ $ invite ->setExpiredAt ($ invite ->getCreatedAt () + 2592000000 );
145+ $ invite ->setRecipientEmail ($ email );
146+ $ invite ->setAccepted (false );
147+ $ this ->federatedInviteMapper ->insert ($ invite );
148+
149+ // TODO send email
150+
151+ return new DataResponse (['token ' => $ token ], Http::STATUS_OK );
107152 }
108153
109154 /**
@@ -115,12 +160,12 @@ public function createInvite(string $uid = ''): DataResponse
115160 *
116161 * @param string $token the token of the invite
117162 * @param string $provider the provider of the sender of the invite
118- * @return DataResponse with data signature ['contact' | 'message '] - the new contact url or a message in case of error
163+ * @return DataResponse with data signature ['contact' | 'error '] - the new contact url or an error message in case of error
119164 */
120165 public function inviteAccepted (string $ token = "" , string $ provider = "" ): DataResponse {
121166 if ($ token === "" || $ provider === "" ) {
122167 $ this ->logger ->error ("Both token and provider must be specified. Received: token= $ token, provider= $ provider " , ['app ' => Application::APP_ID ]);
123- return new DataResponse (['message ' => 'Both token and provider must be specified. ' ], Http::STATUS_NOT_FOUND );
168+ return new DataResponse (['error ' => 'Both token and provider must be specified. ' ], Http::STATUS_NOT_FOUND );
124169 }
125170 try {
126171 // delegate further to OCM /invite-accepted
@@ -147,14 +192,14 @@ public function inviteAccepted(string $token = "", string $provider = ""): DataR
147192 $ responseData = $ response ->getBody ();
148193 $ data = json_decode ($ responseData , true );
149194 $ newContact = $ this ->socialApiService ->createFederatedContact (
150- // nextcloud cloud id format, ie. the ocm address
195+ // the ocm address: nextcloud cloud id format
151196 $ data ['userID ' ] . "@ " . $ this ->addressHandler ->removeProtocolFromUrl ($ provider ),
152197 $ data ['email ' ],
153198 $ data ['name ' ],
154199 $ localUser ->getUID (),
155200 );
156201 if (!isset ($ newContact )) {
157- return new DataResponse (['message ' => 'An unexpected error occurred trying to accept invite: could not create new contact ' ], Http::STATUS_NOT_FOUND );
202+ return new DataResponse (['error ' => 'An unexpected error occurred trying to accept invite: could not create new contact ' ], Http::STATUS_NOT_FOUND );
158203 }
159204 $ this ->logger ->info ("Created new contact with UID: " . $ newContact ['UID ' ] . " for user with UID: " . $ localUser ->getUID (), ['app ' => Application::APP_ID ]);
160205
@@ -172,15 +217,15 @@ public function inviteAccepted(string $token = "", string $provider = ""): DataR
172217 $ statusCode = $ e ->getCode ();
173218 switch ($ statusCode ) {
174219 case Http::STATUS_BAD_REQUEST :
175- return new DataResponse (['message ' => 'Invalid, non existing or expired token ' ], $ e ->getCode ());
220+ return new DataResponse (['error ' => 'Invalid, non existing or expired token ' ], $ e ->getCode ());
176221 case Http::STATUS_CONFLICT :
177- return new DataResponse (['message ' => 'Invite already accepted ' ], $ e ->getCode ());
222+ return new DataResponse (['error ' => 'Invite already accepted ' ], $ e ->getCode ());
178223 }
179224 $ this ->logger ->error ("An unexpected error occurred accepting invite with token= $ token and provider= $ provider. Stacktrace: " . $ e ->getTraceAsString (), ['app ' => Application::APP_ID ]);
180- return new DataResponse (['message ' => 'An unexpected error occurred trying to accept invite. ' ], Http::STATUS_NOT_FOUND );
225+ return new DataResponse (['error ' => 'An unexpected error occurred trying to accept invite. ' ], Http::STATUS_NOT_FOUND );
181226 } catch (Exception $ e ) {
182227 $ this ->logger ->error ("An unexpected error occurred accepting invite with token= $ token and provider= $ provider. Stacktrace: " . $ e ->getTraceAsString (), ['app ' => Application::APP_ID ]);
183- return new DataResponse (['message ' => 'An unexpected error occurred trying to accept invite ' ], Http::STATUS_NOT_FOUND );
228+ return new DataResponse (['error ' => 'An unexpected error occurred trying to accept invite ' ], Http::STATUS_NOT_FOUND );
184229 }
185230 }
186231}
0 commit comments