Skip to content

Commit

Permalink
feat-NoticeController-通知发布的时候可以对外广播消息
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Oct 28, 2024
1 parent 9e827ad commit 4bf4997
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.ximatai.muyun.platform;

public class BusChannel {

public static final String ROLE_CHANGED = "role_changed";

public static String channelForUser(String userID) {
return "web.user.%s".formatted(userID);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import net.ximatai.muyun.ability.IReferenceAbility;
import net.ximatai.muyun.base.BaseBusinessTable;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.TableWrapper;
import net.ximatai.muyun.model.ReferenceInfo;
import net.ximatai.muyun.platform.BusChannel;
import net.ximatai.muyun.platform.ScaffoldForPlatform;
import net.ximatai.muyun.platform.ability.IModuleRegisterAbility;
import net.ximatai.muyun.platform.model.Dict;
import net.ximatai.muyun.platform.model.DictCategory;
import net.ximatai.muyun.platform.model.MessageToFrontEnd;
import net.ximatai.muyun.platform.model.ModuleAction;
import net.ximatai.muyun.platform.model.ModuleConfig;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH;
import static net.ximatai.muyun.platform.controller.NoticeController.MODULE_ALIAS;

@Startup
@Tag(name = "公告发布")
@Path(BASE_PATH + "/" + MODULE_ALIAS)
public class NoticeController extends ScaffoldForPlatform implements IModuleRegisterAbility {
public class NoticeController extends ScaffoldForPlatform implements IModuleRegisterAbility, IReferenceAbility {
public final static String MODULE_ALIAS = "notice";

@Inject
Expand All @@ -35,6 +42,9 @@ public class NoticeController extends ScaffoldForPlatform implements IModuleRegi
@Inject
ModuleController moduleController;

@Inject
UserInfoController userInfoController;

@Override
public String getMainTable() {
return "app_notice";
Expand Down Expand Up @@ -72,11 +82,13 @@ public void fitOut(TableWrapper wrapper) {
@Path("/release/{id}")
@Operation(summary = "发布公告")
public int release(@PathParam("id") String id) {
return getDB().updateItem(getSchemaName(), getMainTable(), Map.of(
int updated = getDB().updateItem(getSchemaName(), getMainTable(), Map.of(
"id", id,
"b_release", true,
"t_release", LocalDateTime.now()
));
broadcast(id);
return updated;
}

@GET
Expand All @@ -103,4 +115,45 @@ public ModuleConfig getModuleConfig() {
.addAction(new ModuleAction("release", "发布通知", ModuleAction.TypeLike.UPDATE))
.addAction(new ModuleAction("rollback", "撤销发布", ModuleAction.TypeLike.UPDATE));
}

private void broadcast(String id) {
Map<String, ?> map = view(id);
String dictNoticeAccessScope = (String) map.get("dict_notice_access_scope");
Set<String> users = switch (dictNoticeAccessScope) {
case "open" ->
getDB().query("select id from platform.auth_user where b_enabled = true").stream().map(it -> (String) it.get("id")).collect(Collectors.toSet());
case "organization" -> getDB().query("""
select auth_user.id
from platform.auth_user
join platform.auth_userinfo on auth_user.id = auth_userinfo.id
where b_enabled = true
and id_at_org_organization = any(select ids_at_org_organization from platform.app_notice where id = ?)
""", id).stream().map(it -> (String) it.get("id")).collect(Collectors.toSet());
case "department" -> getDB().query("""
select auth_user.id
from platform.auth_user
join platform.auth_userinfo on auth_user.id = auth_userinfo.id
where b_enabled = true
and id_at_org_department = any(select ids_at_org_department from platform.app_notice where id = ?)
""", id).stream().map(it -> (String) it.get("id")).collect(Collectors.toSet());
default -> Set.of();
};

MessageToFrontEnd message = new MessageToFrontEnd(
"收到新通知啦",
"%s 刚刚发布了《%s》的通知".formatted(map.get("v_name_from"), map.get("v_title")),
""
);

users.forEach(it -> {
getEventBus().publish(BusChannel.channelForUser(it), message.toJson());
});
}

@Override
public List<ReferenceInfo> getReferenceList() {
return List.of(
userInfoController.toReferenceInfo("id_at_auth_user__create").add("v_name", "v_name_from")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static net.ximatai.muyun.platform.BusChannel.ROLE_CHANGED;
import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH;
import static net.ximatai.muyun.platform.controller.UserInfoController.MODULE_ALIAS;

Expand Down Expand Up @@ -272,7 +273,7 @@ public Set<String> roles(@PathParam("userID") String userID) {
public Integer setRoles(@PathParam("userID") String userID, List<String> roles) {
userController.putChildTableList(userID, "auth_user_role", List.of());
int create = userController.putChildTableList(userID, "auth_user_role", roles.stream().map(it -> Map.of("id_at_auth_role", it)).toList()).getCreate();
getEventBus().publish("role_changed", userID);
getEventBus().publish(ROLE_CHANGED, userID);
return create;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.ximatai.muyun.platform.model;

import io.vertx.core.json.JsonObject;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class MessageToFrontEnd {
private final String title;
private final String content;
private final LocalDateTime createdAt;
private final String url;

public MessageToFrontEnd(String title, String content, String url) {
this(title, content, url, LocalDateTime.now());
}

public MessageToFrontEnd(String title, String content, String url, LocalDateTime createdAt) {
this.title = title;
this.content = content;
this.url = url;
this.createdAt = createdAt;
}

public JsonObject toJson() {
JsonObject json = new JsonObject();
json.put("title", title);
json.put("content", content);
json.put("createdAt", createdAt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
return json;
}
}

0 comments on commit 4bf4997

Please sign in to comment.