forked from miguelfreitas/twister-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twister_directmsg.js
175 lines (140 loc) · 5.85 KB
/
twister_directmsg.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
// twister_directmsg.js
// 2013 Miguel Freitas
//
// Handle direct messages modal
function requestDMsnippetList(dmThreadList) {
var followList = [];
for( var i = 0; i < followingUsers.length; i++ ) {
followList.push({username:followingUsers[i]});
}
twisterRpc("getdirectmsgs", [defaultScreenName, 1, followList],
function(req, ret) {processDMsnippet(ret, dmThreadList);}, dmThreadList,
function(req, ret) {console.log("ajax error:" + ret);}, null);
}
function processDMsnippet(dmUsers, dmThreadList) {
dmThreadList.empty();
for( var u in dmUsers ) {
if( dmUsers.hasOwnProperty(u) ) {
var dmItem = dmDataToSnippetItem(dmUsers[u][0], u);
dmThreadList.append(dmItem);
}
}
$.MAL.dmThreadListLoaded();
}
function openDmConversation(dm_screenname, dmTitleName, dmConversation) {
getFullname( dm_screenname, dmTitleName );
dmConversation.attr("data-dm-screen-name", dm_screenname);
var dmConvo = dmConversation.find(".direct-messages-thread");
dmConvo.empty();
requestDmConversationModal(dmConvo,dm_screenname);
}
function requestDmConversationModal(dmConvo,dm_screenname) {
if( dmConvo.parents(".modal-blackout").css("display") == 'none' )
return;
requestDmConversation(dmConvo,dm_screenname);
setTimeout( function() {requestDmConversationModal(dmConvo,dm_screenname);}, 1000);
}
function requestDmConversation(dmConvo,dm_screenname) {
var since_id = undefined;
var oldItems = dmConvo.children();
if( oldItems.length ) {
since_id = parseInt(oldItems.eq(oldItems.length-1).attr("data-id"));
}
var userDmReq = [{username:dm_screenname}];
if( since_id != undefined ) userDmReq[0].since_id = since_id;
var count = 100;
twisterRpc("getdirectmsgs", [defaultScreenName,count,userDmReq],
function(args, ret) { processDmConversation(args.dmConvo, args.dmUser, ret); },
{dmConvo:dmConvo,dmUser:dm_screenname},
function(arg, ret) { var msg = ("message" in ret) ? ret.message : ret;
alert(polyglot.t("ajax_error", { error: msg })); }, null);
}
function processDmConversation(dmConvo, dm_screenname, dmData) {
var lastId = undefined;
if(dm_screenname in dmData) {
var dmList = dmData[dm_screenname];
if( dmList.length ) {
for( var i = 0; i < dmList.length; i++) {;
var dmItem = dmDataToConversationItem(dmList[i],defaultScreenName,dm_screenname);
dmItem.attr("data-id",dmList[i].id);
dmConvo.append(dmItem);
lastId = dmList[i].id;
}
$.MAL.dmChatListLoaded(dmConvo);
}
}
if( lastId != undefined ) {
resetNewDMsCountForUser(dm_screenname, lastId);
}
}
function directMsgSubmit(e)
{
e.stopPropagation();
e.preventDefault();
var $this = $( this );
var $replyText = $this.closest(".post-area-new").find("textarea");
var $dmConversation = $(".directMessages");
newDirectMsg($replyText.val(), $dmConversation.attr("data-dm-screen-name"));
$replyText.val("");
}
function newDirectMsg(msg, dm_screenname) {
if( lastPostId != undefined ) {
var params = [defaultScreenName, lastPostId + 1, dm_screenname, msg]
twisterRpc("newdirectmsg", params,
function(arg, ret) { incLastPostId(); }, null,
function(arg, ret) { var msg = ("message" in ret) ? ret.message : ret;
alert("Ajax error: " + msg); }, null);
} else {
alert(polyglot.t("Internal error: lastPostId unknown (following yourself may fix!)"));
}
}
//dispara o modal de direct messages
function directMessagesPopup()
{
var directMessagesClass = "directMessages";
openModal( directMessagesClass );
var directMessagesContent = $( ".direct-messages-template" ).html();
$( directMessagesContent ).clone().appendTo( ".directMessages .modal-content" );
//título do modal
$( ".directMessages h3" ).text( polyglot.t("Direct Messages") );
requestDMsnippetList($(".directMessages").find(".direct-messages-list"));
}
//exibe a thread de mensagens individual
function hideDmSnippetShowDmThread()
{
var $this = $( this );
//escondo a listagem de mensagens diretas...
var containerToAnimate = $this.parents( ".direct-messages-list" );
containerToAnimate.hide();
var dm_screenname = $this.attr("data-dm-screen-name");
openDmWithUserModal(dm_screenname);
}
function directMessagesWithUserPopup()
{
var $userInfo = $(this).closest("[data-screen-name]");
var dm_screenname = $userInfo.attr("data-screen-name");
openDmWithUserModal( dm_screenname );
}
function openDmWithUserModal(dm_screenname)
{
var directMessagesClass = "directMessages";
openModal( directMessagesClass );
//para poder exibir a thread selecionada...
var retweetContent = $( ".messages-thread-template" ).html();
$( retweetContent ).clone().appendTo( ".directMessages .modal-content" ).hide().fadeIn( "fast" );
var dmTitle = $( ".directMessages h3" );
dmTitle.html("Direct messages with <span></span>");
dmTitle = dmTitle.find("span");
var dmConversation = $(".directMessages");
openDmConversation(dm_screenname, dmTitle, dmConversation);
var $dmForm = $( ".dm-form-template" ).children().clone(true);
$dmForm.addClass("open");
$dmForm.appendTo( ".directMessages .modal-wrapper" ).hide().fadeIn( "fast" );
}
function initInterfaceDirectMsg() {
$( ".direct-messages" ).bind( "click", directMessagesPopup );
$( "#dm-snippet-template" ).bind( "click", hideDmSnippetShowDmThread );
$( ".dm-submit").click( directMsgSubmit );
$( ".userMenu-messages a" ).bind( "click", directMessagesPopup );
$( ".direct-messages-with-user" ).bind( "click", directMessagesWithUserPopup );
}