 
 
 
-update_opinion_views = (filter) -> 
-  users = fetch '/users'
-  filter_out = fetch 'filtered'
-
-  return if !users.users
+save 
+  key: 'opinion_views'
+  active_views: {}
+
+activate_opinion_view = (view) -> 
+
+  opinion_views = fetch('opinion_views')
+  opinion_views.active_views ?= {}
+  active_views = opinion_views.active_views
+
+  # TODO: In the future, we'll allow conjunctive views, but 
+  #       for now, only allow one at a time
+  to_delete = []
+  for k,v of active_views
+    if v.created_by == 'activate_opinion_view'
+      to_delete.push k 
+  for k in to_delete
+    delete active_views[k]
+
+  view_name = view.key or view.label
+
+  active_views[view_name] = 
+    created_by: 'activate_opinion_view'
+    get_salience: (u, opinion, proposal) ->
+      if view.salience?
+        view.salience u, opinion, proposal
+      else if !view.pass || view.pass(u, opinion, proposal)
+        1
+      else if opinion_views.enable_comparison 
+        .1
+      else 
+        0
+    get_weight: (u, opinion, proposal) ->
+      if view.weight?
+        view.weight(u, opinion, proposal)
+      else if opinion_views.enable_comparison || view.pass?(u, opinion, proposal)
+        1
+      else 
+        0
+
+  invalidate_proposal_sorts()
+  save opinion_views
+
+
+
+opinion_view_cache = {}
+window.compose_opinion_views = (opinions, proposal, opinion_views) -> 
+  opinion_views ?= fetch('opinion_views')
+  opinion_views.active_views ?= {}
+  active_views = opinion_views.active_views
+
+  if !opinions
+    opinions = opinionsForProposal(proposal)
+
+  weights = {}
+  salience = {}
+  groups = {}
+
+  for o in opinions
+    weight = 1
+    min_salience = 1
+    groups = []
+    u = o.user.key or o.user
+
+    for view_name, view of active_views
+      if view.get_salience?
+        s = view.get_salience(u, o, proposal)
+        if s < min_salience
+          min_salience = s
+      if view.get_weight?
+        weight *= view.get_weight(u, o, proposal)
+      if view.groups? 
+        groups.concat view.groups(u, o, proposal)
+    weights[u] = weight
+    salience[u] = min_salience
+    groups[u] = groups
+  {weights, salience, groups}
   
-  filter_out.users = {}
-
-  if filter_out.current_filter == filter
-    filter_out.current_filter = null 
-  else 
-    filter_out.current_filter = filter 
-
-  filter_func = filter_out.current_filter?.pass
-
-  if filter_func
-    for user in users.users   
-      passes_filter = filter_func(user) 
-      if !passes_filter
-        filter_out.users[user.key] = 1
-
-  invalidate_proposal_sorts()
-  save filter_out
-
-set_comparison_mode = (enabled) ->
-  filter_out = fetch 'filtered'
-
-  if enabled == null # toggle if argument not passed 
-    filter_out.enable_comparison = !filter_out.enable_comparison
-  else 
-    filter_out.enable_comparison = enabled
-  save filter_out 
+
+window.get_opinions_for_proposal = (opinions, proposal, weights) ->
+  if !opinions
+    opinions = opinionsForProposal proposal
+  if !weights
+    {weights, salience, groups} = compose_opinion_views(opinions, proposal)
+
+  (o for o in opinions when weights[o.user] > 0)
+
+
+
+default_views = 
+  everyone: 
+    key: 'everyone'
+    label: 'everyone'
+    tooltip: null 
+    pass: (u) -> true 
+
+  just_you: 
+    key: 'just_you'
+    label: 'just you'
+    tooltip: null 
+    pass: (u) -> 
+      user = fetch(u)
+      user.key == fetch('/current_user').user
+
+  weighed_by_activity: 
+    key: 'weighed_by_activity'
+    label: 'weighed by reasons given'
+    weight: (u, opinion, proposal) ->
+      point_inclusions = opinion.point_inclusions?.length or 0 
+      .1 + point_inclusions
+
+  weighed_by_recency: 
+    key: 'weighed_by_recency'
+    label: 'weighed by recency'
+    weight: (u, opinion, proposal) ->
+      if !proposal.time_created 
+        proposal.time_created = new Date(proposal.created_at).getTime()
+      earliest = proposal.time_created
+
+      ot = new Date(opinion.updated_at).getTime()
+
+      time_since_creation = ot - earliest 
+
+      # based on days since creation
+      Math.log .1 + time_since_creation / (1000 * 60 * 60 * 24)
 
 
 OpinionViews = ReactiveComponent

                      user.tags.verified


window.opinion_trickle = -> 
  filter_out = fetch 'filtered'
  
  proposals = fetch '/proposals'

  users = {}
  for prop in proposals.proposals
    for o in prop.opinions
      users[o.user] = o.created_at or o.updated_at

  users = ([k,v] for k,v of users)
  users.sort (a,b) -> 
    i = new Date(a[1])
    j = new Date(b[1])
    i.getTime() - j.getTime()

  users = (u[0] for u in users)
  cnt = users.length

  steps = 1
  tick = (interval) => 
    if cnt >= 0
      setTimeout => 
        filter_out.users = {}
        for user, idx in users
          filter_out.users[user] = 1
          break if idx > cnt 

        cnt--
        #cnt -= Math.ceil(steps / 2)
        #tick(interval * .9)
        tick(interval * .9)
        steps++
        dirty = true
        setTimeout -> 
          if dirty
            save filter_out
            dirty = false
        , 2000
      , interval

  tick 1000


window.OpinionViews = OpinionViews


            open_menu_on: 'activation'

            selection_made_callback: (filter) =>
              activate_opinion_view filter
              @local.current_filter = filter
              save @local

            render_anchor: ->
              
              key = if (current_filter?.key or current_filter?.label) not in ['everyone', 'just_you', 'weighed_by_activity', 'weighed_by_recency'] then "/translations/#{fetch('/subdomain').name}" else null
              SPAN 
                style: 
                  fontWeight: 'bold'      
