-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (108 loc) · 4.77 KB
/
index.js
File metadata and controls
121 lines (108 loc) · 4.77 KB
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
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);
});
// 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;
}
}