Modified: branches/event_logging/app/helpers/application_helper.rb (1933 => 1934)
--- branches/event_logging/app/helpers/application_helper.rb 2008-11-10 11:56:06 UTC (rev 1933)
+++ branches/event_logging/app/helpers/application_helper.rb 2008-11-10 13:00:21 UTC (rev 1934)
@@ -1438,6 +1438,10 @@
events_as_admin = []
related_contributors = []
+ # error if "contributor" is NIL or set to zero - this method works only for non-anonymous users
+ return events if (contributor.nil? || contributor == 0)
+
+
case contributor.class.to_s.downcase
when "user"
unless contributor_news_only
@@ -1573,19 +1577,22 @@
events = ActivityLog.find(:all, :conditions => ["created_at > ? AND created_at < ?", after, before])
# interpret activity log entries into news entries
+ # (contributor set to NIL to indicate anonymous home page generation)
rtn = news_entries_from_log_entries(events, limit, current_viewer, nil, false)
return rtn
end
+ # Helper method that interprets an array of activity log entries into an array of news entries
+ # (works in a generic way for anonymous home page, user home page, user/group profile pages)
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|
+ events_unique = events.uniq.sort { |a, b|
b.created_at <=> a.created_at
}
- events.delete([])
+ events_unique.delete([])
# produce news from event list;
# if enough events are available in the "events" array, this method will
@@ -1594,7 +1601,7 @@
# (and it will interpret ALL events in "events" array if "limit" is set to NIL
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|
+ events_unique.each do |e|
news_item = news_entry_from_log_entry!(e, current_viewer, contributor, contributor_news_only)
rtn << news_item unless news_item.nil?
@@ -1638,7 +1645,12 @@
loggable_type = log_entry.activity_loggable_type
timestamp = log_entry.created_at
+ # if "current_viewer" is set to NIL or zero, we have anonymous viewer;
+ # ID for such user should be treated as "0", but "0.id == 1" --> hence the need for the line below
+ # (still, ".authorized?" method will treat both ways of defining "current_viewer" as anonymous)
+ current_viewer_id = ((current_viewer.nil? || current_viewer == 0) ? 0 : current_viewer.id)
+
# basic check that a "culprit" still exists
# (further checks for other related objects are carried out in the respective sections for every ActivityLoggable type)
unless ["Membership", "User"].include? loggable_type
@@ -1650,7 +1662,7 @@
return nil if culprit.nil?
culprit_link = name(culprit)
- my_event = (log_entry.culprit_id == current_viewer.id)
+ my_event = (log_entry.culprit_id == current_viewer_id)
rescue ActiveRecord::RecordNotFound
# culprit wasn't found - skip this news entry
# (left as additional security measure, but most likely will never be called)
@@ -1787,7 +1799,7 @@
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)
+ authorized = (current_viewer_id == user_id || current_viewer_id == network_admin.id) if authorized
else
# unknown action, not authorized for viewing
authorized = false
@@ -1839,7 +1851,7 @@
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)
+ authorized = (my_event || current_viewer_id == referenced_user.id) if authorized
else
# unknown action, not authorized for viewing
authorized = false
@@ -1881,7 +1893,7 @@
group = Network.find(log_entry.referenced_id)
# public announcements visible to anyone; private should only be shown to group members
- authorized = ( my_event || ann.public || (!ann.public && group.member?(current_viewer.id)) )
+ authorized = ( my_event || ann.public || (!ann.public && group.member?(current_viewer_id)) )
if authorized
# title can't be blank for group announcements, so it's safe to assume it's going to be present;
@@ -1924,10 +1936,10 @@
case shared_with_entity.class.to_s
when "User"
shared_with_name_or_title = shared_with_entity.name
- authorized = my_event || shared_with_entity.id == current_viewer.id
+ authorized = my_event || shared_with_entity.id == current_viewer_id
when "Network"
shared_with_name_or_title = shared_with_entity.title
- authorized = my_event || shared_with_entity.member?(current_viewer.id)
+ authorized = my_event || shared_with_entity.member?(current_viewer_id)
else
shared_with_name_or_title = shared_with_entity.to_s
authorized = false # unknown type..
@@ -2180,7 +2192,7 @@
# partly because every time when user logs in "last seen" field is set, which
# which triggers this event
# DISABLED FRAGMENT
- #if log_entry.activity_loggable_id == current_viewer.id
+ #if log_entry.activity_loggable_id == current_viewer_id
# rtn << [timestamp, "#{name(log_entry.activity_loggable_id)} updated their #{link_to "account", user_path(log_entry.activity_loggable_id)} settings."]
#end
# END OF DISABLED FRAGMENT