Merge pull request 'server: refactor follow request functions to be named exports' (#296) from refactor/follow-requests into main
All checks were successful
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/lint-sw Pipeline was successful
ci/woodpecker/push/test Pipeline was successful

Reviewed-on: #296
This commit is contained in:
Norm 2022-12-23 02:06:31 +00:00
commit 0e1459e5cf
10 changed files with 33 additions and 17 deletions

View file

@ -1,5 +1,5 @@
import { CacheableRemoteUser } from '@/models/entities/user.js'; import { CacheableRemoteUser } from '@/models/entities/user.js';
import accept from '@/services/following/requests/accept.js'; import { acceptFollowRequest } from '@/services/following/requests/accept.js';
import { relayAccepted } from '@/services/relay.js'; import { relayAccepted } from '@/services/relay.js';
import { IFollow } from '@/remote/activitypub/type.js'; import { IFollow } from '@/remote/activitypub/type.js';
import { DbResolver } from '@/remote/activitypub/db-resolver.js'; import { DbResolver } from '@/remote/activitypub/db-resolver.js';
@ -24,6 +24,6 @@ export default async (actor: CacheableRemoteUser, activity: IFollow): Promise<st
return await relayAccepted(match[1]); return await relayAccepted(match[1]);
} }
await accept(actor, follower); await acceptFollowRequest(actor, follower);
return 'ok'; return 'ok';
}; };

View file

@ -1,5 +1,5 @@
import unfollow from '@/services/following/delete.js'; import unfollow from '@/services/following/delete.js';
import cancelRequest from '@/services/following/requests/cancel.js'; import { cancelFollowRequest } from '@/services/following/requests/cancel.js';
import { CacheableRemoteUser } from '@/models/entities/user.js'; import { CacheableRemoteUser } from '@/models/entities/user.js';
import { FollowRequests, Followings } from '@/models/index.js'; import { FollowRequests, Followings } from '@/models/index.js';
import { IFollow } from '@/remote/activitypub/type.js'; import { IFollow } from '@/remote/activitypub/type.js';
@ -29,7 +29,7 @@ export default async (actor: CacheableRemoteUser, activity: IFollow): Promise<st
]); ]);
if (req) { if (req) {
await cancelRequest(followee, actor); await cancelFollowRequest(followee, actor);
return 'ok: follow request canceled'; return 'ok: follow request canceled';
} else if (following) { } else if (following) {
await unfollow(actor, followee); await unfollow(actor, followee);

View file

@ -1,4 +1,4 @@
import acceptFollowRequest from '@/services/following/requests/accept.js'; import { acceptFollowRequest } from '@/services/following/requests/accept.js';
import define from '../../../define.js'; import define from '../../../define.js';
import { ApiError } from '../../../error.js'; import { ApiError } from '../../../error.js';
import { getUser } from '../../../common/getters.js'; import { getUser } from '../../../common/getters.js';

View file

@ -1,4 +1,4 @@
import cancelFollowRequest from '@/services/following/requests/cancel.js'; import { cancelFollowRequest } from '@/services/following/requests/cancel.js';
import { Users } from '@/models/index.js'; import { Users } from '@/models/index.js';
import { IdentifiableError } from '@/misc/identifiable-error.js'; import { IdentifiableError } from '@/misc/identifiable-error.js';
import define from '../../../define.js'; import define from '../../../define.js';

View file

@ -2,7 +2,7 @@ import RE2 from 're2';
import * as mfm from 'mfm-js'; import * as mfm from 'mfm-js';
import { notificationTypes } from 'foundkey-js'; import { notificationTypes } from 'foundkey-js';
import { publishMainStream, publishUserEvent } from '@/services/stream.js'; import { publishMainStream, publishUserEvent } from '@/services/stream.js';
import acceptAllFollowRequests from '@/services/following/requests/accept-all.js'; import { acceptAllFollowRequests } from '@/services/following/requests/accept-all.js';
import { publishToFollowers } from '@/services/i/update.js'; import { publishToFollowers } from '@/services/i/update.js';
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm.js'; import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm.js';
import { extractHashtags } from '@/misc/extract-hashtags.js'; import { extractHashtags } from '@/misc/extract-hashtags.js';

View file

@ -15,7 +15,7 @@ import { getActiveWebhooks } from '@/misc/webhook-cache.js';
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js'; import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js';
import Logger from '../logger.js'; import Logger from '../logger.js';
import { createNotification } from '../create-notification.js'; import { createNotification } from '../create-notification.js';
import createFollowRequest from './requests/create.js'; import { createFollowRequest } from './requests/create.js';
const logger = new Logger('following/create'); const logger = new Logger('following/create');

View file

@ -1,18 +1,18 @@
import { User } from '@/models/entities/user.js'; import { User } from '@/models/entities/user.js';
import { FollowRequests, Users } from '@/models/index.js'; import { FollowRequests, Users } from '@/models/index.js';
import accept from './accept.js'; import { acceptFollowRequest } from './accept.js';
/** /**
* * Approve all follow requests addressed to the specified user.
* @param user * @param user The user whom to accept all follow requests to
*/ */
export default async function(user: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }) { export async function acceptAllFollowRequests(user: User): Promise<void> {
const requests = await FollowRequests.findBy({ const requests = await FollowRequests.findBy({
followeeId: user.id, followeeId: user.id,
}); });
for (const request of requests) { for (const request of requests) {
const follower = await Users.findOneByOrFail({ id: request.followerId }); const follower = await Users.findOneByOrFail({ id: request.followerId });
accept(user, follower); acceptFollowRequest(user, follower);
} }
} }

View file

@ -3,12 +3,17 @@ import renderFollow from '@/remote/activitypub/renderer/follow.js';
import renderAccept from '@/remote/activitypub/renderer/accept.js'; import renderAccept from '@/remote/activitypub/renderer/accept.js';
import { deliver } from '@/queue/index.js'; import { deliver } from '@/queue/index.js';
import { publishMainStream } from '@/services/stream.js'; import { publishMainStream } from '@/services/stream.js';
import { User, CacheableUser } from '@/models/entities/user.js'; import { User } from '@/models/entities/user.js';
import { FollowRequests, Users } from '@/models/index.js'; import { FollowRequests, Users } from '@/models/index.js';
import { IdentifiableError } from '@/misc/identifiable-error.js'; import { IdentifiableError } from '@/misc/identifiable-error.js';
import { insertFollowingDoc } from '../create.js'; import { insertFollowingDoc } from '../create.js';
export default async function(followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, follower: CacheableUser) { /**
* Accept a follow request from user `followee` to follow `follower`.
* @param followee User who is being followed
* @param follower User making the follow request
*/
export async function acceptFollowRequest(followee: User, follower: User): Promise<void> {
const request = await FollowRequests.findOneBy({ const request = await FollowRequests.findOneBy({
followeeId: followee.id, followeeId: followee.id,
followerId: follower.id, followerId: follower.id,

View file

@ -7,7 +7,12 @@ import { IdentifiableError } from '@/misc/identifiable-error.js';
import { User } from '@/models/entities/user.js'; import { User } from '@/models/entities/user.js';
import { Users, FollowRequests } from '@/models/index.js'; import { Users, FollowRequests } from '@/models/index.js';
export default async function(followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox'] }, follower: { id: User['id']; host: User['host']; uri: User['host'] }) { /**
* Cancel a follow request from `follower` to `followee`.
* @param followee User that was going to be followed
* @param follower User who is making the follow request
*/
export async function cancelFollowRequest(followee: User, follower: User): Promise<void> {
if (Users.isRemoteUser(followee)) { if (Users.isRemoteUser(followee)) {
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower)); const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));

View file

@ -7,7 +7,13 @@ import { Blockings, FollowRequests, Users } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js'; import { genId } from '@/misc/gen-id.js';
import { createNotification } from '@/services/create-notification.js'; import { createNotification } from '@/services/create-notification.js';
export default async function(follower: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, requestId?: string) { /**
* Make a follow request from `follower` to `followee`.
* @param follower User making the follow request
* @param followee User to make the follow request to
* @param requestId Follow request ID
*/
export async function createFollowRequest(follower: User, followee: User, requestId?: string): Promise<void> {
if (follower.id === followee.id) return; if (follower.id === followee.id) return;
// check blocking // check blocking