Completed
Push — master ( fbed40...6fa740 )
by Jochen
05:48
created

get_comment()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
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 find_comment(comment_id: MatchCommentID) -> DbMatchComment:
23
    """Return the comment, or `None` if not found."""
24
    return DbMatchComment.query.get(comment_id)
25
26
27
def get_comment(comment_id: MatchCommentID) -> DbMatchComment:
28
    """Return the comment.
29
30
    Raise exception if comment ID is unknown.
31
    """
32
    comment = find_comment(comment_id)
33
34
    if comment is None:
35
        raise ValueError('Unknown match comment ID')
36
37
    return comment
38
39
40
def get_comments(
41
    match_id: MatchID,
42
    *,
43
    party_id: Optional[PartyID] = None,
44
    include_hidden: bool = False,
45
) -> Sequence[DbMatchComment]:
46
    """Return comments on the match, ordered chronologically."""
47
    query = DbMatchComment.query \
48
        .for_match(match_id)
49
50
    if not include_hidden:
51
        query = query.filter_by(hidden=False)
52
53
    comments = query \
54
        .for_match(match_id) \
55
        .order_by(DbMatchComment.created_at) \
56
        .all()
57
58
    # Add creator objects.
59
    creator_ids = {comment.created_by_id for comment in comments}
60
    creators_by_id = _get_users_by_id(creator_ids, party_id=party_id)
61
    for comment in comments:
62
        comment.creator = creators_by_id[comment.created_by_id]
63
64
    # Add rendered bodies.
65
    for comment in comments:
66
        comment.body_rendered = text_markup_service.render_html(comment.body)
67
68
    return comments
69
70
71
def _get_users_by_id(
72
    user_ids: Set[UserID], *, party_id: Optional[PartyID] = None
73
) -> Dict[UserID, User]:
74
    users = user_service.find_users(
75
        user_ids, include_avatars=True, include_orga_flags_for_party_id=party_id
76
    )
77
78
    return user_service.index_users_by_id(users)
79
80
81
def find_comment(comment_id: MatchCommentID) -> DbMatchComment:
82
    """Return match comment."""
83
    return DbMatchComment.query.get(comment_id)
84
85
86
def create_comment(
87
    match_id: MatchID, creator_id: UserID, body: str
88
) -> DbMatchComment:
89
    """Create a comment on a match."""
90
    comment = DbMatchComment(match_id, creator_id, body)
91
92
    db.session.add(comment)
93
    db.session.commit()
94
95
    return comment
96
97
98
def update_comment(
99
    comment_id: MatchCommentID, editor_id: UserID, body: str
100
) -> DbMatchComment:
101
    """Update a comment on a match."""
102
    comment = get_comment(comment_id)
103
104
    comment.body = body
105
    comment.last_edited_at = datetime.utcnow()
106
    comment.last_edited_by_id = editor_id
107
108
    db.session.commit()
109
110
    return comment
111
112
113
def hide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None:
114
    """Hide the match comment."""
115
    comment = get_comment(comment_id)
116
117
    comment.hidden = True
118
    comment.hidden_at = datetime.utcnow()
119
    comment.hidden_by_id = initiator_id
120
121
    db.session.commit()
122
123
124
def unhide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None:
125
    """Un-hide the match comment."""
126
    comment = get_comment(comment_id)
127
128
    comment.hidden = False
129
    comment.hidden_at = None
130
    comment.hidden_by_id = None
131
132
    db.session.commit()
133