diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-12 17:53:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 16:50:33 +0100 |
commit | 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch) | |
tree | e5ca358287fca6ecacce83defcf23af1e8e9f419 /server/controllers/api/server/follows.ts | |
parent | c893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff) | |
download | PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip |
Move models to typescript-sequelize
Diffstat (limited to 'server/controllers/api/server/follows.ts')
-rw-r--r-- | server/controllers/api/server/follows.ts | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index c2fb37c39..913998e3a 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts | |||
@@ -1,24 +1,24 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { UserRight } from '../../../../shared/models/users/user-right.enum' | 2 | import { UserRight } from '../../../../shared/models/users' |
3 | import { getFormattedObjects } from '../../../helpers' | 3 | import { getAccountFromWebfinger, getFormattedObjects, getServerAccount, logger, retryTransactionWrapper } from '../../../helpers' |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 4 | import { sequelizeTypescript, SERVER_ACCOUNT_NAME } from '../../../initializers' |
5 | import { logger } from '../../../helpers/logger' | 5 | import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub' |
6 | import { getServerAccount } from '../../../helpers/utils' | 6 | import { sendUndoFollow } from '../../../lib/activitypub/send' |
7 | import { getAccountFromWebfinger } from '../../../helpers/webfinger' | ||
8 | import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants' | ||
9 | import { database as db } from '../../../initializers/database' | ||
10 | import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub/account' | ||
11 | import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo' | ||
12 | import { sendFollow } from '../../../lib/index' | 7 | import { sendFollow } from '../../../lib/index' |
13 | import { asyncMiddleware, paginationValidator, removeFollowingValidator, setFollowersSort, setPagination } from '../../../middlewares' | 8 | import { |
14 | import { authenticate } from '../../../middlewares/oauth' | 9 | asyncMiddleware, |
15 | import { setBodyHostsPort } from '../../../middlewares/servers' | 10 | authenticate, |
16 | import { setFollowingSort } from '../../../middlewares/sort' | 11 | ensureUserHasRight, |
17 | import { ensureUserHasRight } from '../../../middlewares/user-right' | 12 | paginationValidator, |
18 | import { followValidator } from '../../../middlewares/validators/follows' | 13 | removeFollowingValidator, |
19 | import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort' | 14 | setBodyHostsPort, |
20 | import { AccountInstance } from '../../../models/account/account-interface' | 15 | setFollowersSort, |
21 | import { AccountFollowInstance } from '../../../models/index' | 16 | setFollowingSort, |
17 | setPagination | ||
18 | } from '../../../middlewares' | ||
19 | import { followersSortValidator, followingSortValidator, followValidator } from '../../../middlewares/validators' | ||
20 | import { AccountModel } from '../../../models/account/account' | ||
21 | import { AccountFollowModel } from '../../../models/account/account-follow' | ||
22 | 22 | ||
23 | const serverFollowsRouter = express.Router() | 23 | const serverFollowsRouter = express.Router() |
24 | 24 | ||
@@ -63,14 +63,14 @@ export { | |||
63 | 63 | ||
64 | async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) { | 64 | async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) { |
65 | const serverAccount = await getServerAccount() | 65 | const serverAccount = await getServerAccount() |
66 | const resultList = await db.AccountFollow.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) | 66 | const resultList = await AccountFollowModel.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) |
67 | 67 | ||
68 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 68 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
69 | } | 69 | } |
70 | 70 | ||
71 | async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) { | 71 | async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) { |
72 | const serverAccount = await getServerAccount() | 72 | const serverAccount = await getServerAccount() |
73 | const resultList = await db.AccountFollow.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) | 73 | const resultList = await AccountFollowModel.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) |
74 | 74 | ||
75 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 75 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
76 | } | 76 | } |
@@ -110,14 +110,14 @@ async function followRetry (req: express.Request, res: express.Response, next: e | |||
110 | return res.status(204).end() | 110 | return res.status(204).end() |
111 | } | 111 | } |
112 | 112 | ||
113 | async function follow (fromAccount: AccountInstance, targetAccount: AccountInstance, targetAlreadyInDB: boolean) { | 113 | async function follow (fromAccount: AccountModel, targetAccount: AccountModel, targetAlreadyInDB: boolean) { |
114 | try { | 114 | try { |
115 | await db.sequelize.transaction(async t => { | 115 | await sequelizeTypescript.transaction(async t => { |
116 | if (targetAlreadyInDB === false) { | 116 | if (targetAlreadyInDB === false) { |
117 | await saveAccountAndServerIfNotExist(targetAccount, t) | 117 | await saveAccountAndServerIfNotExist(targetAccount, t) |
118 | } | 118 | } |
119 | 119 | ||
120 | const [ accountFollow ] = await db.AccountFollow.findOrCreate({ | 120 | const [ accountFollow ] = await AccountFollowModel.findOrCreate({ |
121 | where: { | 121 | where: { |
122 | accountId: fromAccount.id, | 122 | accountId: fromAccount.id, |
123 | targetAccountId: targetAccount.id | 123 | targetAccountId: targetAccount.id |
@@ -145,9 +145,9 @@ async function follow (fromAccount: AccountInstance, targetAccount: AccountInsta | |||
145 | } | 145 | } |
146 | 146 | ||
147 | async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) { | 147 | async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) { |
148 | const follow: AccountFollowInstance = res.locals.follow | 148 | const follow: AccountFollowModel = res.locals.follow |
149 | 149 | ||
150 | await db.sequelize.transaction(async t => { | 150 | await sequelizeTypescript.transaction(async t => { |
151 | if (follow.state === 'accepted') await sendUndoFollow(follow, t) | 151 | if (follow.state === 'accepted') await sendUndoFollow(follow, t) |
152 | 152 | ||
153 | await follow.destroy({ transaction: t }) | 153 | await follow.destroy({ transaction: t }) |
@@ -164,7 +164,7 @@ async function removeFollow (req: express.Request, res: express.Response, next: | |||
164 | 164 | ||
165 | async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { | 165 | async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { |
166 | let loadedFromDB = true | 166 | let loadedFromDB = true |
167 | let account = await db.Account.loadByNameAndHost(name, host) | 167 | let account = await AccountModel.loadByNameAndHost(name, host) |
168 | 168 | ||
169 | if (!account) { | 169 | if (!account) { |
170 | const nameWithDomain = name + '@' + host | 170 | const nameWithDomain = name + '@' + host |