File: person.py

package info (click to toggle)
python-pypump 0.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: python: 3,153; makefile: 134
file content (219 lines) | stat: -rw-r--r-- 7,140 bytes parent folder | download | duplicates (4)
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
214
215
216
217
218
219
##
# Copyright (C) 2013 Jessica T. (Tsyesika) <xray7224@googlemail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##

import six

from pypump.models import PumpObject, Addressable
from pypump.exceptions import PyPumpException
from pypump.models.feed import (Followers, Following, Lists,
                                Favorites, Inbox, Outbox)


class Person(PumpObject, Addressable):
    """ This object represents a pump.io **person**,
    a person is a user on the pump.io network.

    :param webfinger: User ID in ``nickname@hostname`` format.

    Example:
        >>> alice = pump.Person('alice@example.org')
        >>> print(alice.summary)
        Hi, I'm Alice
        >>> mynote = pump.Note('Hey Alice, it's Bob!')
        >>> mynote.to = alice
        >>> mynote.send()
    """

    object_type = 'person'
    _ignore_attr = ['liked', 'in_reply_to']
    _mapping = {
        "username": "preferredUsername",
        "location": "location",
    }

    _inbox = None
    _outbox = None
    _followers = None
    _following = None
    _favorites = None
    _lists = None

    @property
    def outbox(self):
        """ :class:`Outbox feed <pypump.models.feed.Outbox>` with all
        :class:`activities <pypump.models.activity.Activity>` sent by the person.

        Example:
            >>> for activity in pump.me.outbox[:2]:
            ...     print(activity)
            ...
            pypumptest2 unliked a comment in reply to a note
            pypumptest2 deleted a note
        """
        self._outbox = self._outbox or Outbox(self.links['activity-outbox'], pypump=self._pump)
        return self._outbox

    @property
    def followers(self):
        """ :class:`Feed <pypump.models.feed.Feed>` with all
        :class:`Person <pypump.models.person.Person>` objects following the person.

        Example:
            >>> alice = pump.Person('alice@example.org')
            >>> for follower in alice.followers[:2]:
            ...     print(follower.id)
            ...
            acct:bob@example.org
            acct:carol@example.org
        """
        self._followers = self._followers or Followers(self.links['followers'], pypump=self._pump)
        return self._followers

    @property
    def following(self):
        """ :class:`Feed <pypump.models.feed.Feed>` with all
        :class:`Person <pypump.models.person.Person>` objects followed by the person.

        Example:
            >>> bob = pump.Person('bob@example.org')
            >>> for followee in bob.following[:3]:
            ...     print(followee.id)
            ...
            acct:alice@example.org
            acct:duncan@example.org
        """
        self._following = self._following or Following(self.links['following'], pypump=self._pump)
        return self._following

    @property
    def favorites(self):
        """ :class:`Feed <pypump.models.feed.Feed>` with all objects
        liked/favorited by the person.

        Example:
            >>> for like in pump.me.favorites[:3]:
            ...     print(like)
            ...
            note by alice@example.org
            image by bob@example.org
            comment by evan@e14n.com
        """
        self._favorites = self._favorites or Favorites(self.links['favorites'], pypump=self._pump)
        return self._favorites

    @property
    def lists(self):
        """ :class:`Lists feed <pypump.models.feed.Lists>` with all lists
        owned by the person.

        Example:
            >>> for list in pump.me.lists:
            ...     print(list)
            ...
            Acquaintances
            Family
            Coworkers
            Friends
        """
        self._lists = self._lists or Lists(self.links['lists'], pypump=self._pump)
        return self._lists

    @property
    def inbox(self):
        """ :class:`Inbox feed <pypump.models.feed.Inbox>` with all
        :class:`activities <pypump.models.activity.Activity>`
        received by the person, can only be read if logged in as the owner.

        Example:
            >>> for activity in pump.me.inbox[:2]:
            ...     print(activity.id)
            ...
            https://microca.st/api/activity/BvqXQOwXShSey1HxYuJQBQ
            https://pumpyourself.com/api/activity/iQGdnz5-T-auXnbUUdXh-A
        """
        if not self.isme:
            raise PyPumpException("You can't read other people's inboxes")
        self._inbox = self._inbox or Inbox(self.links['activity-inbox'], pypump=self._pump)
        return self._inbox

    @property
    def webfinger(self):
        return self.id.replace("acct:", "")

    @property
    def server(self):
        return self.id.split("@")[-1]

    @property
    def isme(self):
        return (self.username == self._pump.client.nickname and self.server == self._pump.client.server)

    def __init__(self, webfinger=None, **kwargs):
        super(Person, self).__init__(**kwargs)

        if isinstance(webfinger, six.string_types):
            if "@" not in webfinger:  # TODO do better validation
                raise PyPumpException("Not a valid webfinger: %s" % webfinger)

            self.id = "acct:{0}".format(webfinger)
            self.username = webfinger.split("@")[0]

            self._add_link('self', "{0}://{1}/api/user/{2}/profile".format(
                self._pump.protocol, self.server, self.username)
            )
            try:
                data = self._pump.request(self.links['self'])
                self.unserialize(data)
            except:
                pass

    def serialize(self, verb):
        data = super(Person, self).serialize()
        data.update({
            "verb": verb,
            "object": {
                "id": self.id,
                "objectType": self.object_type,
                "displayName": self.display_name,
                "summary": self.summary,
                "location": self.location.serialize()
            }
        })

        return data

    def follow(self):
        """ Follow person """
        self._verb('follow')

    def unfollow(self):
        """ Unfollow person """
        self._verb('stop-following')

    def update(self):
        """ Updates person object"""
        data = self.serialize(verb="update")
        self._post_activity(data)

    def __repr__(self):
        return "<{type}: {webfinger}>".format(
            type=self.object_type.capitalize(),
            webfinger=getattr(self, 'webfinger', 'unknown')
        )

    def __unicode__(self):
        return u"{0}".format(self.display_name or self.username or self.webfinger)