Skip to content

Commit

Permalink
SAK-28099; sort presence list, do incremental updates of presence
Browse files Browse the repository at this point in the history
git-svn-id: https://source.sakaiproject.org/svn/chat/trunk@315749 66ffb92e-73f9-0310-93c1-f5514f145a0a
  • Loading branch information
clhedrick committed Nov 28, 2014
1 parent 9ecb297 commit 7bb512d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,30 @@ public void roomDeleted(String roomId)
}
}

public class MyDelivery extends DirectRefreshDelivery {
public MyDelivery(String address, String elementId) {
public class MyAddDelivery extends DirectRefreshDelivery {
private String m_username;
public void setUsername(String username) {
m_username = username;
}

public MyAddDelivery(String address, String elementId, String username) {
super(address, elementId);
m_username = username;
}
public String compose() {
return "addUser('" + m_username + "')";
}
}

public class MyDelDelivery extends DirectRefreshDelivery {
private String m_username;

public MyDelDelivery(String address, String elementId, String username) {
super(address, elementId);
m_username = username;
}
public String compose() {
return "updateUsers()";
return "delUser('" + m_username + "')";
}
}

Expand All @@ -427,7 +445,8 @@ public String compose() {
*/
public void userJoined(String location, String user)
{
m_courierService.deliver(new MyDelivery(sessionId+location, IFRAME_ROOM_USERS));
MyAddDelivery delivery = new MyAddDelivery(sessionId+location, IFRAME_ROOM_USERS, user);
m_courierService.deliver(delivery);
}

/**
Expand All @@ -453,7 +472,7 @@ public void userLeft(String location, String user)
m_courierService.clear(sessionId+location);
}
else
m_courierService.deliver(new MyDelivery(sessionId+location, IFRAME_ROOM_USERS));
m_courierService.deliver(new MyDelDelivery(sessionId+location, IFRAME_ROOM_USERS, user));
}

//********************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.sakaiproject.event.api.Event;
import org.sakaiproject.event.api.EventTrackingService;
import org.sakaiproject.presence.cover.PresenceService;
import org.sakaiproject.user.cover.UserDirectoryService;
import org.sakaiproject.user.api.User;

/**
Expand Down Expand Up @@ -143,11 +144,21 @@ public void update(Observable o, Object arg)
if (!check(arg)) return;

Event event = (Event) arg;

String userId = event.getUserId();
String username = "";
User user = null;
if (userId != null)
try {
user = UserDirectoryService.getUser(userId);
} catch (Exception e) {}
if (user != null)
username = user.getDisplayName();

if(event.getEvent().equals(PresenceService.EVENT_PRESENCE))
presenceObserver.userJoined(location, "");
presenceObserver.userJoined(location, username);
else
presenceObserver.userLeft(location, "");
presenceObserver.userLeft(location, username);

}
}
Expand Down
60 changes: 59 additions & 1 deletion chat/chat-tool/tool/src/webapp/js/chatscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,71 @@ function updateShownText() {
$("#chat2_messages_shown_total").html(countText);
}

// can't just pass a list of <li>'s, because $(string) will only parse a single object
function sortChildren(list) {
var children = list.children('li');

// sort uses last name if present, and is case-insensitive.
children = children.sort(function(a,b){
var an = a.innerHTML.toLowerCase();
var i = an.indexOf(' ');
if (i >= 0)
an = an.substring(i);
var bn = b.innerHTML.toLowerCase();
var j = bn.indexOf(' ');
if (j >= 0)
bn = bn.substring(j);

if(an > bn) {
return 1;
}
if(an < bn) {
return -1;
}
// equal, take full name first
if (i <= 0 && j >= 0)
return 1;
if (j <= 0 && i >= 0)
return -1;
// equal, now do it on original so uppercase comes first
an = a.innerHTML;
bn = b.innerHTML;
if(an > bn) {
return 1;
}
if(an < bn) {
return -1;
}
return 0;
});
list.empty();
list.append(children);
return list;
}

function addUser(user) {
var existing = $("#presence").find("li:contains('" + user + "')");
if (existing.size() == 0) {
$("#presence").append($('<li>' + user + '</li>'));
var newChildren = sortChildren($("#presence")).children();
$("#presence").empty();
$("#presence").append(newChildren);
}
}

function delUser(user) {
$("#presence").find("li:contains('" + user + "')").remove();
}

function updateUsers() {
var url = "roomUsers?channel=" + currentChatChannelId;
$.ajax({
url: url,
type: "GET"})
.done(function(data) {
$("#presence").html(data);
var newChildren = sortChildren($('<ul>' + data + '</ul>')).children();
$("#presence").empty();
$("#presence").append(newChildren);
});
}

Expand Down

0 comments on commit 7bb512d

Please sign in to comment.