forked from jeffdonthemic/nforce-chatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (154 loc) · 6.57 KB
/
index.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
var _ = require('lodash');
module.exports = function(nforce, pluginName) {
if (!pluginName) pluginName = 'chatter';
// throws if the plugin already exists
var plugin = nforce.plugin(pluginName);
// http://www.salesforce.com/us/developer/docs/chatterapi/Content/quickreference_get_activity_statistics_for_user.htm
plugin.fn('userStatistics', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/users/' + args.id;
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// http://www.salesforce.com/us/developer/docs/chatterapi/Content/quickreference_get_news_feed.htm
plugin.fn('myNewsFeed', function(args, callback) {
var opts = this._getOpts(args, callback);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/feeds/record/me/feed-elements';
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// http://www.salesforce.com/us/developer/docs/chatterapi/Content/quickreference_get_feed_items_posted_to_record.htm
plugin.fn('recordFeed', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/feeds/record/' + args.id + '/feed-elements';
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// http://www.salesforce.com/us/developer/docs/chatterapi/Content/quickreference_get_group_feed.htm
plugin.fn('groupFeed', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/feeds/record/' + args.id + '/feed-elements';
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// http://www.salesforce.com/us/developer/docs/chatterapi/Content/quickreference_post_comment_to_feed_element.htm
plugin.fn('postComment', function(args, callback) {
var validator = validate(args, ['id', 'text']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/feed-elements/' + args.id + '/capabilities/comments/items';
opts.method = 'POST';
var body = {
"body":
{ "messageSegments":
[ { "type":"Text", "text": args.text } ]
}
}
opts.body = JSON.stringify(body);
return this._apiRequest(opts, opts.callback);
});
// http://www.salesforce.com/us/developer/docs/chatterapi/Content/quickreference_like_feed_item.htm
plugin.fn('likeFeedItem', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/feed-elements/' + args.id + '/capabilities/chatter-likes/items';
opts.method = 'POST';
return this._apiRequest(opts, opts.callback);
});
// http://www.salesforce.com/us/developer/docs/chatterapi/Content/quickreference_post_feed_item.htm
plugin.fn('postFeedItem', function(args, callback) {
var validator = validate(args, ['id', 'text']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/feed-elements';
var body = {
"body":
{ "messageSegments":
[ { "type":"Text", "text": args.text } ]
},
"feedElementType" : "FeedItem",
"subjectId" : args.id
}
opts.method = 'POST';
opts.body = JSON.stringify(body);
return this._apiRequest(opts, opts.callback);
});
// http://www.salesforce.com/us/developer/docs/chatterapi/Content/quickreference_post_feed_item.htm
plugin.fn('postFeedItemLink', function(args, callback) {
var validator = validate(args, ['id', 'urlText', 'url']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/feed-elements';
var body = {
"feedElementType" : "FeedItem",
"subjectId" : args.id,
"capabilities": {
"link": {
"url": args.url,
"urlName": args.urlText
}
}
};
if(args.text) {
body.body = { "messageSegments": [ { "type": "Text", "text": args.text } ] };
}
opts.method = 'POST';
opts.body = JSON.stringify(body);
return this._apiRequest(opts, opts.callback);
});
// https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/connect_resources_feed_element_specific.htm#connect_resources_feed_element_specific
plugin.fn('editFeedItemLink', function(args, callback) {
var validator = validate(args, ['id', 'urlText', 'url']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/chatter/feed-elements/' + args.id;
var body = {
"capabilities": {
"link": {
"url": args.url,
"urlName": args.urlText
}
}
};
if(args.text) {
body.body = { "messageSegments": [ { "type": "Text", "text": args.text } ] };
}
opts.method = 'PATCH';
opts.body = JSON.stringify(body);
return this._apiRequest(opts, opts.callback);
});
// utility method to validate inputs
function validate(args, required) {
var result = {
error: false,
message: 'No errors'
}
// ensure required properties were passed in the arguments hash
if (required) {
var keys = _.keys(args);
required.forEach(function(field) {
if(!_.contains(keys, field)) {
result.error = true;
result.message = 'The following values must be passed: ' + required.join(', ');
}
})
}
return result;
}
}