Skip to content

Commit

Permalink
Update Mod Log to support several channels
Browse files Browse the repository at this point in the history
- Listen in all channels you are mod in that you joined
- Add ability to unlisten from modlog topic (when channel is left)
- Cache messages in Mod Log dialog and display depending on active channel
- Implement new topic format
- Add mod actions to User object for display in User Info Dialog
- Include User object in onChannelJoined event
- Improve PubSub debug messages a bit
- Update help
  • Loading branch information
tduva committed Sep 13, 2016
1 parent 2dda7ce commit 7a616ae
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 42 deletions.
26 changes: 21 additions & 5 deletions src/chatty/TwitchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ else if (command.equals("bantest")) {
args.add("tirean");
args.add("300");
args.add("still not using LiveSplit Autosplitter D:");
g.printModerationAction(new ModeratorActionData("", "", "tduvatest", "timeout", args, "tduva"));
g.printModerationAction(new ModeratorActionData("", "", "tduvatest", "timeout", args, "tduva"), false);
} else if (command.equals("loadsoferrors")) {
for (int i=0;i<10000;i++) {
SwingUtilities.invokeLater(new Runnable() {
Expand Down Expand Up @@ -1623,8 +1623,13 @@ private class PubSubResults implements PubSubListener {
public void messageReceived(Message message) {
if (message.data != null && message.data instanceof ModeratorActionData) {
ModeratorActionData data = (ModeratorActionData)message.data;
g.printModerationAction(data);
chatLog.modAction(data);
if (data.stream != null) {
g.printModerationAction(data, data.created_by.equals(c.getUsername()));
chatLog.modAction(data);
User user = c.getUser(Helper.toChannel(data.stream), data.created_by);
user.addModAction(data.getCommandAndParameters());
g.updateUserinfo(user);
}
}
}

Expand Down Expand Up @@ -2144,8 +2149,16 @@ public void aboutToSaveSettings(Settings settings) {

private class Messages implements TwitchConnection.ConnectionListener {

private void checkModLogListen(User user) {
if (user.hasChannelModeratorRights() && user.nick.equals(c.getUsername())) {
pubsub.setLocalUsername(c.getUsername());
pubsub.listenModLog(Helper.toStream(user.getChannel()), settings.getString("token"));
}
}

@Override
public void onChannelJoined(String channel) {
public void onChannelJoined(User user) {
String channel = user.getChannel();
channelFavorites.addChannelToHistory(channel);

g.printLine(channel,"You have joined " + channel);
Expand All @@ -2154,13 +2167,15 @@ public void onChannelJoined(String channel) {
api.requestChatIcons(Helper.toStream(channel), false);
requestChannelEmotes(channel);
frankerFaceZ.joined(channel);
checkModLogListen(user);
}

@Override
public void onChannelLeft(String channel) {
chatLog.info(channel, "You have left "+channel);
closeChannel(channel);
frankerFaceZ.left(channel);
pubsub.unlistenModLog(Helper.toStream(channel));
}

@Override
Expand Down Expand Up @@ -2188,6 +2203,7 @@ public void onUserUpdated(User user) {
g.updateUser(user);
}
g.updateUserinfo(user);
checkModLogListen(user);
}

@Override
Expand Down Expand Up @@ -2287,7 +2303,7 @@ public void onBan(User user, long duration, String reason) {
@Override
public void onRegistered() {
g.updateHighlightSetUsername(c.getUsername());
pubsub.listenModLog(c.getUsername(), settings.getString("token"));
//pubsub.listenModLog(c.getUsername(), settings.getString("token"));
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/chatty/TwitchConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,10 @@ void onJoin(String channel, String nick, String prefix) {
*/
joinChecker.cancel(channel);
debug("JOINED: " + channel);
User user = userJoined(channel, nick);
if (this == irc && !onChannel(channel)) {
listener.onChannelJoined(channel);
listener.onChannelJoined(user);
}
userJoined(channel, nick);
joinedChannels.add(channel);
} else {
/**
Expand Down Expand Up @@ -1285,7 +1285,7 @@ public interface ConnectionListener {

void onJoinAttempt(String channel);

void onChannelJoined(String channel);
void onChannelJoined(User channel);

void onChannelLeft(String channel);

Expand Down
16 changes: 16 additions & 0 deletions src/chatty/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ public synchronized void addSub(String message, int months) {
addLine(new SubMessage(System.currentTimeMillis(), message, months));
}

public synchronized void addModAction(String commandAndParameters) {
addLine(new ModAction(System.currentTimeMillis(), commandAndParameters));
}

/**
* Adds a Message.
*
Expand Down Expand Up @@ -776,6 +780,7 @@ public static class Message {
public static final int MESSAGE = 0;
public static final int BAN = 1;
public static final int SUB = 2;
public static final int MOD_ACTION = 3;

private final Long time;
private final int type;
Expand Down Expand Up @@ -840,6 +845,17 @@ public SubMessage(Long time, String message, int months) {
}
}

public static class ModAction extends Message {

public final String commandAndParameters;

public ModAction(Long time, String commandAndParameters) {
super(MOD_ACTION, time);
this.commandAndParameters = commandAndParameters;
}

}

// public static final void main(String[] args) {
// ArrayList<User> list = new ArrayList<>();
// for (int i=0;i<100000;i++) {
Expand Down
6 changes: 4 additions & 2 deletions src/chatty/gui/MainGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,7 @@ public void stateChanged(ChangeEvent e) {
state.update(true);
updateChannelInfoDialog();
emotesDialog.updateStream(channels.getLastActiveChannel().getStreamName());
moderationLog.setChannel(channels.getLastActiveChannel().getStreamName());
}
}

Expand Down Expand Up @@ -2833,14 +2834,15 @@ public void run() {
});
}

public void printModerationAction(final ModeratorActionData data) {
public void printModerationAction(final ModeratorActionData data,
final boolean ownAction) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
moderationLog.add(data);
String channel = Helper.toValidChannel(data.stream);
if (client.settings.getBoolean("showModActions") && channels.isChannel(channel)) {
if (!ownAction && client.settings.getBoolean("showModActions") && channels.isChannel(channel)) {
channels.getChannel(channel).printLine(String.format("[ModAction] %s: /%s %s",
data.created_by,
data.moderation_action,
Expand Down
59 changes: 51 additions & 8 deletions src/chatty/gui/components/ModerationLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import chatty.util.StringUtil;
import chatty.util.api.pubsub.ModeratorActionData;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JDialog;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
Expand All @@ -26,6 +30,10 @@ public class ModerationLog extends JDialog {
private final JTextArea log;
private final JScrollPane scroll;

private final Map<String, List<String>> cache = new HashMap<>();

private String currentChannel;

public ModerationLog(MainGui owner) {
super(owner);
log = createLogArea();
Expand All @@ -50,33 +58,68 @@ private static JTextArea createLogArea() {
return text;
}

public void setChannel(String channel) {
if (channel != null && !channel.equals(currentChannel)) {
currentChannel = channel;
setTitle("Moderation Actions ("+channel+")");

List<String> cached = cache.get(channel);
if (cached != null) {
StringBuilder b = new StringBuilder();
for (String line : cached) {
b.append(line);
b.append("\n");
}
log.setText(b.toString());
scrollDown();
} else {
log.setText(null);
}
}
}

public void add(ModeratorActionData data) {
if (data.stream != null) {
setTitle("Moderator Actions ("+data.stream+")");
if (data.stream == null) {
return;
}
String line = String.format("%s: /%s %s",
String channel = data.stream;

String line = String.format("[%s] %s: /%s %s",
DateTime.currentTime(),
data.created_by,
data.moderation_action,
StringUtil.join(data.args," "));
printLine(log, line);

if (channel.equals(currentChannel)) {
printLine(log, line);
}

if (!cache.containsKey(channel)) {
cache.put(channel, new ArrayList<String>());
}
cache.get(channel).add(line);
}

private void printLine(JTextArea text, String line) {
try {
Document doc = text.getDocument();
String linebreak = doc.getLength() > 0 ? "\n" : "";
doc.insertString(doc.getLength(), linebreak+"["+DateTime.currentTime()+"] "+line, null);
String linebreak = "\n";
doc.insertString(doc.getLength(), line+linebreak, null);
JScrollBar bar = scroll.getVerticalScrollBar();
boolean scrollDown = bar.getValue() == bar.getMaximum() - bar.getVisibleAmount();
if (scrollDown) {
text.setCaretPosition(doc.getLength());
scrollDown();
}
clearSomeChat(doc);
} catch (BadLocationException e) {
e.printStackTrace();
}
}

private void scrollDown() {
log.setCaretPosition(log.getDocument().getLength());
}

/**
* Removes some lines from the given Document so it won't exceed the maximum
* number of lines.
Expand All @@ -96,7 +139,7 @@ public void clearSomeChat(Document doc) {
* @param doc
* @param amount
*/
public void removeFirstLines(Document doc, int amount) {
private void removeFirstLines(Document doc, int amount) {
if (amount < 1) {
amount = 1;
}
Expand Down
8 changes: 8 additions & 0 deletions src/chatty/gui/components/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import chatty.User;
import chatty.User.BanMessage;
import chatty.User.Message;
import chatty.User.ModAction;
import chatty.User.SubMessage;
import chatty.User.TextMessage;
import chatty.gui.GuiUtil;
Expand Down Expand Up @@ -567,6 +568,13 @@ else if (sm.months > 1) {
b.append(sm.message);
b.append("\n");
}
else if (m.getType() == Message.MOD_ACTION) {
ModAction ma = (ModAction)m;
b.append(DateTime.format(m.getTime(), TIMESTAMP_SPECIAL));
b.append("ModAction: /");
b.append(ma.commandAndParameters);
b.append("\n");
}
}
return b.toString();
}
Expand Down
2 changes: 1 addition & 1 deletion src/chatty/gui/components/help/help-releases.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h2>
- Added setting to save Addressbook to file on change
- Some changes to the build process
- Fixed bug with empty Whisper TAB appearing
- Added initial support for displaying mod actions (Broadcaster only)
- Added initial support for displaying mod actions (Broadcaster/Mods only)
- Fixed some dialogs not reopening even with the appropriate setting
</pre>

Expand Down
10 changes: 9 additions & 1 deletion src/chatty/gui/components/help/help-settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ <h3>Other</h3>
</dl>

</dd>

<dt>Show moderator actions in chat</dt>
<dd>Outputs extra messages for commands that moderators execute in the
channel, except for the commands you exexcute yourself.</dd>
<dd>This is only available for the broadcaster and moderators.</dd>
<dd>This is a Twitch Beta, so it may still change and break things.</dd>
<dd>To view mod actions you can also open a separate dialog via
<code>Extra - Moderation Log</code>.</dd>
</dl>

<h3><a name="capitalization">Name Capitalization</a></h3>
Expand Down Expand Up @@ -930,7 +938,7 @@ <h3>Messages Types</h3>
<dd><code>VIEWERS: 12,521</code></dd>

<dt>Mod Actions</dt>
<dd>Logs the commands performed by mods in your channel (Broadcaster only).</dd>
<dd>Logs the commands performed by mods in your channel (Broadcaster/Mods only).</dd>
<dd><code>MOD_ACTION: tduva (host coollertmb)</code></dd>
</dl>

Expand Down
4 changes: 2 additions & 2 deletions src/chatty/gui/components/settings/MessageSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public MessageSettings(final SettingsDialog d) {

otherSettingsPanel.add(d.addSimpleBooleanSetting(
"showModActions",
"Show moderator actions in chat (Broadcaster only)",
"Show what commands mods perform in your channel (you can also open Extra - Moderation Log)"),
"Show moderator actions in chat (Broadcaster/Mods only) [Beta]",
"Show what commands mods perform, except your own (you can also open Extra - Moderation Log)"),
d.makeGbc(0, 4, 3, 1, GridBagConstraints.WEST));


Expand Down
2 changes: 1 addition & 1 deletion src/chatty/util/api/pubsub/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static String createOutgoingMessage(String type, String nonce, Object dat

public static String getStreamFromTopic(String topic, Map<Long, String> userIds) {
try {
long userId = Long.valueOf(topic.substring(topic.indexOf(".")+1));
long userId = Long.valueOf(topic.substring(topic.lastIndexOf(".")+1));
return userIds.get(userId);
} catch (NumberFormatException ex) {
return null;
Expand Down
Loading

0 comments on commit 7a616ae

Please sign in to comment.