1
|
|
|
""" |
2
|
|
|
byceps.services.tourney.match_comment_service |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2019 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from datetime import datetime |
10
|
|
|
from typing import Dict, Optional, Sequence, Set |
11
|
|
|
|
12
|
|
|
from ...database import db |
13
|
|
|
from ...services.text_markup import service as text_markup_service |
14
|
|
|
from ...services.user import service as user_service |
15
|
|
|
from ...services.user.transfer.models import User |
16
|
|
|
from ...typing import PartyID, UserID |
17
|
|
|
|
18
|
|
|
from .models.match import MatchComment as DbMatchComment |
19
|
|
|
from .transfer.models import MatchID, MatchCommentID |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def get_comments( |
23
|
|
|
match_id: MatchID, |
24
|
|
|
*, |
25
|
|
|
party_id: Optional[PartyID] = None, |
26
|
|
|
include_hidden: bool = False, |
27
|
|
|
) -> Sequence[DbMatchComment]: |
28
|
|
|
"""Return comments on the match, ordered chronologically.""" |
29
|
|
|
query = DbMatchComment.query \ |
30
|
|
|
.for_match(match_id) |
31
|
|
|
|
32
|
|
|
if not include_hidden: |
33
|
|
|
query = query.filter_by(hidden=False) |
34
|
|
|
|
35
|
|
|
comments = query \ |
36
|
|
|
.for_match(match_id) \ |
37
|
|
|
.order_by(DbMatchComment.created_at) \ |
38
|
|
|
.all() |
39
|
|
|
|
40
|
|
|
# Add creator objects. |
41
|
|
|
creator_ids = {comment.created_by_id for comment in comments} |
42
|
|
|
creators_by_id = _get_users_by_id(creator_ids, party_id=party_id) |
43
|
|
|
for comment in comments: |
44
|
|
|
comment.creator = creators_by_id[comment.created_by_id] |
45
|
|
|
|
46
|
|
|
# Add rendered bodies. |
47
|
|
|
for comment in comments: |
48
|
|
|
comment.body_rendered = text_markup_service.render_html(comment.body) |
49
|
|
|
|
50
|
|
|
return comments |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def _get_users_by_id( |
54
|
|
|
user_ids: Set[UserID], *, party_id: Optional[PartyID] = None |
55
|
|
|
) -> Dict[UserID, User]: |
56
|
|
|
users = user_service.find_users( |
57
|
|
|
user_ids, include_avatars=True, include_orga_flags_for_party_id=party_id |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
return user_service.index_users_by_id(users) |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def find_comment(comment_id: MatchCommentID) -> DbMatchComment: |
64
|
|
|
"""Return match comment.""" |
65
|
|
|
return DbMatchComment.query.get(comment_id) |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def create_comment( |
69
|
|
|
match_id: MatchID, creator_id: UserID, body: str |
70
|
|
|
) -> DbMatchComment: |
71
|
|
|
"""Create a comment on a match.""" |
72
|
|
|
comment = DbMatchComment(match_id, creator_id, body) |
73
|
|
|
|
74
|
|
|
db.session.add(comment) |
75
|
|
|
db.session.commit() |
76
|
|
|
|
77
|
|
|
return comment |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def hide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None: |
81
|
|
|
"""Hide the match comment.""" |
82
|
|
|
comment = DbMatchComment.query.get(comment_id) |
83
|
|
|
if comment is None: |
84
|
|
|
raise ValueError('Unknown match comment ID') |
85
|
|
|
|
86
|
|
|
comment.hidden = True |
87
|
|
|
comment.hidden_at = datetime.utcnow() |
88
|
|
|
comment.hidden_by_id = initiator_id |
89
|
|
|
|
90
|
|
|
db.session.commit() |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def unhide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None: |
94
|
|
|
"""Un-hide the match comment.""" |
95
|
|
|
comment = DbMatchComment.query.get(comment_id) |
96
|
|
|
if comment is None: |
97
|
|
|
raise ValueError('Unknown match comment ID') |
98
|
|
|
|
99
|
|
|
comment.hidden = False |
100
|
|
|
comment.hidden_at = None |
101
|
|
|
comment.hidden_by_id = None |
102
|
|
|
|
103
|
|
|
db.session.commit() |
104
|
|
|
|