You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
240 lines
7.5 KiB
240 lines
7.5 KiB
// groups.ts
|
|
// Copyright (C) 2020 Dwayne Harris
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import { apiFetch } from '../api'
|
|
import { setEntities } from '../actions/entities'
|
|
import { listSet, listAppend } from '../actions/lists'
|
|
import { startRequest, finishRequest } from '../actions/requests'
|
|
|
|
import { objectToQuerystring } from '../utils'
|
|
import { normalize } from '../utils/normalization'
|
|
import { AppThunkAction, Entity, RequestKey, EntityType, User, EntityListKey } from '../types'
|
|
|
|
export const fetchGroup = (id: string): AppThunkAction => {
|
|
return async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroup))
|
|
|
|
try {
|
|
const group = await apiFetch<Entity>({
|
|
path: `/v1/group/${id}`
|
|
})
|
|
|
|
const groups = normalize([group], EntityType.Group)
|
|
|
|
dispatch(setEntities(groups.entities))
|
|
dispatch(finishRequest(RequestKey.FetchGroup, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroup, false))
|
|
throw err
|
|
}
|
|
}
|
|
}
|
|
|
|
interface GroupsResponse {
|
|
groups: Entity[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchGroups = (sort?: string, continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroups))
|
|
|
|
try {
|
|
const response = await apiFetch<GroupsResponse>({
|
|
path: `/v1/groups?${objectToQuerystring({ sort, continuation })}`,
|
|
})
|
|
|
|
const groups = normalize(response.groups, EntityType.Group)
|
|
|
|
dispatch(setEntities(groups.entities))
|
|
|
|
if (continuation) {
|
|
dispatch(listAppend(EntityListKey.Groups, groups.keys, response.continuation))
|
|
} else {
|
|
dispatch(listSet(EntityListKey.Groups, groups.keys, response.continuation))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchGroups, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroups, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface GroupMembersResponse {
|
|
members: User[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchGroupMembers = (id: string, type?: string, continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroupMembers))
|
|
|
|
try {
|
|
const response = await apiFetch<GroupMembersResponse>({
|
|
path: `/v1/group/${id}/members?${objectToQuerystring({ type, continuation })}`,
|
|
})
|
|
|
|
const users = normalize(response.members, EntityType.User)
|
|
|
|
dispatch(setEntities(users.entities))
|
|
|
|
if (continuation) {
|
|
dispatch(listAppend(EntityListKey.GroupMembers, users.keys, response.continuation))
|
|
} else {
|
|
dispatch(listSet(EntityListKey.GroupMembers, users.keys, response.continuation))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchGroupMembers, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroupMembers, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface GroupLogsResponse {
|
|
logs: Entity[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchLogs = (continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroupLogs))
|
|
|
|
try {
|
|
const response = await apiFetch<GroupLogsResponse>({
|
|
path: `/v1/group/logs?${objectToQuerystring({ continuation })}`,
|
|
})
|
|
|
|
const logs = normalize(response.logs, EntityType.Log)
|
|
|
|
dispatch(setEntities(logs.entities))
|
|
|
|
if (continuation) {
|
|
dispatch(listAppend(EntityListKey.Logs, logs.keys, response.continuation))
|
|
} else {
|
|
dispatch(listSet(EntityListKey.Logs, logs.keys, response.continuation))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchGroupLogs, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroupLogs, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface CreateInvitationResponse {
|
|
code: string
|
|
}
|
|
|
|
export const createInvitation = (expiration?: number, limit?: number): AppThunkAction<string> => async dispatch => {
|
|
dispatch(startRequest(RequestKey.CreateInvitation))
|
|
|
|
try {
|
|
const response = await apiFetch<CreateInvitationResponse>({
|
|
path: `/v1/group/invitation`,
|
|
method: 'post',
|
|
body: {
|
|
expiration,
|
|
limit,
|
|
}
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.CreateInvitation, true))
|
|
return response.code
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.CreateInvitation, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface InvitationsResponse {
|
|
invitations: Entity[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchInvitations = (): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchInvitations))
|
|
|
|
try {
|
|
const response = await apiFetch<InvitationsResponse>({
|
|
path: `/v1/group/invitations`,
|
|
})
|
|
|
|
const invitations = normalize(response.invitations, EntityType.Invitation)
|
|
|
|
dispatch(setEntities(invitations.entities))
|
|
dispatch(listSet(EntityListKey.Invitations, invitations.keys, response.continuation))
|
|
dispatch(finishRequest(RequestKey.FetchInvitations, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchInvitations, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const updateGroup = (id: string, updates: object): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.UpdateGroup))
|
|
|
|
try {
|
|
await apiFetch({
|
|
path: `/v1/group/${id}`,
|
|
method: 'put',
|
|
body: updates,
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.UpdateGroup, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.UpdateGroup, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface FetchPendingGroupsResponse {
|
|
groups: Entity[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchPendingGroups = (continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchPendingGroups))
|
|
|
|
try {
|
|
const response = await apiFetch<FetchPendingGroupsResponse>({
|
|
path: `/v1/groups/pending?${objectToQuerystring({ continuation })}`,
|
|
method: 'get',
|
|
})
|
|
|
|
const groups = normalize(response.groups, EntityType.Group)
|
|
dispatch(setEntities(groups.entities))
|
|
dispatch(listSet(EntityListKey.PendingGroups, groups.keys, response.continuation))
|
|
dispatch(finishRequest(RequestKey.FetchPendingGroups, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchPendingGroups, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const activateGroup = (id: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.ActivateGroup))
|
|
|
|
try {
|
|
await apiFetch({
|
|
path: `/v1/group/${id}/activate`,
|
|
method: 'post',
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.ActivateGroup, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.ActivateGroup, false))
|
|
throw err
|
|
}
|
|
}
|