unread count sync

This commit is contained in:
Jeremie Panzer
2013-03-22 09:29:30 +01:00
parent 680d87fe9b
commit 25f0871195
16 changed files with 319 additions and 29 deletions

View File

@@ -2,12 +2,15 @@ package com.commafeed.backend.dao;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.TypedQuery;
import com.commafeed.model.Feed;
import com.commafeed.model.FeedEntry;
import com.commafeed.model.User;
@Stateless
public class FeedEntryService extends GenericDAO<FeedEntry, String> {
@@ -28,4 +31,13 @@ public class FeedEntryService extends GenericDAO<FeedEntry, String> {
em.merge(feed);
}
public List<FeedEntry> getUnreadEntries(Feed feed, User user) {
String query = "select entry from FeedEntry entry where entry.feed = :feed and not in (select status.entry from FeedEntryStatus status where status.user = :user and status.read = true)";
TypedQuery<FeedEntry> typedQuery = em.createQuery(query,
FeedEntry.class);
typedQuery.setParameter("feed", feed);
typedQuery.setParameter("user", user);
return typedQuery.getResultList();
}
}

View File

@@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import com.commafeed.frontend.components.auth.LoginPage;
import com.commafeed.frontend.components.auth.LogoutPage;
import com.commafeed.frontend.pages.home.HomePage;
import com.commafeed.frontend.rest.FeedSubscriptionsREST;
import com.commafeed.frontend.utils.exception.DisplayExceptionPage;
public class CommaFeedApplication extends AuthenticatedWebApplication {
@@ -42,6 +43,8 @@ public class CommaFeedApplication extends AuthenticatedWebApplication {
mountPage("login", LoginPage.class);
mountPage("logout", LogoutPage.class);
mountPage("error", DisplayExceptionPage.class);
mountPage("subscriptions", FeedSubscriptionsREST.class);
setupInjection();

View File

@@ -2,6 +2,7 @@ package com.commafeed.frontend.components.auth;
import org.apache.wicket.markup.html.WebPage;
@SuppressWarnings("serial")
public class LogoutPage extends WebPage {
public LogoutPage() {
getSession().invalidate();

View File

@@ -15,10 +15,12 @@
<script src="vendor/jquery/jquery-1.9.1.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="vendor/angular/angular.min.js"></script>
<script src="vendor/angular/angular-resource.min.js"></script>
<script src="vendor/angular-ui/angular-ui.min.js"></script>
<script src="vendor/angular-ui-bootstrap/ui-bootstrap-tpls-0.2.0.min.js"></script>
<script src="js/main.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>
<script src="js/services.js"></script>
</body>
</html>

View File

@@ -7,6 +7,7 @@ import javax.inject.Inject;
import org.apache.commons.lang.ObjectUtils;
import com.commafeed.backend.dao.FeedCategoryService;
import com.commafeed.backend.dao.FeedEntryService;
import com.commafeed.backend.dao.FeedSubscriptionService;
import com.commafeed.frontend.CommaFeedSession;
import com.commafeed.frontend.pages.JSONPage;
@@ -25,12 +26,14 @@ public class FeedSubscriptionsREST extends JSONPage {
@Inject
FeedCategoryService FeedCategoryService;
@Inject
FeedEntryService feedEntryService;
User user = CommaFeedSession.get().getUser();
@Override
protected Object getObject() {
User user = CommaFeedSession.get().getUser();
List<FeedCategory> categories = FeedCategoryService.findAll(user);
Category root = new Category();
addChildren(categories, root);
for (FeedSubscription subscription : feedSubscriptionService
@@ -39,7 +42,10 @@ public class FeedSubscriptionsREST extends JSONPage {
Subscription sub = new Subscription();
sub.setId(subscription.getId());
sub.setName(subscription.getTitle());
sub.setUnread(77);
int size = feedEntryService.getUnreadEntries(
subscription.getFeed(), user).size();
sub.setUnread(size);
root.getFeeds().add(sub);
}
return root;
}
@@ -58,7 +64,10 @@ public class FeedSubscriptionsREST extends JSONPage {
Subscription sub = new Subscription();
sub.setId(subscription.getId());
sub.setName(subscription.getTitle());
sub.setUnread(77);
int size = feedEntryService.getUnreadEntries(
subscription.getFeed(), user).size();
sub.setUnread(size);
child.getFeeds().add(sub);
}
current.getChildren().add(child);
}

View File

@@ -0,0 +1,46 @@
package com.commafeed.frontend.rest.model;
import java.util.List;
import com.commafeed.frontend.rest.FeedSubscriptionsREST.Subscription;
import com.google.common.collect.Lists;
public class Category {
private Long id;
private String name;
private List<Category> children = Lists.newArrayList();
private List<Subscription> feeds = Lists.newArrayList();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Category> getChildren() {
return children;
}
public void setChildren(List<Category> children) {
this.children = children;
}
public List<Subscription> getFeeds() {
return feeds;
}
public void setFeeds(List<Subscription> feeds) {
this.feeds = feeds;
}
}

View File

@@ -0,0 +1,25 @@
package com.commafeed.frontend.rest.model;
import java.util.List;
public class Entries {
private String name;
private List<Entry> entries;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Entry> getEntries() {
return entries;
}
public void setEntries(List<Entry> entries) {
this.entries = entries;
}
}

View File

@@ -0,0 +1,86 @@
package com.commafeed.frontend.rest.model;
public class Entry {
private String id;
private String title;
private String content;
private String date;
private String feedId;
private String feedName;
private String url;
private boolean read;
private boolean starred;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getFeedId() {
return feedId;
}
public void setFeedId(String feedId) {
this.feedId = feedId;
}
public String getFeedName() {
return feedName;
}
public void setFeedName(String feedName) {
this.feedName = feedName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isRead() {
return read;
}
public void setRead(boolean read) {
this.read = read;
}
public boolean isStarred() {
return starred;
}
public void setStarred(boolean starred) {
this.starred = starred;
}
}

View File

@@ -0,0 +1,32 @@
package com.commafeed.frontend.rest.model;
public class Subscription {
private Long id;
private String name;
private int unread;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnread() {
return unread;
}
public void setUnread(int unread) {
this.unread = unread;
}
}

View File

@@ -10,7 +10,6 @@
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
<property name="toplink.ddl-generation" value="create-tables"/>
<property name="toplink.cache.shared.default" value="false" />
<property name="eclipselink.weaving" value="false" />
</properties>

View File

@@ -15,10 +15,12 @@
<script src="vendor/jquery/jquery-1.9.1.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="vendor/angular/angular.min.js"></script>
<script src="vendor/angular/angular-resource.min.js"></script>
<script src="vendor/angular-ui/angular-ui.min.js"></script>
<script src="vendor/angular-ui-bootstrap/ui-bootstrap-tpls-0.2.0.min.js"></script>
<script src="js/main.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>
<script src="js/services.js"></script>
</body>
</html>

View File

@@ -1,10 +1,18 @@
var module = angular.module('commafeed.controllers', []);
module.run(function($rootScope) {
$rootScope.$on('emitMarkAsRead', function(event, args) {
$rootScope.$broadcast('markAsRead', args);
});
});
module.controller('CategoryTreeCtrl',
function($scope, $routeParams, $location) {
function($scope, $routeParams, $location, CategoryService) {
$scope.selectedType = $routeParams._type;
$scope.selectedId = $routeParams._id;
$scope.root2 = CategoryService.get();
$scope.root = {
children : [ {
@@ -75,31 +83,70 @@ module.controller('CategoryTreeCtrl',
$scope.categoryClicked = function(id) {
$location.path('/feeds/view/category/' + id);
};
var markAsRead = function(children, entry, read) {
for ( var i = 0; i < children.length; i++) {
var child = children[i];
if (child.children) {
markAsRead(child.children, entry, read);
}
if (child.feeds) {
for ( var j = 0; j < child.feeds.length; j++) {
var feed = child.feeds[j];
console.log(entry.feedId)
if (feed.id == entry.feedId) {
var c = read ? -1 : 1;
console.log(c)
feed.unread = feed.unread + c;
}
}
}
}
};
$scope.$on('markAsRead', function(event, args) {
markAsRead($scope.root.children, args.entry, args.read)
});
});
module.controller('FeedListCtrl', function($scope, $routeParams, $http) {
$scope.entries = [ {
id : '1',
title : 'my title',
content : 'my content',
date : 'my date',
feed : 'my feed',
url : 'my url',
read : false,
starred : false,
}, {
id : '1',
title : 'my title',
content : 'my content',
date : 'my date',
feed : 'my feed',
url : 'my url',
read : false,
starred : false,
} ];
$scope.selectedType = $routeParams._type;
$scope.selectedId = $routeParams._id;
$scope.entryList = {
name : 'aaa',
entries : [ {
id : '1',
title : 'my title',
content : 'my content',
date : 'my date',
feedId : '1',
feedName : 'my feed',
url : 'my url',
read : false,
starred : false,
}, {
id : '2',
title : 'my other title',
content : 'my other content',
date : 'my other date',
feedId : '2',
feedName : 'my other feed',
url : 'my other url',
read : false,
starred : false,
} ]
};
$scope.markAsRead = function(entry) {
var read = entry.read;
entry.read = true;
if (entry.read != read) {
$scope.$emit('emitMarkAsRead', {
entry : entry,
read : true
});
}
};
});

View File

@@ -1,4 +1,6 @@
var app = angular.module('commafeed', [ 'ui', 'ui.bootstrap', 'commafeed.directives', 'commafeed.controllers' ]);
var app = angular
.module('commafeed', [ 'ui', 'ui.bootstrap', 'commafeed.directives',
'commafeed.controllers', 'commafeed.services' ]);
app.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/feeds/view/:_type/:_id', {

View File

@@ -0,0 +1,13 @@
var module = angular.module('commafeed.services', [ 'ngResource' ]);
module.factory('CategoryService', [ '$resource', '$http',
function($resource, $http) {
var actions = {
'get' : {
method : 'GET'
}
}
res = $resource('subscriptions', {}, actions);
return res
} ]);

View File

@@ -13,12 +13,13 @@
</div>
</div>
<div class="span10" ng-controller="FeedListCtrl">
<span>{{entryList.name}}</span><span ng-show="selectedType == 'category'">&#187;</span>
<div class="accordion" id="feed-accordion">
<div class="accordion-group" ng-repeat="entry in entries">
<div class="accordion-group" ng-repeat="entry in entryList.entries">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" ng-click="markAsRead(entry)" ng-class="{unread: entry.read == false}"
data-parent="#feed-accordion" href="{{'#feed-body' + $index}}">
{{entry.title}}
<span ng-show="selectedType == 'category'">{{entry.feedName}} - </span>{{entry.title}}
</a>
</div>
<div id="{{'feed-body' + $index}}" class="accordion-body collapse">

View File

@@ -0,0 +1,10 @@
/*
AngularJS v1.0.5
(c) 2010-2012 Google, Inc. http://angularjs.org
License: MIT
*/
(function(C,d,w){'use strict';d.module("ngResource",["ng"]).factory("$resource",["$http","$parse",function(x,y){function s(b,e){return encodeURIComponent(b).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(e?null:/%20/g,"+")}function t(b,e){this.template=b+="#";this.defaults=e||{};var a=this.urlParams={};h(b.split(/\W/),function(f){f&&RegExp("(^|[^\\\\]):"+f+"\\W").test(b)&&(a[f]=!0)});this.template=b.replace(/\\:/g,":")}function u(b,e,a){function f(m,a){var b=
{},a=o({},e,a);h(a,function(a,z){var c;a.charAt&&a.charAt(0)=="@"?(c=a.substr(1),c=y(c)(m)):c=a;b[z]=c});return b}function g(a){v(a||{},this)}var k=new t(b),a=o({},A,a);h(a,function(a,b){a.method=d.uppercase(a.method);var e=a.method=="POST"||a.method=="PUT"||a.method=="PATCH";g[b]=function(b,c,d,B){var j={},i,l=p,q=null;switch(arguments.length){case 4:q=B,l=d;case 3:case 2:if(r(c)){if(r(b)){l=b;q=c;break}l=c;q=d}else{j=b;i=c;l=d;break}case 1:r(b)?l=b:e?i=b:j=b;break;case 0:break;default:throw"Expected between 0-4 arguments [params, data, success, error], got "+
arguments.length+" arguments.";}var n=this instanceof g?this:a.isArray?[]:new g(i);x({method:a.method,url:k.url(o({},f(i,a.params||{}),j)),data:i}).then(function(b){var c=b.data;if(c)a.isArray?(n.length=0,h(c,function(a){n.push(new g(a))})):v(c,n);(l||p)(n,b.headers)},q);return n};g.prototype["$"+b]=function(a,d,h){var m=f(this),j=p,i;switch(arguments.length){case 3:m=a;j=d;i=h;break;case 2:case 1:r(a)?(j=a,i=d):(m=a,j=d||p);case 0:break;default:throw"Expected between 1-3 arguments [params, success, error], got "+
arguments.length+" arguments.";}g[b].call(this,m,e?this:w,j,i)}});g.bind=function(d){return u(b,o({},e,d),a)};return g}var A={get:{method:"GET"},save:{method:"POST"},query:{method:"GET",isArray:!0},remove:{method:"DELETE"},"delete":{method:"DELETE"}},p=d.noop,h=d.forEach,o=d.extend,v=d.copy,r=d.isFunction;t.prototype={url:function(b){var e=this,a=this.template,f,g,b=b||{};h(this.urlParams,function(h,c){f=b.hasOwnProperty(c)?b[c]:e.defaults[c];d.isDefined(f)&&f!==null?(g=s(f,!0).replace(/%26/gi,"&").replace(/%3D/gi,
"=").replace(/%2B/gi,"+"),a=a.replace(RegExp(":"+c+"(\\W)","g"),g+"$1")):a=a.replace(RegExp("(/?):"+c+"(\\W)","g"),function(a,b,c){return c.charAt(0)=="/"?c:b+c})});var a=a.replace(/\/?#$/,""),k=[];h(b,function(a,b){e.urlParams[b]||k.push(s(b)+"="+s(a))});k.sort();a=a.replace(/\/*$/,"");return a+(k.length?"?"+k.join("&"):"")}};return u}])})(window,window.angular);