Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
Merge branch 'release/0.3.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Jun 15, 2012
2 parents 7ade3de + e9a2d5d commit dcf9bbb
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,5 +2,6 @@
*~
log/*
tmp/*
*.rdb

config.rb
17 changes: 17 additions & 0 deletions main.rb
Expand Up @@ -80,6 +80,16 @@ def uri_decode(str)

end

configure do
EM::next_tick do
EM::add_periodic_timer(30) do
settings.streams.each do |user_name, connection|
connection << "\n"
end
end
end
end

get '/', provides: :html do
erb(:index)
end
Expand Down Expand Up @@ -187,6 +197,13 @@ def uri_decode(str)
end
end

get '/channels' do
protect!
StarChat::Channel.all.select do |channel|
channel.public?
end.to_json
end

before %r{^/channels/([^/]+)} do
protect!
channel_name = params[:captures][0]
Expand Down
42 changes: 38 additions & 4 deletions public/javascripts/main.js
Expand Up @@ -37,6 +37,7 @@ $(function() {
}
view.update();
});
// TODO: use starChat.Channel
var url = '/channels/' + encodeURIComponent(channel.name()) + '/messages/recent';
starChat.ajaxRequest(session, url, 'GET', null, function (sessionId, uri, method, data) {
var view = getView();
Expand Down Expand Up @@ -140,7 +141,25 @@ $(function() {
}
form.find('[name="name"]').val('');
var channel = starChat.Channel.find(channelName);
// The first message will be loaded when location.hash is changed
channel.loadUsers(view.session(), function () {
var view = getView();
if (view.session().id() !== sessionId) {
return;
}
view.update();
});
if (!channel.firstMessage()) {
channel.loadFirstMessage(session, function (sessionId) {
var view = getView();
if (view.session().id() !== sessionId) {
return;
}
if (!channel.firstMessage()) {
return;
}
view.update();
});
}
channel.loadRecentMessages(session, function (sessionId, data) {
var view = getView();
if (view.session().id() !== sessionId) {
Expand Down Expand Up @@ -386,6 +405,18 @@ $(function() {
});
return false;
});
$('#allChannelsLink a').click(function () {
var view = getView();
starChat.Channel.loadAll(view.session(), function (sessionId) {
var view = getView();
if (view.session().id() !== sessionId) {
return;
}
view.isShowingAllChannelsDialog(true);
view.update();
});
return false;
});
$('#invitationLink a').click(function () {
var view = getView();
var channelName = view.channelName;
Expand Down Expand Up @@ -440,6 +471,12 @@ $(function() {
view.update();
return false;
});
$('#allChannelsDialog [data-tool-id="closeDialog"]').click(function () {
var view = getView();
view.isShowingAllChannelsDialog(false);
view.update();
return false;
});
$('#invitationURLDialog [data-tool-id="closeDialog"]').click(function () {
var view = getView();
view.isShowingInvitationURLDialog(false);
Expand Down Expand Up @@ -540,9 +577,6 @@ $(function () {
$('#messages > h2').outerHeight() -
$('#messages > form').height());
$('.sidebar').height($(window).height() - $('header').outerHeight());
var messages = $('#messages');
$('#timeline').css('top', (messages.offset().top + 50) + 'px').
css('right', (messages.offset().left + 20) + 'px');
}
var isRequestedRelayouting = false;
$(window).resize(function () {
Expand Down
49 changes: 49 additions & 0 deletions public/javascripts/star_chat/channel.js
Expand Up @@ -13,9 +13,19 @@ starChat.Channel = function (obj) {
this.users_ = [];
this.messagesByTimeSpan_ = {};
this.firstMessage_ = null;
/**
* @type {number}
*/
this.userNum_ = 0;
this.update(obj);
};

/**
* @private
* @type {!Array.<!starChat.Channel>}
*/
starChat.Channel.all_ = [];

/**
* @param {!string} name
* @return {!starChat.Channel}
Expand All @@ -32,6 +42,35 @@ starChat.Channel.find = (function () {
};
})();

/**
* @this {function(new:starChat.Channel,!Object.<string,*>):undefined}
* @param {!starChat.Session} session
* @param {function(number)=} callback
* @return {undefined}
*/
starChat.Channel.loadAll = function (session, callback) {
var url = '/channels';
var self = this;
starChat.ajaxRequest(session, url, 'GET', null, function (sessionId, url, method, data) {
self.all_ = data.map(function (obj) {
var channel = starChat.Channel.find(obj.name);
channel.update(obj);
return channel;
});
if (callback !== void(0)) {
callback(sessionId);
}
});
};

/**
* @this {function(new:starChat.Channel,!Object.<string,*>):undefined}
* @return {!Array.<!starChat.Channel>}
*/
starChat.Channel.all = function () {
return this.all_;
};

/**
* @param {!Object.<string,*>} obj
* @return {undefined}
Expand All @@ -43,6 +82,9 @@ starChat.Channel.prototype.update = function (obj) {
if ('privacy' in obj) {
this.privacy_ = obj.privacy;
}
if ('user_num' in obj) {
this.userNum_ = obj.user_num;
}
};

/**
Expand Down Expand Up @@ -117,6 +159,13 @@ starChat.Channel.prototype.removeUser = function (name) {
}
};

/**
* @return {number}
*/
starChat.Channel.prototype.userNum = function () {
return this.userNum_;
};

/**
* @return {Object.<string,*>}
*/
Expand Down
2 changes: 1 addition & 1 deletion public/javascripts/star_chat/hashchange.js
Expand Up @@ -98,7 +98,7 @@
return;
}
data.forEach(function (message) {
view.addNewMessage(message);
view.addNewMessage(channel.name(), message);
});
view.update();
});
Expand Down
5 changes: 4 additions & 1 deletion public/javascripts/star_chat/session.js
Expand Up @@ -53,9 +53,12 @@ starChat.Session.prototype.user = function () {
}

/**
* @return {string}
* @return {?string}
*/
starChat.Session.prototype.userName = function () {
if (!this.user_) {
return null;
}
return this.user_.name();
};

Expand Down
11 changes: 9 additions & 2 deletions public/javascripts/star_chat/stream.js
Expand Up @@ -35,7 +35,7 @@ starChat.Stream.prototype.start = function (view, startMessageId) {
var self = this;
var session = view.session();
var streamReadIndex = 0;
var url = '/users/' + encodeURIComponent(session.userName()) + '/stream';
var url = '/users/' + encodeURIComponent(String(session.userName())) + '/stream';
if (startMessageId !== void(0) && 1 < startMessageId) {
url += '?start_message_id=' + encodeURIComponent(String(startMessageId));
}
Expand Down Expand Up @@ -79,12 +79,12 @@ starChat.Stream.prototype.start = function (view, startMessageId) {
},
xhrFields: {
onprogress: function () {
console.log('Reading stream...');
self.continuingErrorNum_ = 0;
// TODO: Reconnecting if overflow
var xhr = this;
var text = xhr.responseText;
var subText = text.substring(streamReadIndex);
var processedPacketsNum = 0;
while (true) {
var tokenLength = subText.search("\n");
if (tokenLength === -1) {
Expand All @@ -93,13 +93,20 @@ starChat.Stream.prototype.start = function (view, startMessageId) {
streamReadIndex += tokenLength + 1;
var token = subText.substring(0, tokenLength);
subText = subText.substring(tokenLength + 1);
if (token === '') {
continue;
}
try {
var packet = JSON.parse(token);
} catch (e) {
console.log(e);
continue;
}
self.packetProcessor_.process(packet, view);
processedPacketsNum++;
}
if (0 < processedPacketsNum) {
console.log('Packets processed...');
}
view.update();
}
Expand Down

0 comments on commit dcf9bbb

Please sign in to comment.