Skip to content

Commit

Permalink
feat(config): admin ability to view all server users rewinds
Browse files Browse the repository at this point in the history
  • Loading branch information
RaunoT authored Aug 2, 2024
1 parent 39c056e commit 5c2134a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/app/_components/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default function Home({ settings }: Props) {
<div className='animate-fade-in animation-delay-700'>
{!isLoggedIn && (
<button
className='button button-sm button--plex mx-auto'
className='button button-sm button--plex mx-auto mb-4'
onClick={() => handleLogin()}
>
Log in with Plex
Expand Down Expand Up @@ -166,7 +166,7 @@ export default function Home({ settings }: Props) {
))}
</>
) : (
<Link href='/rewind' className='button'>
<Link href='/rewind' className='button mb-4'>
Start Rewind
</Link>
))}
Expand All @@ -181,8 +181,8 @@ export default function Home({ settings }: Props) {
: '',
)}${settings.dashboard.defaultStyle === 'personal' && isLoggedIn ? '?personal=true' : ''}`}
className={clsx(
'mx-auto mt-4 block',
!settings.rewind.isActive ? 'button' : 'link',
'mx-auto block',
!settings.rewind.isActive && isLoggedIn ? 'button' : 'link',
)}
>
Dashboard
Expand Down
18 changes: 15 additions & 3 deletions src/app/dashboard/users/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { authOptions } from '@/lib/auth'
import { DashboardSearchParams, Settings, TautulliItem } from '@/types'
import {
DashboardSearchParams,
Settings,
TautulliItem,
TautulliUser,
} from '@/types'
import {
fetchOverseerrUserId,
fetchPaginatedOverseerrStats,
Expand Down Expand Up @@ -157,9 +162,16 @@ async function getTotalDuration(period: string, settings: Settings) {

async function getUsersCount(settings: Settings) {
if (settings.dashboard.activeTotalStatistics.includes('count')) {
const usersCount = await fetchTautulli<[]>('get_users')
const usersRes = await fetchTautulli<TautulliUser[]>('get_users')
let users = usersRes?.response?.data

if (users) {
users = users.filter(
(user) => user.is_active && user.username !== 'Local',
)
}

return usersCount?.response?.data.slice(1).length
return users?.length
}

return undefined
Expand Down
2 changes: 1 addition & 1 deletion src/app/rewind/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default async function RewindPage({ searchParams }: Props) {
)
const queriedUser = res?.response.data

if (queriedUser && queriedUser.user_id === queryUserId) {
if (queriedUser && queriedUser.user_id == queryUserId) {
user = {
image: queriedUser.user_thumb,
name: queriedUser.friendly_name,
Expand Down
24 changes: 22 additions & 2 deletions src/app/settings/rewind/_components/RewindSettingsForm.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
'use client'

import { Settings } from '@/types'
import { Settings, TautulliUser } from '@/types'
import Link from 'next/link'
import { Switch } from 'react-aria-components'
import SettingsForm from '../../_components/SettingsForm'
import saveRewindSettings from '../_actions/updateRewindSettings'

type Props = {
settings: Settings
users: TautulliUser[] | undefined
}

export default function RewindSettingsForm({ settings }: Props) {
export default function RewindSettingsForm({ settings, users }: Props) {
const rewindSettings = settings.rewind

return (
Expand Down Expand Up @@ -42,6 +44,24 @@ export default function RewindSettingsForm({ settings }: Props) {
</span>
</Switch>
</section>
{users?.length && rewindSettings.isActive && (
<section className='group-settings group'>
<h2 className='heading-settings'>Users Rewinds</h2>
<ul className='flex flex-wrap gap-2 sm:ml-48'>
{users.map((user) => (
<li key={user.user_id}>
<Link
href={`/rewind?userId=${user.user_id}`}
target='_blank'
className='button button-sm'
>
{user.friendly_name}
</Link>
</li>
))}
</ul>
</section>
)}
</SettingsForm>
)
}
9 changes: 8 additions & 1 deletion src/app/settings/rewind/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TautulliUser } from '@/types'
import fetchTautulli from '@/utils/fetchTautulli'
import getSettings from '@/utils/getSettings'
import { Metadata } from 'next'
import RewindSettingsForm from './_components/RewindSettingsForm'
Expand All @@ -8,6 +10,11 @@ export const metadata: Metadata = {

export default async function RewindSettingsPage() {
const settings = getSettings()
const usersRes = await fetchTautulli<TautulliUser[]>('get_users')
const users = usersRes?.response?.data
const fileredUsers = users?.filter(
(user) => user.is_active && user.username !== 'Local',
)

return <RewindSettingsForm settings={settings} />
return <RewindSettingsForm settings={settings} users={fileredUsers} />
}
20 changes: 9 additions & 11 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TautulliUser } from '@/types'
import { AuthOptions } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import qs from 'qs'
Expand Down Expand Up @@ -67,17 +68,14 @@ export const authOptions: AuthOptions = {
console.log('[AUTH] - User logged in with data:', userData)

if (res.ok && userData) {
const checkUser = await fetchTautulli<{ email: string }>(
'get_user',
{
user_id: userData.id,
},
)

const userExists =
checkUser?.response?.data?.email === userData.email

if (userExists) {
const checkUserRes = await fetchTautulli<TautulliUser>('get_user', {
user_id: userData.id,
})
const checkUser = checkUserRes?.response?.data
const userValid =
checkUser?.email === userData.email && checkUser?.is_active

if (userValid) {
return userData
} else {
console.error('[AUTH] - User does not belong to this server!')
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export type TautulliUser = {
friendly_name: string
thumb: string
user_thumb: string
username: string
email: string
}

export type SettingsFormInitialState = {
Expand Down

0 comments on commit 5c2134a

Please sign in to comment.