[core] Add default trigger value

This commit is contained in:
Cathy Hu 2020-09-08 13:02:07 +02:00
parent 6d3664968d
commit 09810680e8
6 changed files with 68 additions and 5 deletions

View file

@ -20,6 +20,51 @@
if the remaining message is legitimate. For example insults or spam if the remaining message is legitimate. For example insults or spam
words like "blockchain". words like "blockchain".
</p> </p>
<p>
<strong>Example: </strong>
For example say you want all messages to be distributed except
messages with insult/spam words "fuck", "asshat", "blockchain" then
you can add these words to the Badwords like so:
<img class="image" src="assets/badwords.png" alt="image" />
Note the dot in the Triggers section. It means that every message is
being relayed except those in badwords. If you would remove it, no
message would be relayed at all, so only change the Trigger section if
you are sure what you do.
</p>
<p>
Now if a user sends a message including "asshat" the message will
<strong>not be distributed</strong>. For example:
</p>
<div class="example-container">
<mat-card>
<mat-card-title>xyz is an asshat!</mat-card-title>
<mat-card-subtitle
>This message will
<strong>not be distributed</strong>.</mat-card-subtitle
>
</mat-card>
<mat-card>
<mat-card-title>xyz is an verybigAsshat!</mat-card-title>
<mat-card-subtitle
>This message will
<strong>not be distributed</strong>.</mat-card-subtitle
>
</mat-card>
<mat-card>
<mat-card-title>xyz is nice!</mat-card-title>
<mat-card-subtitle
>This message will be distributed.</mat-card-subtitle
>
</mat-card>
</div>
<p>
<strong>For experts only:</strong> We use python re module with
<strong>re.search(pattern, string, flags=re.IGNORECASE)</strong> for
both Triggers and Badwords. View
<a href="https://docs.python.org/3/library/re.html" target="_blank"
>Documentation</a
>.
</p>
</mat-expansion-panel> </mat-expansion-panel>
</mat-accordion> </mat-accordion>
</div> </div>

View file

@ -22,3 +22,16 @@
margin-top: 2px; margin-top: 2px;
margin-left: 10px; margin-left: 10px;
} }
.image {
width: 80%;
margin-left: 10%;
margin-right: 10%;
}
.example-container {
margin: 5%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View file

@ -9,7 +9,7 @@ from asyncio import create_task, Queue
from enum import auto, Enum from enum import auto, Enum
from kibicara.model import BadWord, Trigger from kibicara.model import BadWord, Trigger
from logging import getLogger from logging import getLogger
from re import match from re import search, IGNORECASE
logger = getLogger(__name__) logger = getLogger(__name__)
@ -147,11 +147,11 @@ class Censor:
async def __is_appropriate(self, message): async def __is_appropriate(self, message):
for badword in await BadWord.objects.filter(hood=self.hood).all(): for badword in await BadWord.objects.filter(hood=self.hood).all():
if match(badword.pattern, message.text): if search(badword.pattern, message.text, IGNORECASE):
logger.debug('Matched bad word - dropped message: %s' % message.text) logger.debug('Matched bad word - dropped message: %s' % message.text)
return False return False
for trigger in await Trigger.objects.filter(hood=self.hood).all(): for trigger in await Trigger.objects.filter(hood=self.hood).all():
if match(trigger.pattern, message.text): if search(trigger.pattern, message.text, IGNORECASE):
logger.debug('Matched trigger - passed message: %s' % message.text) logger.debug('Matched trigger - passed message: %s' % message.text)
return True return True
logger.debug('Did not match any trigger - dropped message: %s' % message.text) logger.debug('Did not match any trigger - dropped message: %s' % message.text)

View file

@ -72,6 +72,10 @@ async def hood_create(values: BodyHood, response: Response, admin=Depends(get_ad
hood = await Hood.objects.create(**values.__dict__) hood = await Hood.objects.create(**values.__dict__)
await AdminHoodRelation.objects.create(admin=admin.id, hood=hood.id) await AdminHoodRelation.objects.create(admin=admin.id, hood=hood.id)
spawner.start(hood) spawner.start(hood)
# Initialize Triggers to match all
await Trigger.objects.create(hood=hood, pattern='.')
response.headers['Location'] = '%d' % hood.id response.headers['Location'] = '%d' % hood.id
return hood return hood
except IntegrityError: except IntegrityError:

View file

@ -22,14 +22,15 @@ def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
r'(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', r'(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
body, body,
)[0] )[0]
start = len('token=')
response = client.post( response = client.post(
'/api/hoods/%d/email/subscribe/confirm/%s' '/api/hoods/%d/email/subscribe/confirm/%s'
% (hood_id, urlparse(confirm_url).query[len('token=') :]) % (hood_id, urlparse(confirm_url).query[start:])
) )
assert response.status_code == status.HTTP_201_CREATED assert response.status_code == status.HTTP_201_CREATED
response = client.post( response = client.post(
'/api/hoods/%d/email/subscribe/confirm/%s' '/api/hoods/%d/email/subscribe/confirm/%s'
% (hood_id, urlparse(confirm_url).query[len('token=') :]) % (hood_id, urlparse(confirm_url).query[start:])
) )
assert response.status_code == status.HTTP_409_CONFLICT assert response.status_code == status.HTTP_409_CONFLICT
token = to_token(email=mail['to'], hood=hood_id) token = to_token(email=mail['to'], hood=hood_id)