/
RoomPage.java
145 lines (115 loc) · 3.78 KB
/
RoomPage.java
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
package com.wideplay.crosstalk.web;
import com.google.appengine.api.channel.ChannelService;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.google.sitebricks.At;
import com.google.sitebricks.http.Get;
import com.wideplay.crosstalk.CrosstalkModule;
import com.wideplay.crosstalk.data.Message;
import com.wideplay.crosstalk.data.Occupancy;
import com.wideplay.crosstalk.data.Occupancy.TimeSegment;
import com.wideplay.crosstalk.data.Room;
import com.wideplay.crosstalk.data.User;
import com.wideplay.crosstalk.data.store.MessageStore;
import com.wideplay.crosstalk.data.store.RoomStore;
import com.wideplay.crosstalk.data.store.UserStore;
import com.wideplay.crosstalk.web.auth.twitter.TwitterMode;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* @author dhanji@gmail.com (Dhanji R. Prasanna)
*/
@At("/r/chat/:room")
public class RoomPage {
public static final double MAX_ACTIVITY_BUBBLES = 6.0;
@Inject
private ChannelService channelService;
@Inject
private CurrentUser currentUser;
@Inject
private RoomStore roomStore;
@Inject
private MessageStore messageStore;
@Inject
private UserStore userStore;
@Inject @TwitterMode
private boolean twitterMode;
private String token;
private Room room;
private List<Message> messages;
private int maxActivity;
// Not thread safe, so need to create it each turn.
private final DateFormat messageDateFormat = new SimpleDateFormat(CrosstalkModule.POST_DATE_FORMAT);
@Get
String displayRoom(@Named("room") String roomId, HttpServletResponse response) {
if (roomId == null || roomId.isEmpty()) {
return "/";
}
// Find the room with this id.
room = roomStore.named(roomId);
// No such room!
if (null == room) {
return "/";
}
response.setContentType("text/html; charset=utf-8");
// Create channel token specific to this user.
User user = getUser();
// Create a room & user-specific unique id for this channel.
String userChannelId = Long.toHexString(UUID.randomUUID().getLeastSignificantBits());
token = channelService.createChannel(userChannelId);
roomStore.connectClient(user, userChannelId, room);
// Set up presence for this room.
Occupancy occupancy = room.getOccupancy();
occupancy.add(user);
// Update occupancy.
roomStore.save(occupancy);
// Load the initial set of messages (currently loads everything).
messages = messageStore.list(room);
return null;
}
public String getCometToken() {
return token;
}
public User getUser() {
return currentUser.getUser();
}
public Room getRoom() {
return room;
}
public Collection<User> getOccupants() {
Collection<User> users = userStore.resolve(room.getOccupancy().getUsers()).values();
users.remove(User.anonymous());
return users;
}
// For current room.
public List<Message> getMessages() {
return messages;
}
public String format(Date date) {
return messageDateFormat.format(date);
}
public String activity(TimeSegment segment) {
return renderActivity(getRoom(), segment);
}
public static String renderActivity(Room room, TimeSegment segment) {
// Show a max of MAX_ACTIVITY_BUBBLES bubbles.
double ratio = segment.getCount() / ((double) room.getOccupancy().getMaxActivity());
int count = (int)(ratio * MAX_ACTIVITY_BUBBLES);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; i++) {
builder.append("<div class='bubble'></div>");
}
return builder.toString();
}
public boolean getTwitterMode() {
return twitterMode;
}
public boolean isTwitterMode() {
return twitterMode;
}
}