summaryrefslogtreecommitdiff
path: root/src/models/following.ts
blob: 8aa588f557eda30780866b1d41b10c0667db2649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as mongo from 'mongodb';
import db from '../db/mongodb';

const Following = db.get<IFollowing>('following');
Following.createIndex(['followerId', 'followeeId'], { unique: true });
export default Following;

export type IFollowing = {
	_id: mongo.ObjectID;
	createdAt: Date;
	followeeId: mongo.ObjectID;
	followerId: mongo.ObjectID;
	stalk: boolean;

	// 非正規化
	_followee: {
		host: string;
		inbox?: string;
		sharedInbox?: string;
	},
	_follower: {
		host: string;
		inbox?: string;
		sharedInbox?: string;
	}
};

/**
 * Followingを物理削除します
 */
export async function deleteFollowing(following: string | mongo.ObjectID | IFollowing) {
	let f: IFollowing;

	// Populate
	if (mongo.ObjectID.prototype.isPrototypeOf(following)) {
		f = await Following.findOne({
			_id: following
		});
	} else if (typeof following === 'string') {
		f = await Following.findOne({
			_id: new mongo.ObjectID(following)
		});
	} else {
		f = following as IFollowing;
	}

	if (f == null) return;

	// このFollowingを削除
	await Following.remove({
		_id: f._id
	});
}