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