Skip to content

Commit

Permalink
SAK-50323 announcementswc Allow super users to view announcements (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored Sep 11, 2024
1 parent d03e033 commit 6999e60
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ public AnnouncementChannelEdit addAnnouncementChannel(String ref) throws IdUsedE
* if the user does not have read permission to the channel.
* @exception NullPointerException
*/
public List getMessages(String channelReference, Filter filter, boolean ascending, boolean merged) throws IdUnusedException, PermissionException, NullPointerException;
public List<AnnouncementMessage> getMessages(String channelReference, Filter filter, boolean ascending, boolean merged) throws IdUnusedException, PermissionException, NullPointerException;

public Filter getMaxAgeInDaysAndAmountFilter(Integer maxAgeInDays, Integer ammount);

/**
* Return a list of messages based on the supplied arguments. If you want all of a user's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1065,9 +1065,10 @@ public AnnouncementChannelEdit addAnnouncementChannel(String ref) throws IdUsedE
* if the user does not have read permission to the channel.
* @exception NullPointerException
*/
public List getMessages(String channelReference,Filter filter, boolean ascending, boolean merged) throws IdUnusedException, PermissionException, NullPointerException {
public List<AnnouncementMessage> getMessages(String channelReference,Filter filter, boolean ascending, boolean merged) throws IdUnusedException, PermissionException, NullPointerException {

List<AnnouncementMessage> messageList = new ArrayList<>();

List<Message> messageList = new ArrayList<>();
filter = new PrivacyFilter(filter); // filter out drafts this user cannot see
Site site = null;
String initMergeList = null;
Expand Down Expand Up @@ -1271,15 +1272,25 @@ private List<String> getExcludedSitesFromTabs() {
return l;
}

@Override
public Filter getMaxAgeInDaysAndAmountFilter(Integer maxAgeInDays, Integer amount) {

if (maxAgeInDays == null) maxAgeInDays = 10;

if (amount == null) amount = 100;

ViewableFilter viewableFilter = new ViewableFilter(null, null, amount, this);
long now = Instant.now().toEpochMilli();
Time afterDate = m_timeService.newTime(now - (maxAgeInDays * 24 * 60 * 60 * 1000));
viewableFilter.setFilter(new MessageSelectionFilter(afterDate, null, false));
return viewableFilter;
}

public List<AnnouncementMessage> getChannelMessages(String channelReference, Filter filter, boolean ascending,
String mergedChannelDelimitedList, boolean allUsersSites, boolean isSynopticTool, String siteId, Integer maxAgeInDays) throws PermissionException {

if (filter == null && maxAgeInDays != null) {
ViewableFilter viewableFilter = new ViewableFilter(null, null, Integer.MAX_VALUE, this);
long now = Instant.now().toEpochMilli();
Time afterDate = m_timeService.newTime(now - (maxAgeInDays * 24 * 60 * 60 * 1000));
viewableFilter.setFilter(new MessageSelectionFilter(afterDate, null, false));
filter = viewableFilter;
filter = getMaxAgeInDaysAndAmountFilter(maxAgeInDays, Integer.MAX_VALUE);
}

List<AnnouncementMessage> messageList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.sakaiproject.entity.api.Entity;
import org.sakaiproject.entity.api.EntityManager;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.exception.PermissionException;
import org.sakaiproject.javax.Filter;
import org.sakaiproject.portal.api.PortalService;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.site.api.Site;
Expand All @@ -28,16 +30,15 @@
import org.springframework.http.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -54,10 +55,6 @@ public class AnnouncementsController extends AbstractSakaiApiController {
@Autowired
private PortalService portalService;

@Autowired
@Qualifier("org.sakaiproject.component.api.ServerConfigurationService")
private ServerConfigurationService serverConfigurationService;

@Autowired
private SiteService siteService;

Expand All @@ -66,27 +63,31 @@ public List<AnnouncementRestBean> getUserAnnouncements() throws UserNotDefinedEx

checkSakaiSession();

List<String> pinnedSites = portalService.getPinnedSites();
Filter filter = announcementService.getMaxAgeInDaysAndAmountFilter(10, 100);

try {
return announcementService.getChannelMessages(null, null, true, null, true, true, null, 10)
.stream()
.map(am -> {

Optional<String> optionalUrl = entityManager.getUrl(am.getReference(), Entity.UrlType.PORTAL);
String siteId = entityManager.newReference(am.getReference()).getContext();

if (!pinnedSites.contains(siteId)) return null;

try {
return new AnnouncementRestBean(siteService.getSite(siteId), am, optionalUrl.get());
} catch (IdUnusedException idue) {
log.error("Invalid announcement message. No site for id {}", siteId, idue.toString());
}
return null;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
return portalService.getPinnedSites().stream().flatMap(siteId -> {

try {
Site site = siteService.getSite(siteId);

return announcementService.getMessages(announcementService.channelReference(siteId, SiteService.MAIN_CONTAINER), filter, true, false)
.stream()
.map(am -> {

Optional<String> optionalUrl = entityManager.getUrl(am.getReference(), Entity.UrlType.PORTAL);
return new AnnouncementRestBean(site, am, optionalUrl.get());
});
} catch (IdUnusedException idue) {
log.warn("Failed to get messages for site {}: {}", siteId, idue.toString());
return Stream.<AnnouncementRestBean>empty();
} catch (PermissionException pe) {
log.warn("No permission to get messages for site id {}", siteId, pe.toString());
return Stream.<AnnouncementRestBean>empty();
}
})
.collect(Collectors.toList());
} catch (Exception ex) {
log.error("Error getting announcements: {}", ex.toString());
}
Expand Down

0 comments on commit 6999e60

Please sign in to comment.