Version 0.7.0

This commit is contained in:
Aslan 2026-01-20 14:08:51 -05:00
parent 603d969972
commit 8d3b0fa7d3
44 changed files with 611 additions and 41 deletions

View file

@ -7,6 +7,8 @@ import {
isUserOwnerOrAdmin,
} from "../auth/helpers.js";
import { PERMISSION } from "../auth/permission.js";
import { getChannelById } from "../channel/channel.js";
import { getRoleById } from "../role/role.js";
import { getUserIdsInCommunity } from "../user/user.js";
import { SocketMessageTypes } from "../websocket/types.js";
import { sendMessageToUsersWS } from "../websocket/websocket.js";
@ -16,7 +18,6 @@ import type {
ICommunityChannel,
ICommunityMember,
ICommunityRole,
ICommunityInvite,
ICreateInvite,
} from "./types.js";
@ -60,7 +61,7 @@ const updateCommunityById = async (
id: string,
update: IUpdateCommunity,
): Promise<Community | null> => {
return await getDB().community.update({
const updatedCommunity = await getDB().community.update({
where: {
id: id,
},
@ -68,6 +69,17 @@ const updateCommunityById = async (
...update,
},
});
const userIds = await getUserIdsInCommunity(id);
sendMessageToUsersWS(userIds, {
type: SocketMessageTypes.UPDATE_COMMUNITY,
payload: {
communityId: id,
},
});
return updatedCommunity;
};
const updateCommunityByIdAuth = async (
@ -133,6 +145,7 @@ const getCommunityMembersById = async (
id: true,
username: true,
nickname: true,
avatar: true,
},
});
};
@ -170,6 +183,9 @@ const getCommunityChannelsById = async (
select: {
id: true,
name: true,
description: true,
category: true,
order: true,
},
});
};
@ -205,6 +221,9 @@ const getCommunityRolesById = async (id: string): Promise<ICommunityRole[]> => {
select: {
id: true,
name: true,
color: true,
order: true,
showInMembers: true,
},
});
};
@ -232,23 +251,18 @@ const getCommunityRolesByIdAuth = async (
return await getCommunityRolesById(id);
};
const getCommunityInvitesById = async (
id: string,
): Promise<ICommunityInvite[]> => {
const getCommunityInvitesById = async (id: string): Promise<Invite[]> => {
return await getDB().invite.findMany({
where: {
communityId: id,
},
select: {
id: true,
},
});
};
const getCommunityInvitesByIdAuth = async (
id: string,
authHeader: string | undefined,
): Promise<ICommunityInvite[] | API_ERROR.ACCESS_DENIED> => {
): Promise<Invite[] | API_ERROR.ACCESS_DENIED> => {
const authUser = await getUserFromAuth(authHeader);
const community = await getCommunityById(id);
@ -268,6 +282,118 @@ const getCommunityInvitesByIdAuth = async (
return await getCommunityInvitesById(id);
};
const updateCommunityChannelOrderById = async (id: string, order: string[]) => {
for (let i = 0; i < order.length; i++) {
const channelId = order[i];
if (!channelId) {
continue;
}
const channel = await getChannelById(channelId);
if (channel?.communityId !== id) {
continue;
}
await getDB().channel.update({
where: {
id: channelId,
},
data: {
order: i,
},
});
}
const userIds = await getUserIdsInCommunity(id);
sendMessageToUsersWS(userIds, {
type: SocketMessageTypes.UPDATE_CHANNELS,
payload: {
communityId: id,
},
});
};
const updateCommunityChannelOrderByIdAuth = async (
id: string,
order: string[],
authHeader: string | undefined,
): Promise<void | API_ERROR.ACCESS_DENIED> => {
const authUser = await getUserFromAuth(authHeader);
const community = await getCommunityById(id);
if (
!(await isUserAllowed(
authUser,
{
community: community,
},
community,
[PERMISSION.CHANNELS_MANAGE],
))
) {
return API_ERROR.ACCESS_DENIED;
}
await updateCommunityChannelOrderById(id, order);
};
const updateCommunityRoleOrderById = async (id: string, order: string[]) => {
for (let i = 0; i < order.length; i++) {
const roleId = order[i];
if (!roleId) {
continue;
}
const role = await getRoleById(roleId);
if (role?.communityId !== id) {
continue;
}
await getDB().role.update({
where: {
id: roleId,
},
data: {
order: i,
},
});
}
const userIds = await getUserIdsInCommunity(id);
sendMessageToUsersWS(userIds, {
type: SocketMessageTypes.UPDATE_ROLES,
payload: {
communityId: id,
},
});
};
const updateCommunityRoleOrderByIdAuth = async (
id: string,
order: string[],
authHeader: string | undefined,
): Promise<void | API_ERROR.ACCESS_DENIED> => {
const authUser = await getUserFromAuth(authHeader);
const community = await getCommunityById(id);
if (
!(await isUserAllowed(
authUser,
{
community: community,
},
community,
[PERMISSION.ROLES_MANAGE],
))
) {
return API_ERROR.ACCESS_DENIED;
}
await updateCommunityRoleOrderById(id, order);
};
const createInvite = async (
id: string,
creatorId: string,
@ -377,6 +503,10 @@ export {
getCommunityRolesByIdAuth,
getCommunityInvitesById,
getCommunityInvitesByIdAuth,
updateCommunityChannelOrderById,
updateCommunityChannelOrderByIdAuth,
updateCommunityRoleOrderById,
updateCommunityRoleOrderByIdAuth,
createInvite,
createInviteAuth,
deleteMemberById,