myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [1933] branches/event_logging: News generation.


From: noreply
Subject: [myexperiment-hackers] [1933] branches/event_logging: News generation.
Date: Mon, 10 Nov 2008 06:56:06 -0500 (EST)

Revision
1933
Author
alekses6
Date
2008-11-10 06:56:06 -0500 (Mon, 10 Nov 2008)

Log Message

News generation. "Home" page for anonymous (i.e. not logged in) users will now show "public news" feed - latest public events by any users on the website.

Modified Paths

Diff

Modified: branches/event_logging/app/helpers/application_helper.rb (1932 => 1933)


--- branches/event_logging/app/helpers/application_helper.rb	2008-11-07 18:02:32 UTC (rev 1932)
+++ branches/event_logging/app/helpers/application_helper.rb	2008-11-10 11:56:06 UTC (rev 1933)
@@ -586,6 +586,7 @@
   # Parameters:
   # (default parameters defined in environment_private.rb)
   # 1) contributor - instance of a User or Network, for which the news are generated
+  #                  (NIL to generate public news for "home" page)
   # 2) restrict_contributor - when set to "true" will generate news only for the current contributor;
   #                           when set to "false" will pick up also all related contributors;
   # 3-4) before, after - Time objects; the news will be generated in a time slice [after..before]
@@ -599,8 +600,13 @@
     # if event logging is switched off in "environment_private.rb" - no news can be generated
     return hash unless USE_EVENT_LOG
     
-    # collect all activity log events and interpret them as news entries
-    news_array = contributor_news_from_log(contributor, before, after, restrict_contributor, false, limit, current_viewer)
+    # collect all activity log events and interpret them as news entries;
+    # choose to display "my news" page for logged in users or a general one for anonymous viewers:
+    if contributor.nil?
+      news_array = news_from_log_public(before, after, limit, current_viewer)
+    else
+      news_array = news_from_log_for_contributor(contributor, before, after, restrict_contributor, false, limit, current_viewer)
+    end
     
     news_array.sort! { |a, b|
       b[0] <=> a[0]
@@ -1406,7 +1412,7 @@
   #            (NIL will yield news entries interpreting ALL events in ["after".."before"] timeframe)
   # 7) current_viewer - User instance, who is currently requests news to be generated
   #                     (zero or NIL for anonymous user - that is for public news feed: like for RSS feeds, etc)
-  def contributor_news_from_log(contributor, before, after, contributor_news_only, return_raw_events, limit, current_viewer)
+  def news_from_log_for_contributor(contributor, before, after, contributor_news_only, return_raw_events, limit, current_viewer)
     
     # DEBUG
     # puts "=================================="
@@ -1493,7 +1499,7 @@
     #  than some of the contributor in the original query)
     events_for_related_contributor = []
     related_contributors.each do |c|
-      events_for_related_contributor = contributor_news_from_log(c, before, after, true, true, limit, current_viewer) 
+      events_for_related_contributor = news_from_log_for_contributor(c, before, after, true, true, limit, current_viewer) 
       events.concat(events_for_related_contributor) 
     end
     return events if return_raw_events
@@ -1548,8 +1554,32 @@
     # ============================================================
     #  ALL RELEVANT EVENT LOG ENTRIES HAVE BEEN FETCHED UP TO NOW
     # ============================================================
+    rtn = news_entries_from_log_entries(events, limit, current_viewer, contributor, contributor_news_only)
     
+    return rtn
+  end
+  
+  
+  # Produces an array of news items in a form of [timestamp, description string] from ActivityLog table.
+  # (this is a worker method, usually called from news() - therefore, the caller method has default
+  #  parameter values declared; the worker method needs to get all the parameters)
+  #
+  # These are the latest news entries visible to anonymous users on the "home" page;
+  # ("contributor" for which these news are generated is treated as NIL in this case)
+  def news_from_log_public(before, after, limit, current_viewer)
+    events = []
     
+    # get all events from activity log in the required timeframe
+    events = ActivityLog.find(:all, :conditions => ["created_at > ? AND created_at < ?", after, before])
+    
+    # interpret activity log entries into news entries
+    rtn = news_entries_from_log_entries(events, limit, current_viewer, nil, false)
+    
+    return rtn
+  end
+  
+  
+  def news_entries_from_log_entries(events, limit, current_viewer, contributor, contributor_news_only)
     # remove any duplicates (which may arise when getting same event log entry from friends' related events),  
     # then sort by date descending; then delete a single (only possible after "uniq") empty element
     events = events.uniq.sort { |a, b|
@@ -1565,7 +1595,7 @@
     rtn = []
     news_item = nil # contributor_news_from_log!() will return NIL if event is not to be added to the feed for the current user
     events.each do |e|
-      news_item = contributor_news_from_log!(e, current_viewer, contributor, contributor_news_only)
+      news_item = news_entry_from_log_entry!(e, current_viewer, contributor, contributor_news_only)
       rtn << news_item unless news_item.nil?
       
       # no need to interpret more events than is required by the method call;
@@ -1602,7 +1632,7 @@
   # NIL is returned when the user viewing the news is not authorized to see
   # current news entry OR entry in the event log is no longer valid because of
   # some of the referenced objects missing (perhaps, they were deleted over time)  
-  def contributor_news_from_log!(log_entry, current_viewer, contributor, contributor_news_only)
+  def news_entry_from_log_entry!(log_entry, current_viewer, contributor, contributor_news_only)
     rtn = [] # despite this, NIL will be returned on errors / when news entry not to be shown for current user
     action = ""
     loggable_type = log_entry.activity_loggable_type
@@ -1756,6 +1786,7 @@
             authorized = true
           elsif ["invite", "request", "reject"].include? action
             # only group admin OR the user in this action allowed to view
+            authorized = false if (contributor.nil? || current_viewer.nil? || current_viewer == 0)
             authorized = (current_viewer.id == user_id || current_viewer.id == network_admin.id)
           else
             # unknown action, not authorized for viewing
@@ -1807,6 +1838,7 @@
           authorized = true
         elsif ["create", "reject"].include? action
           # only the "user" OR the "friend" in this action allowed to view
+          authorized = false if (contributor.nil? || current_viewer.nil? || current_viewer == 0)
           authorized = (my_event || current_viewer.id == referenced_user.id)
         else
           # unknown action, not authorized for viewing

Modified: branches/event_logging/app/views/home/_news.rhtml (1932 => 1933)


--- branches/event_logging/app/views/home/_news.rhtml	2008-11-07 18:02:32 UTC (rev 1932)
+++ branches/event_logging/app/views/home/_news.rhtml	2008-11-10 11:56:06 UTC (rev 1933)
@@ -3,18 +3,17 @@
 <div class="box">
 	<b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b>
 	<div class="box_title">
-		My News
+		<%= (logged_in? ? "My News" : "Public News") -%>
 	</div>
 	<div class="content">
-		<% if logged_in? %>
-			<% cache_timeout({ :controller => 'news', :action ="" 'home', :id => current_user.id } , NEWS_CACHE_TIMEOUT.seconds.from_now ) do -%>
-			  <%= render :partial => "layouts/news", :locals => { :collection => news(current_user, false, Time.now, Time.now-DEFAULT_HOME_PAGE_NEWS_TIMEFRAME, DEFAULT_HOME_PAGE_NEWS_COUNT, current_user) } %>
-		  <% end %>
-		<% else %>
-			<p>
-				<b><%= link_to "Log in", new_session_url %> to view your News</b>
-			</p>
-		<% end %>
+		<% news_for = (logged_in? ? current_user : nil) %>
+		<% required_news_count = (logged_in? ? DEFAULT_USER_HOME_PAGE_NEWS_COUNT : DEFAULT_ANONYMOUS_HOME_PAGE_NEWS_COUNT) %>
+		<% required_news_timeframe = (logged_in? ? DEFAULT_USER_HOME_PAGE_NEWS_TIMEFRAME : DEFAULT_ANONYMOUS_HOME_PAGE_NEWS_TIMEFRAME) %>
+		
+		<% viewer_id = (logged_in? ? current_user.id : "public") %>
+		<% cache_timeout({ :controller => 'news', :action ="" 'home', :id => viewer_id } , NEWS_CACHE_TIMEOUT.seconds.from_now ) do -%>
+		  <%= render :partial => "layouts/news", :locals => { :collection => news(news_for, false, Time.now, Time.now-required_news_timeframe, required_news_count, current_user) } %>
+	  <% end %>
 	</div>
 	<b class="xbottom"><b class="xb5"></b><b class="xb6"></b><b class="xb7"></b><b class="xb1"></b></b>
 </div>

Modified: branches/event_logging/config/environment_private.rb.pre (1932 => 1933)


--- branches/event_logging/config/environment_private.rb.pre	2008-11-07 18:02:32 UTC (rev 1932)
+++ branches/event_logging/config/environment_private.rb.pre	2008-11-10 11:56:06 UTC (rev 1933)
@@ -86,12 +86,14 @@
 # Default timeframes for various types of news
 # [this means that only events after (Time.now - DEFAULT_<>_TIMEFRAME) will be fetched from event log]
 DEFAULT_NEWS_TIMEFRAME = 1.week
-DEFAULT_HOME_PAGE_NEWS_TIMEFRAME = DEFAULT_NEWS_TIMEFRAME
+DEFAULT_USER_HOME_PAGE_NEWS_TIMEFRAME = DEFAULT_NEWS_TIMEFRAME
+DEFAULT_ANONYMOUS_HOME_PAGE_NEWS_TIMEFRAME = DEFAULT_NEWS_TIMEFRAME
 DEFAULT_USER_NEWS_TIMEFRAME = DEFAULT_NEWS_TIMEFRAME
 DEFAULT_GROUP_NEWS_TIMEFRAME = 2.weeks
 
 # Default news entry counts
 DEFAULT_NEWS_COUNT = 30
-DEFAULT_HOME_PAGE_NEWS_COUNT = DEFAULT_NEWS_COUNT
+DEFAULT_USER_HOME_PAGE_NEWS_COUNT = DEFAULT_NEWS_COUNT
+DEFAULT_ANONYMOUS_HOME_PAGE_NEWS_COUNT = 50
 DEFAULT_USER_NEWS_COUNT = DEFAULT_NEWS_COUNT
 DEFAULT_GROUP_NEWS_COUNT = DEFAULT_NEWS_COUNT
\ No newline at end of file

reply via email to

[Prev in Thread] Current Thread [Next in Thread]