Message fetch handling

This commit is contained in:
Jeremy Zhang 2017-04-05 03:46:48 +00:00
parent 2647021bf7
commit 6791f2b29b
2 changed files with 56 additions and 14 deletions

View File

@ -4,6 +4,20 @@
/* global guild_id */ /* global guild_id */
var logintimer; // timer to keep track of user inactivity after hitting login var logintimer; // timer to keep track of user inactivity after hitting login
var last_message_id;
function element_in_view(element, fullyInView) {
var pageTop = $(window).scrollTop();
var pageBottom = pageTop + $(window).height();
var elementTop = $(element).offset().top;
var elementBottom = elementTop + $(element).height();
if (fullyInView === true) {
return ((pageTop < elementTop) && (pageBottom > elementBottom));
} else {
return ((elementTop <= pageBottom) && (elementBottom >= pageTop));
}
}
function resize_messagebox() { function resize_messagebox() {
var namebox_width = $("#nameplate").outerWidth(true); var namebox_width = $("#nameplate").outerWidth(true);
@ -51,8 +65,7 @@ function fetch(channel_id, after=null) {
} }
$(function(){ $(function(){
resize_messagebox(); resize_messagebox();
$("#loginmodal").modal({ $("#loginmodal").modal({
dismissible: false, // Modal can be dismissed by clicking outside of the modal dismissible: false, // Modal can be dismissed by clicking outside of the modal
opacity: .5, // Opacity of modal background opacity: .5, // Opacity of modal background
@ -178,13 +191,40 @@ function _wait_for_discord_login(index) {
}, 5000); }, 5000);
} }
function fill_discord_messages(messages, jumpscroll) {
if (messages.length == 0) {
return last_message_id;
}
var last = 0;
var template = $('#mustache_usermessage').html();
Mustache.parse(template);
for (var i = messages.length-1; i >= 0; i--) {
var message = messages[i];
var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.timestamp, "time": message.timestamp, "username": message.author.username, "discriminator": message.author.discriminator, "content": message.content});
$("#chatcontent").append(rendered);
last = message.id;
}
$("html, body").animate({ scrollTop: $(document).height() }, "slow");
return last;
}
function run_fetch_routine() { function run_fetch_routine() {
var channel_id = guild_id; //TODO: implement channel selector var channel_id = guild_id; //TODO: implement channel selector
var fet = fetch(channel_id); var fet;
var jumpscroll;
if (last_message_id == null) {
$("#chatcontent").empty();
fet = fetch(channel_id);
jumpscroll = true;
} else {
fet = fetch(channel_id, last_message_id);
jumpscroll = element_in_view($('#discordmessage_'+last_message_id), true);
}
fet.done(function(data) { fet.done(function(data) {
console.log(data); console.log(data);
var status = data.status; var status = data.status;
update_embed_userchip(status.authenticated, status.avatar, status.username, status.user_id); update_embed_userchip(status.authenticated, status.avatar, status.username, status.user_id);
last_message_id = fill_discord_messages(data.messages, jumpscroll);
var guild = query_guild(); var guild = query_guild();
guild.done(function(guildobj) { guild.done(function(guildobj) {
fill_channels(guildobj.channels); fill_channels(guildobj.channels);
@ -195,15 +235,16 @@ function run_fetch_routine() {
}); });
}); });
fet.fail(function(data) { fet.fail(function(data) {
if (data.status == 403) { if (data.status == 403) {
$('#loginmodal').modal('open'); $('#loginmodal').modal('open');
Materialize.toast('Authentication error! You have been banned.', 10000); Materialize.toast('Authentication error! You have been banned.', 10000);
} else if (data.status == 401) { } else if (data.status == 401) {
$('#loginmodal').modal('open'); $('#loginmodal').modal('open');
Materialize.toast('Session expired! You have been logged out.', 10000); Materialize.toast('Session expired! You have been logged out.', 10000);
} else { }
setTimeout(run_fetch_routine, 10000); });
} fet.catch(function(data) {
setTimeout(run_fetch_routine, 10000);
}); });
} }
@ -216,6 +257,7 @@ function update_embed_userchip(authenticated, avatar, username, userid) {
$("#currentuserimage").hide(); $("#currentuserimage").hide();
$("#currentusername").text(username + "#" + userid); $("#currentusername").text(username + "#" + userid);
} }
resize_messagebox();
} }
$("#discordlogin_btn").click(function() { $("#discordlogin_btn").click(function() {

View File

@ -25,7 +25,7 @@
</nav> </nav>
</div> </div>
<main> <main>
<div class="chatcontent"> <div id="chatcontent" class="chatcontent">
<p><span title="March 31, 2017 3:30PM" class="chattimestamp">3:30PM</span> <span class="chatusername">EndenDragon#69420</span> Hello everyone!</p> <p><span title="March 31, 2017 3:30PM" class="chattimestamp">3:30PM</span> <span class="chatusername">EndenDragon#69420</span> Hello everyone!</p>
<p><span title="March 31, 2017 3:30PM" class="chattimestamp">3:30PM</span> <span class="chatusername">EndenDragon#69420</span> Hello everyone!</p> <p><span title="March 31, 2017 3:30PM" class="chattimestamp">3:30PM</span> <span class="chatusername">EndenDragon#69420</span> Hello everyone!</p>
<p><span title="March 31, 2017 3:30PM" class="chattimestamp">3:30PM</span> <span class="chatusername">EndenDragon#69420</span> Hello everyone!</p> <p><span title="March 31, 2017 3:30PM" class="chattimestamp">3:30PM</span> <span class="chatusername">EndenDragon#69420</span> Hello everyone!</p>
@ -121,7 +121,7 @@
</script> </script>
<script id="mustache_usermessage" type="text/template"> <script id="mustache_usermessage" type="text/template">
<p><span title="March 31, 2017 3:30PM" class="chattimestamp">3:30PM</span> <span class="chatusername">EndenDragon#69420</span> Hello everyone!</p> <p><span id="discordmessage_{{id}}" title="{{full_timestamp}}" class="chattimestamp">{{time}}</span> <span class="chatusername">{{username}}#{{discriminator}}</span> {{content}}</p>
</script> </script>
{% endraw %} {% endraw %}