1# Copyright (C) 2021 by the Free Software Foundation, Inc.
2#
3# This file is part of mailman.client.
4#
5# mailman.client is free software: you can redistribute it and/or modify it
6# under the terms of the GNU Lesser General Public License as published by the
7# Free Software Foundation, version 3 of the License.
8#
9# mailman.client is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
12# License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public License
15# along with mailman.client.  If not, see <http://www.gnu.org/licenses/>.
16
17"""Async Mailinglist object.."""
18
19__all__ = [
20    'MailingList',
21]
22
23from enum import Enum
24from typing import List
25from mailmanclient.restbase.async_base import RESTObject
26from mailmanclient.asyncobjects.member import Member
27from mailmanclient.restobjects.utils import list_of_objects
28from mailmanclient.restobjects.types import ConnectionProto, ContentType
29
30
31class MemberRole(Enum):
32    """Member role values."""
33    member = 'member'
34    owner = 'owner'
35    moderator = 'moderator'
36    nonmember = 'nonmember'
37
38
39class MailingList(RESTObject):
40
41    _properties = ('advertised', 'display_name', 'fqdn_listname', 'list_id',
42                   'list_name', 'mail_host', 'member_count', 'volume',
43                   'self_link', 'description')
44
45    def __repr__(self) -> str:
46        return '<MailingList {}>'.format(self.fqdn_listname)
47
48    async def config(self) -> 'Config':
49        """Get MailingList settings.
50
51        /<api>/lists/<listid>/config
52        """
53        path = 'lists/{}/config'.format(self.list_id)
54        _, content = await self._connection.call(path)
55        return Config(self, self._connection, content)
56
57    async def get_roster(self, role) -> List[Member]:
58        """Get MailingList roster.
59
60        /<api>/lists/<listid>/roster/<role>
61        """
62        path = 'lists/{}/roster/{}'.format(self.fqdn_listname, role)
63        _, content = await self._connection.call(path)
64        return list_of_objects(Member, content, self._connection)
65
66    async def members(self) -> List[Member]:
67        """Get Mailinglist members (subscribers.)
68
69        /<api>/lists/<listid>/roster/member
70        """
71        return await self.get_roster(MemberRole.member)
72
73    async def owners(self) -> List[Member]:
74        """Get Mailinglist owners.
75
76        /<api>/lists/<listid>/roster/owner
77        """
78        return await self.get_roster(MemberRole.owner)
79
80    async def moderators(self) -> List[Member]:
81        """Get Mailinglist moderators.
82
83        /<api>/lists/<listid>/roster/moderator
84        """
85        return await self.get_roster(MemberRole.moderator)
86
87    async def nonmember(self) -> List[Member]:
88        """Get Mailinglist nonmembers.
89
90        /<api>/lists/<listid>/roster/nonmember
91        """
92        return await self.get_roster(MemberRole.nonmember)
93
94
95class Config(RESTObject):
96
97    _read_only_properties = (
98        'bounces_address',
99        'created_at',
100        'digest_last_sent_at',
101        'fqdn_listname',
102        'join_address',
103        'last_post_at',
104        'leave_address',
105        'list_id',
106        'list_name',
107        'mail_host',
108        'next_digest_number',
109        'no_reply_address',
110        'owner_address',
111        'post_id',
112        'posting_address',
113        'request_address',
114        'scheme',
115        'self_link',
116        'volume',
117        'web_host',
118        )
119
120    def __init__(self,
121                 mailing_list: MailingList,
122                 connection: ConnectionProto,
123                 data: ContentType) -> None:
124        super().__init__(connection, data)
125        self.mailing_list = mailing_list
126
127    def __repr__(self) -> str:
128        return '<Settings for {}>'.format(self.mailing_list.fqdn_listname)
129