Move unliking a post from Sone to Post.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
1 /*
2  * Sone - Sone.java - Copyright © 2010–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
20 import static com.google.common.collect.FluentIterable.from;
21 import static java.util.Arrays.asList;
22 import static net.pterodactylus.sone.core.SoneUri.create;
23 import static net.pterodactylus.sone.data.Album.FLATTENER;
24 import static net.pterodactylus.sone.data.Album.IMAGES;
25
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Set;
31
32 import net.pterodactylus.sone.core.Options;
33 import net.pterodactylus.sone.database.AlbumBuilder;
34 import net.pterodactylus.sone.database.PostBuilder;
35 import net.pterodactylus.sone.database.PostReplyBuilder;
36 import net.pterodactylus.sone.freenet.wot.Identity;
37 import net.pterodactylus.sone.template.SoneAccessor;
38
39 import freenet.keys.FreenetURI;
40
41 import com.google.common.base.Function;
42 import com.google.common.base.Predicate;
43 import com.google.common.primitives.Ints;
44
45 /**
46  * A Sone defines everything about a user: her profile, her status updates, her
47  * replies, her likes and dislikes, etc.
48  *
49  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50  */
51 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
52
53         /**
54          * Enumeration for the possible states of a {@link Sone}.
55          *
56          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57          */
58         public enum SoneStatus {
59
60                 /** The Sone is unknown, i.e. not yet downloaded. */
61                 unknown,
62
63                 /** The Sone is idle, i.e. not being downloaded or inserted. */
64                 idle,
65
66                 /** The Sone is currently being inserted. */
67                 inserting,
68
69                 /** The Sone is currently being downloaded. */
70                 downloading,
71         }
72
73         /**
74          * The possible values for the “show custom avatars” option.
75          *
76          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
77          */
78         public static enum ShowCustomAvatars {
79
80                 /** Never show custom avatars. */
81                 NEVER,
82
83                 /** Only show custom avatars of followed Sones. */
84                 FOLLOWED,
85
86                 /** Only show custom avatars of Sones you manually trust. */
87                 MANUALLY_TRUSTED,
88
89                 /** Only show custom avatars of automatically trusted Sones. */
90                 TRUSTED,
91
92                 /** Always show custom avatars. */
93                 ALWAYS,
94
95         }
96
97         /** comparator that sorts Sones by their nice name. */
98         public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
99
100                 @Override
101                 public int compare(Sone leftSone, Sone rightSone) {
102                         int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
103                         if (diff != 0) {
104                                 return diff;
105                         }
106                         return leftSone.getId().compareToIgnoreCase(rightSone.getId());
107                 }
108
109         };
110
111         /** Comparator that sorts Sones by last activity (least recent active first). */
112         public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
113
114                 @Override
115                 public int compare(Sone firstSone, Sone secondSone) {
116                         return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
117                 }
118         };
119
120         /** Comparator that sorts Sones by numbers of posts (descending). */
121         public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
122
123                 /**
124                  * {@inheritDoc}
125                  */
126                 @Override
127                 public int compare(Sone leftSone, Sone rightSone) {
128                         return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
129                 }
130         };
131
132         /** Comparator that sorts Sones by number of images (descending). */
133         public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
134
135                 /**
136                  * {@inheritDoc}
137                  */
138                 @Override
139                 public int compare(Sone leftSone, Sone rightSone) {
140                         int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
141                         int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
142                         /* sort descending. */
143                         return Ints.compare(rightSoneImageCount, leftSoneImageCount);
144                 }
145         };
146
147         /** Filter to remove Sones that have not been downloaded. */
148         public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
149
150                 @Override
151                 public boolean apply(Sone sone) {
152                         return (sone == null) ? false : sone.getTime() != 0;
153                 }
154         };
155
156         /** Filter that matches all {@link Sone#isLocal() local Sones}. */
157         public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
158
159                 @Override
160                 public boolean apply(Sone sone) {
161                         return (sone == null) ? false : sone.isLocal();
162                 }
163
164         };
165
166         /** Filter that matches Sones that have at least one album. */
167         public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
168
169                 @Override
170                 public boolean apply(Sone sone) {
171                         return (sone == null) ? false : !sone.getRootAlbum().getAlbums().isEmpty();
172                 }
173         };
174
175         public static final Function<Sone, FreenetURI> TO_FREENET_URI = new Function<Sone, FreenetURI>() {
176                 @Override
177                 public FreenetURI apply(Sone sone) {
178                         return (sone == null) ? null : create(sone.getIdentity().getRequestUri());
179                 }
180         };
181
182         public static final Function<Sone, List<Post>> TO_POSTS = new Function<Sone, List<Post>>() {
183                 @Override
184                 public List<Post> apply(Sone sone) {
185                         return (sone == null) ? Collections.<Post>emptyList() : sone.getPosts();
186                 }
187         };
188
189         /**
190          * Returns the identity of this Sone.
191          *
192          * @return The identity of this Sone
193          */
194         Identity getIdentity();
195
196         /**
197          * Returns the name of this Sone.
198          *
199          * @return The name of this Sone
200          */
201         String getName();
202
203         /**
204          * Returns whether this Sone is a local Sone.
205          *
206          * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
207          */
208         boolean isLocal();
209
210         /**
211          * Returns the latest edition of this Sone.
212          *
213          * @return The latest edition of this Sone
214          */
215         long getLatestEdition();
216
217         /**
218          * Return the time of the last inserted update of this Sone.
219          *
220          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
221          */
222         long getTime();
223
224         /**
225          * Sets the time of the last inserted update of this Sone.
226          *
227          * @param time
228          *              The time of the update (in milliseconds since Jan 1, 1970 UTC)
229          * @return This Sone (for method chaining)
230          */
231         Sone setTime(long time);
232
233         /**
234          * Returns the status of this Sone.
235          *
236          * @return The status of this Sone
237          */
238         SoneStatus getStatus();
239
240         /**
241          * Sets the new status of this Sone.
242          *
243          * @param status
244          *              The new status of this Sone
245          * @return This Sone
246          * @throws IllegalArgumentException
247          *              if {@code status} is {@code null}
248          */
249         Sone setStatus(SoneStatus status);
250
251         /**
252          * Returns a copy of the profile. If you want to update values in the profile
253          * of this Sone, update the values in the returned {@link Profile} and use
254          * {@link #setProfile(Profile)} to change the profile in this Sone.
255          *
256          * @return A copy of the profile
257          */
258         Profile getProfile();
259
260         /**
261          * Sets the profile of this Sone. A copy of the given profile is stored so that
262          * subsequent modifications of the given profile are not reflected in this
263          * Sone!
264          *
265          * @param profile
266          *              The profile to set
267          */
268         void setProfile(Profile profile);
269
270         /**
271          * Returns the client used by this Sone.
272          *
273          * @return The client used by this Sone, or {@code null}
274          */
275         Client getClient();
276
277         /**
278          * Returns whether this Sone is known.
279          *
280          * @return {@code true} if this Sone is known, {@code false} otherwise
281          */
282         boolean isKnown();
283
284         /**
285          * Sets whether this Sone is known.
286          *
287          * @param known
288          *              {@code true} if this Sone is known, {@code false} otherwise
289          * @return This Sone
290          */
291         Sone setKnown(boolean known);
292
293         /**
294          * Returns all friend Sones of this Sone.
295          *
296          * @return The friend Sones of this Sone
297          */
298         Collection<String> getFriends();
299
300         /**
301          * Returns whether this Sone has the given Sone as a friend Sone.
302          *
303          * @param friendSoneId
304          *              The ID of the Sone to check for
305          * @return {@code true} if this Sone has the given Sone as a friend, {@code
306          *         false} otherwise
307          */
308         boolean hasFriend(String friendSoneId);
309
310         /**
311          * Adds the given Sone as a friend Sone.
312          *
313          * @param friendSone
314          *              The friend Sone to add
315          * @return This Sone (for method chaining)
316          */
317         Sone addFriend(String friendSone);
318
319         /**
320          * Removes the given Sone as a friend Sone.
321          *
322          * @param friendSoneId
323          *              The ID of the friend Sone to remove
324          * @return This Sone (for method chaining)
325          */
326         Sone removeFriend(String friendSoneId);
327
328         /**
329          * Returns the list of posts of this Sone, sorted by time, newest first.
330          *
331          * @return All posts of this Sone
332          */
333         List<Post> getPosts();
334
335         /**
336          * Sets all posts of this Sone at once.
337          *
338          * @param posts
339          *              The new (and only) posts of this Sone
340          * @return This Sone (for method chaining)
341          */
342         Sone setPosts(Collection<Post> posts);
343
344         /**
345          * Adds the given post to this Sone. The post will not be added if its {@link
346          * Post#getSone() Sone} is not this Sone.
347          *
348          * @param post
349          *              The post to add
350          */
351         void addPost(Post post);
352
353         /**
354          * Removes the given post from this Sone.
355          *
356          * @param post
357          *              The post to remove
358          */
359         void removePost(Post post);
360
361         /**
362          * Returns all replies this Sone made.
363          *
364          * @return All replies this Sone made
365          */
366         Set<PostReply> getReplies();
367
368         /**
369          * Sets all replies of this Sone at once.
370          *
371          * @param replies
372          *              The new (and only) replies of this Sone
373          * @return This Sone (for method chaining)
374          */
375         Sone setReplies(Collection<PostReply> replies);
376
377         /**
378          * Adds a reply to this Sone. If the given reply was not made by this Sone,
379          * nothing is added to this Sone.
380          *
381          * @param reply
382          *              The reply to add
383          */
384         void addReply(PostReply reply);
385
386         /**
387          * Removes a reply from this Sone.
388          *
389          * @param reply
390          *              The reply to remove
391          */
392         void removeReply(PostReply reply);
393
394         /**
395          * Returns the IDs of all liked posts.
396          *
397          * @return All liked posts’ IDs
398          */
399         Set<String> getLikedPostIds();
400
401         /**
402          * Sets the IDs of all liked posts.
403          *
404          * @param likedPostIds
405          *              All liked posts’ IDs
406          * @return This Sone (for method chaining)
407          */
408         Sone setLikePostIds(Set<String> likedPostIds);
409
410         /**
411          * Checks whether the given post ID is liked by this Sone.
412          *
413          * @param postId
414          *              The ID of the post
415          * @return {@code true} if this Sone likes the given post, {@code false}
416          *         otherwise
417          */
418         boolean isLikedPostId(String postId);
419
420         /**
421          * Returns the IDs of all liked replies.
422          *
423          * @return All liked replies’ IDs
424          */
425         Set<String> getLikedReplyIds();
426
427         /**
428          * Sets the IDs of all liked replies.
429          *
430          * @param likedReplyIds
431          *              All liked replies’ IDs
432          * @return This Sone (for method chaining)
433          */
434         Sone setLikeReplyIds(Set<String> likedReplyIds);
435
436         /**
437          * Checks whether the given reply ID is liked by this Sone.
438          *
439          * @param replyId
440          *              The ID of the reply
441          * @return {@code true} if this Sone likes the given reply, {@code false}
442          *         otherwise
443          */
444         boolean isLikedReplyId(String replyId);
445
446         /**
447          * Adds the given reply ID to the list of replies this Sone likes.
448          *
449          * @param replyId
450          *              The ID of the reply
451          * @return This Sone (for method chaining)
452          */
453         Sone addLikedReplyId(String replyId);
454
455         /**
456          * Removes the given post ID from the list of replies this Sone likes.
457          *
458          * @param replyId
459          *              The ID of the reply
460          * @return This Sone (for method chaining)
461          */
462         Sone removeLikedReplyId(String replyId);
463
464         /**
465          * Returns the root album that contains all visible albums of this Sone.
466          *
467          * @return The root album of this Sone
468          */
469         Album getRootAlbum();
470
471         /**
472          * Returns Sone-specific options.
473          *
474          * @return The options of this Sone
475          */
476         Options getOptions();
477
478         /**
479          * Sets the options of this Sone.
480          *
481          * @param options
482          *              The options of this Sone
483          */
484         /* TODO - remove this method again, maybe add an option provider */
485         void setOptions(Options options);
486
487         AlbumBuilder newAlbumBuilder() throws IllegalStateException;
488
489         PostBuilder newPostBuilder();
490
491         PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;
492
493         Modifier modify();
494
495         interface Modifier {
496
497                 Modifier setLatestEdition(long latestEdition);
498                 Sone update();
499
500         }
501
502 }