~homeworkprod/byceps

ref: 87b31ba68669551f699c7d12cf18401663b2d043 byceps/byceps/announce/text_assembly/board.py -rw-r--r-- 8.3 KiB
87b31ba6 — Jochen Kupperschmidt Fix type errors 1 year, 3 months ago
                                                                                
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
byceps.announce.text_assembly.board
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Announce board events.

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from ...events.board import (
    BoardPostingCreated,
    BoardPostingHidden,
    BoardPostingUnhidden,
    BoardTopicCreated,
    BoardTopicHidden,
    BoardTopicLocked,
    BoardTopicMoved,
    BoardTopicPinned,
    BoardTopicUnhidden,
    BoardTopicUnlocked,
    BoardTopicUnpinned,
)
from ...services.board.transfer.models import TopicID
from ...services.board import topic_query_service as board_topic_query_service
from ...services.brand import service as brand_service

from ._helpers import get_screen_name_or_fallback


def assemble_text_for_board_topic_created(
    event: BoardTopicCreated,
    webhook_format: str,
) -> str:
    topic_creator_screen_name = get_screen_name_or_fallback(
        event.topic_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{topic_creator_screen_name} hat{board_label_segment} '
        f'das Thema "{event.topic_title}" erstellt: {url}'
    )


def assemble_text_for_board_topic_hidden(
    event: BoardTopicHidden,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    topic_creator_screen_name = get_screen_name_or_fallback(
        event.topic_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} das Thema '
        f'"{event.topic_title}" von {topic_creator_screen_name} '
        f'versteckt: {url}'
    )


def assemble_text_for_board_topic_unhidden(
    event: BoardTopicUnhidden,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    topic_creator_screen_name = get_screen_name_or_fallback(
        event.topic_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} das Thema '
        f'"{event.topic_title}" von {topic_creator_screen_name} '
        f'wieder sichtbar gemacht: {url}'
    )


def assemble_text_for_board_topic_locked(
    event: BoardTopicLocked,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    topic_creator_screen_name = get_screen_name_or_fallback(
        event.topic_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} das Thema '
        f'"{event.topic_title}" von {topic_creator_screen_name} '
        f'geschlossen: {url}'
    )


def assemble_text_for_board_topic_unlocked(
    event: BoardTopicUnlocked,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    topic_creator_screen_name = get_screen_name_or_fallback(
        event.topic_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} das Thema '
        f'"{event.topic_title}" von {topic_creator_screen_name} '
        f'wieder geöffnet: {url}'
    )


def assemble_text_for_board_topic_pinned(
    event: BoardTopicPinned,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    topic_creator_screen_name = get_screen_name_or_fallback(
        event.topic_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} das Thema '
        f'"{event.topic_title}" von {topic_creator_screen_name} '
        f'angepinnt: {url}'
    )


def assemble_text_for_board_topic_unpinned(
    event: BoardTopicUnpinned,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    topic_creator_screen_name = get_screen_name_or_fallback(
        event.topic_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} das Thema '
        f'"{event.topic_title}" von {topic_creator_screen_name} '
        f'wieder gelöst: {url}'
    )


def assemble_text_for_board_topic_moved(
    event: BoardTopicMoved,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    topic_creator_screen_name = get_screen_name_or_fallback(
        event.topic_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} das Thema '
        f'"{event.topic_title}" von {topic_creator_screen_name} '
        f'aus "{event.old_category_title}" in "{event.new_category_title}" '
        f'verschoben: {url}'
    )


def assemble_text_for_board_posting_created(
    event: BoardPostingCreated,
    webhook_format: str,
) -> str:
    posting_creator_screen_name = get_screen_name_or_fallback(
        event.posting_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{posting_creator_screen_name} hat{board_label_segment} '
        f'auf das Thema "{event.topic_title}" geantwortet: {url}'
    )


def assemble_text_for_board_posting_hidden(
    event: BoardPostingHidden,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    posting_creator_screen_name = get_screen_name_or_fallback(
        event.posting_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} '
        f'eine Antwort von {posting_creator_screen_name} '
        f'im Thema "{event.topic_title}" versteckt: {url}'
    )


def assemble_text_for_board_posting_unhidden(
    event: BoardPostingUnhidden,
    webhook_format: str,
) -> str:
    moderator_screen_name = get_screen_name_or_fallback(
        event.moderator_screen_name
    )
    posting_creator_screen_name = get_screen_name_or_fallback(
        event.posting_creator_screen_name
    )
    board_label_segment = _get_board_label_segment(
        event.topic_id, webhook_format
    )
    url = _format_url(event.url, webhook_format)

    return (
        f'{moderator_screen_name} hat{board_label_segment} '
        f'eine Antwort von {posting_creator_screen_name} '
        f'im Thema "{event.topic_title}" wieder sichtbar gemacht: {url}'
    )


# helpers


def _get_board_label(topic_id: TopicID) -> str:
    topic = board_topic_query_service.find_topic_by_id(topic_id)
    brand_id = topic.category.board.brand_id
    brand = brand_service.find_brand(brand_id)
    return f'"{brand.title}"-Forum'


def _get_board_label_segment(topic_id: TopicID, webhook_format: str) -> str:
    if webhook_format != 'weitersager':
        return ''

    board_label = _get_board_label(topic_id)
    return f' im {board_label}'


def _format_url(url: str, webhook_format: str) -> str:
    return _escape_url_for_discord(url) if webhook_format == 'discord' else url


def _escape_url_for_discord(url: str) -> str:
    """Wrap URL in angle brackets (``<…>``) as that prevents preview
    embedding on Discord.
    """
    return f'<{url}>'