Molodetz blogrol volgens DPP-template
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
from molodetz.cache import TTLCache
|
||||
from molodetz import config
|
||||
from molodetz.database.core import db, generate_uid, now_iso
|
||||
|
||||
_unread_cache = TTLCache(ttl=config.UNREAD_TTL, max_size=1000)
|
||||
|
||||
|
||||
def insert_notification(user_uid, kind, message, url):
|
||||
row = {
|
||||
"uid": generate_uid(),
|
||||
"user_uid": user_uid,
|
||||
"kind": kind,
|
||||
"message": message,
|
||||
"url": url,
|
||||
"read_at": None,
|
||||
"created_at": now_iso(),
|
||||
"deleted_at": None,
|
||||
"deleted_by": None,
|
||||
}
|
||||
db["notifications"].insert(row)
|
||||
_unread_cache.pop(user_uid)
|
||||
return row
|
||||
|
||||
|
||||
def list_notifications(user_uid, limit=100):
|
||||
if "notifications" not in db.tables:
|
||||
return []
|
||||
return list(db["notifications"].find(user_uid=user_uid, deleted_at=None, order_by=["-created_at"], _limit=limit))
|
||||
|
||||
|
||||
def unread_count(user_uid):
|
||||
if not user_uid:
|
||||
return 0
|
||||
cached = _unread_cache.get(user_uid)
|
||||
if cached is not None:
|
||||
return cached
|
||||
count = 0
|
||||
if "notifications" in db.tables:
|
||||
count = db["notifications"].count(user_uid=user_uid, deleted_at=None, read_at=None)
|
||||
_unread_cache.set(user_uid, count)
|
||||
return count
|
||||
|
||||
|
||||
def mark_all_read(user_uid):
|
||||
if "notifications" not in db.tables:
|
||||
return
|
||||
with db:
|
||||
db.query(
|
||||
"UPDATE notifications SET read_at = :stamp WHERE user_uid = :user_uid "
|
||||
"AND read_at IS NULL AND deleted_at IS NULL",
|
||||
stamp=now_iso(),
|
||||
user_uid=user_uid,
|
||||
)
|
||||
_unread_cache.pop(user_uid)
|
||||
Reference in New Issue
Block a user