]>
Commit | Line | Data |
---|---|---|
cf405589 | 1 | import 'multer' |
8054669f C |
2 | import * as express from 'express' |
3 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' | |
4 | import { buildNSFWFilter, getCountVideos } from '../../../helpers/express-utils' | |
cf405589 | 5 | import { getFormattedObjects } from '../../../helpers/utils' |
74dc3bca | 6 | import { WEBSERVER } from '../../../initializers/constants' |
8054669f C |
7 | import { sequelizeTypescript } from '../../../initializers/database' |
8 | import { JobQueue } from '../../../lib/job-queue' | |
cf405589 C |
9 | import { |
10 | asyncMiddleware, | |
11 | asyncRetryTransactionMiddleware, | |
12 | authenticate, | |
13 | commonVideosFiltersValidator, | |
14 | paginationValidator, | |
15 | setDefaultPagination, | |
16 | setDefaultSort, | |
8054669f | 17 | setDefaultVideosSort, |
cf405589 C |
18 | userSubscriptionAddValidator, |
19 | userSubscriptionGetValidator | |
20 | } from '../../../middlewares' | |
225a7682 RK |
21 | import { |
22 | areSubscriptionsExistValidator, | |
8054669f | 23 | userSubscriptionListValidator, |
225a7682 | 24 | userSubscriptionsSortValidator, |
8054669f | 25 | videosSortValidator |
225a7682 | 26 | } from '../../../middlewares/validators' |
cf405589 | 27 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' |
8054669f | 28 | import { VideoModel } from '../../../models/video/video' |
cf405589 C |
29 | |
30 | const mySubscriptionsRouter = express.Router() | |
31 | ||
32 | mySubscriptionsRouter.get('/me/subscriptions/videos', | |
33 | authenticate, | |
34 | paginationValidator, | |
35 | videosSortValidator, | |
8054669f | 36 | setDefaultVideosSort, |
cf405589 C |
37 | setDefaultPagination, |
38 | commonVideosFiltersValidator, | |
39 | asyncMiddleware(getUserSubscriptionVideos) | |
40 | ) | |
41 | ||
42 | mySubscriptionsRouter.get('/me/subscriptions/exist', | |
43 | authenticate, | |
44 | areSubscriptionsExistValidator, | |
45 | asyncMiddleware(areSubscriptionsExist) | |
46 | ) | |
47 | ||
48 | mySubscriptionsRouter.get('/me/subscriptions', | |
49 | authenticate, | |
50 | paginationValidator, | |
51 | userSubscriptionsSortValidator, | |
52 | setDefaultSort, | |
53 | setDefaultPagination, | |
4f5d0459 | 54 | userSubscriptionListValidator, |
cf405589 C |
55 | asyncMiddleware(getUserSubscriptions) |
56 | ) | |
57 | ||
58 | mySubscriptionsRouter.post('/me/subscriptions', | |
59 | authenticate, | |
60 | userSubscriptionAddValidator, | |
a1587156 | 61 | addUserSubscription |
cf405589 C |
62 | ) |
63 | ||
64 | mySubscriptionsRouter.get('/me/subscriptions/:uri', | |
65 | authenticate, | |
66 | userSubscriptionGetValidator, | |
67 | getUserSubscription | |
68 | ) | |
69 | ||
70 | mySubscriptionsRouter.delete('/me/subscriptions/:uri', | |
71 | authenticate, | |
72 | userSubscriptionGetValidator, | |
73 | asyncRetryTransactionMiddleware(deleteUserSubscription) | |
74 | ) | |
75 | ||
76 | // --------------------------------------------------------------------------- | |
77 | ||
78 | export { | |
79 | mySubscriptionsRouter | |
80 | } | |
81 | ||
82 | // --------------------------------------------------------------------------- | |
83 | ||
84 | async function areSubscriptionsExist (req: express.Request, res: express.Response) { | |
85 | const uris = req.query.uris as string[] | |
dae86118 | 86 | const user = res.locals.oauth.token.User |
cf405589 C |
87 | |
88 | const handles = uris.map(u => { | |
89 | let [ name, host ] = u.split('@') | |
6dd9de95 | 90 | if (host === WEBSERVER.HOST) host = null |
cf405589 C |
91 | |
92 | return { name, host, uri: u } | |
93 | }) | |
94 | ||
95 | const results = await ActorFollowModel.listSubscribedIn(user.Account.Actor.id, handles) | |
96 | ||
97 | const existObject: { [id: string ]: boolean } = {} | |
98 | for (const handle of handles) { | |
99 | const obj = results.find(r => { | |
100 | const server = r.ActorFollowing.Server | |
101 | ||
102 | return r.ActorFollowing.preferredUsername === handle.name && | |
103 | ( | |
104 | (!server && !handle.host) || | |
105 | (server.host === handle.host) | |
106 | ) | |
107 | }) | |
108 | ||
109 | existObject[handle.uri] = obj !== undefined | |
110 | } | |
111 | ||
112 | return res.json(existObject) | |
113 | } | |
114 | ||
a1587156 | 115 | function addUserSubscription (req: express.Request, res: express.Response) { |
dae86118 | 116 | const user = res.locals.oauth.token.User |
cf405589 C |
117 | const [ name, host ] = req.body.uri.split('@') |
118 | ||
119 | const payload = { | |
120 | name, | |
121 | host, | |
2c0ccd4b | 122 | assertIsChannel: true, |
cf405589 C |
123 | followerActorId: user.Account.Actor.id |
124 | } | |
125 | ||
126 | JobQueue.Instance.createJob({ type: 'activitypub-follow', payload }) | |
cf405589 C |
127 | |
128 | return res.status(204).end() | |
129 | } | |
130 | ||
131 | function getUserSubscription (req: express.Request, res: express.Response) { | |
dae86118 | 132 | const subscription = res.locals.subscription |
cf405589 C |
133 | |
134 | return res.json(subscription.ActorFollowing.VideoChannel.toFormattedJSON()) | |
135 | } | |
136 | ||
137 | async function deleteUserSubscription (req: express.Request, res: express.Response) { | |
dae86118 | 138 | const subscription = res.locals.subscription |
cf405589 C |
139 | |
140 | await sequelizeTypescript.transaction(async t => { | |
141 | return subscription.destroy({ transaction: t }) | |
142 | }) | |
143 | ||
144 | return res.type('json').status(204).end() | |
145 | } | |
146 | ||
147 | async function getUserSubscriptions (req: express.Request, res: express.Response) { | |
dae86118 | 148 | const user = res.locals.oauth.token.User |
cf405589 C |
149 | const actorId = user.Account.Actor.id |
150 | ||
4f5d0459 RK |
151 | const resultList = await ActorFollowModel.listSubscriptionsForApi({ |
152 | actorId, | |
153 | start: req.query.start, | |
154 | count: req.query.count, | |
155 | sort: req.query.sort, | |
156 | search: req.query.search | |
157 | }) | |
cf405589 C |
158 | |
159 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
160 | } | |
161 | ||
dae86118 C |
162 | async function getUserSubscriptionVideos (req: express.Request, res: express.Response) { |
163 | const user = res.locals.oauth.token.User | |
fe987656 C |
164 | const countVideos = getCountVideos(req) |
165 | ||
cf405589 C |
166 | const resultList = await VideoModel.listForApi({ |
167 | start: req.query.start, | |
168 | count: req.query.count, | |
169 | sort: req.query.sort, | |
170 | includeLocalVideos: false, | |
171 | categoryOneOf: req.query.categoryOneOf, | |
172 | licenceOneOf: req.query.licenceOneOf, | |
173 | languageOneOf: req.query.languageOneOf, | |
174 | tagsOneOf: req.query.tagsOneOf, | |
175 | tagsAllOf: req.query.tagsAllOf, | |
176 | nsfw: buildNSFWFilter(res, req.query.nsfw), | |
177 | filter: req.query.filter as VideoFilter, | |
178 | withFiles: false, | |
179 | followerActorId: user.Account.Actor.id, | |
fe987656 C |
180 | user, |
181 | countVideos | |
cf405589 C |
182 | }) |
183 | ||
184 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
185 | } |