mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 02:39:33 +02:00
Refractor activity pub lib/helpers
This commit is contained in:
parent
eb8b27c93e
commit
5414139835
47 changed files with 844 additions and 498 deletions
|
@ -6,14 +6,16 @@ import { getServerAccount } from '../../../helpers/utils'
|
|||
import { getAccountFromWebfinger } from '../../../helpers/webfinger'
|
||||
import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants'
|
||||
import { database as db } from '../../../initializers/database'
|
||||
import { sendFollow } from '../../../lib/activitypub/send-request'
|
||||
import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../../middlewares'
|
||||
import { asyncMiddleware, paginationValidator, removeFollowingValidator, setFollowersSort, setPagination } from '../../../middlewares'
|
||||
import { authenticate } from '../../../middlewares/oauth'
|
||||
import { setBodyHostsPort } from '../../../middlewares/servers'
|
||||
import { setFollowingSort } from '../../../middlewares/sort'
|
||||
import { ensureUserHasRight } from '../../../middlewares/user-right'
|
||||
import { followValidator } from '../../../middlewares/validators/servers'
|
||||
import { followValidator } from '../../../middlewares/validators/follows'
|
||||
import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort'
|
||||
import { AccountFollowInstance } from '../../../models/index'
|
||||
import { sendFollow } from '../../../lib/index'
|
||||
import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo'
|
||||
|
||||
const serverFollowsRouter = express.Router()
|
||||
|
||||
|
@ -33,6 +35,13 @@ serverFollowsRouter.post('/following',
|
|||
asyncMiddleware(follow)
|
||||
)
|
||||
|
||||
serverFollowsRouter.delete('/following/:accountId',
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
|
||||
removeFollowingValidator,
|
||||
asyncMiddleware(removeFollow)
|
||||
)
|
||||
|
||||
serverFollowsRouter.get('/followers',
|
||||
paginationValidator,
|
||||
followersSortValidator,
|
||||
|
@ -96,10 +105,12 @@ async function follow (req: express.Request, res: express.Response, next: expres
|
|||
},
|
||||
transaction: t
|
||||
})
|
||||
accountFollow.AccountFollowing = targetAccount
|
||||
accountFollow.AccountFollower = fromAccount
|
||||
|
||||
// Send a notification to remote server
|
||||
if (accountFollow.state === 'pending') {
|
||||
await sendFollow(fromAccount, targetAccount, t)
|
||||
await sendFollow(accountFollow, t)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -117,6 +128,17 @@ async function follow (req: express.Request, res: express.Response, next: expres
|
|||
return res.status(204).end()
|
||||
}
|
||||
|
||||
async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||
const following: AccountFollowInstance = res.locals.following
|
||||
|
||||
await db.sequelize.transaction(async t => {
|
||||
await sendUndoFollow(following, t)
|
||||
await following.destroy({ transaction: t })
|
||||
})
|
||||
|
||||
return res.status(204).end()
|
||||
}
|
||||
|
||||
async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) {
|
||||
let loadedFromDB = true
|
||||
let account = await db.Account.loadByNameAndHost(name, host)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue