use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.
the class TelegramMessageReceivedAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventBus bus = getEventListenerContext().getEventBus();
EventListenerContext ctx = getEventListenerContext();
if (event instanceof TelegramMessageReceivedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
Update update = ((TelegramMessageReceivedEvent) event).getUpdate();
Message message = update.getMessage();
CallbackQuery callbackQuery = update.getCallbackQuery();
if (message != null && message.isCommand()) {
wonTelegramBotHandler.getCommandRegistry().executeCommand(wonTelegramBotHandler, message);
} else if (callbackQuery != null && update.hasCallbackQuery()) {
message = callbackQuery.getMessage();
String data = callbackQuery.getData();
WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getMessageId());
AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery();
answerCallbackQuery.setCallbackQueryId(callbackQuery.getId());
switch(correspondingURI.getType()) {
case NEED:
break;
case CONNECTION:
if ("0".equals(data)) {
// CLOSE CONNECTION
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
bus.publish(new CloseCommandEvent(con));
answerCallbackQuery.setText("Closed Connection");
} else if ("1".equals(data)) {
// ACCEPT CONNECTION
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
URI remoteNeed = WonRdfUtils.ConnectionUtils.getRemoteNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
URI localNeed = WonRdfUtils.ConnectionUtils.getLocalNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
bus.publish(new ConnectCommandEvent(localNeed, remoteNeed));
answerCallbackQuery.setText("Opened Connection");
}
break;
}
wonTelegramBotHandler.answerCallbackQuery(answerCallbackQuery);
EditMessageReplyMarkup editMessageReplyMarkup = new EditMessageReplyMarkup();
editMessageReplyMarkup.setMessageId(message.getMessageId());
editMessageReplyMarkup.setChatId(message.getChatId());
editMessageReplyMarkup.setReplyMarkup(null);
wonTelegramBotHandler.editMessageReplyMarkup(editMessageReplyMarkup);
} else if (message != null && message.isReply() && message.hasText()) {
WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getReplyToMessage().getMessageId());
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
Model messageModel = WonRdfUtils.MessageUtils.textMessage(message.getText());
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
}
}
}
use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.
the class Message2TelegramAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventListenerContext ctx = getEventListenerContext();
if (event instanceof MessageFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
Connection con = ((MessageFromOtherNeedEvent) event).getCon();
WonMessage wonMessage = ((MessageFromOtherNeedEvent) event).getWonMessage();
URI yourNeedUri = con.getNeedURI();
URI remoteNeedUri = con.getRemoteNeedURI();
Long chatId = botContextWrapper.getChatIdForURI(yourNeedUri);
if (chatId == null) {
logger.error("No chatId found for the specified needUri");
return;
}
try {
Message message = wonTelegramBotHandler.sendMessage(wonTelegramBotHandler.getTelegramMessageGenerator().getConnectionTextMessage(chatId, remoteNeedUri, yourNeedUri, wonMessage));
botContextWrapper.addMessageIdWonURIRelation(message.getMessageId(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
} catch (TelegramApiException te) {
logger.error(te.getMessage());
}
}
}
use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.
the class AbstractMessageCommandResultEvent method makeConnection.
protected static Connection makeConnection(URI needURI, URI remoteNeedURI, URI connectionURI) {
Connection con = new Connection();
con.setConnectionURI(connectionURI);
con.setNeedURI(needURI);
con.setRemoteNeedURI(remoteNeedURI);
return con;
}
use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.
the class DatasetBackedOwnerCallbackAdapter method makeConnection.
@Override
protected Connection makeConnection(final WonMessage wonMessage) {
URI connUri = wonMessage.getReceiverURI();
ParameterizedSparqlString pss = new ParameterizedSparqlString();
pss.setNsPrefix("won", WON.BASE_URI);
pss.setCommandText(QUERY_CONNECTION);
pss.setIri("con", connUri.toString());
Query query = pss.asQuery();
QueryExecution qExec = QueryExecutionFactory.create(query, dataset);
qExec.getContext().set(TDB.symUnionDefaultGraph, true);
try {
Connection con = null;
final ResultSet results = qExec.execSelect();
if (results.hasNext()) {
QuerySolution soln = results.next();
if (results.hasNext()) {
throw new DataIntegrityException("Query must not yield multiple solutions");
}
con = new Connection();
con.setConnectionURI(getURIFromSolution(soln, "con"));
con.setTypeURI(getURIFromSolution(soln, "type"));
con.setNeedURI(getURIFromSolution(soln, "need"));
con.setState(ConnectionState.fromURI(getURIFromSolution(soln, "state")));
con.setRemoteNeedURI(getURIFromSolution(soln, "remoteNeed"));
con.setRemoteConnectionURI(getURIFromSolution(soln, "remoteCon"));
}
return con;
} finally {
if (!qExec.isClosed()) {
qExec.close();
}
}
}
use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.
the class MessageExtractingOwnerCallbackAdapter method toConnection.
/**
* Creates a connection object representing the connection
* that the wonMessage is addressed at, if any.
* The resulting Connection object will not have a state or type property set.
* @param wonMessage or null if the message is not directed at
* a connection
*/
private Connection toConnection(WonMessage wonMessage) {
Connection con = new Connection();
con.setConnectionURI(wonMessage.getReceiverURI());
con.setRemoteConnectionURI(wonMessage.getSenderURI());
con.setNeedURI(wonMessage.getReceiverNeedURI());
con.setRemoteNeedURI(wonMessage.getSenderNeedURI());
return con;
}
Aggregations