Skip to content

Commit

Permalink
Added tags
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Mar 10, 2024
1 parent 3a5908b commit 550b419
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class PasteController extends HttpController {

@Post
@With({"rate-limiter", "auth-login-required-create", "awaiting-access-check", "blocked-check"})
public CreatePasteResponse create(Exchange exchange, @Body CreatePasteRequest request, @Attrib("user") User user, @Attrib("authkey") AuthKey authKey, @Path("id") String pasteId) {
public CreatePasteResponse create(Exchange exchange, @Body CreatePasteRequest request, @Attrib("user") User user, @Attrib("authkey") AuthKey authKey) {
if (authKey != null)
authKey.checkPermission("pastes:create", "pastes:write");

Expand Down Expand Up @@ -77,6 +77,18 @@ public CreatePasteResponse create(Exchange exchange, @Body CreatePasteRequest re

paste.save();


if (request.tags != null) {
System.out.println("Adding tags");
for (String tag : request.tags) {
System.out.println("Adding"+tag);
PasteTag pTag = new PasteTag();
pTag.paste = paste.getKey();
pTag.tag = tag;
pTag.save();
}
}

response.success = true;
response.paste = new PasteResponse(paste);

Expand All @@ -94,6 +106,7 @@ public List<PasteResponse> getPastes(Exchange exchange, @Attrib("user") User use
RequestHelper.pagination(query, exchange);
query.search(exchange.query("search"));
RequestHelper.queryFilter(query, exchange.getQueryParameters());
RequestHelper.filterTags(query, exchange.getQueryParameters());

return query.order("created_at", true).all().stream().map(p -> PasteResponse.create(p, exchange)).collect(Collectors.toList());
}
Expand Down Expand Up @@ -129,6 +142,19 @@ public ActionResponse putPaste(@Body EditPasteRequest request, @Path("id") Strin
paste.setExpireAt(request.expireAt);
}

if (request.tags != null) {
List<String> tags = paste.getTags();
request.tags.stream().filter(t -> !tags.contains(t)).forEach(tag -> {
PasteTag pTag = new PasteTag();
pTag.paste = paste.getKey();
pTag.tag = tag;
pTag.save();
});
tags.stream().filter(t -> !request.tags.contains(t)).forEach(t -> {
Repo.get(PasteTag.class).where("paste", paste.getKey()).where("tag", t).delete();
});
}

paste.save();
response.success = true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public List<PasteResponse> getPublicPastes(Exchange exchange) {
RequestHelper.pagination(query, exchange);
query.search(exchange.query("search"));
RequestHelper.queryFilter(query, exchange.getQueryParameters());
RequestHelper.filterTags(query, exchange.getQueryParameters());

return query
.stream()
Expand All @@ -43,7 +44,7 @@ public List<PasteResponse> getTrending(Exchange exchange) {

RequestHelper.pagination(publicPasteEngagements, exchange);

if ("true".equals(exchange.rawRequest().getParameter("trending"))) {
if (equals(exchange.getQueryParameters().has("trending"))) {
Date date = new Date();
date.setTime(System.currentTimeMillis() - (1000 * 60 * 60 * 24 * 4));
publicPasteEngagements.where("createdAt", ">", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
Expand All @@ -65,8 +66,7 @@ public List<PasteResponse> getTrending(Exchange exchange) {

query.search(exchange.query("search"));
RequestHelper.queryFilter(query, exchange.getQueryParameters());


RequestHelper.filterTags(query, exchange.getQueryParameters());

return query
.stream()
Expand All @@ -85,6 +85,7 @@ public List<PasteResponse> getLatest(Exchange exchange) {
RequestHelper.pagination(query, exchange);
query.search(exchange.query("search"));
RequestHelper.queryFilter(query, exchange.getQueryParameters());
RequestHelper.filterTags(query, exchange.getQueryParameters());

return query
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public List<Notification> getNotifications(Exchange exchange, @Attrib("user") Us

Query<Notification> query = Repo.get(Notification.class).where("userId", user.getId());

if (exchange.rawRequest().getParameter("not_received") != null)
if (exchange.getQueryParameters().has("not_received"))
query.where("received", false);


if (exchange.rawRequest().getParameter("not_read") != null)
if (exchange.getQueryParameters().has("not_read"))
query.where("alreadyRead", false);

List<Notification> notifications = query.all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import de.interaapps.pastefy.model.responses.paste.PasteResponse;
import de.interaapps.pastefy.model.responses.user.UserPastesResponse;
import de.interaapps.pastefy.model.responses.user.UserResponse;
import org.javawebstack.abstractdata.AbstractElement;
import org.javawebstack.abstractdata.AbstractNull;
import org.javawebstack.httpserver.Exchange;
import org.javawebstack.httpserver.router.annotation.PathPrefix;
import org.javawebstack.httpserver.router.annotation.With;
Expand Down Expand Up @@ -36,11 +38,11 @@ public UserPastesResponse userPastes(Exchange exchange, @Attrib("user") User use

UserPastesResponse response = new UserPastesResponse();
int page = 0;
if (exchange.rawRequest().getParameter("page") != null)
page = Integer.parseInt(exchange.rawRequest().getParameter("page")) - 1;
if (exchange.getQueryParameters().has("page"))
page = Integer.parseInt(exchange.getQueryParameters().get("page").string()) - 1;

response.pastes = Repo.get(Paste.class).where("userId", user.getId()).whereNull("folder").order("updated_at", true).limit(10).offset(page * 10).all().stream().map(paste -> PasteResponse.create(paste, exchange)).collect(Collectors.toList());
response.folder = Repo.get(Folder.class).where("userId", user.getId()).whereNull("parent").order("updated_at", true).all().stream().map(folder -> new FolderResponse(folder, exchange.rawRequest().getParameter("hide_children") == null)).collect(Collectors.toList());
response.folder = Repo.get(Folder.class).where("userId", user.getId()).whereNull("parent").order("updated_at", true).all().stream().map(folder -> new FolderResponse(folder, !exchange.getQueryParameters().has("hide_children"))).collect(Collectors.toList());

return response;
}
Expand All @@ -51,7 +53,11 @@ public List<FolderResponse> getFolder(Exchange exchange, @Attrib("user") User us
if (authKey != null)
authKey.checkPermission("folders:read");

return user.getFolderTree(exchange.rawRequest().getParameter("hide_children") == null, exchange.rawRequest().getParameter("hide_sub_children") == null, exchange.rawRequest().getParameter("hide_pastes") == null);
return user.getFolderTree(
!exchange.getQueryParameters().has("hide_children"),
!exchange.getQueryParameters().has("hide_sub_children"),
!exchange.getQueryParameters().has("hide_pastes")
);
}

@Get("/pastes")
Expand All @@ -69,6 +75,7 @@ public List<PasteResponse> getPastes(Exchange exchange, @Attrib("user") User use
RequestHelper.pagination(query, exchange);
query.search(exchange.query("search"));
RequestHelper.queryFilter(query, exchange.getQueryParameters());
RequestHelper.filterTags(query, exchange.getQueryParameters());


return query.all().stream().map(p -> PasteResponse.create(p, exchange)).collect(Collectors.toList());
Expand All @@ -82,8 +89,8 @@ public List<PasteResponse> getSharedPastes(Exchange exchange, @Attrib("user") Us

List<PasteResponse> pastes = new ArrayList<>();
int page = 0;
if (exchange.rawRequest().getParameter("page") != null)
page = Integer.parseInt(exchange.rawRequest().getParameter("page")) - 1;
if (exchange.getQueryParameters().has("page"))
page = Integer.parseInt(exchange.getQueryParameters().get("page").string()) - 1;

pastes = Repo.get(SharedPaste.class)
.where("targetId", user.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import de.interaapps.pastefy.Pastefy;
import de.interaapps.pastefy.exceptions.PermissionsDeniedException;
import de.interaapps.pastefy.model.database.Paste;
import de.interaapps.pastefy.model.database.PasteTag;
import de.interaapps.pastefy.model.database.User;
import org.javawebstack.abstractdata.AbstractObject;
import org.javawebstack.httpserver.Exchange;
Expand Down Expand Up @@ -34,6 +36,16 @@ public static void queryFilter(Query<?> query, AbstractObject params) {
}
}

public static void filterTags(Query<Paste> query, AbstractObject params) {
if (params.has("filter_tags")) {
String[] filterTags = params.get("filter_tags").string().split(",");

for (String filterTag : filterTags) {
query.whereExists(PasteTag.class, (pasteTagQuery) -> pasteTagQuery.where("tag", filterTag).where(PasteTag.class, "paste", "=", Paste.class, "key"));
}
}
}

public static void userIdPastesFilter(User user, Query<?> query, Exchange exchange) {
boolean isAdmin = user != null && user.type == User.Type.ADMIN;
String filterUserId = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import org.javawebstack.orm.Model;
import org.javawebstack.orm.Repo;
import org.javawebstack.orm.annotation.*;
import java.util.List;

import java.sql.Timestamp;
import java.util.stream.Collectors;

@Dates
@Table("pastes")
Expand Down Expand Up @@ -156,6 +158,10 @@ public Type getType() {
return type;
}

public List<String> getTags() {
return Repo.get(PasteTag.class).where("paste", key).get().stream().map(t -> t.tag).collect(Collectors.toList());
}

public void setForkedFrom(String forkedFrom) {
this.forkedFrom = forkedFrom;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.interaapps.pastefy.model.database;

import org.javawebstack.orm.Model;
import org.javawebstack.orm.annotation.Column;
import org.javawebstack.orm.annotation.Filterable;
import org.javawebstack.orm.annotation.Table;

@Table("paste_tags")
public class PasteTag extends Model {
@Column
private int id;


@Column(size = 8)
@Filterable
public String paste;

@Column
@Filterable
public String tag;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public List<Folder> getFolders() {
}

public List<FolderResponse> getFolderTree(boolean fetchChildren, boolean fetchSubChildren, boolean fetchPastes) {
return Repo.get(Folder.class).where("userId", id).isNull("parent").all().stream().map(folder -> new FolderResponse(folder, fetchChildren, fetchSubChildren, fetchPastes, true)).collect(Collectors.toList());
return Repo.get(Folder.class).where("userId", id).whereNotNull("parent").all().stream().map(folder -> new FolderResponse(folder, fetchChildren, fetchSubChildren, fetchPastes, true)).collect(Collectors.toList());
}

public List<FolderResponse> getFolderWithChildren() {
return Repo.get(Folder.class).where("userId", id).isNull("parent").all().stream().map(folder -> new FolderResponse(folder, true, true, false, true)).collect(Collectors.toList());
return Repo.get(Folder.class).where("userId", id).whereNotNull("parent").all().stream().map(folder -> new FolderResponse(folder, true, true, false, true)).collect(Collectors.toList());
}

public void sendNotification(Notification notification) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class CreatePasteRequest {

public String forkedFrom;

public String[] tags = null;

public Paste.Visibility visibility = Paste.Visibility.UNLISTED;

public Paste.Type type = Paste.Type.PASTE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import de.interaapps.pastefy.model.database.Paste;
import org.javawebstack.validator.Rule;

import java.util.List;

public class EditPasteRequest {
@Rule("string")
public String title;
Expand All @@ -13,6 +15,8 @@ public class EditPasteRequest {
public String folder;
public Paste.Type type;

public List<String> tags;

public Paste.Visibility visibility;

public String expireAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import com.google.gson.annotations.SerializedName;
import de.interaapps.pastefy.Pastefy;
import de.interaapps.pastefy.model.database.Paste;
import de.interaapps.pastefy.model.database.PasteTag;
import org.javawebstack.httpserver.Exchange;
import org.javawebstack.orm.Repo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class PasteResponse {
public boolean exists = false;
Expand All @@ -20,6 +26,7 @@ public class PasteResponse {
public Paste.Type type;
public String createdAt = "0000-00-00 00:00:00";
public String expireAt = null;
public List<String> tags;

public PasteResponse(Paste paste) {
if (paste == null) {
Expand All @@ -42,6 +49,8 @@ public PasteResponse(Paste paste) {
folder = paste.getFolderId();
type = paste.getType();
exists = true;

tags = paste.getTags();
}

public PasteResponse shortenContent() {
Expand Down

0 comments on commit 550b419

Please sign in to comment.