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