Skip to content

Commit

Permalink
support unfurl in converse
Browse files Browse the repository at this point in the history
  • Loading branch information
deleolajide committed Dec 22, 2021
1 parent f26e89b commit 9c4eeaa
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@
<version>1.8.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.linkedin.urls/url-detector -->
<dependency>
<groupId>com.linkedin.urls</groupId>
<artifactId>url-detector</artifactId>
<version>0.1.17</version>
</dependency>

</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.xmpp.packet.Packet;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -45,6 +46,10 @@
import nl.martijndwars.webpush.*;
import org.jivesoftware.util.*;
import com.j256.twofactorauth.TimeBasedOneTimePasswordUtil;
import com.linkedin.urls.detection.*;
import com.linkedin.urls.*;
import org.broadbear.link.preview.*;


public class PushInterceptor implements PacketInterceptor, OfflineMessageListener
{
Expand Down Expand Up @@ -100,6 +105,56 @@ public void interceptPacket( final Packet packet, final Session session, final b
return;
}

Element originId = ((Message) packet).getChildElement("origin-id", "urn:xmpp:sid:0");

if (originId != null && originId.attribute("id") != null) {
String id = originId.attribute("id").getStringValue();

if (id != null) {
UrlDetector parser = new UrlDetector(body, UrlDetectorOptions.Default);
List<Url> found = parser.detect();

for(Url url : found) {
Log.info("found URL " + url + " " + id);

SourceContent sourceContent = TextCrawler.scrape(url.toString(), 3);

if (sourceContent != null) {
String image = null;
try {
image = sourceContent.getImages().get(0);
} catch (Exception e) {}
String descriptionShort = sourceContent.getDescription();
String title = sourceContent.getTitle();

Log.debug("found unfurl " + image + " " + descriptionShort + " " + title);

if (image != null || !"".equals(descriptionShort) || !"".equals(title)) {
String msgId = "unfurl-" + System.currentTimeMillis() ;
Message message = new Message();
message.setFrom(packet.getFrom().toBareJID());
message.setID(msgId);
message.setTo(packet.getTo());
message.setType(Message.Type.groupchat);
message.addChildElement("x", "http://jabber.org/protocol/muc#user");

Element applyTo = message.addChildElement("apply-to", "urn:xmpp:fasten:0").addAttribute("id", id);
applyTo.addElement("meta", "http://www.w3.org/1999/xhtml").addAttribute("property", "og:url").addAttribute("content", url.toString());
applyTo.addElement("meta", "http://www.w3.org/1999/xhtml").addAttribute("property", "og:title").addAttribute("content", title);
applyTo.addElement("meta", "http://www.w3.org/1999/xhtml").addAttribute("property", "og:description").addAttribute("content", descriptionShort);

if (image != null) {
applyTo.addElement("meta", "http://www.w3.org/1999/xhtml").addAttribute("property", "og:image").addAttribute("content", image);
}

message.addChildElement("origin-id", "urn:xmpp:sid:0").addAttribute("id", msgId);
XMPPServer.getInstance().getRoutingTable().routePacket(packet.getTo(), message, true);
}
}
}
}
}

final User user;
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ private boolean featureExists(Element element, String feature)
}


public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed)
throws PacketRejectedException {
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
if (!processed && lobbyService != null) {
if (packet instanceof IQ) {
interceptIQ((IQ) packet, session, incoming, processed);
Expand Down Expand Up @@ -278,8 +277,7 @@ protected void interceptPresence(Presence presence, Session session, boolean inc
}
}

protected void interceptMessage(Message message, Session session, boolean incoming, boolean processed)
throws PacketRejectedException {
protected void interceptMessage(Message message, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
Element childElement = message.getChildElement("x", "http://jabber.org/protocol/muc#user");

if (childElement != null && MAIN_MUC.equals(message.getTo().getDomain())) {
Expand Down

0 comments on commit 9c4eeaa

Please sign in to comment.