2023-01-16 10:11:44 +03:00
|
|
|
import React, { useEffect, useState, useContext, ReactElement } from 'react';
|
2022-01-13 00:52:37 +03:00
|
|
|
import { Table, Avatar, Button, Tabs } from 'antd';
|
|
|
|
import { ColumnsType, SortOrder } from 'antd/lib/table/interface';
|
|
|
|
import format from 'date-fns/format';
|
2023-01-16 09:31:36 +03:00
|
|
|
import dynamic from 'next/dynamic';
|
2022-04-20 05:57:27 +03:00
|
|
|
import { ServerStatusContext } from '../../../utils/server-status-context';
|
2022-01-13 00:52:37 +03:00
|
|
|
import {
|
|
|
|
FOLLOWERS,
|
|
|
|
FOLLOWERS_PENDING,
|
|
|
|
SET_FOLLOWER_APPROVAL,
|
|
|
|
FOLLOWERS_BLOCKED,
|
|
|
|
fetchData,
|
2022-04-20 05:57:27 +03:00
|
|
|
} from '../../../utils/apis';
|
|
|
|
import { isEmptyObject } from '../../../utils/format';
|
2022-01-13 00:52:37 +03:00
|
|
|
|
2023-01-16 10:11:44 +03:00
|
|
|
import { AdminLayout } from '../../../components/layouts/AdminLayout';
|
|
|
|
|
2023-01-16 09:31:36 +03:00
|
|
|
// Lazy loaded components
|
|
|
|
|
|
|
|
const UserAddOutlined = dynamic(() => import('@ant-design/icons/UserAddOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const UserDeleteOutlined = dynamic(() => import('@ant-design/icons/UserDeleteOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
2022-01-13 00:52:37 +03:00
|
|
|
export interface Follower {
|
|
|
|
link: string;
|
|
|
|
username: string;
|
|
|
|
image: string;
|
|
|
|
name: string;
|
|
|
|
timestamp: Date;
|
|
|
|
approved: Date;
|
|
|
|
}
|
|
|
|
|
2023-03-23 00:54:34 +03:00
|
|
|
const FollowersTable = ({ data, tableColumns, totalCount, setCurrentPage }) => (
|
|
|
|
<Table
|
|
|
|
dataSource={data}
|
|
|
|
columns={tableColumns}
|
|
|
|
size="small"
|
|
|
|
rowKey={row => row.link}
|
|
|
|
pagination={{
|
|
|
|
pageSize: 25,
|
|
|
|
hideOnSinglePage: true,
|
|
|
|
showSizeChanger: false,
|
|
|
|
total: totalCount,
|
|
|
|
}}
|
|
|
|
onChange={pagination => {
|
|
|
|
const page = pagination.current;
|
|
|
|
setCurrentPage(page);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2022-01-13 00:52:37 +03:00
|
|
|
export default function FediverseFollowers() {
|
|
|
|
const [followersPending, setFollowersPending] = useState<Follower[]>([]);
|
|
|
|
const [followersBlocked, setFollowersBlocked] = useState<Follower[]>([]);
|
|
|
|
const [followers, setFollowers] = useState<Follower[]>([]);
|
2022-03-08 01:41:54 +03:00
|
|
|
const [totalCount, setTotalCount] = useState<number>(0);
|
|
|
|
const [currentPage, setCurrentPage] = useState<number>(0);
|
2022-01-13 00:52:37 +03:00
|
|
|
|
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
|
|
|
const { serverConfig } = serverStatusData || {};
|
|
|
|
const { federation } = serverConfig;
|
|
|
|
const { isPrivate } = federation;
|
|
|
|
|
|
|
|
const getFollowers = async () => {
|
|
|
|
try {
|
2023-03-23 00:54:34 +03:00
|
|
|
const limit = 25;
|
2022-03-08 01:41:54 +03:00
|
|
|
const offset = currentPage * limit;
|
|
|
|
const u = `${FOLLOWERS}?offset=${offset}&limit=${limit}`;
|
|
|
|
|
2022-01-13 00:52:37 +03:00
|
|
|
// Active followers
|
2022-03-08 01:41:54 +03:00
|
|
|
const result = await fetchData(u, { auth: true });
|
|
|
|
const { results, total } = result;
|
|
|
|
|
|
|
|
if (isEmptyObject(results)) {
|
2022-01-13 00:52:37 +03:00
|
|
|
setFollowers([]);
|
|
|
|
} else {
|
2022-03-08 01:41:54 +03:00
|
|
|
setTotalCount(total);
|
|
|
|
setFollowers(results);
|
2022-01-13 00:52:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pending follow requests
|
|
|
|
const pendingFollowersResult = await fetchData(FOLLOWERS_PENDING, { auth: true });
|
|
|
|
if (isEmptyObject(pendingFollowersResult)) {
|
|
|
|
setFollowersPending([]);
|
|
|
|
} else {
|
|
|
|
setFollowersPending(pendingFollowersResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blocked/rejected followers
|
|
|
|
const blockedFollowersResult = await fetchData(FOLLOWERS_BLOCKED, { auth: true });
|
|
|
|
if (isEmptyObject(followersBlocked)) {
|
|
|
|
setFollowersBlocked([]);
|
|
|
|
} else {
|
|
|
|
setFollowersBlocked(blockedFollowersResult);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log('==== error', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getFollowers();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const columns: ColumnsType<Follower> = [
|
|
|
|
{
|
|
|
|
title: '',
|
|
|
|
dataIndex: 'image',
|
|
|
|
key: 'image',
|
|
|
|
width: 90,
|
|
|
|
render: image => <Avatar size={40} src={image || '/img/logo.svg'} />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'name',
|
|
|
|
render: (_, follower) => (
|
|
|
|
<a href={follower.link} target="_blank" rel="noreferrer">
|
|
|
|
{follower.name || follower.username}
|
|
|
|
</a>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'URL',
|
|
|
|
dataIndex: 'link',
|
|
|
|
key: 'link',
|
|
|
|
render: (_, follower) => (
|
|
|
|
<a href={follower.link} target="_blank" rel="noreferrer">
|
|
|
|
{follower.link}
|
|
|
|
</a>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
async function approveFollowRequest(request) {
|
|
|
|
try {
|
|
|
|
await fetchData(SET_FOLLOWER_APPROVAL, {
|
|
|
|
auth: true,
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
actorIRI: request.link,
|
|
|
|
approved: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Refetch and update the current data.
|
|
|
|
getFollowers();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function rejectFollowRequest(request) {
|
|
|
|
try {
|
|
|
|
await fetchData(SET_FOLLOWER_APPROVAL, {
|
|
|
|
auth: true,
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
actorIRI: request.link,
|
|
|
|
approved: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Refetch and update the current data.
|
|
|
|
getFollowers();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const pendingColumns: ColumnsType<Follower> = [...columns];
|
|
|
|
pendingColumns.unshift(
|
|
|
|
{
|
|
|
|
title: 'Approve',
|
|
|
|
dataIndex: null,
|
|
|
|
key: null,
|
|
|
|
render: request => (
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
icon={<UserAddOutlined />}
|
|
|
|
onClick={() => {
|
|
|
|
approveFollowRequest(request);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
width: 50,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Reject',
|
|
|
|
dataIndex: null,
|
|
|
|
key: null,
|
|
|
|
render: request => (
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
danger
|
|
|
|
icon={<UserDeleteOutlined />}
|
|
|
|
onClick={() => {
|
|
|
|
rejectFollowRequest(request);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
width: 50,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
pendingColumns.push({
|
|
|
|
title: 'Requested',
|
|
|
|
dataIndex: 'timestamp',
|
|
|
|
key: 'requested',
|
|
|
|
width: 200,
|
|
|
|
render: timestamp => {
|
|
|
|
const dateObject = new Date(timestamp);
|
|
|
|
return <>{format(dateObject, 'P')}</>;
|
|
|
|
},
|
|
|
|
sorter: (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),
|
|
|
|
sortDirections: ['descend', 'ascend'] as SortOrder[],
|
|
|
|
defaultSortOrder: 'descend' as SortOrder,
|
|
|
|
});
|
|
|
|
|
|
|
|
const blockedColumns: ColumnsType<Follower> = [...columns];
|
|
|
|
blockedColumns.unshift({
|
|
|
|
title: 'Approve',
|
|
|
|
dataIndex: null,
|
|
|
|
key: null,
|
|
|
|
render: request => (
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
icon={<UserAddOutlined />}
|
|
|
|
size="large"
|
|
|
|
onClick={() => {
|
|
|
|
approveFollowRequest(request);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
width: 50,
|
|
|
|
});
|
|
|
|
|
|
|
|
blockedColumns.push(
|
|
|
|
{
|
|
|
|
title: 'Requested',
|
|
|
|
dataIndex: 'timestamp',
|
|
|
|
key: 'requested',
|
|
|
|
width: 200,
|
|
|
|
render: timestamp => {
|
|
|
|
const dateObject = new Date(timestamp);
|
|
|
|
return <>{format(dateObject, 'P')}</>;
|
|
|
|
},
|
|
|
|
sorter: (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),
|
|
|
|
sortDirections: ['descend', 'ascend'] as SortOrder[],
|
|
|
|
defaultSortOrder: 'descend' as SortOrder,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Rejected/Blocked',
|
|
|
|
dataIndex: 'timestamp',
|
|
|
|
key: 'disabled_at',
|
|
|
|
width: 200,
|
|
|
|
render: timestamp => {
|
|
|
|
const dateObject = new Date(timestamp);
|
|
|
|
return <>{format(dateObject, 'P')}</>;
|
|
|
|
},
|
|
|
|
sorter: (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),
|
|
|
|
sortDirections: ['descend', 'ascend'] as SortOrder[],
|
|
|
|
defaultSortOrder: 'descend' as SortOrder,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const followersColumns: ColumnsType<Follower> = [...columns];
|
|
|
|
|
|
|
|
followersColumns.push(
|
|
|
|
{
|
|
|
|
title: 'Added',
|
|
|
|
dataIndex: 'timestamp',
|
|
|
|
key: 'timestamp',
|
|
|
|
width: 200,
|
|
|
|
render: timestamp => {
|
|
|
|
const dateObject = new Date(timestamp);
|
|
|
|
return <>{format(dateObject, 'P')}</>;
|
|
|
|
},
|
|
|
|
sorter: (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),
|
|
|
|
sortDirections: ['descend', 'ascend'] as SortOrder[],
|
|
|
|
defaultSortOrder: 'descend' as SortOrder,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remove',
|
|
|
|
dataIndex: null,
|
|
|
|
key: null,
|
|
|
|
render: request => (
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
danger
|
|
|
|
icon={<UserDeleteOutlined />}
|
|
|
|
onClick={() => {
|
|
|
|
rejectFollowRequest(request);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
width: 50,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-03-22 03:33:59 +03:00
|
|
|
const followersTabTitle = <span>Followers {totalCount > 0 && `(${totalCount})`}</span>;
|
2022-10-11 03:53:14 +03:00
|
|
|
const followersTab = (
|
|
|
|
<>
|
|
|
|
<p>The following accounts get notified when you go live or send a post.</p>
|
2023-03-23 00:54:34 +03:00
|
|
|
<FollowersTable
|
|
|
|
data={followers}
|
|
|
|
tableColumns={followersColumns}
|
|
|
|
totalCount={totalCount}
|
|
|
|
setCurrentPage={setCurrentPage}
|
|
|
|
/>
|
2022-10-11 03:53:14 +03:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
const pendingRequestsTabTitle = (
|
|
|
|
<span>Requests {followersPending.length > 0 && `(${followersPending.length})`}</span>
|
|
|
|
);
|
|
|
|
const pendingRequestsTab = (
|
|
|
|
<>
|
2022-01-13 00:52:37 +03:00
|
|
|
<p>
|
|
|
|
The following people are requesting to follow your Owncast server on the{' '}
|
|
|
|
<a href="https://en.wikipedia.org/wiki/Fediverse" target="_blank" rel="noopener noreferrer">
|
|
|
|
Fediverse
|
|
|
|
</a>{' '}
|
|
|
|
and be alerted to when you go live. Each must be approved.
|
|
|
|
</p>
|
2023-03-23 00:54:34 +03:00
|
|
|
<Table
|
|
|
|
dataSource={followersPending}
|
|
|
|
columns={pendingColumns}
|
|
|
|
size="small"
|
|
|
|
rowKey={row => row.link}
|
|
|
|
/>
|
2022-10-11 03:53:14 +03:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
const blockedTabTitle = (
|
|
|
|
<span>Blocked {followersBlocked.length > 0 && `(${followersBlocked.length})`}</span>
|
2022-01-13 00:52:37 +03:00
|
|
|
);
|
2022-10-11 03:53:14 +03:00
|
|
|
const blockedTab = (
|
|
|
|
<>
|
|
|
|
<p>
|
|
|
|
The following people were either rejected or blocked by you. You can approve them as a
|
|
|
|
follower.
|
|
|
|
</p>
|
2023-03-23 00:54:34 +03:00
|
|
|
<p>
|
|
|
|
<Table
|
|
|
|
dataSource={followersBlocked}
|
|
|
|
columns={blockedColumns}
|
|
|
|
size="small"
|
|
|
|
rowKey={row => row.link}
|
|
|
|
/>
|
|
|
|
</p>
|
2022-10-11 03:53:14 +03:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
const items = [
|
|
|
|
{ label: followersTabTitle, key: '1', children: followersTab },
|
|
|
|
isPrivate && { label: pendingRequestsTabTitle, key: '2', children: pendingRequestsTab },
|
|
|
|
{ label: blockedTabTitle, key: '3', children: blockedTab },
|
|
|
|
];
|
2022-01-13 00:52:37 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="followers-section">
|
2022-10-11 03:53:14 +03:00
|
|
|
<Tabs defaultActiveKey="1" items={items} />
|
2022-01-13 00:52:37 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2023-01-16 10:11:44 +03:00
|
|
|
|
|
|
|
FediverseFollowers.getLayout = function getLayout(page: ReactElement) {
|
|
|
|
return <AdminLayout page={page} />;
|
|
|
|
};
|