This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
/
follow.js
183 lines (172 loc) · 4.19 KB
/
follow.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import mongoose, { Schema } from 'mongoose';
import timestamps from 'mongoose-timestamp';
import mongooseStringQuery from 'mongoose-string-query';
import autopopulate from 'mongoose-autopopulate';
import stream from 'getstream';
import config from '../config';
import RSS from './rss';
import Podcast from './podcast';
import { getStreamClient } from '../utils/stream';
export const FollowSchema = new Schema(
{
user: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
autopopulate: {
select: [
'name',
'email',
'username',
'bio',
'url',
'twitter',
'background',
'admin',
],
},
index: true,
},
followee: {
type: Schema.Types.ObjectId,
ref: 'User',
autopopulate: {
select: [
'name',
'email',
'username',
'bio',
'url',
'twitter',
'background',
],
},
index: true,
},
podcast: {
type: Schema.Types.ObjectId,
ref: 'Podcast',
autopopulate: {
select: [
'url',
'title',
'categories',
'description',
'feedUrl',
'image',
'publicationDate',
'public',
'featured',
'images',
'duplicateOf',
],
},
index: true,
},
rss: {
type: Schema.Types.ObjectId,
ref: 'RSS',
autopopulate: {
select: [
'url',
'title',
'categories',
'description',
'favicon',
'publicationDate',
'public',
'featured',
'images',
'feedUrl',
'duplicateOf',
],
},
index: true,
},
feed: {
type: String,
enum: ['rss', 'podcast', 'timeline'],
},
},
{ collection: 'follows' },
);
FollowSchema.plugin(timestamps, {
createdAt: { index: true },
updatedAt: { index: true },
});
FollowSchema.plugin(mongooseStringQuery);
FollowSchema.plugin(autopopulate);
FollowSchema.index({ user: 1, rss: 1, podcast: 1 }, { unique: true });
FollowSchema.methods.removeFromStream = async function remove(follows) {
const publicationType = this.rss ? 'rss' : 'podcast';
const feedGroup = this.rss ? 'user_article' : 'user_episode';
const publicationID = this.rss ? this.rss._id : this.podcast._id;
if (!this.user) {
return [];
}
const timelineFeed = getStreamClient().feed('timeline', this.user._id);
const otherFeed = getStreamClient().feed(feedGroup, this.user._id);
const results = await Promise.all([
timelineFeed.unfollow(publicationType, publicationID),
otherFeed.unfollow(publicationType, publicationID),
]);
return results;
};
FollowSchema.statics.getOrCreateMany = async function getOrCreateMany(follows) {
// validate
for (const f of follows) {
if (f.type != 'rss' && f.type != 'podcast') {
throw new Error(`invalid follow type ${f.type}`);
}
}
// batch create the follow relationships
const followInstances = await Promise.all(
follows.map(async (f) => {
const query = { [f.type]: f.publicationID, user: f.userID };
let instance = await this.findOne(query).lean();
if (!instance) {
instance = await this.create(query);
}
return instance;
}),
);
// sync to stream in a batch
const feedRelationsTimeline = follows.map((f) => {
return {
source: `timeline:${f.userID}`,
target: `${f.type}:${f.publicationID}`,
};
});
const feedRelationsGroup = follows.map((f) => {
const feedGroup = f.type == 'rss' ? 'user_article' : 'user_episode';
return {
source: `${feedGroup}:${f.userID}`,
target: `${f.type}:${f.publicationID}`,
};
});
const feedRelations = feedRelationsTimeline.concat(feedRelationsGroup);
if (feedRelations.length > 0) {
await getStreamClient().followMany(feedRelations);
}
// update the counts
await Promise.all(
follows.map(async (f) => {
const followerCount = await this.count({ [f.type]: f.publicationID });
const schema = f.type == 'rss' ? RSS : Podcast;
await schema.update({ _id: f.publicationID }, { followerCount });
}),
);
return followInstances;
};
FollowSchema.statics.getOrCreate = async function getOrCreate(
followType,
userID,
publicationID,
) {
const instances = await this.getOrCreateMany([
{ type: followType, userID: userID, publicationID: publicationID },
]);
return instances[0];
};
module.exports = exports = mongoose.model('Follow', FollowSchema);
module.exports.FollowSchema = FollowSchema;