|
| 1 | +"""API for working with Activity App.""" |
| 2 | + |
| 3 | +import dataclasses |
| 4 | +import datetime |
| 5 | +import typing |
| 6 | + |
| 7 | +from ._exceptions import NextcloudExceptionNotModified |
| 8 | +from ._misc import check_capabilities, nc_iso_time_to_datetime |
| 9 | +from ._session import NcSessionBasic |
| 10 | + |
| 11 | + |
| 12 | +@dataclasses.dataclass |
| 13 | +class ActivityFilter: |
| 14 | + """Activity filter description.""" |
| 15 | + |
| 16 | + def __init__(self, raw_data: dict): |
| 17 | + self._raw_data = raw_data |
| 18 | + |
| 19 | + @property |
| 20 | + def icon(self) -> str: |
| 21 | + """Icon for filter.""" |
| 22 | + return self._raw_data["icon"] |
| 23 | + |
| 24 | + @property |
| 25 | + def filter_id(self) -> str: |
| 26 | + """Filter ID.""" |
| 27 | + return self._raw_data["id"] |
| 28 | + |
| 29 | + @property |
| 30 | + def name(self) -> str: |
| 31 | + """Filter name.""" |
| 32 | + return self._raw_data["name"] |
| 33 | + |
| 34 | + @property |
| 35 | + def priority(self) -> int: |
| 36 | + """Arrangement priority in ascending order. Values from 0 to 99.""" |
| 37 | + return self._raw_data["priority"] |
| 38 | + |
| 39 | + |
| 40 | +@dataclasses.dataclass |
| 41 | +class Activity: |
| 42 | + """Description of one activity.""" |
| 43 | + |
| 44 | + def __init__(self, raw_data: dict): |
| 45 | + self._raw_data = raw_data |
| 46 | + |
| 47 | + @property |
| 48 | + def activity_id(self) -> int: |
| 49 | + """Unique for one Nextcloud instance activity ID.""" |
| 50 | + return self._raw_data["activity_id"] |
| 51 | + |
| 52 | + @property |
| 53 | + def app(self) -> str: |
| 54 | + """App that created the activity (e.g. 'files', 'files_sharing', etc.).""" |
| 55 | + return self._raw_data["app"] |
| 56 | + |
| 57 | + @property |
| 58 | + def activity_type(self) -> str: |
| 59 | + """String describing the activity type, depends on the **app** field.""" |
| 60 | + return self._raw_data["type"] |
| 61 | + |
| 62 | + @property |
| 63 | + def actor_id(self) -> str: |
| 64 | + """User ID of the user that triggered/created this activity. |
| 65 | +
|
| 66 | + .. note:: Can be empty in case of public link/remote share action. |
| 67 | + """ |
| 68 | + return self._raw_data["user"] |
| 69 | + |
| 70 | + @property |
| 71 | + def subject(self) -> str: |
| 72 | + """Translated simple subject without markup, ready for use (e.g. 'You created hello.jpg').""" |
| 73 | + return self._raw_data["subject"] |
| 74 | + |
| 75 | + @property |
| 76 | + def subject_rich(self) -> list: |
| 77 | + """`0` is the string subject including placeholders, `1` is an array with the placeholders.""" |
| 78 | + return self._raw_data["subject_rich"] |
| 79 | + |
| 80 | + @property |
| 81 | + def message(self) -> str: |
| 82 | + """Translated message without markup, ready for use (longer text, unused by core apps).""" |
| 83 | + return self._raw_data["message"] |
| 84 | + |
| 85 | + @property |
| 86 | + def message_rich(self) -> list: |
| 87 | + """See description of **subject_rich**.""" |
| 88 | + return self._raw_data["message_rich"] |
| 89 | + |
| 90 | + @property |
| 91 | + def object_type(self) -> str: |
| 92 | + """The Type of the object this activity is about (e.g. 'files' is used for files and folders).""" |
| 93 | + return self._raw_data["object_type"] |
| 94 | + |
| 95 | + @property |
| 96 | + def object_id(self) -> int: |
| 97 | + """ID of the object this activity is about (e.g., ID in the file cache is used for files and folders).""" |
| 98 | + return self._raw_data["object_id"] |
| 99 | + |
| 100 | + @property |
| 101 | + def object_name(self) -> str: |
| 102 | + """The name of the object this activity is about (e.g., for files it's the relative path to the user's root).""" |
| 103 | + return self._raw_data["object_name"] |
| 104 | + |
| 105 | + @property |
| 106 | + def objects(self) -> dict: |
| 107 | + """Contains the objects involved in multi-object activities, like editing multiple files in a folder. |
| 108 | +
|
| 109 | + .. note:: They are stored in objects as key-value pairs of the object_id and the object_name: |
| 110 | + { object_id: object_name} |
| 111 | + """ |
| 112 | + return self._raw_data["objects"] |
| 113 | + |
| 114 | + @property |
| 115 | + def link(self) -> str: |
| 116 | + """A full URL pointing to a suitable location (e.g. 'http://localhost/apps/files/?dir=%2Ffolder' for folder).""" |
| 117 | + return self._raw_data["link"] |
| 118 | + |
| 119 | + @property |
| 120 | + def icon(self) -> str: |
| 121 | + """URL of the icon.""" |
| 122 | + return self._raw_data["icon"] |
| 123 | + |
| 124 | + @property |
| 125 | + def activity_time(self) -> datetime.datetime: |
| 126 | + """Time when the activity occurred.""" |
| 127 | + return nc_iso_time_to_datetime(self._raw_data["datetime"]) |
| 128 | + |
| 129 | + |
| 130 | +class _ActivityAPI: |
| 131 | + """The class provides the Activity Application API.""" |
| 132 | + |
| 133 | + _ep_base: str = "/ocs/v1.php/apps/activity" |
| 134 | + last_given: int |
| 135 | + """Used by ``get_activities``, when **since** param is ``True``.""" |
| 136 | + |
| 137 | + def __init__(self, session: NcSessionBasic): |
| 138 | + self._session = session |
| 139 | + self.last_given = 0 |
| 140 | + |
| 141 | + @property |
| 142 | + def available(self) -> bool: |
| 143 | + """Returns True if the Nextcloud instance supports this feature, False otherwise.""" |
| 144 | + return not check_capabilities("activity.apiv2", self._session.capabilities) |
| 145 | + |
| 146 | + def get_activities( |
| 147 | + self, |
| 148 | + filter_id: typing.Union[ActivityFilter, str] = "", |
| 149 | + since: typing.Union[int, bool] = 0, |
| 150 | + limit: int = 50, |
| 151 | + object_type: str = "", |
| 152 | + object_id: int = 0, |
| 153 | + sort: str = "desc", |
| 154 | + ) -> list[Activity]: |
| 155 | + """Returns activities for the current user. |
| 156 | +
|
| 157 | + :param filter_id: Filter to apply, if needed. |
| 158 | + :param since: Last activity ID you have seen. When specified, only activities after provided are returned. |
| 159 | + Can be set to ``True`` to automatically use last ``last_given`` from previous calls. Default = **0**. |
| 160 | + :param limit: Max number of activities to be returned. |
| 161 | + :param object_type: Filter the activities to a given object. |
| 162 | + :param object_id: Filter the activities to a given object. |
| 163 | + :param sort: Sort activities ascending or descending. Default is ``desc``. |
| 164 | +
|
| 165 | + .. note:: ``object_type`` and ``object_id`` should only appear together with ``filter_id`` unset. |
| 166 | + """ |
| 167 | + if bool(object_id) != bool(object_type): |
| 168 | + raise ValueError("Either specify both `object_type` and `object_id`, or don't specify any at all.") |
| 169 | + if since is True: |
| 170 | + since = self.last_given |
| 171 | + filter_id = filter_id.filter_id if isinstance(filter_id, ActivityFilter) else filter_id |
| 172 | + params = { |
| 173 | + "since": since, |
| 174 | + "limit": limit, |
| 175 | + "object_type": object_type, |
| 176 | + "object_id": object_id, |
| 177 | + "sort": sort, |
| 178 | + } |
| 179 | + url = ( |
| 180 | + f"/api/v2/activity/{filter_id}" |
| 181 | + if filter_id |
| 182 | + else "/api/v2/activity/filter" if object_id else "/api/v2/activity" |
| 183 | + ) |
| 184 | + try: |
| 185 | + result = self._session.ocs("GET", self._ep_base + url, params=params) |
| 186 | + except NextcloudExceptionNotModified: |
| 187 | + return [] |
| 188 | + self.last_given = int(self._session.response_headers["X-Activity-Last-Given"]) |
| 189 | + return [Activity(i) for i in result] |
| 190 | + |
| 191 | + def get_filters(self) -> list[ActivityFilter]: |
| 192 | + """Returns avalaible activity filters.""" |
| 193 | + return [ActivityFilter(i) for i in self._session.ocs("GET", self._ep_base + "/api/v2/activity/filters")] |
0 commit comments