189 lines
4 KiB
TypeScript
189 lines
4 KiB
TypeScript
import {
|
|
fetchCommunityApi,
|
|
createCommunityApi,
|
|
updateCommunityApi,
|
|
removeCommunityApi,
|
|
fetchCommunityChannelsApi,
|
|
fetchCommunityRolesApi,
|
|
fetchCommunityMembersApi,
|
|
fetchCommunityInvitesApi,
|
|
} from "../../api/community";
|
|
import { setChannel } from "../../store/channel";
|
|
import {
|
|
deleteCommunity,
|
|
setCommunity,
|
|
setCommunityChannels,
|
|
setCommunityEncryptionKey,
|
|
setCommunityInvites,
|
|
setCommunityMembers,
|
|
setCommunityRoles,
|
|
} from "../../store/community";
|
|
import { setInvite } from "../../store/invite";
|
|
import { setRole } from "../../store/role";
|
|
import { state } from "../../store/state";
|
|
import { setUser } from "../../store/user";
|
|
import { DB_STORE, dbLoadEncrypted } from "../database";
|
|
import { fetchUserCommunities } from "../user";
|
|
|
|
const fetchCommunity = async (id: string) => {
|
|
const data = await fetchCommunityApi({
|
|
id: id,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setCommunity(data);
|
|
};
|
|
|
|
const createCommunity = async (name: string) => {
|
|
const data = await createCommunityApi({
|
|
name: name,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setCommunity(data);
|
|
|
|
if (state.user.loggedUserId) {
|
|
fetchUserCommunities(state.user.loggedUserId);
|
|
}
|
|
};
|
|
|
|
const updateCommunity = async (
|
|
id: string,
|
|
name?: string,
|
|
description?: string,
|
|
) => {
|
|
const data = await updateCommunityApi({
|
|
id: id,
|
|
name: name,
|
|
description: description,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setCommunity(data);
|
|
|
|
if (state.user.loggedUserId) {
|
|
fetchUserCommunities(state.user.loggedUserId);
|
|
}
|
|
};
|
|
|
|
const removeCommunity = async (id: string) => {
|
|
const data = await removeCommunityApi({
|
|
id: id,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
deleteCommunity(data.id);
|
|
|
|
if (state.user.loggedUserId) {
|
|
fetchUserCommunities(state.user.loggedUserId);
|
|
}
|
|
};
|
|
|
|
const fetchCommunityChannels = async (id: string) => {
|
|
const data = await fetchCommunityChannelsApi({
|
|
id: id,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setCommunityChannels(data.id, data.channels);
|
|
|
|
data.channels.forEach((channel) => {
|
|
setChannel(channel);
|
|
});
|
|
};
|
|
|
|
const fetchCommunityRoles = async (id: string) => {
|
|
const data = await fetchCommunityRolesApi({
|
|
id: id,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setCommunityRoles(data.id, data.roles);
|
|
|
|
data.roles.forEach((role) => {
|
|
setRole(role);
|
|
});
|
|
};
|
|
|
|
const fetchCommunityMembers = async (id: string) => {
|
|
const data = await fetchCommunityMembersApi({
|
|
id: id,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setCommunityMembers(data.id, data.members);
|
|
|
|
data.members.forEach((member) => {
|
|
setUser(member);
|
|
});
|
|
};
|
|
|
|
const fetchCommunityInvites = async (id: string) => {
|
|
const data = await fetchCommunityInvitesApi({
|
|
id: id,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setCommunityInvites(data.id, data.invites);
|
|
|
|
data.invites.forEach((invite) => {
|
|
setInvite(invite);
|
|
});
|
|
};
|
|
|
|
const loadCommunityCryptoStates = async () => {
|
|
if (!state.user.loggedUserId) {
|
|
return;
|
|
}
|
|
const communities = state.user.users[state.user.loggedUserId]?.communities;
|
|
if (!communities) {
|
|
return;
|
|
}
|
|
|
|
communities.forEach((communityId) => {
|
|
dbLoadEncrypted<string>(
|
|
DB_STORE.COMMUNITY_ENCRYPTION_KEYS,
|
|
communityId,
|
|
).then((communityEncryptionKey) => {
|
|
if (communityEncryptionKey) {
|
|
setCommunityEncryptionKey(communityId, communityEncryptionKey);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
export {
|
|
fetchCommunity,
|
|
createCommunity,
|
|
updateCommunity,
|
|
removeCommunity,
|
|
fetchCommunityChannels,
|
|
fetchCommunityRoles,
|
|
fetchCommunityMembers,
|
|
fetchCommunityInvites,
|
|
loadCommunityCryptoStates,
|
|
};
|