Commit d9f4a94e authored by PGoski's avatar PGoski

get count of users unread messages

parent 1f68e1bc
......@@ -44,6 +44,11 @@
REST API Plugin Changelog
</h1>
<p><b>1.1.3</b> -- August 15th, 2015</p>
<ul>
<li>Added: get count of users unread messages</li>
</ul>
<p><b>1.1.2</b> -- August 4th, 2015</p>
<ul>
<li>Added: CORS to all endpoints</li>
......
......@@ -5,8 +5,8 @@
<name>REST API</name>
<description>Allows administration over a RESTful API.</description>
<author>Roman Soldatow</author>
<version>1.1.2</version>
<date>08/04/2015</date>
<version>1.1.3</version>
<date>08/15/2015</date>
<minServerVersion>3.9.0</minServerVersion>
<adminconsole>
......
package org.jivesoftware.openfire.plugin.rest.controller;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* The Class MsgArchiveController.
*/
public class MsgArchiveController {
private static final Logger Log = LoggerFactory.getLogger(MsgArchiveController.class);
/** The Constant INSTANCE. */
public static final MsgArchiveController INSTANCE = new MsgArchiveController();
/** The server. */
private XMPPServer server;
private static final String USER_MESSAGE_COUNT = "select COUNT(1) from ofMessageArchive a " +
"join ofPresence p on (a.sentDate > p.offlineDate) " +
"WHERE a.toJID = ? AND p.username = ?";
/**
* Gets the single instance of MsgArchiveController.
*
* @return single instance of MsgArchiveController
*/
public static MsgArchiveController getInstance() {
return INSTANCE;
}
/**
* Instantiates a new user service controller.
*/
private MsgArchiveController() {
server = XMPPServer.getInstance();
}
/**
* Returns the total number of messages that haven't been delivered to the user.
*
* @return the total number of user unread messages.
*/
public int getUnReadMessagesCount(JID jid) {
int messageCount = 0;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(USER_MESSAGE_COUNT);
pstmt.setString(1, jid.toBareJID());
pstmt.setString(2, jid.getNode());
rs = pstmt.executeQuery();
if (rs.next()) {
messageCount = rs.getInt(1);
}
} catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle);
} finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
}
return messageCount;
}
}
package org.jivesoftware.openfire.plugin.rest.entity;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* The Class MsgArchiveEntity.
*/
@XmlRootElement(name = "archive")
public class MsgArchiveEntity {
@XmlElement
String jid;
/**
* unread messages count
*/
@XmlElement
int count;
public MsgArchiveEntity() {
}
public MsgArchiveEntity(String jid, int count) {
this.jid = jid;
this.count = count;
}
}
......@@ -82,6 +82,8 @@ public class JerseyWrapper extends ServletContainer {
prc.getClasses().add(SessionService.class);
prc.getClasses().add(RESTExceptionMapper.class);
prc.getClasses().add(MsgArchiveService.class);
}
/**
......
package org.jivesoftware.openfire.plugin.rest.service;
import org.jivesoftware.openfire.plugin.rest.controller.MsgArchiveController;
import org.jivesoftware.openfire.plugin.rest.entity.MsgArchiveEntity;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
import org.xmpp.packet.JID;
import javax.annotation.PostConstruct;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("restapi/v1/archive/messages/unread/{jid}")
public class MsgArchiveService {
private MsgArchiveController archive;
@PostConstruct
public void init() {
archive = MsgArchiveController.getInstance();
}
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public MsgArchiveEntity getUnReadMessagesCount(@PathParam("jid") String jidStr) throws ServiceException {
JID jid = new JID(jidStr);
int msgCount = archive.getUnReadMessagesCount(jid);
return new MsgArchiveEntity(jidStr, msgCount);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment