Filters as whitelist
This commit is contained in:
parent
680924fc6e
commit
a0a1435478
@ -125,7 +125,7 @@ class FilterSerializer(DynamicModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Filter
|
||||
fields = ("id", "name", "keywords", "regex", "guild_id")
|
||||
fields = ("id", "name", "keywords", "regex", "whitelist", "guild_id")
|
||||
|
||||
|
||||
class SubscriptionSerializer(DynamicModelSerializer):
|
||||
|
@ -111,7 +111,7 @@ class Filter_ListView(generics.ListCreateAPIView):
|
||||
serializer_class = FilterSerializer
|
||||
|
||||
filter_backends = [filters.SearchFilter, rest_filters.DjangoFilterBackend, filters.OrderingFilter]
|
||||
filterset_fields = ["id", "name", "keywords", "regex", "guild_id"]
|
||||
filterset_fields = ["id", "name", "keywords", "regex", "whitelist", "guild_id"]
|
||||
search_fields = ["name", "keywords", "regex"]
|
||||
|
||||
def get_queryset(self):
|
||||
|
18
apps/home/migrations/0003_filter_whitelist.py
Normal file
18
apps/home/migrations/0003_filter_whitelist.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.0.4 on 2024-06-12 08:30
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('home', '0002_trackedcontent_blocked'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='filter',
|
||||
name='whitelist',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
@ -164,6 +164,8 @@ class Filter(models.Model):
|
||||
blank=True
|
||||
)
|
||||
|
||||
whitelist = models.BooleanField(default=False)
|
||||
|
||||
# Have to use charfield instead of positiveBigIntegerField due to an Sqlite
|
||||
# issue that rounds down the value
|
||||
# https://github.com/sequelize/sequelize/issues/9335
|
||||
|
@ -53,6 +53,13 @@ function initFiltersTable() {
|
||||
if (!data) return "-";
|
||||
return data;
|
||||
}
|
||||
},
|
||||
{
|
||||
title: "Type",
|
||||
data: "whitelist",
|
||||
render: function(data) {
|
||||
return data ? "whitelist" : "blacklist";
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
@ -88,6 +95,7 @@ async function showEditFilterModal(filterId) {
|
||||
$("#filterFormModal .form-edit").hide();
|
||||
$("#filterAdvancedMode").prop("checked", false);
|
||||
$("#filterAdvancedMode").trigger("change");
|
||||
$("#filterWhitelist").prop("checked", false);
|
||||
}
|
||||
else {
|
||||
const filter = filtersTable.row(function(idx, data, node) {
|
||||
@ -101,6 +109,9 @@ async function showEditFilterModal(filterId) {
|
||||
$("#filterAdvancedMode").prop("checked", filter.regex !== "");
|
||||
$("#filterAdvancedMode").trigger("change");
|
||||
|
||||
$("#filterWhitelist").prop("checked", filter.whitelist);
|
||||
$("#filterWhitelist").trigger("change");
|
||||
|
||||
$("#filterFormModal .form-create").hide();
|
||||
$("#filterFormModal .form-edit").show();
|
||||
}
|
||||
@ -117,6 +128,7 @@ $("#filterForm").on("submit", async function(event) {
|
||||
name = $("#filterName").val();
|
||||
keywords = advancedFiltering ? "" : $("#filterKeywords").val();
|
||||
regex = advancedFiltering ? $("#filterRegex").val() : "";
|
||||
whitelistMode = $("#filterWhitelist").prop("checked");
|
||||
guildId = getCurrentlyActiveServer().guild_id;
|
||||
|
||||
if (advancedFiltering) {
|
||||
@ -126,7 +138,7 @@ $("#filterForm").on("submit", async function(event) {
|
||||
regex = ""
|
||||
}
|
||||
|
||||
var filterPrimaryKey = await saveFilter(id, name, keywords, regex, guildId);
|
||||
var filterPrimaryKey = await saveFilter(id, name, keywords, regex, whitelistMode, guildId);
|
||||
|
||||
if (filterPrimaryKey) {
|
||||
showToast("success", "Filter Saved", "Filter ID " + filterPrimaryKey);
|
||||
@ -137,11 +149,12 @@ $("#filterForm").on("submit", async function(event) {
|
||||
$("#filterFormModal").modal("hide");
|
||||
});
|
||||
|
||||
async function saveFilter(id, name, keywords, regex, guildId) {
|
||||
async function saveFilter(id, name, keywords, regex, whitelistMode, guildId) {
|
||||
var formData = new FormData();
|
||||
formData.append("name", name);
|
||||
formData.append("keywords", keywords);
|
||||
formData.append("regex", regex);
|
||||
formData.append("whitelist", whitelistMode)
|
||||
formData.append("guild_id", guildId);
|
||||
|
||||
var response;
|
||||
@ -205,3 +218,11 @@ $("#deleteSelectedFiltersBtn").on("click", async function() {
|
||||
loadFilterOptions(guildId);
|
||||
}, 500)
|
||||
})
|
||||
|
||||
$("#deleteEditFilter").on("click", async function() {
|
||||
const filterId = $("#filterId").val();
|
||||
await deleteFilter(filterId);
|
||||
await loadFilters(getCurrentlyActiveServer().guild_id);
|
||||
$("#filterFormModal").modal("hide");
|
||||
showToast("danger", "Deleted Filter", "Filter ID: " + filterId);
|
||||
});
|
@ -1,7 +1,7 @@
|
||||
|
||||
<div id="filterFormModal" class="modal fade" data-bs-backdrop="static" tabindex="-1">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-content rounded-1">
|
||||
<form id="filterForm" class="mb-0" novalidate>
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">
|
||||
@ -10,33 +10,40 @@
|
||||
Filter
|
||||
</h5>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="modal-body p-4">
|
||||
<input type="hidden" id="filterId" name="filterId">
|
||||
<div class="mb-3">
|
||||
<div class="mb-4">
|
||||
<label for="filterName" class="form-label">Name</label>
|
||||
<input type="text" id="filterName" name="filterName" class="form-control" placeholder="Remove Common Words">
|
||||
<input type="text" id="filterName" name="filterName" class="form-control rounded-1" placeholder="Remove Common Words">
|
||||
</div>
|
||||
<div class="mb-3 form-switch">
|
||||
<input type="checkbox" id="filterAdvancedMode" name="filterAdvancedMode" class="form-check-input" />
|
||||
<label for="filterAdvancedMode" class="form-check-label">Advanced Filtering</label>
|
||||
<div class="mb-4">
|
||||
<div class="form-switch form-check-inline pe-4">
|
||||
<input type="checkbox" id="filterAdvancedMode" name="filterAdvancedMode" class="form-check-input" />
|
||||
<label for="filterAdvancedMode" class="form-check-label d-inline">Advanced Filtering</label>
|
||||
</div>
|
||||
<div class="form-switch form-check-inline">
|
||||
<input type="checkbox" id="filterWhitelist" name="filterWhitelist" class="form-check-input" />
|
||||
<label for="filterWhitelist" class="form-check-label d-inline">Whitelist Mode</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="simple-filtering">
|
||||
<label for="filterKeywords" class="form-label">Keywords</label>
|
||||
<textarea id="filterKeywords" name="filterKeywords" class="form-control" placeholder="one,common,word,or,another"></textarea>
|
||||
<textarea id="filterKeywords" name="filterKeywords" class="form-control rounded-1" placeholder="one,common,word,or,another"></textarea>
|
||||
<div class="form-text">Commma separated words to block.</div>
|
||||
</div>
|
||||
<div class="advanced-filtering">
|
||||
<label for="filterRegex" class="form-label">Regex</label>
|
||||
<input type="text" id="filterRegex" name="filterRegex" class="form-control" placeholder="(?:^|(?<= ))(one|common|word|or|another)(?:(?= )|$)">
|
||||
<input type="text" id="filterRegex" name="filterRegex" class="form-control rounded-1" placeholder="(?:^|(?<= ))(one|common|word|or|another)(?:(?= )|$)">
|
||||
<div class="form-text">Block content matching the provided regex.</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<button type="button" id="deleteEditFilter" class="btn btn-danger rounded-1 me-auto ms-0 form-edit">Delete</button>
|
||||
<button type="submit" class="btn btn-primary rounded-1">
|
||||
<span class="form-create">Create</span>
|
||||
<span class="form-edit">Confirm Edit</span>
|
||||
</button>
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
||||
<button type="button" class="btn btn-secondary rounded-1" data-bs-dismiss="modal">Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user