Skip to content

Loading…

working implementation of Subscription #290

Merged
merged 1 commit into from

2 participants

@t1mmyl33

No description provided.

@amshali amshali merged commit 424d7d9 into w7cook:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 12, 2012
  1. @t1mmyl33

    master

    t1mmyl33 committed
View
28 app/controllers/Subscriptions.java
@@ -10,10 +10,34 @@
@With(Secure.class)
public class Subscriptions extends OBController {
- public static void addSubscription(Long id) {
-
+ public static void addSubscription(Long userId) {
+ User user = user();
+ User other = User.findById(userId);
+ if(other == null)
+ notFound();
+ Subscription s = Subscription.find("SELECT s FROM Subscription s WHERE s.subscriber = ?1 AND s.subscribed = ?2", user, other).first();
+ if(s == null) {
+ s = new Subscription(other, user).save();
+ user.subscribedTo.add(s);
+ other.subscribers.add(s);
+ user.save();
+ other.save();
+ }
+ subscriptions(other.id);
}
+ public static void removeSubscription(Long userId) {
+ User user = user();
+ User other = User.findById(userId);
+ if(other == null)
+ notFound();
+ List<Subscription> s = Subscription.find("SELECT s FROM Subscription s WHERE s.subscriber = ?1 AND s.subscribed = ?2", user, other).fetch();
+ for(int x = s.size()-1;x >= 0;x--) {
+ s.get(x).delete();
+ ok();
+ }
+ subscriptions(other.id);
+ }
public static void subscriptions(Long id) {
User user = id == null ? user() : (User) User.findById(id);
render(user);
View
18 app/models/User.java
@@ -118,6 +118,8 @@ public User(String email, String password, String username) {
this.username = username;
this.friends = new HashSet<User>();
this.friendRequests = new HashSet<User>();
+ this.subscribedTo = new ArrayList<Subscription>();
+ this.subscribers = new ArrayList<Subscription>();
this.save();
friends.add(this);
@@ -138,6 +140,8 @@ public User(String email, String password, String username, String first_name, S
this.friends = new HashSet<User>();
this.friendRequests = new HashSet<User>();
+ this.subscribedTo = new ArrayList<Subscription>();
+ this.subscribers = new ArrayList<Subscription>();
this.save();
friends.add(this);
@@ -161,6 +165,8 @@ public User(TempUser user) {
this.friends = new HashSet<User>();
this.friendRequests = new HashSet<User>();
+ this.subscribedTo = new ArrayList<Subscription>();
+ this.subscribers = new ArrayList<Subscription>();
this.save();
friends.add(this);
@@ -207,6 +213,18 @@ public long unreadCount() {
return Post.find("SELECT p FROM Post p, IN(p.owner.subscribers) u WHERE u.subscriber.id = ?1 and p.postedObj.id = u.subscribed.id order by p.updatedAt desc",
this.id).fetch();
}
+
+ public List<User> subscribedTo() {
+ return Subscription.find("SELECT s FROM Subscription s WHERE s.subscriber = ?", this).fetch();
+ }
+
+ public List<User> subscribers() {
+ return Subscription.find("SELECT s FROM Subscription s WHERE s.subscribed = ?", this).fetch();
+ }
+
+ public boolean isSubscribed(User user) {
+ return !(Subscription.find("SELECT s FROM Subscription s WHERE s.subscribed = ?1 AND s.subscriber = ?2", user, this).first() == null);
+ }
public void createTimeline() {
if (this.timeline == null) {
View
85 app/views/Subscriptions/subscriptions.html
@@ -14,86 +14,7 @@
#{/list}
</div>
#{list items:user.subscriptionNews(), as:'item'}
- <div class="post">
- <div class="hideable">
- <div class="left">
- <img src=@{Thumbnails.get50x50(item.owner.profile.profilePhoto.id)} width="50" height="50" />
- </div>
- <div class="tohide hide">
- <div class="post-content">${item.content.markdown().nl2br()}</div>
- #{list items:item.getComments(), as:'comment'}
- <div class="comment">
- <div class="comment-metadata">
- <div class="comment-author">by ${comment.owner},</div>
- <div class="comment-date">
- ${comment.updatedAt.format('dd MMM yy')}
- </div>
- #{if comment.owner == currentUser.first_name}
- <a class="button" href="@{Comments.deleteComment(comment.id,user.id)}">delete</a>
- #{/if}
- #{ if comment.currentUserLiked()}
- <a class="button" href="@{Comments.unLike(comment.id,user.id)}"> Unlike</a>
- #{/if}
- #{else}
- <a class="button" href="@{Comments.addLike(comment.id,user.id)}">Like</a>
- #{/else}
- <div>${comment.likes.size()} likes</div>
- </div>
- <div class="comment-content">
- ${comment.content.escape().nl2br()}
- </div>
- </div>
- #{/list}
- <a class='triggerhide' href='#' onclick='return false;'>
- -less<br />
- </a>
- </div>
-
- <div class="individual-post-metadata">
- ${item.likes.size()}
- #{if user.isFriendsWith(currentUser) || user==currentUser}
- #{ if item.currentUserLiked()}
- <a href="@{Application.unLike(item.id,user.id)}">Unlike</a>
- #{/if}
- #{else}
- <a href="@{Application.addLike(item.id,user.id)}">Like</a>
- #{/else}
- #{/if}
-
- . <a href="#" onclick="showDiv('comment ${item.id}')">Comment</a> .
- ${item.updatedAt.format('dd MMM yy')}
- #{if item.owner == currentUser}
- . <a href="@{Posts.deletePost(item.id,user.id)}">delete my post</a>
- #{/if}
-
- <div id="comment ${item.id}" style="clear: both;"
- class="comment-textarea">
- #{form @Comments.postComment(item.id, user.id, commentContent)}
- <div style="clear: both;">
- <textarea name="commentContent" id="commentContent"
- style="width: 420px; padding: 2px; height: 20px; display: block; font-family: serif; resize: none; overflow-y: hidden;"></textarea>
- </div>
- <input type="submit" value="Comment"
- style="clear: right; margin-top: 3px;" />
- #{/form}
- </div>
- </div>
- </div>
- </div>
- </div>
- #{/list}
-
- <script>
- $('textarea#postContent').autoResize({
- maxHeight: 150,
- minHeight: 50,
- extraSpace: 30
- });
- $('textarea#commentContent').autoResize({
- maxHeight: 70,
- minHeight: 20,
- extraSpace: 20
- });
- </script>
+ #{post user:user,item:item /}
+ #{/list}
+ </div>
</div>
-
View
8 app/views/Timeline/Timeline.html
@@ -1,4 +1,4 @@
-#{extends 'three_panel.html' /}
+#{extends 'three_panel.html' /}
<div id="contentArea-AutoPage">
<h1>${user.first_name} ${user.last_name}</h1>
@@ -56,6 +56,12 @@ <h4 class="alert-heading">${user.first_name} wants to be friends with you.</h4>
You have asked ${user.first_name} to be friends.
<button class="btn btn-warning btn-mini" onclick="unfriend(${user.id})">Retract</button>
</div>
+ <div id="subscribedTo" class="#{if currentUser.isSubscribed(user)}hide#{/if}">
+ <button class="btn btn-warning btn-mini" onclick="unfriend(${user.id})">Subscribe</button>
+ </div>
+ <div id="subscribed" class="#{if !currentUser.isSubscribed(user)}hide#{/if}">
+ <button class="btn btn-success btn-mini" onclick="unfriend(${user.id})">Subscribed</button>
+ </div>
</div>
#{/if}
View
13 app/views/Users/user.html
@@ -1,4 +1,4 @@
-#{extends 'main.html' /}
+#{extends 'main.html' /}
<div class="span2">
<div class="profile-pic">
#{if (user.profile.profilePhoto.id == -1 || user.profile.profilePhoto == null)}
@@ -22,7 +22,7 @@
<div class="span10" id="postsList">
#{if !currentUser.equals(user)}
<div id="friends" class="#{if !currentUser.friends.contains(user)}hide#{/if}">
- You and ${user.first_name} are freinds.
+ You and ${user.first_name} are friends.
<button class="btn btn-warning btn-mini" onclick="unfriend(${user.id})">unfriend</button>
</div>
<div id="not-friends" class="#{if currentUser.friends.contains(user) || user.friendRequests.contains(currentUser) || currentUser.friendRequests.contains(user)}hide#{/if}">
@@ -36,9 +36,15 @@ <h4 class="alert-heading">${user.first_name} wants to be friends with you.</h4>
<button class="btn btn-warning btn-mini span1" onclick="unfriend(${user.id})">reject</button>
</div>
<div id="pending-friends" class="#{if !user.friendRequests.contains(currentUser)}hide#{/if}">
- You have asked ${user.first_name} to be freinds.
+ You have asked ${user.first_name} to be friends.
<button class="btn btn-warning btn-mini" onclick="unfriend(${user.id})">Retract</button>
</div>
+ <div id="subscribedTo" class="#{if currentUser.isSubscribed(user)}hide#{/if}">
+ <button class="btn btn-warning btn-mini" onclick="subscribe(${user.id})">Subscribe</button>
+ </div>
+ <div id="subscribed" class="#{if !currentUser.isSubscribed(user)}hide#{/if}">
+ <button class="btn btn-success btn-mini" onclick="unsubscribe(${user.id})">Unsubscribe</button>
+ </div>
#{/if}
#{list items:user.posts, as:'item'}
#{post user:currentUser, item:item /}
@@ -47,3 +53,4 @@ <h4 class="alert-heading">${user.first_name} wants to be friends with you.</h4>
<script src="/public/javascripts/post.js"></script>
<script src="/public/javascripts/friends.js"></script>
+<script src="/public/javascripts/subscribe.js"></script>
View
2 conf/routes
@@ -23,6 +23,8 @@ GET /users/{<[0-9]+>userId}/profile Users.profile
GET /users/{<[0-9]+>userId}/friends Users.friends
POST /users/{<[0-9]+>userId}/friends Users.requestFriends
DELETE /users/{<[0-9]+>userId}/friends Users.removeFriends
+POST /users/{<[0-9]+>userId}/subscribe Subscriptions.addSubscription
+DELETE /users/{<[0-9]+>userId}/subscribe Subscriptions.removeSubscription
GET /users/{<[0-9]+>userId}/requests Users.friendRequests
GET /users/{<[0-9]+>ownerId}/photos/? Photos.photos
GET /users/{<[0-9]+>userId}/posts/? Posts.posts #get all posts by a user
View
27 public/javascripts/subscribe.js
@@ -0,0 +1,27 @@
+function subscribe(userId) {
+ $.ajax({
+ url: '/users/' + userId + '/subscribe',
+ type: 'POST',
+ success: function(data, textStatus, jqXHR) {
+ $('#subscribedTo').hide();
+ $('#subscribed').fadeIn();
+ },
+ error: function(jqXHR, textStatus, errorType) {
+ alert(errorType);
+ }
+ });
+}
+
+function unsubscribe(userId) {
+ $.ajax({
+ url: '/users/' + userId + '/subscribe',
+ type: 'DELETE',
+ success: function(data, textStatus, jqXHR) {
+ $('#subscribed').hide();
+ $('#subscribedTo').fadeIn();
+ },
+ error: function(jqXHR, textStatus, errorType) {
+ alert(errorType);
+ }
+ });
+}
Something went wrong with that request. Please try again.