|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from typing import Final |
| 21 | + |
| 22 | +import sqlmodel |
| 23 | + |
| 24 | +import atr.db as db |
| 25 | +import atr.models.sql as sql |
| 26 | +import atr.storage as storage |
| 27 | + |
| 28 | +_MAX_MESSAGE_LENGTH: Final[int] = 1024 |
| 29 | + |
| 30 | + |
| 31 | +class FoundationCommitter: |
| 32 | + def __init__(self, write: storage.Write, write_as: storage.WriteAsFoundationCommitter, data: db.Session): |
| 33 | + self.__write = write |
| 34 | + self.__write_as = write_as |
| 35 | + self.__data = data |
| 36 | + self.__asf_uid = write_as.asf_uid |
| 37 | + |
| 38 | + async def dismiss(self, notification_id: int) -> bool: |
| 39 | + via = sql.validate_instrumented_attribute |
| 40 | + result = await self.__data.execute( |
| 41 | + sqlmodel.delete(sql.Notification).where( |
| 42 | + via(sql.Notification.id) == notification_id, |
| 43 | + via(sql.Notification.asf_uid) == self.__asf_uid, |
| 44 | + ) |
| 45 | + ) |
| 46 | + await self.__data.commit() |
| 47 | + dismissed = (getattr(result, "rowcount", 0) or 0) > 0 |
| 48 | + if dismissed: |
| 49 | + self.__write_as.append_to_audit_log( |
| 50 | + asf_uid=self.__asf_uid, |
| 51 | + notification_id=notification_id, |
| 52 | + ) |
| 53 | + return dismissed |
| 54 | + |
| 55 | + |
| 56 | +class UserService: |
| 57 | + def __init__(self, waus: storage.WriteAsUserService, data: db.Session): |
| 58 | + self.__waus = waus |
| 59 | + self.__data = data |
| 60 | + self.__asf_uid = waus.asf_uid |
| 61 | + |
| 62 | + async def create( |
| 63 | + self, |
| 64 | + message: str, |
| 65 | + level: sql.NotificationLevel = sql.NotificationLevel.ERROR, |
| 66 | + ) -> sql.Notification: |
| 67 | + notification = sql.Notification( |
| 68 | + asf_uid=self.__asf_uid, |
| 69 | + message=_normalised_message(message), |
| 70 | + level=level, |
| 71 | + ) |
| 72 | + self.__data.add(notification) |
| 73 | + await self.__data.commit() |
| 74 | + self.__waus.append_to_audit_log( |
| 75 | + asf_uid=self.__asf_uid, |
| 76 | + notification_id=notification.id, |
| 77 | + level=level.value, |
| 78 | + ) |
| 79 | + return notification |
| 80 | + |
| 81 | + |
| 82 | +def _normalised_message(message: str) -> str: |
| 83 | + message = " ".join(message.strip().split()) |
| 84 | + if not message: |
| 85 | + raise ValueError("Notification message cannot be empty") |
| 86 | + if len(message) > _MAX_MESSAGE_LENGTH: |
| 87 | + return message[: _MAX_MESSAGE_LENGTH - 3] + "..." |
| 88 | + return message |
0 commit comments