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