diff --git a/overseerr-api.yml b/overseerr-api.yml index c27b7fe0a..be0a431ce 100644 --- a/overseerr-api.yml +++ b/overseerr-api.yml @@ -33,6 +33,10 @@ components: type: string example: '2020-09-02T05:02:23.000Z' readOnly: true + requests: + type: array + items: + $ref: '#/components/schemas/MediaRequest' required: - id - email diff --git a/package.json b/package.json index b4caade12..9b70ecfb2 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "intl": "^1.2.5", "lodash": "^4.17.20", "next": "9.5.4", + "node-schedule": "^1.3.2", "nookies": "^2.4.0", "plex-api": "^5.3.1", "react": "16.13.1", @@ -58,6 +59,7 @@ "@types/express-session": "^1.17.0", "@types/lodash": "^4.14.161", "@types/node": "^14.10.0", + "@types/node-schedule": "^1.3.1", "@types/react": "^16.9.49", "@types/react-dom": "^16.9.8", "@types/react-toast-notifications": "^2.4.0", diff --git a/server/entity/User.ts b/server/entity/User.ts index d2b610d0b..b04ec3afe 100644 --- a/server/entity/User.ts +++ b/server/entity/User.ts @@ -39,7 +39,7 @@ export class User { public avatar: string; @OneToMany(() => MediaRequest, (request) => request.requestedBy) - public requests: MediaRequest; + public requests: MediaRequest[]; @CreateDateColumn() public createdAt: Date; diff --git a/server/index.ts b/server/index.ts index bf5b3a017..a702c4df1 100644 --- a/server/index.ts +++ b/server/index.ts @@ -13,6 +13,7 @@ import { OpenApiValidator } from 'express-openapi-validator'; import { Session } from './entity/Session'; import { getSettings } from './lib/settings'; import logger from './logger'; +import { startJobs } from './job/schedule'; const API_SPEC_PATH = path.join(__dirname, '../overseerr-api.yml'); @@ -27,6 +28,9 @@ app // Load Settings getSettings().load(); + // Start Jobs + startJobs(); + const server = express(); server.use(cookieParser()); server.use(bodyParser.json()); diff --git a/server/job/plexsync.ts b/server/job/plexsync.ts index 9b9ebc13c..90e282ef0 100644 --- a/server/job/plexsync.ts +++ b/server/job/plexsync.ts @@ -67,7 +67,8 @@ class JobPlexSync { existing.status = MediaStatus.AVAILABLE; mediaRepository.save(existing); this.log( - `Request for ${metadata.title} exists. Setting status AVAILABLE` + `Request for ${metadata.title} exists. Setting status AVAILABLE`, + 'info' ); } else { newMedia.status = MediaStatus.AVAILABLE; @@ -90,7 +91,8 @@ class JobPlexSync { existing.status = MediaStatus.AVAILABLE; await mediaRepository.save(existing); this.log( - `Request for ${plexitem.title} exists. Setting status AVAILABLE` + `Request for ${plexitem.title} exists. Setting status AVAILABLE`, + 'info' ); } else if (tmdbMovie) { const newMedia = new Media(); @@ -139,8 +141,11 @@ class JobPlexSync { } } - private log(message: string): void { - logger.info(message, { label: 'Plex Sync' }); + private log( + message: string, + level: 'info' | 'error' | 'debug' = 'debug' + ): void { + logger[level](message, { label: 'Plex Sync' }); } public async run(): Promise { @@ -160,7 +165,7 @@ class JobPlexSync { for (const library of this.libraries) { this.currentLibrary = library; - this.log(`Beginning to process library: ${library.name}`); + this.log(`Beginning to process library: ${library.name}`, 'info'); this.items = await this.plexClient.getLibraryContents(library.id); await this.loop(); } diff --git a/server/job/schedule.ts b/server/job/schedule.ts new file mode 100644 index 000000000..5589a109c --- /dev/null +++ b/server/job/schedule.ts @@ -0,0 +1,15 @@ +import schedule from 'node-schedule'; +import jobPlexSync from './plexsync'; +import logger from '../logger'; + +export const scheduledJobs: Record = {}; + +export const startJobs = (): void => { + // Run full plex sync every 6 hours + scheduledJobs.plexFullSync = schedule.scheduleJob('* */6 * * *', () => { + logger.info('Starting scheduled job: Plex Full Sync', { label: 'Jobs' }); + jobPlexSync.run(); + }); + + logger.info('Scheduled jobs loaded', { label: 'Jobs' }); +}; diff --git a/server/routes/user.ts b/server/routes/user.ts index 58aef5de5..646a64846 100644 --- a/server/routes/user.ts +++ b/server/routes/user.ts @@ -7,7 +7,7 @@ const router = Router(); router.get('/', async (req, res) => { const userRepository = getRepository(User); - const users = await userRepository.find(); + const users = await userRepository.find({ relations: ['requests'] }); return res.status(200).json(User.filterMany(users)); }); diff --git a/src/components/UserList/index.tsx b/src/components/UserList/index.tsx new file mode 100644 index 000000000..2f3d93498 --- /dev/null +++ b/src/components/UserList/index.tsx @@ -0,0 +1,114 @@ +import React from 'react'; +import useSWR from 'swr'; +import LoadingSpinner from '../Common/LoadingSpinner'; +import type { User } from '../../../server/entity/User'; +import Badge from '../Common/Badge'; +import { FormattedDate } from 'react-intl'; +import Button from '../Common/Button'; +import { hasPermission } from '../../../server/lib/permissions'; +import { Permission } from '../../hooks/useUser'; + +const UserList: React.FC = () => { + const { data, error } = useSWR('/api/v1/user'); + + if (!data && !error) { + return ; + } + + return ( + <> +
+
+

+ User List +

+
+
+
+
+
+
+ + + + + + + + + + + + + + {data?.map((user) => ( + + + + + + + + + + ))} + +
+ Name + + Total Requests + + User Type + + Role + + Created + + Last Updated +
+
+
+ +
+
+
+ {user.username} +
+
+ {user.email} +
+
+
+
+
+ {user.requests.length} +
+
+ Plex User + + {hasPermission(Permission.ADMIN, user.permissions) + ? 'Admin' + : 'User'} + + + + + + + +
+
+
+
+
+ + ); +}; + +export default UserList; diff --git a/src/pages/users/index.tsx b/src/pages/users/index.tsx new file mode 100644 index 000000000..5a7dafe33 --- /dev/null +++ b/src/pages/users/index.tsx @@ -0,0 +1,9 @@ +import React from 'react'; +import type { NextPage } from 'next'; +import UserList from '../../components/UserList'; + +const UsersPage: NextPage = () => { + return ; +}; + +export default UsersPage; diff --git a/yarn.lock b/yarn.lock index 7df5cde58..2bd527933 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1792,6 +1792,13 @@ resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6" integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY= +"@types/node-schedule@^1.3.1": + version "1.3.1" + resolved "https://registry.yarnpkg.com/@types/node-schedule/-/node-schedule-1.3.1.tgz#6785ea71b12b0b8899c3fce0650b2ef5a7ea9d1e" + integrity sha512-xAY/ZATrThUkMElSDfOk+5uXprCrV6c6GQ5gTw3U04qPS6NofE1dhOUW+yrOF2UyrUiAax/Zc4WtagrbPAN3Tw== + dependencies: + "@types/node" "*" + "@types/node@*", "@types/node@^14.10.0": version "14.10.0" resolved "https://registry.yarnpkg.com/@types/node/-/node-14.10.0.tgz#15815dff82c8dc30827f6b1286f865902945095a" @@ -3702,6 +3709,14 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7: safe-buffer "^5.0.1" sha.js "^2.4.8" +cron-parser@^2.7.3: + version "2.17.0" + resolved "https://registry.yarnpkg.com/cron-parser/-/cron-parser-2.17.0.tgz#5707421a7e0a73ee74675d1c032a2f14123f2cf8" + integrity sha512-oTmzVEwlurRe51HqTm4afshVr8Rkxy9kFiWxh5e6SmrY2o9NDYU4S6SduanBZYXLgkLy0skA98y7/tztW/DmjQ== + dependencies: + is-nan "^1.3.0" + moment-timezone "^0.5.31" + cross-fetch@3.0.5: version "3.0.5" resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.5.tgz#2739d2981892e7ab488a7ad03b92df2816e03f4c" @@ -6004,6 +6019,13 @@ is-installed-globally@^0.3.1: global-dirs "^2.0.1" is-path-inside "^3.0.1" +is-nan@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/is-nan/-/is-nan-1.3.0.tgz#85d1f5482f7051c2019f5673ccebdb06f3b0db03" + integrity sha512-z7bbREymOqt2CCaZVly8aC4ML3Xhfi0ekuOnjO2L8vKdl+CttdVoGZQhd4adMFAsxQ5VeRVwORs4tU8RH+HFtQ== + dependencies: + define-properties "^1.1.3" + is-npm@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/is-npm/-/is-npm-4.0.0.tgz#c90dd8380696df87a7a6d823c20d0b12bbe3c84d" @@ -6571,6 +6593,11 @@ logform@^2.2.0: ms "^2.1.1" triple-beam "^1.3.0" +long-timeout@0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/long-timeout/-/long-timeout-0.1.1.tgz#9721d788b47e0bcb5a24c2e2bee1a0da55dab514" + integrity sha1-lyHXiLR+C8taJMLivuGg2lXatRQ= + longest@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-2.0.1.tgz#781e183296aa94f6d4d916dc335d0d17aefa23f8" @@ -6972,6 +6999,18 @@ mkdirp@^1.0.3, mkdirp@^1.0.4: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== +moment-timezone@^0.5.31: + version "0.5.31" + resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.31.tgz#9c40d8c5026f0c7ab46eda3d63e49c155148de05" + integrity sha512-+GgHNg8xRhMXfEbv81iDtrVeTcWt0kWmTEY1XQK14dICTXnWJnT0dxdlPspwqF3keKMVPXwayEsk1DI0AA/jdA== + dependencies: + moment ">= 2.9.0" + +"moment@>= 2.9.0": + version "2.29.1" + resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3" + integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ== + move-concurrently@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92" @@ -7257,6 +7296,15 @@ node-releases@^1.1.58, node-releases@^1.1.60: resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.60.tgz#6948bdfce8286f0b5d0e5a88e8384e954dfe7084" integrity sha512-gsO4vjEdQaTusZAEebUWp2a5d7dF5DYoIpDG7WySnk7BuZDW+GPpHXoXXuYawRBr/9t5q54tirPz79kFIWg4dA== +node-schedule@^1.3.2: + version "1.3.2" + resolved "https://registry.yarnpkg.com/node-schedule/-/node-schedule-1.3.2.tgz#d774b383e2a6f6ade59eecc62254aea07cd758cb" + integrity sha512-GIND2pHMHiReSZSvS6dpZcDH7pGPGFfWBIEud6S00Q8zEIzAs9ommdyRK1ZbQt8y1LyZsJYZgPnyi7gpU2lcdw== + dependencies: + cron-parser "^2.7.3" + long-timeout "0.1.1" + sorted-array-functions "^1.0.0" + nodemon@^2.0.4: version "2.0.4" resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-2.0.4.tgz#55b09319eb488d6394aa9818148c0c2d1c04c416" @@ -9306,6 +9354,11 @@ sort-keys@^4.0.0: dependencies: is-plain-obj "^2.0.0" +sorted-array-functions@^1.0.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/sorted-array-functions/-/sorted-array-functions-1.3.0.tgz#8605695563294dffb2c9796d602bd8459f7a0dd5" + integrity sha512-2sqgzeFlid6N4Z2fUQ1cvFmTOLRi/sEDzSQ0OKYchqgoPmQBVyM3959qYx3fpS6Esef80KjmpgPeEr028dP3OA== + source-list-map@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/source-list-map/-/source-list-map-2.0.1.tgz#3993bd873bfc48479cca9ea3a547835c7c154b34"