1616use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
1717use OCP \AppFramework \Http \Attribute \NoCSRFRequired ;
1818use OCP \AppFramework \Http \DataResponse ;
19+ use OCP \AppFramework \OCS \OCSBadRequestException ;
20+ use OCP \AppFramework \OCS \OCSNotFoundException ;
1921use OCP \AppFramework \OCSController ;
2022use OCP \AppFramework \Services \IAppConfig ;
2123use OCP \Config \IUserConfig ;
2628use OCP \Dashboard \IManager ;
2729use OCP \Dashboard \IOptionWidget ;
2830use OCP \Dashboard \IReloadableWidget ;
31+ use OCP \Dashboard \ITeamWidget ;
2932use OCP \Dashboard \IWidget ;
3033use OCP \Dashboard \Model \WidgetButton ;
3134use OCP \Dashboard \Model \WidgetItem ;
3235use OCP \Dashboard \Model \WidgetOptions ;
3336use OCP \IRequest ;
37+ use OCP \Teams \ITeamManager ;
38+ use Psr \Log \LoggerInterface ;
3439
3540/**
3641 * @psalm-import-type DashboardWidget from ResponseDefinitions
@@ -47,6 +52,8 @@ public function __construct(
4752 private IUserConfig $ userConfig ,
4853 private ?string $ userId ,
4954 private DashboardService $ service ,
55+ private ITeamManager $ teamManager ,
56+ private LoggerInterface $ logger ,
5057 ) {
5158 parent ::__construct ($ appName , $ request );
5259 }
@@ -69,6 +76,44 @@ static function (IWidget $widget) use ($widgetIds) {
6976 );
7077 }
7178
79+ private function validateTeamAccess (?string $ teamId ): ?string {
80+ if ($ teamId === null ) {
81+ return null ;
82+ }
83+ if ($ teamId === '' ) {
84+ throw new OCSBadRequestException ('Empty team ID ' );
85+ }
86+ if ($ this ->userId === null || !$ this ->teamManager ->isMemberOfTeam ($ teamId , $ this ->userId )) {
87+ throw new OCSNotFoundException ('Team not found ' );
88+ }
89+ return $ teamId ;
90+ }
91+
92+ /**
93+ * @param list<string> $widgetIds
94+ * @return array<string, ITeamWidget>
95+ */
96+ private function getTeamWidgets (string $ teamId , array $ widgetIds ): array {
97+ $ widgets = [];
98+ foreach ($ this ->dashboardManager ->getWidgets () as $ id => $ widget ) {
99+ if (!$ widget instanceof ITeamWidget || ($ widgetIds !== [] && !in_array ($ id , $ widgetIds , true ))) {
100+ continue ;
101+ }
102+ try {
103+ if ($ widget ->isEnabledForTeam ($ teamId , $ this ->userId )) {
104+ $ widgets [$ id ] = $ widget ;
105+ }
106+ } catch (\Throwable $ e ) {
107+ $ this ->logger ->warning ('Failed to check dashboard widget applicability for team ' , [
108+ 'widget ' => $ id ,
109+ 'team ' => $ teamId ,
110+ 'exception ' => $ e ,
111+ ]);
112+ }
113+ }
114+ return $ widgets ;
115+ }
116+
72117 /**
73118 * Get the items for the widgets
74119 *
@@ -104,15 +149,34 @@ public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widg
104149 * @param int $limit Limit number of result items per widget, not more than 30 are allowed
105150 * @psalm-param int<1, 30> $limit
106151 * @param list<string> $widgets Limit results to specific widgets
152+ * @param ?string $teamId Team to show widgets for; omit for the personal dashboard
107153 * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}>
154+ * @throws OCSNotFoundException Team not found
155+ * @throws OCSBadRequestException Empty team ID
108156 *
109157 * 200: Widget items returned
110158 */
111159 #[NoAdminRequired]
112160 #[NoCSRFRequired]
113161 #[ApiRoute(verb: 'GET ' , url: '/api/v2/widget-items ' )]
114- public function getWidgetItemsV2 (array $ sinceIds = [], int $ limit = 7 , array $ widgets = []): DataResponse {
162+ public function getWidgetItemsV2 (array $ sinceIds = [], int $ limit = 7 , array $ widgets = [], ?string $ teamId = null ): DataResponse {
163+ $ teamId = $ this ->validateTeamAccess ($ teamId );
115164 $ items = [];
165+ if ($ teamId !== null ) {
166+ foreach ($ this ->getTeamWidgets ($ teamId , $ widgets ) as $ id => $ widget ) {
167+ try {
168+ $ items [$ id ] = $ widget ->getTeamItems ($ teamId , $ this ->userId , $ sinceIds [$ id ] ?? null , $ limit )->jsonSerialize ();
169+ } catch (\Throwable $ e ) {
170+ $ this ->logger ->warning ('Failed to get dashboard widget items for team ' , [
171+ 'widget ' => $ id ,
172+ 'team ' => $ teamId ,
173+ 'exception ' => $ e ,
174+ ]);
175+ }
176+ }
177+ return new DataResponse ($ items );
178+ }
179+
116180 $ widgets = $ this ->getShownWidgets ($ widgets );
117181 foreach ($ widgets as $ widget ) {
118182 if ($ widget instanceof IAPIWidgetV2) {
@@ -128,55 +192,80 @@ public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $wi
128192 /**
129193 * Get the widgets
130194 *
195+ * @param ?string $teamId Team to show widgets for; omit for the personal dashboard
131196 * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidget>, array{}>
197+ * @throws OCSNotFoundException Team not found
198+ * @throws OCSBadRequestException Empty team ID
132199 *
133200 * 200: Widgets returned
134201 */
135202 #[NoAdminRequired]
136203 #[NoCSRFRequired]
137204 #[ApiRoute(verb: 'GET ' , url: '/api/v1/widgets ' )]
138- public function getWidgets (): DataResponse {
139- $ widgets = $ this ->dashboardManager ->getWidgets ();
140-
141- $ items = array_map (function (IWidget $ widget ) {
142- $ options = ($ widget instanceof IOptionWidget) ? $ widget ->getWidgetOptions () : WidgetOptions ::getDefault ();
143- $ data = [
144- 'id ' => $ widget ->getId (),
145- 'title ' => $ widget ->getTitle (),
146- 'order ' => $ widget ->getOrder (),
147- 'icon_class ' => $ widget ->getIconClass (),
148- 'icon_url ' => ($ widget instanceof IIconWidget) ? $ widget ->getIconUrl () : '' ,
149- 'widget_url ' => $ widget ->getUrl (),
150- 'item_icons_round ' => $ options ->withRoundItemIcons (),
151- 'item_api_versions ' => [],
152- 'reload_interval ' => 0 ,
153- ];
154- if ($ widget instanceof IButtonWidget) {
155- $ data += [
156- 'buttons ' => array_map (function (WidgetButton $ button ) {
157- return [
158- 'type ' => $ button ->getType (),
159- 'text ' => $ button ->getText (),
160- 'link ' => $ button ->getLink (),
161- ];
162- }, $ widget ->getWidgetButtons ($ this ->userId )),
163- ];
205+ public function getWidgets (?string $ teamId = null ): DataResponse {
206+ $ teamId = $ this ->validateTeamAccess ($ teamId );
207+ if ($ teamId !== null ) {
208+ $ items = [];
209+ foreach ($ this ->getTeamWidgets ($ teamId , []) as $ id => $ widget ) {
210+ try {
211+ $ items [$ id ] = $ this ->serializeWidget ($ widget , $ widget ->getTeamUrl ($ teamId , $ this ->userId ) ?? $ widget ->getUrl ());
212+ } catch (\Throwable $ e ) {
213+ $ this ->logger ->warning ('Failed to get dashboard widget metadata for team ' , [
214+ 'widget ' => $ id ,
215+ 'team ' => $ teamId ,
216+ 'exception ' => $ e ,
217+ ]);
218+ }
164219 }
165- if ($ widget instanceof IReloadableWidget) {
166- $ data ['reload_interval ' ] = $ widget ->getReloadInterval ();
167- }
168- if ($ widget instanceof IAPIWidget) {
169- $ data ['item_api_versions ' ][] = 1 ;
170- }
171- if ($ widget instanceof IAPIWidgetV2) {
172- $ data ['item_api_versions ' ][] = 2 ;
173- }
174- return $ data ;
175- }, $ widgets );
220+ } else {
221+ $ items = array_map (
222+ fn (IWidget $ widget ) => $ this ->serializeWidget ($ widget , $ widget ->getUrl ()),
223+ $ this ->dashboardManager ->getWidgets (),
224+ );
225+ }
176226
177227 return new DataResponse ($ items );
178228 }
179229
230+ /**
231+ * @return DashboardWidget
232+ */
233+ private function serializeWidget (IWidget $ widget , ?string $ url ): array {
234+ $ options = ($ widget instanceof IOptionWidget) ? $ widget ->getWidgetOptions () : WidgetOptions ::getDefault ();
235+ $ data = [
236+ 'id ' => $ widget ->getId (),
237+ 'title ' => $ widget ->getTitle (),
238+ 'order ' => $ widget ->getOrder (),
239+ 'icon_class ' => $ widget ->getIconClass (),
240+ 'icon_url ' => ($ widget instanceof IIconWidget) ? $ widget ->getIconUrl () : '' ,
241+ 'widget_url ' => $ url ,
242+ 'item_icons_round ' => $ options ->withRoundItemIcons (),
243+ 'item_api_versions ' => [],
244+ 'reload_interval ' => 0 ,
245+ ];
246+ if ($ widget instanceof IButtonWidget) {
247+ $ data += [
248+ 'buttons ' => array_map (function (WidgetButton $ button ) {
249+ return [
250+ 'type ' => $ button ->getType (),
251+ 'text ' => $ button ->getText (),
252+ 'link ' => $ button ->getLink (),
253+ ];
254+ }, $ widget ->getWidgetButtons ($ this ->userId )),
255+ ];
256+ }
257+ if ($ widget instanceof IReloadableWidget) {
258+ $ data ['reload_interval ' ] = $ widget ->getReloadInterval ();
259+ }
260+ if ($ widget instanceof IAPIWidget) {
261+ $ data ['item_api_versions ' ][] = 1 ;
262+ }
263+ if ($ widget instanceof IAPIWidgetV2) {
264+ $ data ['item_api_versions ' ][] = 2 ;
265+ }
266+ return $ data ;
267+ }
268+
180269 /**
181270 * Get the layout
182271 *
0 commit comments