Post: update permissions to support admins properly

This commit is contained in:
Liam Fitzgerald 2021-04-02 11:48:38 +10:00
parent 615be57b7b
commit 16be3b97c4
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
8 changed files with 30 additions and 58 deletions

View File

@ -274,7 +274,7 @@ export default class GraphApi extends BaseApi<StoreState> {
async disableGroupFeed(group: Resource): Promise<void> {
await this.spider(
'graph-view-action',
'resource',
'json',
'graph-disable-group-feed',
{
"disable-group-feed": { resource: group }

View File

@ -47,7 +47,7 @@ export function GroupChannelSettings(props: GroupChannelSettingsProps) {
</Text>
<Text pl='4' gray>Pinning a channel marks it as featured when joining or previewing a group.</Text>
<Col p="4" width="100%" gapY="3">
{channels.map(({ resource, metadata }) => (
{channels.filter(({ metadata }) => !metadata.hidden).map(({ resource, metadata }) => (
<Row justifyContent="space-between" width="100%" key={resource}>
<Row gapX="2">
<Icon icon={getModuleIcon(metadata?.config?.graph)} />

View File

@ -1,6 +1,6 @@
import React from "react";
import { Box, Col, Row, Text, BaseLabel, Label } from "@tlon/indigo-react";
import { Association, resourceFromPath, Group } from "@urbit/api";
import { Association, resourceFromPath, Group, PermVariation } from "@urbit/api";
import GlobalApi from "~/logic/api/global";
import { Formik, Form, FormikHelpers } from "formik";
import {
@ -9,9 +9,10 @@ import {
} from "../Home/Post/GroupFeedPerms";
import { FormSubmit } from "~/views/components/FormSubmit";
import { StatelessAsyncToggle } from "~/views/components/StatelessAsyncToggle";
import useMetadataState from "~/logic/state/metadata";
interface FormSchema {
permissions: GroupFeedPermissions;
permissions: PermVariation;
}
export function GroupFeedSettings(props: {
@ -23,6 +24,7 @@ export function GroupFeedSettings(props: {
const isEnabled = !!association?.metadata?.config?.group;
const resource = resourceFromPath(association.group);
const feedResource = association?.metadata.config?.group?.resource;
const feedAssoc = useMetadataState(s => s.associations.graph[feedResource]);
const toggleFeed = async () => {
if (isEnabled) {
await api.graph.disableGroupFeed(resource);
@ -30,33 +32,17 @@ export function GroupFeedSettings(props: {
await api.graph.enableGroupFeed(resource);
}
};
const writers: Set<string> | undefined =
group.tags.graph?.[feedResource]?.writers;
const vip = feedAssoc?.metadata?.vip || " ";
const initialValues: FormSchema = {
permissions: !writers
? "everyone"
: writers.size === 1 && writers.has(window.ship)
? "host"
: "admins",
permissions: vip
};
const onSubmit = async (
values: FormSchema,
actions: FormikHelpers<FormSchema>
) => {
const tag = {
app: "graph",
resource: association.metadata.config?.group.resource,
tag: "writers",
};
await api.metadata.update(feedAssoc, { vip: values.permissions.trim() });
if (values.permissions === "admins") {
const admins =
Array.from(group.tags?.role?.admin).map((s) => `~${s}`) ?? [];
await api.groups.addTag(resource, tag, admins);
} else if (values.permissions === "host") {
await api.groups.addTag(resource, tag, [`~${window.ship}`]);
}
actions.setStatus({ success: null });
};
return (

View File

@ -11,6 +11,7 @@ import GlobalApi from "~/logic/api/global";
import { resourceFromPath, Tag, resourceAsPath } from "@urbit/api";
import useGroupState, { useGroup } from "~/logic/state/group";
import { useHistory } from 'react-router-dom';
import useMetadataState from "~/logic/state/metadata";
interface FormSchema {
@ -32,33 +33,15 @@ export function EnableGroupFeed(props: {
const initialValues: FormSchema = {
permissions: "everyone",
};
const group = useGroup(groupPath);
const onSubmit = useCallback(
const onSubmit =
async (values: FormSchema, actions: FormikHelpers<FormSchema>) => {
const resource = resourceFromPath(groupPath);
const vip = (
values.permissions === 'host' || values.permissions === 'admins'
) ? 'reader-comments' : '';
const feed = resourceAsPath(
await api.graph.enableGroupFeed(resource, vip)
await api.graph.enableGroupFeed(resource, values.permissions.trim())
);
const tag: Tag = {
app: "graph",
resource: feed,
tag: "writers",
};
if (values.permissions === "admins") {
const admins =
Array.from(group.tags?.role?.admin).map((s) => `~${s}`) ?? [];
await api.groups.addTag(resource, tag, admins);
} else if (values.permissions === "host") {
await api.groups.addTag(resource, tag, [`~${window.ship}`]);
}
actions.setStatus({ success: null });
history.replace(`${baseUrl}/feed`);
},
[groupPath, baseUrl]
);
};
return (
<ModalOverlay spacing={[3, 5, 7]} bg="white" dismiss={dismiss}>

View File

@ -37,9 +37,11 @@ export function GroupFeedHeader(props) {
authorText = node.post.author;
}
const permText = (
vip === 'reader-comments'
) ? 'Select ships can post' : 'Everyone can post';
const permText = (vip === 'host-feed')
? 'Only host can post'
: vip === 'admin-feed'
? 'Only admins can post'
: 'Everyone can post';
return (
<Row

View File

@ -7,8 +7,6 @@ import {
} from "@tlon/indigo-react";
import { PropFunc } from "~/types";
export type GroupFeedPermissions = "everyone" | "host" | "admins";
export function GroupFeedPermsInput(
props: {
id: string;
@ -21,19 +19,19 @@ export function GroupFeedPermsInput(
<Text fontWeight="medium">Permissions</Text>
<Radio
name={id}
id="everyone"
id=" "
label="Everyone"
caption="Everyone in this group can post and edit this feed"
/>
<Radio
name={id}
id="host"
id="host-feed"
label="Host Only"
caption="Only the host can post and edit this feed"
caption="Only the host can post this feed. Everyone else may comment"
/>
<Radio
name={id}
id="admins"
id="admin-feed"
label="Host & Admins Only"
caption="Only Hosts and Admins can post and edit this feed"
/>

View File

@ -7,7 +7,7 @@ import tokenizeMessage from '~/logic/lib/tokenizeMessage';
import { useToggleState } from '~/logic/lib/useToggleState';
import { createPost } from '~/logic/api/graph';
import useStorage from '~/logic/lib/useStorage';
import { resourceFromPath, isWriter } from '~/logic/lib/group';
import { resourceFromPath, isWriter, isChannelAdmin, isHost } from '~/logic/lib/group';
function canWrite(props) {
const { group, association, vip, index } = props;
@ -15,8 +15,11 @@ function canWrite(props) {
return true;
}
if (!!index) {
return true;
if(vip === 'admin-feed') {
return isChannelAdmin(group, association.group);
}
if(vip === 'host-feed') {
return isHost(association.group);
}
return isWriter(group, association.resource);

View File

@ -73,4 +73,4 @@ export interface Metadata {
vip: PermVariation;
}
export type PermVariation = '' | 'reader-comments' | 'member-metadata';
export type PermVariation = '' | 'reader-comments' | 'member-metadata' | 'host-feed' | 'admin-feed';