Skip to content

Commit

Permalink
fixes igniterealtime#51: Add new offline setting: redirect to chatroom
Browse files Browse the repository at this point in the history
This introduces a new configurable 'offline setting': redirect to a chat room, adding this option to the database and the admin console.

The embedded converse client is modified to no longer hardcoded falling back to a chatroom, but to do this only when the offline settings instruct it to do so. This routine needs a lot more work (see igniterealtime#49).
  • Loading branch information
guusdk committed Jan 15, 2021
1 parent 5adf07c commit b1affc5
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 23 deletions.
1 change: 1 addition & 0 deletions changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ <h1>

<p><b>4.5.0</b> -- tbd</p>
<ul>
<li>[<a href="https://github.com/igniterealtime/openfire-fastpath-plugin/issues/51">#51</a>] - Add new offline setting: redirect to chat room.</li>
<li>[<a href="https://github.com/igniterealtime/openfire-fastpath-plugin/issues/48">#48</a>] - Converse end-user UI does not get notified of rejection.</li>
<li>[<a href="https://github.com/igniterealtime/openfire-fastpath-plugin/issues/47">#47</a>] - Fix ChatBot's rejection message.</li>
<li>[<a href="https://github.com/igniterealtime/openfire-fastpath-plugin/issues/46">#46</a>] - Allow workgroup's chatbot to be configured in the admin console.</li>
Expand Down
53 changes: 48 additions & 5 deletions classes/ofmeet.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var ofmeet = (function(of)
if (!conversation) conversation = "false";

const fastpath = workgroup + '@workgroup.' + domain;
const roomJid = workgroup + '@conference.' + domain;
const boshUri = 'https://' + server + '/http-bind/';
const wsUri = 'wss://' + server + '/ws/';

Expand Down Expand Up @@ -111,7 +110,7 @@ var ofmeet = (function(of)

}
else {
_converse.api.rooms.open(roomJid, {nick: getNick()});
performOfflineAction();
}
});
}
Expand Down Expand Up @@ -143,9 +142,7 @@ var ofmeet = (function(of)
_converse.connection.addHandler(function(message) {
if (message.querySelector('depart-queue')) {
console.info("joining workgroup failed - departed the queue")
document.getElementById("chatloader").style.visibility = "hidden";
document.getElementById("chatbutton").style.visibility = "visible";
// TODO: give some kind of feedback to the end-user, explaining why this has happened.
performOfflineAction();

// Got a 'depart-queue' message: remove this handler by returning 'false'.
return false; // TODO this handler should also be removed after successfully joining a room.
Expand Down Expand Up @@ -182,6 +179,52 @@ var ofmeet = (function(of)
});
}

function performOfflineAction(callback) {
// Obtain the offline settings options.
const iq = converse.env.$iq({type: 'get', to: fastpath}).c('offline-settings', {xmlns: 'http://jivesoftware.com/protocol/workgroup'});

_converse.connection.sendIQ(iq, function(resp)
{
console.debug("Received Offline Settings", resp);

if (resp.querySelector('redirectPage')) {
const redirectPage = resp.querySelector('redirectPage').innerHTML;
console.debug("Offline Settings suggest to redirect to webpage:", redirectPage);
// TODO show dialog, suggest to redirect to webpage.
document.getElementById("chatloader").style.visibility = "hidden";
document.getElementById("chatbutton").style.visibility = "visible";
} else if (resp.querySelector('emailAddress')) {
console.debug("Offline Settings suggest to ask the user to leave a message.");
const emailAddress = resp.querySelector('emailAddress').innerHTML;
const offlineText = resp.querySelector('offlineText').innerHTML;
const subject = resp.querySelector('subject').innerHTML;
// TODO show form, invite to leave a message, send data back to server (using iq 'send-email' 'http://jivesoftware.com/protocol/workgroup')
document.getElementById("chatloader").style.visibility = "hidden";
document.getElementById("chatbutton").style.visibility = "visible";
} else if (resp.querySelector('redirectMUC')) {
const redirectMUC = resp.querySelector('redirectMUC').innerHTML;
console.debug("Offline Settings suggest to redirect to MUC:", redirectMUC);
document.getElementById("chatloader").style.visibility = "hidden";
// TODO show dialog, suggest to redirect to MUC, open after confirmation.
_converse.api.rooms.open(redirectMUC, {nick: getNick()});
} else {
// TODO show apology

// Remove the spinner, restore the 'chat' button.
document.getElementById("chatloader").style.visibility = "hidden";
document.getElementById("chatbutton").style.visibility = "visible";
}

}, function(err) {
// Service will return a 404 if there are no offline settings for a workgroup (which is the default).
console.info("Offline settings not available.");

// Remove the spinner, restore the 'chat' button.
document.getElementById("chatloader").style.visibility = "hidden";
document.getElementById("chatbutton").style.visibility = "visible";
});
}

function getNick()
{
if (!window.localStorage["webmeet.nick"]) return undefined;
Expand Down
1 change: 1 addition & 0 deletions src/database/fastpath_db2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ CREATE TABLE fpOfflineSetting (
emailAddress varChar(255),
subject varChar(255),
offlineText LONG VARCHAR,
redirectMUC varChar(255),
CONSTRAINT fpOfflineSet_pk PRIMARY KEY(workgroupID)
);

Expand Down
1 change: 1 addition & 0 deletions src/database/fastpath_hsqldb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ CREATE TABLE fpOfflineSetting (
emailAddress varChar(255),
subject varChar(255),
offlineText LONGVARCHAR,
redirectMUC varChar(255),
PRIMARY KEY(workgroupID)
);

Expand Down
1 change: 1 addition & 0 deletions src/database/fastpath_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ CREATE TABLE fpOfflineSetting (
emailAddress varChar(255),
subject varChar(255),
offlineText TEXT,
redirectMUC varChar(255),
PRIMARY KEY(workgroupID)
);

Expand Down
1 change: 1 addition & 0 deletions src/database/fastpath_oracle.sql
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ CREATE TABLE fpOfflineSetting (
emailAddress VARCHAR2(255),
subject VARCHAR2(255),
offlineText LONG,
redirectMUC VARCHAR2(255),
PRIMARY KEY(workgroupID)
);

Expand Down
1 change: 1 addition & 0 deletions src/database/fastpath_postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ CREATE TABLE fpOfflineSetting (
emailAddress varChar(255),
subject varChar(255),
offlineText TEXT,
redirectMUC varChar(255),
PRIMARY KEY(workgroupID)
);

Expand Down
1 change: 1 addition & 0 deletions src/database/fastpath_sqlserver.sql
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ CREATE TABLE fpOfflineSetting (
emailAddress NVARCHAR(255),
subject NVARCHAR(255),
offlineText TEXT,
redirectMUC NVARCHAR(255),
CONSTRAINT fpOfflineSetting_pk PRIMARY KEY(workgroupID)
);

Expand Down
1 change: 1 addition & 0 deletions src/database/upgrade/1/fastpath_db2.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ALTER TABLE fpQueue ADD COLUMN dispatcherClass LONG VARCHAR NULL;
ALTER TABLE fpOfflineSetting ADD COLUMN redirectMUC VARCHAR(255) NULL;

-- Update database version
UPDATE ofVersion SET version = 1 WHERE name = 'fastpath';
1 change: 1 addition & 0 deletions src/database/upgrade/1/fastpath_hsqldb.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ALTER TABLE fpQueue ADD COLUMN dispatcherClass VARCHAR(4000) NULL;
ALTER TABLE fpOfflineSetting ADD COLUMN redirectMUC VARCHAR(255) NULL;

-- Update database version
UPDATE ofVersion SET version = 1 WHERE name = 'fastpath';
1 change: 1 addition & 0 deletions src/database/upgrade/1/fastpath_mysql.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ALTER TABLE fpQueue ADD COLUMN dispatcherClass TEXT NULL;
ALTER TABLE fpOfflineSetting ADD COLUMN redirectMUC VARCHAR(255) NULL;

-- Update database version
UPDATE ofVersion SET version = 1 WHERE name = 'fastpath';
1 change: 1 addition & 0 deletions src/database/upgrade/1/fastpath_oracle.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ALTER TABLE fpQueue ADD dispatcherClass VARCHAR(3900) NULL;
ALTER TABLE fpOfflineSetting ADD redirectMUC VARCHAR2(255) NULL;

-- Update database version
UPDATE ofVersion SET version = 1 WHERE name = 'fastpath';
Expand Down
1 change: 1 addition & 0 deletions src/database/upgrade/1/fastpath_postgresql.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ALTER TABLE fpQueue ADD COLUMN dispatcherClass TEXT NULL;
ALTER TABLE fpOfflineSetting ADD COLUMN redirectMUC VARCHAR(255) NULL;

-- Update database version
UPDATE ofVersion SET version = 1 WHERE name = 'fastpath';
1 change: 1 addition & 0 deletions src/database/upgrade/1/fastpath_sqlserver.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ALTER TABLE fpQueue ADD dispatcherClass NVARCHAR(3900) NULL;
ALTER TABLE fpOfflineSetting ADD redirectMUC NVARCHAR(255) NULL;

-- Update database version
UPDATE ofVersion SET version = 1 WHERE name = 'fastpath';
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.jivesoftware.openfire.fastpath.settings.offline;

import org.jivesoftware.xmpp.workgroup.utils.ModelUtil;
import org.xmpp.packet.JID;

public class OfflineSettings {
private String redirectURL;
Expand All @@ -25,6 +26,8 @@ public class OfflineSettings {
private String emailAddress;
private String subject;

private JID redirectMUC;

public String getRedirectURL() {
if(!ModelUtil.hasLength(redirectURL)){
return "";
Expand Down Expand Up @@ -69,7 +72,19 @@ public void setSubject(String subject) {
this.subject = subject;
}

public boolean redirects(){
public JID getRedirectMUC() {
return redirectMUC;
}

public void setRedirectMUC(JID redirectMUC) {
this.redirectMUC = redirectMUC;
}

public boolean redirectsWeb(){
return (ModelUtil.hasLength(getRedirectURL()));
}

public boolean redirectsMUC(){
return redirectMUC != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jivesoftware.xmpp.workgroup.Workgroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;

/**
* Retrieves and persists offline settings for a workgroup.
Expand All @@ -35,22 +36,22 @@ public class OfflineSettingsManager {
private static final Logger Log = LoggerFactory.getLogger(OfflineSettingsManager.class);

private static final String GET_OFFLINE_SETTTINGS =
"SELECT redirectPage, emailAddress, subject, offlineText FROM " +
"SELECT redirectPage, emailAddress, subject, offlineText, redirectMUC FROM " +
"fpOfflineSetting WHERE workgroupID=?";
private static final String INSERT_OFFLINE_SETTINGS =
"INSERT INTO fpOfflineSetting(workgroupID, redirectPage, emailAddress, subject, " +
"offlineText) VALUES(?,?,?,?,?)";
"offlineText, redirectMUC) VALUES(?,?,?,?,?,?)";
private static final String UPDATE_OFFLINE_SETTINGS =
"UPDATE fpOfflineSetting SET redirectPage=?, emailAddress=?, subject=?, offlineText=? " +
"UPDATE fpOfflineSetting SET redirectPage=?, emailAddress=?, subject=?, offlineText=?, redirectMUC=?" +
"WHERE workgroupID=?";

public OfflineSettings saveOfflineSettings(Workgroup workgroup, String webPage, String email,
String subject, String offlineText)
String subject, String offlineText, JID redirectMUC)
{
OfflineSettings offline;
try {
offline = getOfflineSettings(workgroup);
return updateOfflineSettings(workgroup, webPage, email, subject, offlineText);
return updateOfflineSettings(workgroup, webPage, email, subject, offlineText, redirectMUC);
}
catch (OfflineSettingsNotFound osnf) {
offline = new OfflineSettings();
Expand All @@ -60,6 +61,7 @@ public OfflineSettings saveOfflineSettings(Workgroup workgroup, String webPage,
offline.setEmailAddress(email);
offline.setOfflineText(offlineText);
offline.setSubject(subject);
offline.setRedirectMUC(redirectMUC);

String redirectURL = webPage != null ? webPage : "";
String emailAddress = email != null ? email : "";
Expand All @@ -78,6 +80,7 @@ public OfflineSettings saveOfflineSettings(Workgroup workgroup, String webPage,
pstmt.setString(4, subject);

DbConnectionManager.setLargeTextField(pstmt, 5, offlineText);
pstmt.setString(6, redirectMUC == null ? null : redirectMUC.toString());
pstmt.executeUpdate();
}
catch (Exception ex) {
Expand All @@ -91,14 +94,14 @@ public OfflineSettings saveOfflineSettings(Workgroup workgroup, String webPage,
}

public OfflineSettings updateOfflineSettings(Workgroup workgroup, String webPage,
String email, String subject, String offlineText)
String email, String subject, String offlineText, JID redirectMUC)
{
final OfflineSettings offline = new OfflineSettings();
offline.setRedirectURL(webPage);
offline.setEmailAddress(email);
offline.setOfflineText(offlineText);
offline.setSubject(subject);

offline.setRedirectMUC(redirectMUC);

String redirectURL = webPage != null ? webPage : "";
String emailAddress = email != null ? email : "";
Expand All @@ -115,7 +118,8 @@ public OfflineSettings updateOfflineSettings(Workgroup workgroup, String webPage
pstmt.setString(3, subject);

DbConnectionManager.setLargeTextField(pstmt, 4, offlineText);
pstmt.setLong(5, workgroup.getID());
pstmt.setString(5, redirectMUC == null ? null : redirectMUC.toString());
pstmt.setLong(6, workgroup.getID());
pstmt.executeUpdate();
}
catch (Exception ex) {
Expand All @@ -138,7 +142,7 @@ public OfflineSettings updateOfflineSettings(Workgroup workgroup, String webPage
*/
public OfflineSettings getOfflineSettings(Workgroup workgroup) throws OfflineSettingsNotFound {
OfflineSettings offlineSettings = null;

Log.trace("Get offline settings for workgroup {}", workgroup.getDisplayName());
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Expand All @@ -152,12 +156,20 @@ public OfflineSettings getOfflineSettings(Workgroup workgroup) throws OfflineSet
String emailAddress = rs.getString(2);
String subject = rs.getString(3);
String offlineText = DbConnectionManager.getLargeTextField(rs, 4);
String muc = rs.getString(5);
JID redirectMUC;
if (muc == null || muc.isEmpty()) {
redirectMUC = null;
} else {
redirectMUC = new JID(muc);
}

offlineSettings = new OfflineSettings();
offlineSettings.setRedirectURL(redirectPage);
offlineSettings.setEmailAddress(emailAddress);
offlineSettings.setSubject(subject);
offlineSettings.setOfflineText(offlineText);
offlineSettings.setRedirectMUC(redirectMUC);
}
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public void executeGet(IQ packet, Workgroup workgroup) {
if (ModelUtil.hasLength(settings.getRedirectURL())) {
offline.addElement("redirectPage").setText(settings.getRedirectURL());
}
else if (settings.getRedirectMUC() != null) {
offline.addElement("redirectMUC").setText(settings.getRedirectMUC().toString());
}
else {
offline.addElement("emailAddress").setText(settings.getEmailAddress());
offline.addElement("offlineText").setText(settings.getOfflineText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void setWorkgroup(Workgroup workgroup) {

public void process(IQ packet) {
try {
Log.trace("Processing IQ: {}", packet.toXML());
IQ.Type type = packet.getType();
if (type == IQ.Type.set) {
handleIQSet(packet);
Expand Down Expand Up @@ -508,7 +509,7 @@ else if ("vcard-temp".equals(namespace)) {
getVCard(packet);
}
else {

Log.trace("providers");
// Check all Workgroup Providers for handling this GET request. If
// none are found, send bad request error.
for (WorkgroupProvider provider : providerManager.getWorkgroupProviders()) {
Expand Down
Loading

0 comments on commit b1affc5

Please sign in to comment.