Passed
Push — master ( 69ad40...f1cd88 )
by Jochen
02:35
created

byceps.services.tourney.match_comment_service   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 10

5 Functions

Rating   Name   Duplication   Size   Complexity  
A get_comments() 0 26 4
A create_comment() 0 10 1
A _get_users_by_id() 0 8 1
A hide_comment() 0 11 2
A unhide_comment() 0 11 2
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, 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, party_id: PartyID, *, include_hidden: bool = False
24
) -> Sequence[DbMatchComment]:
25
    """Return comments on the match, ordered chronologically."""
26
    query = DbMatchComment.query \
27
        .for_match(match_id)
28
29
    if not include_hidden:
30
        query = query.filter_by(hidden=False)
31
32
    comments = query \
33
        .for_match(match_id) \
34
        .order_by(DbMatchComment.created_at) \
35
        .all()
36
37
    # Add creator objects.
38
    creator_ids = {comment.created_by_id for comment in comments}
39
    creators_by_id = _get_users_by_id(creator_ids, party_id)
40
    for comment in comments:
41
        comment.creator = creators_by_id[comment.created_by_id]
42
43
    # Add rendered bodies.
44
    for comment in comments:
45
        comment.body_rendered = text_markup_service.render_html(comment.body)
46
47
    return comments
48
49
50
def _get_users_by_id(
51
    user_ids: Set[UserID], party_id: PartyID
52
) -> Dict[UserID, User]:
53
    users = user_service.find_users(
54
        user_ids, include_avatars=True, include_orga_flags_for_party_id=party_id
55
    )
56
57
    return user_service.index_users_by_id(users)
58
59
60
def create_comment(
61
    match_id: MatchID, creator_id: UserID, body: str
62
) -> DbMatchComment:
63
    """Create a comment on a match."""
64
    comment = DbMatchComment(match_id, creator_id, body)
65
66
    db.session.add(comment)
67
    db.session.commit()
68
69
    return comment
70
71
72
def hide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None:
73
    """Hide the match comment."""
74
    comment = DbMatchComment.query.get(comment_id)
75
    if comment is None:
76
        raise ValueError('Unknown match comment ID')
77
78
    comment.hidden = True
79
    comment.hidden_at = datetime.utcnow()
80
    comment.hidden_by_id = initiator_id
81
82
    db.session.commit()
83
84
85
def unhide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None:
86
    """Un-hide the match comment."""
87
    comment = DbMatchComment.query.get(comment_id)
88
    if comment is None:
89
        raise ValueError('Unknown match comment ID')
90
91
    comment.hidden = False
92
    comment.hidden_at = None
93
    comment.hidden_by_id = None
94
95
    db.session.commit()
96