Skip to content
This repository was archived by the owner on Oct 12, 2025. It is now read-only.

Commit 94b85f2

Browse files
committed
add community context proper role
1 parent c808c48 commit 94b85f2

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ecency/sdk",
33
"private": false,
4-
"version": "1.0.49",
4+
"version": "1.0.50",
55
"type": "module",
66
"license": "MIT",
77
"main": "./dist/ecency-sdk.umd.js",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./queries";
22
export * from "./types";
3+
export * from "./utils"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { queryOptions } from "@tanstack/react-query";
2+
import { CONFIG } from "@/modules/core";
3+
4+
export function getCommunityContextQueryOptions(
5+
username: string | undefined,
6+
communityName: string | undefined
7+
) {
8+
return queryOptions({
9+
queryKey: ["community", "context", username, communityName],
10+
enabled: !!username && !!communityName,
11+
queryFn: async () => {
12+
const response = await CONFIG.hiveClient.call("bridge", "get_community_context", {
13+
account: username,
14+
name: communityName,
15+
});
16+
17+
return {
18+
role: response?.role ?? "guest",
19+
subscribed: response?.subscribed ?? false,
20+
};
21+
},
22+
});
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./get-communities-query-options";
2+
export * from "./get-community-context-query-options";
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { ROLES } from "../types";
2+
3+
export type CommunityRole = (typeof ROLES)[keyof typeof ROLES]; // "owner" | "member" | ...
4+
export type CommunityType = "Topic" | "Journal" | "Council";
5+
6+
/**
7+
* Determine community type based on its `name` or `type_id`
8+
*/
9+
export function getCommunityType(name: string, type_id: number): CommunityType {
10+
if (name.startsWith("hive-3") || type_id === 3) return "Council";
11+
if (name.startsWith("hive-2") || type_id === 2) return "Journal";
12+
return "Topic";
13+
}
14+
15+
/**
16+
* Compute permissions for a given user role in a community
17+
*/
18+
export function getCommunityPermissions({
19+
communityType,
20+
userRole,
21+
subscribed,
22+
}: {
23+
communityType: CommunityType;
24+
userRole: CommunityRole;
25+
subscribed: boolean;
26+
}) {
27+
const canPost = (() => {
28+
if (userRole === ROLES.MUTED) return false;
29+
30+
if (communityType === "Topic") return true;
31+
32+
// Journal & Council
33+
return [ROLES.OWNER, ROLES.ADMIN, ROLES.MOD, ROLES.MEMBER].includes(userRole);
34+
})();
35+
36+
const canComment = (() => {
37+
if (userRole === ROLES.MUTED) return false;
38+
39+
switch (communityType) {
40+
case "Topic":
41+
return true;
42+
case "Journal":
43+
return userRole !== ROLES.GUEST || subscribed;
44+
case "Council":
45+
return canPost;
46+
}
47+
})();
48+
49+
const isModerator = [ROLES.OWNER, ROLES.ADMIN, ROLES.MOD].includes(userRole);
50+
51+
return {
52+
canPost,
53+
canComment,
54+
isModerator,
55+
};
56+
}

0 commit comments

Comments
 (0)