-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserswatchbutton.js
91 lines (76 loc) · 2.18 KB
/
userswatchbutton.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
(function() {
function followed($user) {
$(".UsersWatchButton[data-user='"+$user+"']").hide();
$(".UsersUnWatchButton[data-user='"+$user+"']").show();
};
function unfollowed($user) {
$(".UsersWatchButton[data-user='"+$user+"']").show();
$(".UsersUnWatchButton[data-user='"+$user+"']").hide();
};
function displayModal() {
$( "#connectionRequiredModal" ).modal();
}
$('.btn-message').click(function() {
if (typeof wgUserId == 'undefined') {
displayModal();
return false;
}
return true;
});
$('.UsersWatchButton').click(function() {
if (typeof wgUserId == 'undefined') {
displayModal();
return;
}
var userToFollow = $(this).attr('data-user');
var button = this;
// fonction to do second request to execute follow action
function ajaxFollow(jsondata) {
var token = jsondata.query.tokens.csrftoken;
$.ajax({
type: "POST",
url: mw.util.wikiScript('api'),
data: { action:'userswatch', format:'json', watch: 'yes', token: token, user: userToFollow},
dataType: 'json',
success: function (jsondata) {
if(jsondata.userswatch.success == 1) {
followed(userToFollow);
}
}});
};
// first request to get token
$.ajax({
type: "GET",
url: mw.util.wikiScript('api'),
data: { action:'query', format:'json', meta: 'tokens', type:'csrf'},
dataType: 'json',
success: ajaxFollow
});
});
$('.UsersUnWatchButton').click(function() {
var userToFollow = $(this).attr('data-user');
var button = this;
// fonction to do second request to execute follow action
function ajaxFollow(jsondata) {
var token = jsondata.query.tokens.csrftoken;
$.ajax({
type: "POST",
url: mw.util.wikiScript('api'),
data: { action:'userswatch', format:'json', watch: 'no', token: token, user: userToFollow},
dataType: 'json',
success: function (jsondata) {
if(jsondata.userswatch.success == 1) {
unfollowed(userToFollow);
}
}});
};
// first request to get token
$.ajax({
type: "GET",
url: mw.util.wikiScript('api'),
data: { action:'query', format:'json', meta: 'tokens', type:'csrf'},
dataType: 'json',
success: ajaxFollow
});
});
})();