Skip to content

Commit

Permalink
SAK-50223 sakai-forums only show sites with activity (sakaiproject#12662
Browse files Browse the repository at this point in the history
)
  • Loading branch information
adrianfish authored Jun 20, 2024
1 parent b70d47c commit 52be9b3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,39 @@

import org.sakaiproject.api.app.messageforums.SynopticMsgcntrManager;
import org.sakaiproject.api.app.messageforums.SynopticMsgcntrItem;
import org.sakaiproject.authz.api.SecurityService;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.entity.api.EntityManager;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.ToolConfiguration;
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.user.api.UserDirectoryService;
import org.sakaiproject.user.api.UserNotDefinedException;

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

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

/**
*/
@Slf4j
@RestController
public class ForumsController extends AbstractSakaiApiController {

@Resource
private SynopticMsgcntrManager msgCenterManager;

@Resource
private EntityManager entityManager;

@Resource
private SecurityService securityService;

@Resource(name = "org.sakaiproject.component.api.ServerConfigurationService")
private ServerConfigurationService serverConfigurationService;
@Autowired
private SynopticMsgcntrManager msgCenterManager;

@Resource
private SiteService siteService;
@Autowired
private SiteService siteService;

@Resource
private UserDirectoryService userDirectoryService;
private Predicate<SynopticMsgcntrItem> countFilter = i -> i.getNewMessagesCount() > 0 || i.getNewForumCount() > 0;

private Function<SynopticMsgcntrItem, Map<String, Object>> handler = (item) -> {
private Function<SynopticMsgcntrItem, Map<String, Object>> handler = item -> {

Map<String, Object> map = new HashMap<>();
map.put("messageCount", item.getNewMessagesCount());
Expand Down Expand Up @@ -94,25 +76,17 @@ public class ForumsController extends AbstractSakaiApiController {
return map;
};

@GetMapping(value = "/users/{userEid}/forums", produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/users/{userEid}/forums/summary", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Map<String, Object>> getUserForums(@PathVariable String userEid) throws UserNotDefinedException {

Session session = checkSakaiSession();
return msgCenterManager.getWorkspaceSynopticMsgcntrItems(checkSakaiSession().getUserId())
.stream().filter(countFilter).map(handler).collect(Collectors.toList());
}

List<String> sites = siteService.getUserSites().stream().map(s -> s.getId()).collect(Collectors.toList());

return msgCenterManager.getWorkspaceSynopticMsgcntrItems(session.getUserId())
.stream().filter(si -> sites.contains(si.getSiteId())).map(handler).collect(Collectors.toList());
//.stream().map(handler).collect(Collectors.toList());
}

@GetMapping(value = "/sites/{siteId}/forums", produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/sites/{siteId}/forums/summary", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Map<String, Object>> getSiteForums(@PathVariable String siteId) throws UserNotDefinedException {

Session session = checkSakaiSession();

return msgCenterManager.getSiteSynopticMsgcntrItems(
Arrays.asList(new String[] {session.getUserId()}), siteId)
.stream().map(handler).collect(Collectors.toList());
}
return msgCenterManager.getSiteSynopticMsgcntrItems(List.of(checkSakaiSession().getUserId()), siteId)
.stream().filter(countFilter).map(handler).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { css, html } from "lit";
import { css, html, nothing } from "lit";
import "@sakai-ui/sakai-icon";
import { SakaiPageableElement } from "@sakai-ui/sakai-pageable-element";

export class SakaiForums extends SakaiPageableElement {

static properties = {

showOptions: { state: true },
_showOptions: { state: true },
_i18n: { state: true },
};

Expand All @@ -22,7 +22,7 @@ export class SakaiForums extends SakaiPageableElement {

this.messagesClass = "three-col";

const url = this.siteId ? `/api/sites/${this.siteId}/forums` : `/api/users/${this.userId}/forums`;
const url = this.siteId ? `/api/sites/${this.siteId}/forums/summary` : `/api/users/${this.userId}/forums/summary`;
return fetch(url)
.then(r => {

Expand Down Expand Up @@ -79,19 +79,19 @@ export class SakaiForums extends SakaiPageableElement {
this.repage();
}

set showOptions(value) {
set _showOptions(value) {

const old = this._showOptions;
this._showOptions = value;
if (this._showOptions) {
const old = this.__showOptions;
this.__showOptions = value;
if (this.__showOptions) {
this.messagesClass = "four-col";
} else {
this.messagesClass = "three-col";
}
this.requestUpdate("showOptions", old);
this.requestUpdate("_showOptions", old);
}

get showOptions() { return this._showOptions; }
get _showOptions() { return this.__showOptions; }

shouldUpdate(changedProperties) {
return this._i18n && super.shouldUpdate(changedProperties);
Expand All @@ -101,11 +101,11 @@ export class SakaiForums extends SakaiPageableElement {

return html`
<div id="options">
<input type="checkbox" id="options-checkbox" @click=${e => this.showOptions = e.target.checked}>
<input type="checkbox" id="options-checkbox" @click=${e => this._showOptions = e.target.checked}>
<label for="options-checkbox">${this._i18n.syn_options}</label>
</div>
<div class="messages ${this.messagesClass}">
${this.showOptions ? html`<div class="header">${this._i18n.syn_hide}</div>` : ""}
${this._showOptions ? html`<div class="header">${this._i18n.syn_hide}</div>` : ""}
<div class="header">
<a href="javascript:;"
@click=${this.sortByMessages}
Expand All @@ -131,8 +131,8 @@ export class SakaiForums extends SakaiPageableElement {
</a>
</div>
${this.dataPage.map((m, i) => html`
${!m.hidden || this.showOptions ? html`
${this.showOptions ? html`
${!m.hidden || this._showOptions ? html`
${this._showOptions ? html`
<div class="cell options ${i % 2 === 0 ? "even" : "odd"}">
<input type="checkbox"
@click=${this.toggleSite}
Expand All @@ -141,11 +141,11 @@ export class SakaiForums extends SakaiPageableElement {
title="${this._i18n.syn_hide_tooltip}"
arial-label="${this._i18n.syn_hide_tooltip}">
</div>`
: ""}
: nothing}
<div class="cell ${i % 2 === 0 ? "even" : "odd"}"><a href="${m.messageUrl}">${m.messageCount}</a></div>
<div class="cell ${i % 2 === 0 ? "even" : "odd"}"><a href="${m.forumUrl}">${m.forumCount}</a></div>
<div class="cell ${i % 2 === 0 ? "even" : "odd"}"><a href="${m.siteUrl}">${m.siteTitle}</a></div>
` : ""}
` : nothing}
`)}
</div>
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ syn_hide_tooltip=Hide this site from this forums/messages view
export const userId = "adrian";
export const siteUrl = "/sites/1";

export const userForumsUrl= `/api/users/${userId}/forums`;
export const userForumsUrl= `/api/users/${userId}/forums/summary`;

export const userForums = [
{ messageUrl: "/forums/1/2", forumUrl: "/forums/1", forumCount: 2, messageCount: 3, siteUrl, siteTitle: "A" },
Expand All @@ -24,7 +24,7 @@ export const userForums = [

export const siteId = "xyz";

export const siteForumsUrl= `/api/sites/${siteId}/forums`;
export const siteForumsUrl= `/api/sites/${siteId}/forums/summary`;

export const siteForums = [
{ messageUrl: "/forums/1/2", forumUrl: "/forums/1", forumCount: 2, messageCount: 3, siteUrl, siteTitle: "A" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ describe("sakai-forums tests", () => {
expect(el.shadowRoot.getElementById("options")).to.exist;
expect(el.shadowRoot.querySelectorAll(".messages > div").length).to.equal(9);

expect(el.shadowRoot.getElementById("options-checkbox")).to.exist;
el.shadowRoot.getElementById("options-checkbox").click();
const optionsCheckbox = el.shadowRoot.getElementById("options-checkbox");
expect(optionsCheckbox).to.exist;

optionsCheckbox.click();
await el.updateComplete;

// The extra visibility column should now have rendered, so we should have 12 in total with headers
expect(el.shadowRoot.querySelectorAll(".messages > div").length).to.equal(12);

optionsCheckbox.click();
await el.updateComplete;

expect(el.shadowRoot.querySelectorAll(".messages > div").length).to.equal(9);

const sortByMessagesLink = el.shadowRoot.querySelector(`a[title="${el._i18n.sort_by_messages_tooltip}"]`);
expect(sortByMessagesLink).to.exist;
sortByMessagesLink.click();
Expand All @@ -42,23 +50,19 @@ describe("sakai-forums tests", () => {
sortByMessagesLink.click();
await el.updateComplete;

expect(el.shadowRoot.querySelectorAll(".messages > div.cell > a").item(1).innerHTML).to.contain("5");
expect(el.shadowRoot.querySelectorAll(".messages > div.cell > a").item(0).innerHTML).to.contain("5");

const sortByForumsLink = el.shadowRoot.querySelector(`a[title="${el._i18n.sort_by_forums_tooltip}"]`);
expect(sortByForumsLink).to.exist;
sortByForumsLink.click();

await el.updateComplete;

expect(el.shadowRoot.querySelectorAll(".messages > div.cell > a").item(1).innerHTML).to.contain("3");
sortByMessagesLink.click();

await el.updateComplete;
expect(el.shadowRoot.querySelectorAll(".messages > div.cell > a").item(1).innerHTML).to.contain("8");
sortByForumsLink.click();

const sortBySiteLink = el.shadowRoot.querySelector(`a[title="${el._i18n.sort_by_site_tooltip}"]`);
expect(sortBySiteLink).to.exist;
sortBySiteLink.click();
await el.updateComplete;
expect(el.shadowRoot.querySelectorAll(".messages > div.cell > a").item(1).innerHTML).to.contain("2");
});

it ("is accessible", async () => {
Expand Down

0 comments on commit 52be9b3

Please sign in to comment.