Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Implement /inbox/summary?user=xxx so to return inbox summary for a particular user

天舟a5fbb55b770c
diff --git a/api/inbox.go b/api/inbox.go
index 23d356acb8..0d3f429dcc 100644
--- a/api/inbox.go
+++ b/api/inbox.go
@@ -81,10 +81,19 @@ type InboxPatch struct {
Status InboxStatus `jsonapi:"attr,status"`
}
+// Contains the inbox summary info.
+// This is used by the frontend to render the inbox sidebar item without fetching the actual inbox items.
+// This returns json instead of jsonapi since it't not dealing with a particular resource.
+type InboxSummary struct {
+ HasUnread bool `json:"hasUnread"`
+ HasUnreadError bool `json:"hasUnreadError"`
+}
+
type InboxService interface {
CreateInbox(ctx context.Context, create *InboxCreate) (*Inbox, error)
// Find the inbox list and return most recent created item first.
FindInboxList(ctx context.Context, find *InboxFind) ([]*Inbox, error)
FindInbox(ctx context.Context, find *InboxFind) (*Inbox, error)
PatchInbox(ctx context.Context, patch *InboxPatch) (*Inbox, error)
+ FindInboxSummary(ctx context.Context, principalId int) (*InboxSummary, error)
}
diff --git a/frontend/src/components/InboxList.vue b/frontend/src/components/InboxList.vue
index b26c92f0e6..cae2fa5507 100644
--- a/frontend/src/components/InboxList.vue
+++ b/frontend/src/components/InboxList.vue
@@ -82,7 +82,7 @@
</template>
<script lang="ts">
-import { PropType } from "@vue/runtime-core";
+import { computed, PropType } from "@vue/runtime-core";
import PrincipalAvatar from "../components/PrincipalAvatar.vue";
import {
ActionIssueCommentCreatePayload,
@@ -115,6 +115,8 @@ export default {
const store = useStore();
const router = useRouter();
+ const currentUser = computed(() => store.getters["auth/currentUser"]());
+
const actionLink = (activity: Activity): string => {
if (activity.actionType.startsWith("bb.issue.")) {
return `/issue/${activity.containerId}`;
@@ -233,12 +235,19 @@ export default {
const clickInbox = (inbox: Inbox) => {
if (inbox.status == "UNREAD") {
- store.dispatch("inbox/patchInbox", {
- inboxId: inbox.id,
- inboxPatch: {
- status: "READ",
- },
- });
+ store
+ .dispatch("inbox/patchInbox", {
+ inboxId: inbox.id,
+ inboxPatch: {
+ status: "READ",
+ },
+ })
+ .then(() => {
+ store.dispatch(
+ "inbox/fetchInboxSummaryByUser",
+ currentUser.value.id
+ );
+ });
}
const link = actionLink(inbox.activity);
if (!isEmpty(link)) {
diff --git a/frontend/src/store/modules/inbox.ts b/frontend/src/store/modules/inbox.ts
index 20d0c3ca72..aa7a5b4688 100644
--- a/frontend/src/store/modules/inbox.ts
+++ b/frontend/src/store/modules/inbox.ts
@@ -5,6 +5,7 @@ import {
InboxId,
InboxPatch,
InboxState,
+ InboxSummary,
PrincipalId,
ResourceIdentifier,
ResourceObject,
@@ -34,8 +35,15 @@ function convert(
};
}
+function convertSummary(inboxSummary: ResourceObject): InboxSummary {
+ return {
+ ...(inboxSummary.attributes as InboxSummary),
+ };
+}
+
const state: () => InboxState = () => ({
inboxListByUser: new Map(),
+ inboxSummaryByUser: new Map(),
});
const getters = {
@@ -44,6 +52,17 @@ const getters = {
(userId: PrincipalId): Inbox[] => {
return state.inboxListByUser.get(userId) || [];
},
+
+ inboxSummaryByUser:
+ (state: InboxState) =>
+ (userId: PrincipalId): InboxSummary => {
+ return (
+ state.inboxSummaryByUser.get(userId) || {
+ hasUnread: false,
+ hasUnreadError: false,
+ }
+ );
+ },
};
const actions = {
@@ -60,6 +79,14 @@ const actions = {
return inboxList;
},
+ async fetchInboxSummaryByUser({ commit }: any, userId: PrincipalId) {
+ const inboxSummary = (await axios.get(`/api/inbox/summary?user=${userId}`))
+ .data;
+
+ commit("setInboxSummaryByUser", { userId, inboxSummary });
+ return inboxSummary;
+ },
+
async patchInbox(
{ commit, rootGetters }: any,
{ inboxId, inboxPatch }: { inboxId: InboxId; inboxPatch: InboxPatch }
@@ -94,6 +121,19 @@ const mutations = {
state.inboxListByUser.set(userId, inboxList);
},
+ setInboxSummaryByUser(
+ state: InboxState,
+ {
+ userId,
+ inboxSummary,
+ }: {
+ userId: PrincipalId;
+ inboxSummary: InboxSummary;
+ }
+ ) {
+ state.inboxSummaryByUser.set(userId, inboxSummary);
+ },
+
updateInboxById(
state: InboxState,
{
diff --git a/frontend/src/types/inbox.ts b/frontend/src/types/inbox.ts
index 1c803bf713..c298657cfd 100644
--- a/frontend/src/types/inbox.ts
+++ b/frontend/src/types/inbox.ts
@@ -18,3 +18,8 @@ export type Inbox = {
export type InboxPatch = {
status: InboxStatus;
};
+
+export type InboxSummary = {
+ hasUnread: boolean;
+ hasUnreadError: boolean;
+};
diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.ts
index 647e1c7033..c6b2a0cf2f 100644
--- a/frontend/src/types/store.ts
+++ b/frontend/src/types/store.ts
@@ -1,5 +1,6 @@
import { Activity } from "./activity";
import { ServerInfo } from "./actuator";
+import { Backup } from "./backup";
import { Bookmark } from "./bookmark";
import { Command } from "./common";
import { Database } from "./database";
@@ -15,7 +16,7 @@ import {
ProjectId,
VCSId,
} from "./id";
-import { Inbox } from "./inbox";
+import { Inbox, InboxSummary } from "./inbox";
import { Instance, MigrationHistory } from "./instance";
import { Issue } from "./issue";
import { IssueSubscriber } from "./issueSubscriber";
@@ -27,7 +28,6 @@ import { Project } from "./project";
import { Repository } from "./repository";
import { Setting, SettingName } from "./setting";
import { Table } from "./table";
-import { Backup } from "./backup";
import { VCS } from "./vcs";
export interface ActuatorState {
@@ -65,6 +65,7 @@ export interface ActivityState {
export interface InboxState {
inboxListByUser: Map<PrincipalId, Inbox[]>;
+ inboxSummaryByUser: Map<PrincipalId, InboxSummary>;
}
export interface IssueState {
diff --git a/frontend/src/views/DashboardSidebar.vue b/frontend/src/views/DashboardSidebar.vue
index eeb304f5c3..55b2c172a6 100644
--- a/frontend/src/views/DashboardSidebar.vue
+++ b/frontend/src/views/DashboardSidebar.vue
@@ -16,22 +16,53 @@
</router-link>
<router-link
to="/inbox"
- class="outline-item group flex items-center px-2 py-2"
+ class="outline-item group flex items-center justify-between px-2 py-2"
>
+ <div class="flex">
+ <svg
+ v-if="inboxSummary.hasUnread"
+ class="w-5 h-5 mr-2"
+ fill="currentColor"
+ viewBox="0 0 20 20"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ d="M8.707 7.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l2-2a1 1 0 00-1.414-1.414L11 7.586V3a1 1 0 10-2 0v4.586l-.293-.293z"
+ ></path>
+ <path
+ d="M3 5a2 2 0 012-2h1a1 1 0 010 2H5v7h2l1 2h4l1-2h2V5h-1a1 1 0 110-2h1a2 2 0 012 2v10a2 2 0 01-2 2H5a2 2 0 01-2-2V5z"
+ ></path>
+ </svg>
+ <svg
+ v-else
+ class="w-5 h-5 mr-2"
+ fill="currentColor"
+ viewBox="0 0 20 20"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ fill-rule="evenodd"
+ d="M5 3a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2V5a2 2 0 00-2-2H5zm0 2h10v7h-2l-1 2H8l-1-2H5V5z"
+ clip-rule="evenodd"
+ ></path>
+ </svg>
+ Inbox
+ </div>
<svg
- class="w-5 h-5 mr-2"
- fill="currentColor"
- viewBox="0 0 20 20"
+ v-if="inboxSummary.hasUnreadError"
+ class="w-5 h-5"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
- d="M8.707 7.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l2-2a1 1 0 00-1.414-1.414L11 7.586V3a1 1 0 10-2 0v4.586l-.293-.293z"
- ></path>
- <path
- d="M3 5a2 2 0 012-2h1a1 1 0 010 2H5v7h2l1 2h4l1-2h2V5h-1a1 1 0 110-2h1a2 2 0 012 2v10a2 2 0 01-2 2H5a2 2 0 01-2-2V5z"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
></path>
</svg>
- Inbox
</router-link>
<div>
<BookmarkListSidePanel />
@@ -49,6 +80,9 @@
import BookmarkListSidePanel from "../components/BookmarkListSidePanel.vue";
import ProjectListSidePanel from "../components/ProjectListSidePanel.vue";
import DatabaseListSidePanel from "../components/DatabaseListSidePanel.vue";
+import { computed, watchEffect } from "@vue/runtime-core";
+import { useStore } from "vuex";
+import { InboxSummary, UNKNOWN_ID } from "../types";
export default {
name: "DashboardSidebar",
@@ -59,7 +93,24 @@ export default {
DatabaseListSidePanel,
},
setup(props, ctx) {
- return {};
+ const store = useStore();
+
+ const currentUser = computed(() => store.getters["auth/currentUser"]());
+
+ const prepareInboxSummary = () => {
+ // It will also be called when user logout
+ if (currentUser.value.id != UNKNOWN_ID) {
+ store.dispatch("inbox/fetchInboxSummaryByUser", currentUser.value.id);
+ }
+ };
+
+ watchEffect(prepareInboxSummary);
+
+ const inboxSummary = computed((): InboxSummary => {
+ return store.getters["inbox/inboxSummaryByUser"](currentUser.value.id);
+ });
+
+ return { inboxSummary };
},
};
</script>
diff --git a/frontend/src/views/Inbox.vue b/frontend/src/views/Inbox.vue
index 995434f36b..5d912ca579 100644
--- a/frontend/src/views/Inbox.vue
+++ b/frontend/src/views/Inbox.vue
@@ -60,7 +60,10 @@
>
Read
</div>
- <InboxList :inboxList="effectiveInboxList(state.readList)" />
+ <InboxList
+ class="opacity-70"
+ :inboxList="effectiveInboxList(state.readList)"
+ />
</div>
</div>
</div>
@@ -174,6 +177,7 @@ export default {
};
const markAllAsRead = () => {
+ var count = state.unreadList.length;
state.unreadList.forEach((item: Inbox) => {
store
.dispatch("inbox/patchInbox", {
@@ -183,6 +187,13 @@ export default {
},
})
.then(() => {
+ count--;
+ if (count == 0) {
+ store.dispatch(
+ "inbox/fetchInboxSummaryByUser",
+ currentUser.value.id
+ );
+ }
state.readList.push(item);
});
});
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index 55aa30d342..8bf730e861 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -44,6 +44,7 @@ p, DBA, /activity, GET
p, DBA, /activity/{id}, PATCH_SELF
p, DBA, /activity/{id}, DELETE_SELF
p, DBA, /inbox, GET
+p, DBA, /inbox/summary, GET
p, DBA, /inbox/{id}, PATCH_SELF
p, DBA, /bookmark, POST
p, DBA, /bookmark, GET
diff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csv
index f518313e5f..02bff4c378 100644
--- a/server/acl_casbin_policy_developer.csv
+++ b/server/acl_casbin_policy_developer.csv
@@ -40,6 +40,7 @@ p, DEVELOPER, /activity, GET
p, DEVELOPER, /activity/{id}, PATCH_SELF
p, DEVELOPER, /activity/{id}, DELETE_SELF
p, DEVELOPER, /inbox, GET
+p, DEVELOPER, /inbox/summary, GET
p, DEVELOPER, /inbox/{id}, PATCH_SELF
p, DEVELOPER, /bookmark, POST
p, DEVELOPER, /bookmark, GET
diff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csv
index 54172019c8..ef0ae8f726 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -48,6 +48,7 @@ p, OWNER, /activity, GET
p, OWNER, /activity/{id}, PATCH_SELF
p, OWNER, /activity/{id}, DELETE_SELF
p, OWNER, /inbox, GET
+p, OWNER, /inbox/summary, GET
p, OWNER, /inbox/{id}, PATCH_SELF
p, OWNER, /bookmark, POST
p, OWNER, /bookmark, GET
diff --git a/server/inbox.go b/server/inbox.go
index 34b207dffe..19bbb8ee8b 100644
--- a/server/inbox.go
+++ b/server/inbox.go
@@ -41,6 +41,24 @@ func (s *Server) registerInboxRoutes(g *echo.Group) {
return nil
})
+ g.GET("/inbox/summary", func(c echo.Context) error {
+ userIdStr := c.QueryParams().Get("user")
+ if userIdStr == "" {
+ return echo.NewHTTPError(http.StatusBadRequest, "Missing query parameter user")
+ }
+ userId, err := strconv.Atoi(userIdStr)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query parameter user is not a number: %s", userIdStr)).SetInternal(err)
+ }
+
+ summary, err := s.InboxService.FindInboxSummary(context.Background(), userId)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch inbox summary for user ID: %d", userId)).SetInternal(err)
+ }
+
+ return c.JSON(http.StatusOK, summary)
+ })
+
g.PATCH("/inbox/:inboxId", func(c echo.Context) error {
id, err := strconv.Atoi(c.Param("inboxId"))
if err != nil {
diff --git a/store/inbox.go b/store/inbox.go
index bededf076c..6203d9be57 100644
--- a/store/inbox.go
+++ b/store/inbox.go
@@ -105,6 +105,58 @@ func (s *InboxService) PatchInbox(ctx context.Context, patch *api.InboxPatch) (*
return inbox, nil
}
+// FindInboxSummary returns the inbox summary for a particular principal
+func (s *InboxService) FindInboxSummary(ctx context.Context, principalId int) (*api.InboxSummary, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ row, err := tx.QueryContext(ctx, `
+ SELECT EXISTS (SELECT 1 FROM inbox WHERE receiver_id = ? AND status = 'UNREAD')
+ `,
+ principalId,
+ )
+
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row.Close()
+
+ row.Next()
+ var inboxSummary api.InboxSummary
+ if err := row.Scan(
+ &inboxSummary.HasUnread,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ if inboxSummary.HasUnread {
+ row2, err := tx.QueryContext(ctx, `
+ SELECT EXISTS (SELECT 1 FROM inbox WHERE receiver_id = ? AND status = 'UNREAD' AND level = 'ERROR')
+ `,
+ principalId,
+ )
+
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row2.Close()
+
+ row2.Next()
+ if err := row2.Scan(
+ &inboxSummary.HasUnreadError,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+ } else {
+ inboxSummary.HasUnreadError = false
+ }
+
+ return &inboxSummary, nil
+}
+
// createInbox creates a new inbox.
func (s *InboxService) createInbox(ctx context.Context, tx *Tx, create *api.InboxCreate) (*api.Inbox, error) {
// Insert row into database.