2019-04-07 21:50:36 +09:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { Users } from '..';
|
|
|
|
import { Following } from '../entities/following';
|
2019-04-23 22:35:26 +09:00
|
|
|
import { awaitAll } from '../../prelude/await-all';
|
2021-03-23 17:43:07 +09:00
|
|
|
import { SchemaType } from '@/misc/schema';
|
2019-04-13 01:43:22 +09:00
|
|
|
|
|
|
|
type LocalFollowerFollowing = Following & {
|
|
|
|
followerHost: null;
|
|
|
|
followerInbox: null;
|
|
|
|
followerSharedInbox: null;
|
|
|
|
};
|
|
|
|
|
|
|
|
type RemoteFollowerFollowing = Following & {
|
|
|
|
followerHost: string;
|
|
|
|
followerInbox: string;
|
|
|
|
followerSharedInbox: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type LocalFolloweeFollowing = Following & {
|
|
|
|
followeeHost: null;
|
|
|
|
followeeInbox: null;
|
|
|
|
followeeSharedInbox: null;
|
|
|
|
};
|
|
|
|
|
|
|
|
type RemoteFolloweeFollowing = Following & {
|
|
|
|
followeeHost: string;
|
|
|
|
followeeInbox: string;
|
|
|
|
followeeSharedInbox: string;
|
|
|
|
};
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
export type PackedFollowing = SchemaType<typeof packedFollowingSchema>;
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
@EntityRepository(Following)
|
|
|
|
export class FollowingRepository extends Repository<Following> {
|
2019-04-13 01:43:22 +09:00
|
|
|
public isLocalFollower(following: Following): following is LocalFollowerFollowing {
|
|
|
|
return following.followerHost == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isRemoteFollower(following: Following): following is RemoteFollowerFollowing {
|
|
|
|
return following.followerHost != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isLocalFollowee(following: Following): following is LocalFolloweeFollowing {
|
|
|
|
return following.followeeHost == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isRemoteFollowee(following: Following): following is RemoteFolloweeFollowing {
|
|
|
|
return following.followeeHost != null;
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
public async pack(
|
|
|
|
src: Following['id'] | Following,
|
|
|
|
me?: any,
|
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
|
|
|
}
|
2019-04-23 22:35:26 +09:00
|
|
|
): Promise<PackedFollowing> {
|
2021-02-13 15:33:38 +09:00
|
|
|
const following = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
if (opts == null) opts = {};
|
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
return await awaitAll({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: following.id,
|
2019-04-23 22:35:26 +09:00
|
|
|
createdAt: following.createdAt.toISOString(),
|
2019-04-07 21:50:36 +09:00
|
|
|
followeeId: following.followeeId,
|
|
|
|
followerId: following.followerId,
|
|
|
|
followee: opts.populateFollowee ? Users.pack(following.followee || following.followeeId, me, {
|
|
|
|
detail: true
|
2019-04-23 22:35:26 +09:00
|
|
|
}) : undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
follower: opts.populateFollower ? Users.pack(following.follower || following.followerId, me, {
|
|
|
|
detail: true
|
2019-04-23 22:35:26 +09:00
|
|
|
}) : undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
}
|
2019-04-25 13:27:07 +09:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
followings: any[],
|
|
|
|
me?: any,
|
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
return Promise.all(followings.map(x => this.pack(x, me, opts)));
|
|
|
|
}
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
2019-04-23 22:35:26 +09:00
|
|
|
|
|
|
|
export const packedFollowingSchema = {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this following.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the following was created.'
|
|
|
|
},
|
|
|
|
followeeId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
followee: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'User',
|
|
|
|
description: 'The followee.'
|
|
|
|
},
|
|
|
|
followerId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
follower: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'User',
|
|
|
|
description: 'The follower.'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|