aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java
blob: 25d1319513d8c58b82fda517a3805681b2406e2a (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2012-2014 lacolaco.net
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.lacolaco.smileessence.twitter;

import net.lacolaco.smileessence.R;
import net.lacolaco.smileessence.data.Account;
import net.lacolaco.smileessence.entity.DirectMessage;
import net.lacolaco.smileessence.entity.Tweet;
import net.lacolaco.smileessence.entity.User;
import net.lacolaco.smileessence.logging.Logger;
import net.lacolaco.smileessence.notification.Notificator;
import net.lacolaco.smileessence.preference.UserPreferenceHelper;
import net.lacolaco.smileessence.viewmodel.EventViewModel;
import net.lacolaco.smileessence.viewmodel.MessageViewModel;
import net.lacolaco.smileessence.viewmodel.StatusViewModel;
import twitter4j.ConnectionLifeCycleListener;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;

public class UserStreamListener implements twitter4j.UserStreamListener, ConnectionLifeCycleListener {

    // ------------------------------ FIELDS ------------------------------

    private final Account account;
    private boolean connected = false;

    // --------------------------- CONSTRUCTORS ---------------------------

    public UserStreamListener(Account account) {
        this.account = account;
    }

    // --------------------- GETTER / SETTER METHODS ---------------------

    public boolean isConnected() {
        return connected;
    }

    // ------------------------ INTERFACE METHODS ------------------------


    // --------------------- Interface ConnectionLifeCycleListener ---------------------

    @Override
    public void onConnect() {
        connected = true;
        Notificator.getInstance().publish(R.string.notice_stream_connect);
    }

    @Override
    public void onDisconnect() {
        connected = false;
        Notificator.getInstance().publish(R.string.notice_stream_disconnect);
    }

    @Override
    public void onCleanUp() {
    }

    // --------------------- Interface StatusListener ---------------------

    @Override
    public void onStatus(Status status) {
        Tweet tweet = Tweet.fromTwitter(status, account.getUserId());
        StatusFilter.getInstance().filter(tweet);
        if (tweet.isRetweet()) {
            if (tweet.getOriginalTweet().getUser().getId() == account.getUserId()) {
                addToHistory(new EventViewModel(EventViewModel.EnumEvent.RETWEETED, tweet.getUser(), tweet));
            }
        } else if (tweet.getMentions().contains(account.getUser().getScreenName())) {
            EventViewModel mentioned = new EventViewModel(EventViewModel.EnumEvent.MENTIONED, tweet.getUser(), tweet);
            Notificator.getInstance().publish(mentioned.getFormattedString());
        }
    }

    @Override
    public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        StatusFilter.getInstance().remove(StatusViewModel.class, statusDeletionNotice.getStatusId());
        Tweet.remove(statusDeletionNotice.getStatusId());
    }

    @Override
    public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
    }

    @Override
    public void onScrubGeo(long userId, long upToStatusId) {
    }

    @Override
    public void onStallWarning(twitter4j.StallWarning warning) {
    }

    // --------------------- Interface StreamListener ---------------------

    @Override
    public void onException(Exception ex) {
        Logger.error(ex.toString());
    }

    // --------------------- Interface UserStreamListener ---------------------

    @Override
    public void onDeletionNotice(long directMessageId, long userId) {
        StatusFilter.getInstance().remove(MessageViewModel.class, directMessageId);
        DirectMessage.remove(directMessageId);
    }

    @Override
    public void onFriendList(long[] friendIds) {
    }

    @Override
    public void onFavorite(twitter4j.User source, twitter4j.User target, Status favoritedStatus) {
        Tweet tweet = Tweet.fromTwitter(favoritedStatus, account.getUserId());
        tweet.addFavoriter(source.getId());
        if (User.fromTwitter(target) == account.getUser()) {
            addToHistory(new EventViewModel(EventViewModel.EnumEvent.FAVORITED, User.fromTwitter(source), tweet));
        }
    }

    @Override
    public void onUnfavorite(twitter4j.User source, twitter4j.User target, twitter4j.Status unfavoritedStatus) {
        Tweet tweet = Tweet.fromTwitter(unfavoritedStatus, account.getUserId());
        tweet.removeFavoriter(source.getId());
        if (User.fromTwitter(target) == account.getUser()) {
            addToHistory(new EventViewModel(EventViewModel.EnumEvent.UNFAVORITED, User.fromTwitter(source), tweet));
        }
    }

    @Override
    public void onFollow(twitter4j.User source, twitter4j.User followedUser) {
        if (User.fromTwitter(followedUser) == account.getUser()) {
            addToHistory(new EventViewModel(EventViewModel.EnumEvent.FOLLOWED, User.fromTwitter(source)));
        }
    }

    @Override
    public void onUnfollow(twitter4j.User source, twitter4j.User unfollowedUser) {
    }

    @Override
    public void onDirectMessage(twitter4j.DirectMessage directMessage) {
        DirectMessage message = DirectMessage.fromTwitter(directMessage);
        StatusFilter.getInstance().filter(message);
        if (message.getRecipient() == account.getUser()) {
            addToHistory(new EventViewModel(EventViewModel.EnumEvent.RECEIVE_MESSAGE, User.fromTwitter(directMessage.getSender())));
        }
    }

    @Override
    public void onUserListMemberAddition(twitter4j.User addedMember, twitter4j.User listOwner, twitter4j.UserList list) {
    }

    @Override
    public void onUserListMemberDeletion(twitter4j.User deletedMember, twitter4j.User listOwner, twitter4j.UserList list) {
    }

    @Override
    public void onUserListSubscription(twitter4j.User subscriber, twitter4j.User listOwner, twitter4j.UserList list) {
        account.addListSubscription(list.getFullName());
    }

    @Override
    public void onUserListUnsubscription(twitter4j.User subscriber, twitter4j.User listOwner, twitter4j.UserList list) {
        account.removeListSubscription(list.getFullName());
    }

    @Override
    public void onUserListCreation(twitter4j.User listOwner, twitter4j.UserList list) {
        account.addListSubscription(list.getFullName());
    }

    @Override
    public void onUserListUpdate(twitter4j.User listOwner, twitter4j.UserList list) {
    }

    @Override
    public void onUserListDeletion(twitter4j.User listOwner, twitter4j.UserList list) {
        account.removeListSubscription(list.getFullName());
    }

    @Override
    public void onUserProfileUpdate(twitter4j.User updatedUser) {
        User.fromTwitter(updatedUser);
    }

    @Override
    public void onUserSuspension(long suspendedUser) {
    }

    @Override
    public void onUserDeletion(long deletedUser) {
    }

    @Override
    public void onBlock(twitter4j.User source, twitter4j.User blockedUser) {
        if (User.fromTwitter(blockedUser) == account.getUser()) {
            addToHistory(new EventViewModel(EventViewModel.EnumEvent.BLOCKED, User.fromTwitter(source)));
        }
    }

    @Override
    public void onUnblock(twitter4j.User source, twitter4j.User unblockedUser) {
        if (User.fromTwitter(unblockedUser) == account.getUser()) {
            addToHistory(new EventViewModel(EventViewModel.EnumEvent.UNBLOCKED, User.fromTwitter(source)));
        }
    }

    @Override
    public void onRetweetedRetweet(twitter4j.User source, twitter4j.User target, twitter4j.Status retweetedStatus) {
    }

    @Override
    public void onFavoritedRetweet(twitter4j.User source, twitter4j.User target, twitter4j.Status favoritedRetweeet) {
    }

    @Override
    public void onQuotedTweet(twitter4j.User source, twitter4j.User target, twitter4j.Status quotingTweet) {
    }

    private void addToHistory(EventViewModel mentioned) {
        StatusFilter.getInstance().filter(mentioned);
        Notificator.getInstance().publish(mentioned.getFormattedString());
    }
}