forked from RCOSDP/weko
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
267 lines (220 loc) · 7.28 KB
/
Copy pathutils.py
File metadata and controls
267 lines (220 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- coding: utf-8 -*-
#
# Copyright (C) 2025 National Institute of Informatics.
#
# WEKO-Notifications is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""Module of weko-notifications."""
import os
import re
import json
import traceback
from marshmallow import ValidationError
import pytz
from datetime import datetime
from flask import current_app, request
from requests import HTTPError
from weko_user_profiles.config import USERPROFILES_TIMEZONE_LIST
from weko_user_profiles.models import UserProfile
from .client import NotificationClient
def inbox_url(endpoint=None, _external=False):
"""Return the inbox URL.
Args:
endpoint (str | None): The endpoint to append to the URL.
_external (bool): Whether to return the URL with the full domain.
Returns:
str: The inbox URL.
"""
url = (
current_app.config["THEME_SITEURL"]
if _external
else current_app.config["WEKO_NOTIFICATIONS_INBOX_ADDRESS"]
)
if endpoint is not None:
url += endpoint
else:
url += current_app.config["WEKO_NOTIFICATIONS_INBOX_ENDPOINT"]
return str(url)
def rfc3339(timezone=None):
"""Return the current time in RFC3339 format.
Args:
timezone (str | None):
The timezone to use. Defaults to "Asia/Tokyo".
Returns:
str: The current time in RFC3339 format.
"""
tz = pytz.timezone(timezone or "Asia/Tokyo")
return datetime.now(tz).isoformat(timespec="seconds").replace("+00:00", "Z")
def create_subscription(user_id, endpoint, expiration_time, p256dh, auth):
"""Create a subscription.
Args:
user_id (int): The user ID.
endpoint (str): The subscription endpoint.
expiration_time (str): The expiration
p256dh (str): The P-256 Diffie-Hellman public key.
auth (str): The authentication secret.
Returns:
dict: The subscription.
"""
root_url = request.host_url
subscription = {
"target": f"{root_url}user/{user_id}",
"endpoint": endpoint,
"expirationTime": expiration_time,
"keys": {
"p256dh": p256dh,
"auth": auth
}
}
return subscription
def create_userprofile(userprofile):
"""Create a user profile.
Args:
userprofile (UserProfile): The user profile.
Returns:
dict: The user profile.
"""
root_url = request.host_url
posix_tz = userprofile.timezone
iana_tz = "GMT+9:00"
for posix, iana in USERPROFILES_TIMEZONE_LIST:
if posix == posix_tz:
pattern = r"\((GMT.*?)\)"
iana_tz = re.search(pattern, str(iana)).group(1)
userprofile = {
"uri": f"{root_url}user/{userprofile.user_id}",
"displayname": userprofile._displayname,
"language": userprofile.language,
"timezone": iana_tz,
}
return userprofile
def get_push_template():
"""Get the push template.
Returns:
dict: The push template.
"""
template_path = current_app.config["WEKO_NOTIFICATIONS_PUSH_TEMPLATE_PATH"]
if (
not template_path
or not os.path.isfile(template_path)
):
current_app.logger.error(
"Push template path is not set or file does not exist: {}"
.format(template_path)
)
return None
with open(template_path, "r", encoding="utf-8") as template_file:
template = json.load(template_file)
if not isinstance(template, dict):
current_app.logger.error(
"Push template is not a valid JSON object: {}".format(template_path)
)
return None
templates = [
{
"name": value.get("name"),
"description": value.get("description"),
"type": value.get("type"),
"language": lang,
"title": tpl.get("title"),
"body": tpl.get("body"),
}
for _, value in template.items()
for lang, tpl in value.get("templates", {}).items()
]
return templates
def _get_params_for_registrant(target_id, actor_id, shared_id):
"""Get parameters for registrant.
Args:
target_id (int): The target ID.
actor_id (int): The actor ID.
shared_id (int): The shared ID.
Returns:
tuple(set, str):
- str: Set of target IDs.
- str: The actor's name.
"""
set_target_id = {target_id}
is_shared = shared_id != -1
if is_shared:
set_target_id.add(shared_id)
set_target_id.discard(actor_id)
actor_profile = UserProfile.get_by_userid(actor_id)
actor_name = actor_profile.username if actor_profile else None
return set_target_id, actor_name
def notify_item_imported(
target_id, recid, actor_id, object_name=None, shared_id=-1
):
"""Notify item imported.
Args:
target_id (int): The target ID.
recid (str): The record ID.
actor_id (int): The actor ID.
shared_id (str): The shared ID.
Returns:
dict: The notification.
"""
set_target_id, actor_name = _get_params_for_registrant(
target_id, actor_id, shared_id
)
from .notifications import Notification
for target_id in set_target_id:
try:
Notification.create_item_registered(
target_id, recid, actor_id,
actor_name=actor_name, object_name=object_name,
).send(NotificationClient(inbox_url()))
except (ValidationError, HTTPError) as ex:
current_app.logger.error(
"Failed to send notification for item import."
)
traceback.print_exc()
return
except Exception as ex:
current_app.logger.error(
"Unexpected error occurred while sending notification for item import."
)
traceback.print_exc()
return
current_app.logger.info(
"{num} notification(s) sent for item import."
.format(num=len(set_target_id))
)
def notify_item_deleted(
target_id, recid, actor_id, object_name=None, shared_id=-1
):
"""Notify item deleted.
Args:
target_id (int): The target ID.
recid (str): The record ID.
actor_id (int): The actor ID.
shared_id (str): The shared ID.
Returns:
dict: The notification.
"""
set_target_id, actor_name = _get_params_for_registrant(
target_id, actor_id, shared_id
)
from .notifications import Notification
for target_id in set_target_id:
try:
Notification.create_item_deleted(
target_id, recid, actor_id,
actor_name=actor_name, object_name=object_name
).send(NotificationClient(inbox_url()))
except (ValidationError, HTTPError) as ex:
current_app.logger.error(
"Failed to send notification for item delete."
)
traceback.print_exc()
return
except Exception as ex:
current_app.logger.error(
"Unexpected error occurred while sending notification for item delete."
)
traceback.print_exc()
return
current_app.logger.info(
"{num} notification(s) sent for item delete."
.format(num=len(set_target_id))
)