Don’t set insert URI of a Sone, let it be generated from the identity.
[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.data.Album.FLATTENER;
23 import static net.pterodactylus.sone.data.Album.IMAGES;
24
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.Set;
30
31 import javax.annotation.Nonnegative;
32 import javax.annotation.Nonnull;
33 import javax.annotation.Nullable;
34
35 import net.pterodactylus.sone.freenet.wot.Identity;
36 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
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) && (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) && (sone.getIdentity() instanceof OwnIdentity);
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) && !sone.getRootAlbum().getAlbums().isEmpty();
172                 }
173         };
174
175         public static final Function<Sone, String> toSoneXmlUri =
176                         new Function<Sone, String>() {
177                                 @Nonnull
178                                 @Override
179                                 public String apply(@Nullable Sone input) {
180                                         return input.getRequestUri()
181                                                         .setMetaString(new String[] { "sone.xml" })
182                                                         .toString();
183                                 }
184                         };
185
186         public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
187                 @Override
188                 public List<Album> apply(@Nullable Sone sone) {
189                         return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
190                                         sone.getRootAlbum());
191                 }
192         };
193
194         public static final Function<Sone, List<Image>> toAllImages = new Function<Sone, List<Image>>() {
195                 @Override
196                 public List<Image> apply(@Nullable Sone sone) {
197                         return (sone == null) ? Collections.<Image>emptyList() :
198                                         from(FLATTENER.apply(sone.getRootAlbum()))
199                                                         .transformAndConcat(IMAGES).toList();
200                 }
201         };
202
203         /**
204          * Returns the identity of this Sone.
205          *
206          * @return The identity of this Sone
207          */
208         Identity getIdentity();
209
210         /**
211          * Returns the name of this Sone.
212          *
213          * @return The name of this Sone
214          */
215         String getName();
216
217         /**
218          * Returns whether this Sone is a local Sone.
219          *
220          * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
221          */
222         boolean isLocal();
223
224         /**
225          * Returns the request URI of this Sone.
226          *
227          * @return The request URI of this Sone
228          */
229         FreenetURI getRequestUri();
230
231         /**
232          * Sets the request URI of this Sone.
233          *
234          * @param requestUri
235          *              The request URI of this Sone
236          * @return This Sone (for method chaining)
237          */
238         Sone setRequestUri(FreenetURI requestUri);
239
240         /**
241          * Returns the insert URI of this Sone.
242          *
243          * @return The insert URI of this Sone
244          */
245         FreenetURI getInsertUri();
246
247         /**
248          * Returns the latest edition of this Sone.
249          *
250          * @return The latest edition of this Sone
251          */
252         long getLatestEdition();
253
254         /**
255          * Sets the latest edition of this Sone. If the given latest edition is not
256          * greater than the current latest edition, the latest edition of this Sone is
257          * not changed.
258          *
259          * @param latestEdition
260          *              The latest edition of this Sone
261          */
262         void setLatestEdition(long latestEdition);
263
264         /**
265          * Return the time of the last inserted update of this Sone.
266          *
267          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
268          */
269         long getTime();
270
271         /**
272          * Sets the time of the last inserted update of this Sone.
273          *
274          * @param time
275          *              The time of the update (in milliseconds since Jan 1, 1970 UTC)
276          * @return This Sone (for method chaining)
277          */
278         Sone setTime(long time);
279
280         /**
281          * Returns the status of this Sone.
282          *
283          * @return The status of this Sone
284          */
285         SoneStatus getStatus();
286
287         /**
288          * Sets the new status of this Sone.
289          *
290          * @param status
291          *              The new status of this Sone
292          * @return This Sone
293          * @throws IllegalArgumentException
294          *              if {@code status} is {@code null}
295          */
296         Sone setStatus(SoneStatus status);
297
298         /**
299          * Returns a copy of the profile. If you want to update values in the profile
300          * of this Sone, update the values in the returned {@link Profile} and use
301          * {@link #setProfile(Profile)} to change the profile in this Sone.
302          *
303          * @return A copy of the profile
304          */
305         Profile getProfile();
306
307         /**
308          * Sets the profile of this Sone. A copy of the given profile is stored so that
309          * subsequent modifications of the given profile are not reflected in this
310          * Sone!
311          *
312          * @param profile
313          *              The profile to set
314          */
315         void setProfile(Profile profile);
316
317         /**
318          * Returns the client used by this Sone.
319          *
320          * @return The client used by this Sone, or {@code null}
321          */
322         Client getClient();
323
324         /**
325          * Sets the client used by this Sone.
326          *
327          * @param client
328          *              The client used by this Sone, or {@code null}
329          * @return This Sone (for method chaining)
330          */
331         Sone setClient(Client client);
332
333         /**
334          * Returns whether this Sone is known.
335          *
336          * @return {@code true} if this Sone is known, {@code false} otherwise
337          */
338         boolean isKnown();
339
340         /**
341          * Sets whether this Sone is known.
342          *
343          * @param known
344          *              {@code true} if this Sone is known, {@code false} otherwise
345          * @return This Sone
346          */
347         Sone setKnown(boolean known);
348
349         /**
350          * Returns all friend Sones of this Sone.
351          *
352          * @return The friend Sones of this Sone
353          */
354         List<String> getFriends();
355
356         /**
357          * Returns whether this Sone has the given Sone as a friend Sone.
358          *
359          * @param friendSoneId
360          *              The ID of the Sone to check for
361          * @return {@code true} if this Sone has the given Sone as a friend, {@code
362          *         false} otherwise
363          */
364         boolean hasFriend(String friendSoneId);
365
366         /**
367          * Adds the given Sone as a friend Sone.
368          *
369          * @param friendSone
370          *              The friend Sone to add
371          * @return This Sone (for method chaining)
372          */
373         Sone addFriend(String friendSone);
374
375         /**
376          * Removes the given Sone as a friend Sone.
377          *
378          * @param friendSoneId
379          *              The ID of the friend Sone to remove
380          * @return This Sone (for method chaining)
381          */
382         Sone removeFriend(String friendSoneId);
383
384         /**
385          * Returns the list of posts of this Sone, sorted by time, newest first.
386          *
387          * @return All posts of this Sone
388          */
389         List<Post> getPosts();
390
391         /**
392          * Sets all posts of this Sone at once.
393          *
394          * @param posts
395          *              The new (and only) posts of this Sone
396          * @return This Sone (for method chaining)
397          */
398         Sone setPosts(Collection<Post> posts);
399
400         /**
401          * Adds the given post to this Sone. The post will not be added if its {@link
402          * Post#getSone() Sone} is not this Sone.
403          *
404          * @param post
405          *              The post to add
406          */
407         void addPost(Post post);
408
409         /**
410          * Removes the given post from this Sone.
411          *
412          * @param post
413          *              The post to remove
414          */
415         void removePost(Post post);
416
417         /**
418          * Returns all replies this Sone made.
419          *
420          * @return All replies this Sone made
421          */
422         Set<PostReply> getReplies();
423
424         /**
425          * Sets all replies of this Sone at once.
426          *
427          * @param replies
428          *              The new (and only) replies of this Sone
429          * @return This Sone (for method chaining)
430          */
431         Sone setReplies(Collection<PostReply> replies);
432
433         /**
434          * Adds a reply to this Sone. If the given reply was not made by this Sone,
435          * nothing is added to this Sone.
436          *
437          * @param reply
438          *              The reply to add
439          */
440         void addReply(PostReply reply);
441
442         /**
443          * Removes a reply from this Sone.
444          *
445          * @param reply
446          *              The reply to remove
447          */
448         void removeReply(PostReply reply);
449
450         /**
451          * Returns the IDs of all liked posts.
452          *
453          * @return All liked posts’ IDs
454          */
455         Set<String> getLikedPostIds();
456
457         /**
458          * Sets the IDs of all liked posts.
459          *
460          * @param likedPostIds
461          *              All liked posts’ IDs
462          * @return This Sone (for method chaining)
463          */
464         Sone setLikePostIds(Set<String> likedPostIds);
465
466         /**
467          * Checks whether the given post ID is liked by this Sone.
468          *
469          * @param postId
470          *              The ID of the post
471          * @return {@code true} if this Sone likes the given post, {@code false}
472          *         otherwise
473          */
474         boolean isLikedPostId(String postId);
475
476         /**
477          * Adds the given post ID to the list of posts this Sone likes.
478          *
479          * @param postId
480          *              The ID of the post
481          * @return This Sone (for method chaining)
482          */
483         Sone addLikedPostId(String postId);
484
485         /**
486          * Removes the given post ID from the list of posts this Sone likes.
487          *
488          * @param postId
489          *              The ID of the post
490          * @return This Sone (for method chaining)
491          */
492         Sone removeLikedPostId(String postId);
493
494         /**
495          * Returns the IDs of all liked replies.
496          *
497          * @return All liked replies’ IDs
498          */
499         Set<String> getLikedReplyIds();
500
501         /**
502          * Sets the IDs of all liked replies.
503          *
504          * @param likedReplyIds
505          *              All liked replies’ IDs
506          * @return This Sone (for method chaining)
507          */
508         Sone setLikeReplyIds(Set<String> likedReplyIds);
509
510         /**
511          * Checks whether the given reply ID is liked by this Sone.
512          *
513          * @param replyId
514          *              The ID of the reply
515          * @return {@code true} if this Sone likes the given reply, {@code false}
516          *         otherwise
517          */
518         boolean isLikedReplyId(String replyId);
519
520         /**
521          * Adds the given reply ID to the list of replies this Sone likes.
522          *
523          * @param replyId
524          *              The ID of the reply
525          * @return This Sone (for method chaining)
526          */
527         Sone addLikedReplyId(String replyId);
528
529         /**
530          * Removes the given post ID from the list of replies this Sone likes.
531          *
532          * @param replyId
533          *              The ID of the reply
534          * @return This Sone (for method chaining)
535          */
536         Sone removeLikedReplyId(String replyId);
537
538         /**
539          * Returns the root album that contains all visible albums of this Sone.
540          *
541          * @return The root album of this Sone
542          */
543         Album getRootAlbum();
544
545         /**
546          * Returns Sone-specific options.
547          *
548          * @return The options of this Sone
549          */
550         SoneOptions getOptions();
551
552         /**
553          * Sets the options of this Sone.
554          *
555          * @param options
556          *              The options of this Sone
557          */
558         /* TODO - remove this method again, maybe add an option provider */
559         void setOptions(SoneOptions options);
560
561 }