-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
205 lines (176 loc) · 8.49 KB
/
api.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var EventEmitter = require('events').EventEmitter;
var models = require('mongodb-models');
var _ = require('lazy.js');
module.exports = api;
function api(options) {
var modeler = models(options);
var Topic = modeler.createClass('Topic', options.topicsCollectionName || 'topics');
var AltTopicID = modeler.createClass('AltTopicID', options.altTopicIDsCollectionName || 'alternateTopicIds');
var Comment = modeler.createClass('Comment', options.commentsCollectionName || 'comments');
var Subscriber = modeler.createClass('Subscriber', options.subscribersCollectionName || 'subscribers');
AltTopicID.addReference('topic', 'topicId', Topic);
Topic.addLink('comments', '_id', {query: {topicId: Topic, commentId: Comment}}, 'topicComments')
.addLink('subscribers', '_id', {query: {topicId: Topic, subscriberId: Subscriber}}, 'topicSubscribers')
.addLink('likes', '_id', {query: {postId: Topic, subscriberId: Subscriber, type: 'topic'}}, 'postLikes')
.addReference('author', 'authorId', Subscriber);
Comment.addLink('topics', '_id', {query: {topicId: Topic, commentId: Comment}}, 'topicComments')
.addLink('likes', '_id', {query: {postId: Comment, subscriberId: Subscriber, type: 'comment'}}, 'postLikes')
.addReference('author', 'authorId', Subscriber);
Subscriber.addLink('likedTopics', '_id', {query: {postId: Topic, subscriberId: Subscriber, type: 'topic'}}, 'postLikes')
.addLink('likedComments', '_id', {query: {postId: Comment, subscriberId: Subscriber, type: 'comment'}}, 'postLikes')
.addLink('subscribedTopics', '_id', {query: {topicId: Topic, subscriberId: Subscriber}}, 'topicSubscribers');
var ret = new EventEmitter();
ret.createTopic = createTopic;
ret.findTopicIdFromAltId = findTopicIdFromAltId;
ret.setSubscriptionState = setSubscriptionState;
ret.getSubscriptionState = getSubscriptionState;
ret.postMessage = postMessage;
ret.getRecentTopics = getRecentTopics;
ret.getAllTopicDetails = getAllTopicDetails;
return ret;
function createTopic(userId, params, done) {
if (!userId) return done('Access denied');
if (!params.subject) return done ('Missing parameter: subject');
if (!params.body) return done('Missing parameter: body');
var subscriberIds = sanitizeSubscriberIds(params.subscriberIds);
var topicId = params.topicId && params.topicId.toString();
var topicPrefix = params.topicPrefix && params.topicPrefix.toString();
params = {
authorId: userId.toString(),
subject: params.subject.toString(),
body: params.body.toString(),
altTopicId: params.altTopicId && params.altTopicId.toString()
};
if (topicId) params._id = topicId;
else if (topicPrefix) params._id = {prefix: topicPrefix};
return addAlternateID.wrapped(Topic.create.one(params)).done(done);
function addAlternateID(topic, done) {
return emitCreatedNotification.wrapped(
topic,
topic.addSubscriberIds([params.authorId].concat(subscriberIds)),
params.altTopicId && AltTopicID.find.orCreate(params.altTopicId, {topicId: topic._id})
).sync(true).done(done);
}
function emitCreatedNotification(topic, _subscriberIgnored, altCreated) {
ret.emit('newTopic', topic);
return {topicId: topic._id};
}
}
function findTopicIdFromAltId(userId, params, done) {
if (!params.altTopicId) return done('Missing parameter: altTopicId');
var altTopicId = params.altTopicId.toString();
return formatResponse.wrapped(AltTopicID.find.byId(altTopicId).get('topicId')).sync(true).done(done)();
function formatResponse(topicId) { return {topicId: topicId}; }
}
function getSubscriptionState(userId, params, done) {
if (!userId) return done('Access denied');
if (!params.topicId) return done('Missing parameter: topicId');
userId = userId.toString();
return formatResponse.wrapped(Topic.find.byId(params.topicId.toString()).method('findSubscriberId', userId))
.sync(true).done(done);
function formatResponse(subId) { return {isSubscribed: (subId == userId)}; }
}
function setSubscriptionState(userId, params, done) {
if (!userId) return done('Access denied');
if (!params.topicId) return done('Missing parameter: topicId');
userId = userId.toString();
return setState.wrapped(Topic.find.byId(params.topicId.toString())).done(done);
function setState(topic, done) {
var op = params.isSubscribed ? topic.addSubscriberId(userId) : topic.removeSubscriberId(userId);
return op.successValue({}).done(done);
}
}
function postMessage(userId, params, done) {
if (!userId) return done('Access denied');
if (!params.topicId) return done('Missing parameter: topicId');
if (!params.body) return done('Missing parameter: body');
userId = userId.toString();
var newSubscriberIds = [userId].concat(sanitizeSubscriberIds(params.subscriberIds));
var topic = Topic.find.byId(params.topicId.toString());
var message = Comment.create.one({body: params.body.toString(), authorId: userId});
var linked = topic.method("addComment", message);
return postProcess.wrapped(topic, message, linked).done(done);
function postProcess(topic, message, linked, done) {
return deliverMessagesAndAddSubscription.wrapped(topic, message, topic.subscriberIds())
.done(done)();
}
function deliverMessagesAndAddSubscription(topic, message, subscriberIds, done) {
var needToAddIds = [];
newSubscriberIds.forEach(function (id) {
if (id && subscriberIds.indexOf(id) == -1 && needToAddIds.indexOf(id) == -1) needToAddIds.push(id);
});
if (needToAddIds.length) return topic.addSubscriberIds(needToAddIds).done(deliver)();
return deliver(null);
function deliver(err) {
deliverMessage(topic, message, userId, subscriberIds, needToAddIds);
return done(err);
}
}
}
function getRecentTopics(userId, params, done) {
if (!userId) return done('Access denied');
userId = userId.toString();
var query;
if (params.type == 'all') query = {deleted: {$ne: 1}};
else if (params.type == 'my') query = {deleted: {$ne: 1}, authorId: userId};
else if (params.type == 'subscribed') return formatTopics.wrapped(getSubscribedTopics(userId)).sync(true).done(done);
else return done('Invalid or missing parameter: type');
var skip = parseInt(params.skip || 0), limit = parseInt(params.limit || 100);
if (isNaN(skip)) skip = 0;
if (isNaN(limit)) limit = 100;
Topic.find.bySearch(query, {_id: -1}, skip, limit, function (err, ret) {
if (err) return done(err);
return done(null, formatTopics(ret));
});
}
function getAllTopicDetails(userId, params, done) {
if (!userId) return done('Access denied');
if (!params.topicId) return done('Missing parameter: topicId');
userId = userId.toString();
var topic = Topic.find.byId(params.topicId.toString());
var messages = topic.method('comments');
var subscriberIds = topic.method('subscriberIds');
return formatResponse.wrapped(topic, messages, subscriberIds).sync(true).done(done);
function formatResponse(topic, messages, subscriberIds) {
var ret = {messages: [], subscriberIds: subscriberIds || []};
ret.topicId = topic._id;
ret.authorId = topic.authorId;
ret.body = topic.body;
ret.subject = topic.subject;
ret.altTopicId = topic.altTopicId;
ret.createdTime = topic.createdTime;
ret.updatedTime = topic.updatedTime;
messages = messages || [];
messages.forEach(function (mm) { ret.messages.push({
messageId: mm._id,
body: mm.body,
authorId: mm.authorId,
createdTime: mm.createdTime,
updatedTime: mm.updatedTime
}); });
return ret;
}
}
function formatTopics(topics) {
var result = {topics: []};
(topics || []).forEach(function (rr) {
result.topics.push({topicId: rr._id, subject: rr.subject, body: rr.body, authorId: rr.authorId});
});
return result;
}
function sanitizeSubscriberIds(ids) {
var ret = [];
if (!ids) return ret;
if (ids instanceof Array) ids.forEach(function (id) { if (id) ret.push(id.toString()); });
return ret;
}
function getSubscribedTopics(userId, done) {
return Topic.fromSubscriberIds([userId.toString()]).done(done || function () {});
}
// expected callbacks
function deliverMessage(topic, message, from, to, addedTo) {
to = _(to).without(from).value();
addTo = _(addedTo).without(from).value();
return ret.emit('mail', {topic: topic, from: from, to: to, addedTo: addedTo, message: message});
}
}