-
Notifications
You must be signed in to change notification settings - Fork 0
/
sosmessage.js
94 lines (77 loc) · 2.3 KB
/
sosmessage.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
var sosmessage = {};
sosmessage.SosMessageClient = function(serverUrl) {
if (!/\/$/.test(serverUrl)) {
serverUrl += '/'
}
function doGet(url, settings) {
doAjaxCall(url, "GET", settings)
}
function doPost(url, settings) {
doAjaxCall(url, "POST", settings)
}
function doAjaxCall(url, type, settings) {
var data = settings.data || {}
$.ajax({
type: type,
url: url,
dataType: 'json',
data: settings.data,
success: function(data) {
if(data.meta.code === 200) {
settings.success && settings.success.call(self, data.response)
} else {
settings.error && settings.error.call(self, data)
}
},
error: function(data) {
settings.error && settings.error.call(self, data)
}
});
}
var client = {}
// categories
client.categories = function(settings) {
var url = serverUrl + 'categories'
doGet(url, settings)
}
// messages
client.messages = function(categoryId, settings) {
var url = serverUrl + 'categories/' + categoryId + '/messages'
doGet(url, settings)
}
client.randomMessage = function(categoryId, settings) {
var url = serverUrl + 'categories/' + categoryId + '/message'
doGet(url, settings)
}
client.bestMessages = function(categoryId, settings) {
var url = serverUrl + 'categories/' + categoryId + '/best'
doGet(url, settings)
}
client.worstMessages = function(categoryId, settings) {
var url = serverUrl + 'categories/' + categoryId + '/worst'
doGet(url, settings)
}
client.postMessage = function(categoryId, settings) {
var url = serverUrl + 'categories/' + categoryId + '/message'
doPost(url, settings)
}
client.voteMessage = function(messageId, settings) {
var url = serverUrl + 'messages/' + messageId + '/vote'
doPost(url, settings)
}
// comments
client.commentsForMessage = function(messageId, settings) {
var url = serverUrl + 'messages/' + messageId + '/comments'
doGet(url, settings)
}
client.postComment = function(messageId, settings) {
var url = serverUrl + 'messages/' + messageId + '/comment'
doPost(url, settings)
}
// announcements
client.announcements = function(settings) {
var url = serverUrl + 'announcements'
doGet(url, settings)
}
return client;
}