~homeworkprod/byceps

ref: 4d998d2b9cf6115f3511c3663e0ec46d1ca5118c byceps/byceps/announce/irc/board.py -rw-r--r-- 10.0 KiB
4d998d2b — Jochen Kupperschmidt Display article status stats as progress bar 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""
byceps.announce.irc.board
~~~~~~~~~~~~~~~~~~~~~~~~~

Announce board events on IRC.

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from ...blueprints.board import signals
from ...events.board import (
    BoardPostingCreated,
    BoardPostingHidden,
    BoardPostingUnhidden,
    BoardTopicCreated,
    BoardTopicHidden,
    BoardTopicLocked,
    BoardTopicMoved,
    BoardTopicPinned,
    BoardTopicUnhidden,
    BoardTopicUnlocked,
    BoardTopicUnpinned,
)
from ...services.board.models.topic import Topic as DbTopic
from ...services.board import (
    category_query_service as board_category_query_service,
    posting_query_service as board_posting_query_service,
    topic_query_service as board_topic_query_service,
)
from ...services.brand import service as brand_service
from ...services.user import service as user_service
from ...util.irc import send_message
from ...util.jobqueue import enqueue

from ._config import CHANNEL_ORGA_LOG, CHANNEL_PUBLIC


@signals.topic_created.connect
def _on_board_topic_created(sender, *, event: BoardTopicCreated = None) -> None:
    enqueue(announce_board_topic_created, event)


def announce_board_topic_created(event: BoardTopicCreated) -> None:
    """Announce that someone has created a board topic."""
    channels = [CHANNEL_ORGA_LOG, CHANNEL_PUBLIC]

    topic = board_topic_query_service.find_topic_by_id(event.topic_id)
    topic_creator = user_service.find_user(topic.creator_id)
    board_label = _get_board_label(topic)

    text = (
        f'{topic_creator.screen_name} hat im {board_label} '
        f'das Thema "{topic.title}" erstellt: {event.url}'
    )

    send_message(channels, text)


@signals.topic_hidden.connect
def _on_board_topic_hidden(sender, *, event: BoardTopicHidden = None) -> None:
    enqueue(announce_board_topic_hidden, event)


def announce_board_topic_hidden(event: BoardTopicHidden) -> None:
    """Announce that a moderator has hidden a board topic."""
    channels = [CHANNEL_ORGA_LOG]

    topic = board_topic_query_service.find_topic_by_id(event.topic_id)
    topic_creator = user_service.find_user(topic.creator_id)
    board_label = _get_board_label(topic)
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'das Thema "{topic.title}" von {topic_creator.screen_name} '
        f'versteckt: {event.url}'
    )

    send_message(channels, text)


@signals.topic_unhidden.connect
def _on_board_topic_unhidden(
    sender, *, event: BoardTopicUnhidden = None
) -> None:
    enqueue(announce_board_topic_unhidden, event)


def announce_board_topic_unhidden(event: BoardTopicUnhidden) -> None:
    """Announce that a moderator has made a board topic visible again."""
    channels = [CHANNEL_ORGA_LOG]

    topic = board_topic_query_service.find_topic_by_id(event.topic_id)
    topic_creator = user_service.find_user(topic.creator_id)
    board_label = _get_board_label(topic)
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'das Thema "{topic.title}" von {topic_creator.screen_name} '
        f'wieder sichtbar gemacht: {event.url}'
    )

    send_message(channels, text)


@signals.topic_locked.connect
def _on_board_topic_locked(sender, *, event: BoardTopicLocked = None) -> None:
    enqueue(announce_board_topic_locked, event)


def announce_board_topic_locked(event: BoardTopicLocked) -> None:
    """Announce that a moderator has locked a board topic."""
    channels = [CHANNEL_ORGA_LOG]

    topic = board_topic_query_service.find_topic_by_id(event.topic_id)
    topic_creator = user_service.find_user(topic.creator_id)
    board_label = _get_board_label(topic)
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'das Thema "{topic.title}" von {topic_creator.screen_name} '
        f'geschlossen: {event.url}'
    )

    send_message(channels, text)


@signals.topic_unlocked.connect
def _on_board_topic_unlocked(
    sender, *, event: BoardTopicUnlocked = None
) -> None:
    enqueue(announce_board_topic_unlocked, event)


def announce_board_topic_unlocked(event: BoardTopicUnlocked) -> None:
    """Announce that a moderator has unlocked a board topic."""
    channels = [CHANNEL_ORGA_LOG]

    topic = board_topic_query_service.find_topic_by_id(event.topic_id)
    topic_creator = user_service.find_user(topic.creator_id)
    board_label = _get_board_label(topic)
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'das Thema "{topic.title}" von {topic_creator.screen_name} '
        f'wieder geöffnet: {event.url}'
    )

    send_message(channels, text)


@signals.topic_pinned.connect
def _on_board_topic_pinned(sender, *, event: BoardTopicPinned = None) -> None:
    enqueue(announce_board_topic_pinned, event)


def announce_board_topic_pinned(event: BoardTopicPinned) -> None:
    """Announce that a moderator has pinned a board topic."""
    channels = [CHANNEL_ORGA_LOG]

    topic = board_topic_query_service.find_topic_by_id(event.topic_id)
    topic_creator = user_service.find_user(topic.creator_id)
    board_label = _get_board_label(topic)
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'das Thema "{topic.title}" von {topic_creator.screen_name} '
        f'angepinnt: {event.url}'
    )

    send_message(channels, text)


@signals.topic_unpinned.connect
def _on_board_topic_unpinned(
    sender, *, event: BoardTopicUnpinned = None
) -> None:
    enqueue(announce_board_topic_unpinned, event)


def announce_board_topic_unpinned(event: BoardTopicUnpinned) -> None:
    """Announce that a moderator has unpinned a board topic."""
    channels = [CHANNEL_ORGA_LOG]

    topic = board_topic_query_service.find_topic_by_id(event.topic_id)
    topic_creator = user_service.find_user(topic.creator_id)
    board_label = _get_board_label(topic)
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'das Thema "{topic.title}" von {topic_creator.screen_name} '
        f'wieder gelöst: {event.url}'
    )

    send_message(channels, text)


@signals.topic_moved.connect
def _on_board_topic_moved(sender, *, event: BoardTopicMoved = None) -> None:
    enqueue(announce_board_topic_moved, event)


def announce_board_topic_moved(event: BoardTopicMoved) -> None:
    """Announce that a moderator has moved a board topic to another category."""
    channels = [CHANNEL_ORGA_LOG]

    topic = board_topic_query_service.find_topic_by_id(event.topic_id)
    topic_creator = user_service.find_user(topic.creator_id)
    board_label = _get_board_label(topic)
    old_category = board_category_query_service.find_category_by_id(
        event.old_category_id
    )
    new_category = board_category_query_service.find_category_by_id(
        event.new_category_id
    )
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'das Thema "{topic.title}" von {topic_creator.screen_name} '
        f'aus "{old_category.title}" in "{new_category.title}" '
        f'verschoben: {event.url}'
    )

    send_message(channels, text)


@signals.posting_created.connect
def _on_board_posting_created(
    sender, *, event: BoardPostingCreated = None
) -> None:
    enqueue(announce_board_posting_created, event)


def announce_board_posting_created(event: BoardPostingCreated) -> None:
    """Announce that someone has created a board posting."""
    channels = [CHANNEL_ORGA_LOG, CHANNEL_PUBLIC]

    posting = board_posting_query_service.find_posting_by_id(event.posting_id)
    posting_creator = user_service.find_user(posting.creator_id)
    board_label = _get_board_label(posting.topic)

    if board_topic_query_service.get_topic(posting.topic_id).muted:
        return

    text = (
        f'{posting_creator.screen_name} hat im {board_label} '
        f'auf das Thema "{posting.topic.title}" geantwortet: {event.url}'
    )

    send_message(channels, text)


@signals.posting_hidden.connect
def _on_board_posting_hidden(
    sender, *, event: BoardPostingHidden = None
) -> None:
    enqueue(announce_board_posting_hidden, event)


def announce_board_posting_hidden(event: BoardPostingHidden) -> None:
    """Announce that a moderator has hidden a board posting."""
    channels = [CHANNEL_ORGA_LOG]

    posting = board_posting_query_service.find_posting_by_id(event.posting_id)
    posting_creator = user_service.find_user(posting.creator_id)
    board_label = _get_board_label(posting.topic)
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'eine Antwort von {posting_creator.screen_name} '
        f'im Thema "{posting.topic.title}" versteckt: {event.url}'
    )

    send_message(channels, text)


@signals.posting_unhidden.connect
def _on_board_posting_unhidden(
    sender, *, event: BoardPostingUnhidden = None
) -> None:
    enqueue(announce_board_posting_unhidden, event)


def announce_board_posting_unhidden(event: BoardPostingUnhidden) -> None:
    """Announce that a moderator has made a board posting visible again."""
    channels = [CHANNEL_ORGA_LOG]

    posting = board_posting_query_service.find_posting_by_id(event.posting_id)
    posting_creator = user_service.find_user(posting.creator_id)
    board_label = _get_board_label(posting.topic)
    moderator = user_service.find_user(event.moderator_id)

    text = (
        f'{moderator.screen_name} hat im {board_label} '
        f'eine Antwort von {posting_creator.screen_name} '
        f'im Thema "{posting.topic.title}" wieder sichtbar gemacht: {event.url}'
    )

    send_message(channels, text)


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