publish/unpublish functionality

This commit is contained in:
Zlatin Balevsky
2020-03-13 08:56:45 +00:00
parent fca8870283
commit f4c96db841
8 changed files with 147 additions and 11 deletions

View File

@@ -136,6 +136,7 @@ public class CertificateServlet extends HttpServlet {
}
File file = Util.getFromPathElements(path);
certificateManager.certify(file);
Util.pause();
}
}

View File

@@ -6,6 +6,7 @@ import java.util.concurrent.ConcurrentHashMap;
import com.muwire.core.Core;
import com.muwire.core.Persona;
import com.muwire.core.SharedFile;
import com.muwire.core.filefeeds.Feed;
import com.muwire.core.filefeeds.FeedFetchEvent;
import com.muwire.core.filefeeds.FeedItem;
@@ -15,14 +16,18 @@ import com.muwire.core.filefeeds.UIDownloadFeedItemEvent;
import com.muwire.core.filefeeds.UIFeedConfigurationEvent;
import com.muwire.core.filefeeds.UIFeedDeletedEvent;
import com.muwire.core.filefeeds.UIFeedUpdateEvent;
import com.muwire.core.filefeeds.UIFilePublishedEvent;
import com.muwire.core.filefeeds.UIFileUnpublishedEvent;
public class FeedManager {
private final Core core;
private final FileManager fileManager;
private final Map<Persona, RemoteFeed> remoteFeeds = new ConcurrentHashMap<>();
public FeedManager(Core core) {
public FeedManager(Core core, FileManager fileManager) {
this.core = core;
this.fileManager = fileManager;
}
public Map<Persona, RemoteFeed> getRemoteFeeds() {
@@ -105,6 +110,39 @@ public class FeedManager {
core.getEventBus().publish(event);
}
void publish(File file) {
if (file.isFile()) {
SharedFile sf = core.getFileManager().getFileToSharedFile().get(file);
if (sf == null)
return;
sf.publish(System.currentTimeMillis());
UIFilePublishedEvent event = new UIFilePublishedEvent();
event.setSf(sf);
core.getEventBus().publish(event);
} else {
long now = System.currentTimeMillis();
for (SharedFile sf : fileManager.getAllFiles(file)) {
if (!sf.isPublished())
sf.publish(now);
UIFilePublishedEvent event = new UIFilePublishedEvent();
event.setSf(sf);
core.getEventBus().publish(event);
}
}
}
void unpublish(File file) {
if (!file.isFile())
throw new UnsupportedOperationException();
SharedFile sf = core.getFileManager().getFileToSharedFile().get(file);
if (sf == null)
return;
sf.unpublish();
UIFileUnpublishedEvent event = new UIFileUnpublishedEvent();
event.setSf(sf);
core.getEventBus().publish(event);
}
static class RemoteFeed {
private final Feed feed;
private volatile long revision;

View File

@@ -223,6 +223,24 @@ public class FeedServlet extends HttpServlet {
feedManager.configure(host, autoDownload, sequential, updateInterval, itemsToKeep);
Util.pause();
resp.sendRedirect("/MuWire/Feeds");
} else if (action.equals("publish")) {
String path = req.getParameter("file");
if (path == null) {
resp.sendError(403, "Bad param");
return;
}
File file = Util.getFromPathElements(path);
feedManager.publish(file);
Util.pause();
} else if (action.equals("unpublish")) {
String path = req.getParameter("file");
if (path == null) {
resp.sendError(403, "Bad param");
return;
}
File file = Util.getFromPathElements(path);
feedManager.unpublish(file);
Util.pause();
}
}

View File

@@ -95,7 +95,8 @@ public class FilesServlet extends HttpServlet {
sf.getCachedPath(),
sf.getCachedLength(),
comment,
core.getCertificateManager().hasLocalCertificate(ih));
core.getCertificateManager().hasLocalCertificate(ih),
sf.isPublished());
entries.add(entry);
});
@@ -187,14 +188,16 @@ public class FilesServlet extends HttpServlet {
private final long size;
private final String comment;
private final boolean certified;
private final boolean published;
FilesTableEntry(String name, InfoHash infoHash, String path, long size, String comment, boolean certified) {
FilesTableEntry(String name, InfoHash infoHash, String path, long size, String comment, boolean certified, boolean published) {
this.name = name;
this.infoHash = infoHash;
this.path = path;
this.size = size;
this.comment = comment;
this.certified = certified;
this.published = published;
}
void toXML(StringBuilder sb) {
@@ -207,6 +210,7 @@ public class FilesServlet extends HttpServlet {
sb.append("<Comment>").append(Util.escapeHTMLinXML(comment)).append("</Comment>");
}
sb.append("<Certified>").append(certified).append("</Certified>");
sb.append("<Published>").append(published).append("</Published>");
sb.append("</File>");
}
}
@@ -268,6 +272,7 @@ public class FilesServlet extends HttpServlet {
}
InfoHash ih = new InfoHash(sf.getRoot());
sb.append("<Certified>").append(core.getCertificateManager().hasLocalCertificate(ih)).append("</Certified>");
sb.append("<Published>").append(sf.isPublished()).append("</Published>");
// TODO: other stuff
sb.append("</File>");
}

View File

@@ -167,7 +167,7 @@ public class MuWireClient {
core.getEventBus().register(UploadEvent.class, uploadManager);
core.getEventBus().register(UploadFinishedEvent.class, uploadManager);
FeedManager feedManager = new FeedManager(core);
FeedManager feedManager = new FeedManager(core, fileManager);
core.getEventBus().register(FeedLoadedEvent.class, feedManager);
core.getEventBus().register(UIFeedConfigurationEvent.class, feedManager);
core.getEventBus().register(FeedFetchEvent.class, feedManager);

View File

@@ -108,6 +108,7 @@ public class Util {
_x("Trust"),
_x("Trusted"),
_x("Trusted User"),
_x("Unpublish"),
_x("Unshare"),
_x("Unsubscribe"),
_x("Update"),

View File

@@ -1,6 +1,6 @@
class Node {
constructor(nodeId, parent, leaf, infoHash, path, size, comment, certified, shared, revision) {
constructor(nodeId, parent, leaf, infoHash, path, size, comment, certified, published, shared, revision) {
this.nodeId = nodeId
this.parent = parent
this.leaf = leaf
@@ -10,6 +10,7 @@ class Node {
this.size = size
this.comment = comment
this.certified = certified
this.published = published
this.revision = revision
this.shared = shared
}
@@ -25,14 +26,23 @@ class Node {
var certifyLink = "<a href='#' onclick='window.certify(\"" + this.nodeId + "\");return false;'>" + _t("Certify") + "</a>"
if (!this.shared)
certifyLink = ""
var publishLink = "<a href='#' onclick='window.publish(\"" + this.nodeId + "\");return false;'>" + _t("Publish") + "</a>"
if (!this.shared)
publishLink = ""
if (this.leaf) {
var certified = ""
if (this.certified == "true")
certified = _t("Certified")
var publish
if (this.published == "true") {
publish = new Link(_t("Unpublish"), "unpublish", [this.nodeId])
} else {
publish = new Link(_t("Publish"), "publish", [this.nodeId])
}
var nameLink = "<a href='/MuWire/DownloadedContent/" + this.infoHash + "'>" + this.path + "</a>"
var html = "<li>" + nameLink
html += "<div class='right'>" + unshareLink + " " + commentLink + " " + certifyLink + " " + certified + "</div>"
html += "<div class='right'>" + unshareLink + " " + commentLink + " " + certifyLink + " " + certified + " " + publish.render() +"</div>"
html += "<div class='centercomment' id='comment-" + this.nodeId + "'></div>"
html += "</li>"
@@ -150,16 +160,17 @@ function expand(nodeId) {
else
comment = null
var certified = element.getElementsByTagName("Certified")[0].childNodes[0].nodeValue
var published = element.getElementsByTagName("Published")[0].childNodes[0].nodeValue
var nodeId = node.nodeId + "_"+ Base64.encode(fileName)
var newFileNode = new Node(nodeId, node, true, infoHash, fileName, size, comment, certified, true, revision)
var newFileNode = new Node(nodeId, node, true, infoHash, fileName, size, comment, certified, published, true, revision)
nodesById.set(nodeId, newFileNode)
node.children.push(newFileNode)
} else if (element.nodeName == "Directory") {
var dirName = element.getElementsByTagName("Name")[0].childNodes[0].nodeValue
var shared = parseBoolean(element.getElementsByTagName("Shared")[0].childNodes[0].nodeValue)
var nodeId = node.nodeId + "_"+ Base64.encode(dirName)
var newDirNode = new Node(nodeId, node, false, null, dirName, -1, null, false, shared, revision)
var newDirNode = new Node(nodeId, node, false, null, dirName, -1, null, false, false, shared, revision)
nodesById.set(nodeId, newDirNode)
node.children.push(newDirNode)
}
@@ -258,3 +269,33 @@ function certify(nodeId) {
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send("action=certify&file=" + encodedPath)
}
function publish(nodeId) {
var node = nodesById.get(nodeId)
var encodedPath = encodedPathToRoot(node)
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
collapse(node.parent.nodeId)
expand(node.parent.nodeId)
}
}
xmlhttp.open("POST", "/MuWire/Feed", true)
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send("action=publish&file=" + encodedPath)
}
function unpublish(nodeId) {
var node = nodesById.get(nodeId)
var encodedPath = encodedPathToRoot(node)
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
collapse(node.parent.nodeId)
expand(node.parent.nodeId)
}
}
xmlhttp.open("POST", "/MuWire/Feed", true)
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send("action=unpublish&file=" + encodedPath)
}

View File

@@ -1,11 +1,12 @@
class SharedFile {
constructor(name, infoHash, path, size, comment, certified) {
constructor(name, infoHash, path, size, comment, certified, published) {
this.name = name
this.infoHash = infoHash
this.path = path
this.size = size
this.comment = comment
this.certified = certified
this.published = published
}
getMapping() {
@@ -16,13 +17,19 @@ class SharedFile {
var certifyHtml = certifyLink.render()
if (this.certified == "true")
certifyHtml += " " + _t("Certified")
var publishLink
if (this.published == "true")
publishLink = new Link(_t("Unpublish"), "unpublish", [this.path])
else
publishLink = new Link(_t("Publish"), "publish", [this.path])
var showCommentHtml = ""
var showCommentLink = new Link(_t("Comment"), "showCommentForm", [this.path])
showCommentHtml = "<span id='comment-link-" + this.path + "'>" + showCommentLink.render() + "</span>"
var commentDiv = "<div class='centercomment' id='comment-" + this.path + "'></div>"
var nameLink = "<a href='/MuWire/DownloadedContent/" + this.infoHash + "'>" + this.name + "</a>"
var html = nameLink + "<div class=\"right\">" + unshareLink.render() + " " + showCommentHtml + " " + certifyHtml + "</div>"
var html = nameLink + "<div class=\"right\">" + unshareLink.render() + " " + showCommentHtml + " " + certifyHtml + " " + publishLink.render()+ "</div>"
html += "<br/>" + commentDiv
mapping.set("File", html)
@@ -90,9 +97,10 @@ function refreshTable() {
else
comment = null
var certified = files[i].getElementsByTagName("Certified")[0].childNodes[0].nodeValue
var published = files[i].getElementsByTagName("Published")[0].childNodes[0].nodeValue
var path = Base64.encode(fileName)
var newSharedFile = new SharedFile(fileName, infoHash, path, size, comment, certified)
var newSharedFile = new SharedFile(fileName, infoHash, path, size, comment, certified, published)
filesByPath.set(path, newSharedFile)
filesList.push(newSharedFile)
}
@@ -194,3 +202,27 @@ function certify(path) {
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send("action=certify&file=" + path)
}
function publish(path) {
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
refreshTable()
}
}
xmlhttp.open("POST", "/MuWire/Feed", true)
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send("action=publish&file=" + path)
}
function unpublish(path) {
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
refreshTable()
}
}
xmlhttp.open("POST", "/MuWire/Feed", true)
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send("action=unpublish&file=" + path)
}