Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save orekyuu/19e57c833a6ee6aa3628 to your computer and use it in GitHub Desktop.
Save orekyuu/19e57c833a6ee6aa3628 to your computer and use it in GitHub Desktop.
update_name
package net.orekyuu.updatename;
import net.orekyuu.javatter.api.twitter.TwitterUser;
import net.orekyuu.javatter.api.twitter.model.Tweet;
public class MessageUtil {
public static final String NEW_NAME = "${NEW_NAME}";
public static final String OLD_NAME = "${OLD_NAME}";
public static final String SENDER_SN = "${SENDER_SN}";
public static final String RECEIVER_SN = "${RECEIVER_SN}";
public static String getSuccessMessage(UpdateNameProfile profile, String newName, String oldName, Tweet tweet, TwitterUser user) {
return replace(profile.getSuccessMessage(), newName, oldName, tweet, user);
}
public static String getErrorMessage(UpdateNameProfile profile, String reqName, String oldName, Tweet tweet, TwitterUser user) {
return replace(profile.getErrorMessage(), reqName, oldName, tweet, user);
}
private static String replace(String text, String newName, String oldName, Tweet tweet, TwitterUser user) {
return text.replace(NEW_NAME, newName)
.replace(OLD_NAME, oldName)
.replace(SENDER_SN, tweet.getOwner().getScreenName())
.replace(RECEIVER_SN, user.getUser().getScreenName());
}
}
package net.orekyuu.updatename;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.text.Text;
import net.orekyuu.javatter.api.storage.DataStorageService;
import javax.inject.Inject;
import java.net.URL;
import java.util.ResourceBundle;
public class SettingsController implements Initializable {
public CheckBox enable;
public CheckBox enableOthers;
public Text desc;
public TextArea successTemp;
public TextArea errorTemp;
@Inject
private DataStorageService storageService;
@Override
public void initialize(URL location, ResourceBundle resources) {
UpdateNameProfile profile = storageService
.find(UpdateName.ID, UpdateNameProfile.class, new UpdateNameProfile());
enable.setSelected(profile.isEnable());
enableOthers.setSelected(profile.isEnableOthers());
successTemp.setText(profile.getSuccessMessage());
errorTemp.setText(profile.getErrorMessage());
enable.selectedProperty().addListener((observable, oldValue, newValue) -> {
profile.setEnable(newValue);
storageService.save(UpdateName.ID, profile);
});
enableOthers.selectedProperty().addListener((observable, oldValue, newValue) -> {
profile.setEnableOthers(newValue);
storageService.save(UpdateName.ID, profile);
});
successTemp.textProperty().addListener((observable, oldValue, newValue) -> {
profile.setSuccessMessage(newValue);
storageService.save(UpdateName.ID, profile);
});
errorTemp.textProperty().addListener((observable, oldValue, newValue) -> {
profile.setErrorMessage(newValue);
storageService.save(UpdateName.ID, profile);
});
StringBuilder desc = new StringBuilder();
desc.append("メッセージでは以下の変数が使用できます\n");
desc.append(MessageUtil.NEW_NAME).append(": 新しい名前 失敗時はリクエストされた名前\n");
desc.append(MessageUtil.OLD_NAME).append(": 変更前の名前\n");
desc.append(MessageUtil.SENDER_SN).append(": update_nameしたユーザーのスクリーンネーム\n");
desc.append(MessageUtil.RECEIVER_SN).append(": update_nameされたユーザーのスクリーンネーム");
this.desc.setText(desc.toString());
}
}
package net.orekyuu.updatename;
import com.gs.collections.api.list.ImmutableList;
import com.gs.collections.impl.factory.Lists;
import net.orekyuu.javatter.api.command.Command;
import net.orekyuu.javatter.api.command.CommandManager;
import net.orekyuu.javatter.api.plugin.OnPostInit;
import net.orekyuu.javatter.api.service.TwitterUserService;
import net.orekyuu.javatter.api.storage.DataStorageService;
import net.orekyuu.javatter.api.twitter.TweetBuilder;
import net.orekyuu.javatter.api.twitter.TwitterUser;
import net.orekyuu.javatter.api.twitter.model.Tweet;
import net.orekyuu.javatter.api.twitter.model.User;
import net.orekyuu.javatter.api.twitter.userstream.events.OnStatus;
import net.orekyuu.javatter.api.util.lookup.Lookup;
import javax.inject.Inject;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UpdateName {
@Inject
private TwitterUserService twitterUserService;
@Inject
private DataStorageService storageService;
public static final String ID = "net.orekyuu.updatename";
private static List<OnStatus> listeners = Lists.mutable.empty();
private ExecutorService executorService = Executors.newSingleThreadExecutor(r -> {
Thread thread = new Thread(r, "UpdateNameThread");
thread.setDaemon(true);
return thread;
});
@OnPostInit
public void initialize() {
ImmutableList<TwitterUser> allUser = twitterUserService.allUser();
for (TwitterUser twitterUser : allUser) {
OnStatus onStatus = tweet -> updateName(twitterUser, tweet);
listeners.add(onStatus);
twitterUser.userStream().onStatus(onStatus);
}
}
private void updateName(TwitterUser user, Tweet tweet) {
String text = tweet.getText();
String oldName = user.getUser().getName();
String name = findName(user, text);
//名前が空なら何もしない
if (name.isEmpty()) {
return;
}
UpdateNameProfile setting = storageService
.find(UpdateName.ID, UpdateNameProfile.class, new UpdateNameProfile());
//有効になっていなければ無視
if (!setting.isEnable()) {
return;
}
if (20 < name.length()) {
//名前長過ぎ
String format = MessageUtil.getErrorMessage(setting, name, oldName, tweet, user);
user.createTweet().setAsync()
.setText(format)
.replyTo(tweet)
.tweet();
return;
}
executorService.submit(() -> {
User profile = user.getUser();
user.updateProfile(name, profile.getWebSite(), profile.getLocation(), profile.getDescription());
//成功報告
String message = MessageUtil.getSuccessMessage(setting, name, oldName, tweet, user);
user.createTweet().setAsync()
.setText(message)
.replyTo(tweet)
.tweet();
});
}
private String findName(TwitterUser user, String text) {
Pattern pattern = Pattern.compile("^\\s*@" + user.getUser().getScreenName() + "\\s+update_name\\s+(\\S+)\\s*$");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
return matcher.group(1);
}
Pattern pattern2 = Pattern.compile("^\\s*(\\S+)\\s*\\(\\s*@" + user.getUser().getScreenName() + "\\s*\\)");
Matcher matcher2 = pattern2.matcher(text);
if (matcher2.find()) {
return matcher2.group(1);
}
return "";
}
}
package net.orekyuu.updatename;
public class UpdateNameProfile {
private boolean enable = true;
private boolean enableOthers = false;
private String successMessage = String.format(".%sになりました。", MessageUtil.NEW_NAME);
private String errorMessage = String.format("@%s %sにupdate_nameできませんでした。", MessageUtil.SENDER_SN, MessageUtil.NEW_NAME);
public boolean isEnableOthers() {
return enableOthers;
}
public void setEnableOthers(boolean enableOthers) {
this.enableOthers = enableOthers;
}
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
public String getSuccessMessage() {
return successMessage;
}
public void setSuccessMessage(String successMessage) {
this.successMessage = successMessage;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="Infinity" maxWidth="Infinity" minHeight="-Infinity" minWidth="-Infinity"
fx:controller="net.orekyuu.updatename.SettingsController"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<CheckBox fx:id="enable" mnemonicParsing="false" text="update_nameを有効化" />
<CheckBox fx:id="enableOthers" mnemonicParsing="false" text="他人からの変更を受け付ける" />
<Text fx:id="desc" strokeType="OUTSIDE" strokeWidth="0.0" text="メッセージでは以下の変数を使用できます${NEW_NAME}:変更後の名前\n${OLD_NAME}\n${SENDER}\n${RECEIVER}" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="成功時のメッセージ" />
<TextArea fx:id="successTemp"/>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="失敗時のメッセージ" />
<TextArea fx:id="errorTemp"/>
</children>
</VBox>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment