Add messaging
This commit is contained in:
parent
23128f25e1
commit
5733975aa0
29 changed files with 986 additions and 8 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { API_ERROR } from "../../controllers/errors.js";
|
||||
import type { Channel } from "../../generated/prisma/client.js";
|
||||
import type { Channel, Message } from "../../generated/prisma/client.js";
|
||||
import { getDB } from "../../store/store.js";
|
||||
import { getUserFromAuth, isUserAllowed } from "../auth/helpers.js";
|
||||
import { PERMISSION } from "../auth/permission.js";
|
||||
|
|
@ -136,6 +136,40 @@ const deleteChannelByIdAuth = async (
|
|||
return await deleteChannelById(id);
|
||||
};
|
||||
|
||||
const getChannelMessagesById = async (
|
||||
id: string,
|
||||
): Promise<Message[] | null> => {
|
||||
return await getDB().message.findMany({
|
||||
where: {
|
||||
channelId: id,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const getChannelMessagesByIdAuth = async (
|
||||
id: string,
|
||||
authHeader: string | undefined,
|
||||
): Promise<Message[] | null | API_ERROR.ACCESS_DENIED> => {
|
||||
const authUser = await getUserFromAuth(authHeader);
|
||||
const channel = await getChannelById(id);
|
||||
const community = await getCommunityById(channel?.communityId ?? "");
|
||||
|
||||
if (
|
||||
!(await isUserAllowed(
|
||||
authUser,
|
||||
{
|
||||
community: community,
|
||||
},
|
||||
community,
|
||||
[PERMISSION.MESSAGES_READ],
|
||||
))
|
||||
) {
|
||||
return API_ERROR.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
return await getChannelMessagesById(id);
|
||||
};
|
||||
|
||||
export {
|
||||
getChannelById,
|
||||
getChannelByIdAuth,
|
||||
|
|
@ -145,4 +179,6 @@ export {
|
|||
updateChannelByIdAuth,
|
||||
deleteChannelById,
|
||||
deleteChannelByIdAuth,
|
||||
getChannelMessagesById,
|
||||
getChannelMessagesByIdAuth,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue