~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/tourney/match_comment_service.py -rw-r--r-- 5.6 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 11 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
"""
byceps.services.tourney.match_comment_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from datetime import datetime
from typing import Dict, Optional, Sequence, Set

from ...database import db
from ...services.text_markup import service as text_markup_service
from ...services.user import service as user_service
from ...services.user.transfer.models import User
from ...typing import PartyID, UserID

from .models.match_comment import MatchComment as DbMatchComment
from .transfer.models import MatchID, MatchComment, MatchCommentID


def find_comment(
    comment_id: MatchCommentID, *, party_id: Optional[PartyID] = None
) -> Optional[MatchComment]:
    """Return the comment, or `None` if not found."""
    comment = DbMatchComment.query.get(comment_id)

    if comment is None:
        return None

    # creator
    creator = _get_user(comment.created_by_id, party_id)

    # last editor
    last_editor = None
    if comment.last_edited_by_id:
        last_editor = _get_user(comment.last_edited_by_id, party_id)

    # moderator
    moderator = None
    if comment.hidden_by_id:
        moderator = _get_user(comment.hidden_by_id, party_id)

    return _db_entity_to_comment(
        comment, creator, last_editor=last_editor, moderator=moderator,
    )


def get_comment(
    comment_id: MatchCommentID, *, party_id: Optional[PartyID] = None
) -> MatchComment:
    """Return the comment.

    Raise exception if comment ID is unknown.
    """
    comment = find_comment(comment_id)

    if comment is None:
        raise ValueError('Unknown match comment ID')

    return comment


def get_comments(
    match_id: MatchID,
    *,
    party_id: Optional[PartyID] = None,
    include_hidden: bool = False,
) -> Sequence[MatchComment]:
    """Return comments on the match, ordered chronologically."""
    query = DbMatchComment.query \
        .for_match(match_id)

    if not include_hidden:
        query = query.filter_by(hidden=False)

    db_comments = query \
        .for_match(match_id) \
        .order_by(DbMatchComment.created_at) \
        .all()

    # creators
    creator_ids = {comment.created_by_id for comment in db_comments}
    creators_by_id = _get_users_by_id(creator_ids, party_id=party_id)

    # last editors
    last_editor_ids = {
        comment.last_edited_by_id
        for comment in db_comments
        if comment.last_edited_by_id
    }
    last_editors_by_id = _get_users_by_id(last_editor_ids, party_id=party_id)

    # moderators
    moderator_ids = {
        comment.hidden_by_id for comment in db_comments if comment.hidden_by_id
    }
    moderators_by_id = _get_users_by_id(moderator_ids, party_id=party_id)

    comments = []
    for db_comment in db_comments:
        creator = creators_by_id[db_comment.created_by_id]
        last_editor = last_editors_by_id.get(db_comment.last_edited_by_id)
        moderator = moderators_by_id.get(db_comment.hidden_by_id)

        comment = _db_entity_to_comment(
            db_comment, creator, last_editor=last_editor, moderator=moderator,
        )
        comments.append(comment)

    return comments


def _get_users_by_id(
    user_ids: Set[UserID], *, party_id: Optional[PartyID] = None
) -> Dict[UserID, User]:
    users = user_service.find_users(
        user_ids, include_avatars=True, include_orga_flags_for_party_id=party_id
    )

    return user_service.index_users_by_id(users)


def create_comment(
    match_id: MatchID, creator_id: UserID, body: str
) -> DbMatchComment:
    """Create a comment on a match."""
    comment = DbMatchComment(match_id, creator_id, body)

    db.session.add(comment)
    db.session.commit()

    return get_comment(comment.id)


def update_comment(
    comment_id: MatchCommentID, editor_id: UserID, body: str
) -> DbMatchComment:
    """Update a comment on a match."""
    comment = _get_db_comment(comment_id)

    comment.body = body
    comment.last_edited_at = datetime.utcnow()
    comment.last_edited_by_id = editor_id

    db.session.commit()

    return get_comment(comment.id)


def hide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None:
    """Hide the match comment."""
    comment = _get_db_comment(comment_id)

    comment.hidden = True
    comment.hidden_at = datetime.utcnow()
    comment.hidden_by_id = initiator_id

    db.session.commit()


def unhide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None:
    """Un-hide the match comment."""
    comment = _get_db_comment(comment_id)

    comment.hidden = False
    comment.hidden_at = None
    comment.hidden_by_id = None

    db.session.commit()


def _get_db_comment(comment_id: MatchCommentID) -> DbMatchComment:
    """Return the comment as database entity.

    Raise exception if comment ID is unknown.
    """
    comment = DbMatchComment.query.get(comment_id)

    if comment is None:
        raise ValueError('Unknown match comment ID')

    return comment


def _get_user(user_id: UserID, party_id: Optional[PartyID] = None) -> User:
    return user_service.get_user(
        user_id, include_avatar=True, include_orga_flag_for_party_id=party_id
    )


def _db_entity_to_comment(
    comment: DbMatchComment,
    creator: User,
    *,
    last_editor: Optional[User],
    moderator: Optional[User],
) -> MatchComment:
    body_html = text_markup_service.render_html(comment.body)

    return MatchComment(
        comment.id,
        comment.match_id,
        comment.created_at,
        creator,
        comment.body,
        body_html,
        comment.last_edited_at,
        last_editor,
        comment.hidden,
        comment.hidden_at,
        moderator,
    )