diff --git a/plugins/community_hub/controllers/public/community_hub_plugin_public_controller.rb b/plugins/community_hub/controllers/public/community_hub_plugin_public_controller.rb index 97dae67..870a13b 100644 --- a/plugins/community_hub/controllers/public/community_hub_plugin_public_controller.rb +++ b/plugins/community_hub/controllers/public/community_hub_plugin_public_controller.rb @@ -2,33 +2,177 @@ class CommunityHubPluginPublicController < PublicController append_view_path File.join(File.dirname(__FILE__) + '/../../views') - #layout false + layout false + def newer_comments - posts = Comment.find(:all) - #render :text => posts_to_json(posts), :content_type => 'text/plain' - render :partial => "post", :collection => posts + latest_post = params[:latest_post] + hub = params[:hub] + posts = Comment.find(:all, + :order => "id desc", + :conditions => ["id > ?", latest_post]) + + if !posts.empty? + oldest_post = posts.last.id + latest_post = posts.first.id + else + oldest_post = 0 + latest_post = 0 + end + + #raise hub.inspect + + render :partial => "post", + :collection => posts, + :locals => { + :latest_post => latest_post, + :oldest_post => oldest_post, + :hub => hub + } end + + def newer_articles + latest_post = params[:latest_post] + hub = params[:hub] + posts = Article.find(:all, + :order => "id desc", + :conditions => ["id > :id and type = :type and parent_id = :hub", { + :id => latest_post, + :type => 'TinyMceArticle', + :hub => hub + }]) + + if !posts.empty? + oldest_post = posts.last.id + latest_post = posts.first.id + else + oldest_post = 0 + latest_post = 0 + end + + render :partial => "post", + :collection => posts, + :locals => { + :latest_post => latest_post, + :oldest_post => oldest_post, + :hub => hub + } + end + + def more_comments @posts = Comment.find(:all) render :partial => "post", :collection => @posts end - def newer_articles - posts = Article.find(:all, :conditions => {:type => 'TinyMceArticle'}, :limit => 3) - render :partial => "post", :collection => posts - end + def settings settings_section = params[:id] - #raise settings_section.inspect render :partial => "settings/twitter", :layout => true end + + def set_hub_view + hub = Article.find(params[:hub]) + hub_owner = hub.profile + role = params[:role] + render :partial => "post_form", :locals => {:hub => hub, :profile => user, :user_role => role} + end + + + def check_user_level + if false + render :text => {'level' => 0}.to_json, :content_type => 'application/json' + else + render :text => {'level' => 1}.to_json, :content_type => 'application/json' + end + end + + + def remove_live_post + + begin + post = Comment.find(params[:id]) + rescue + post = nil + end + + if post && post.destroy + render :text => {'ok' => true}.to_json, :content_type => 'application/json' + else + render :text => {'ok' => false}.to_json, :content_type => 'application/json' + end + + end + + + def promote_live_post + + post_id = params[:id] + user_id = params[:user].to_i + + hub = Article.find(params[:hub]) + + hub.promoted_users += [user_id] unless hub.promoted_users.include?(user_id) + + if hub && hub.save + render :text => {'ok' => true}.to_json, :content_type => 'application/json' + else + render :text => {'ok' => false}.to_json, :content_type => 'application/json' + end + end + + + def pin_live_post + + begin + post = Comment.find(params[:id]) + rescue + post = nil + end + + hub = Article.find(params[:hub]) + + hub.pinned_posts += [post.id] unless hub.pinned_posts.include?(post.id) + + if hub && hub.save + render :text => {'ok' => true}.to_json, :content_type => 'application/json' + else + render :text => {'ok' => false}.to_json, :content_type => 'application/json' + end + end + + + # + # to implement.......... + # + def like_live_post + if false + render :text => {'ok' => true}.to_json, :content_type => 'application/json' + else + render :text => {'ok' => false}.to_json, :content_type => 'application/json' + end + end + + + # + # to implement.......... + # + def dislike_live_post + if false + render :text => {'ok' => true}.to_json, :content_type => 'application/json' + else + render :text => {'ok' => false}.to_json, :content_type => 'application/json' + end + end + + protected def posts_to_json(list) + list.map do |item| { 'id' => item.id, 'created_at' => item.created_at, @@ -38,6 +182,4 @@ class CommunityHubPluginPublicController < PublicController end.to_json end - - end \ No newline at end of file diff --git a/plugins/community_hub/lib/community_hub_plugin/hub.rb b/plugins/community_hub/lib/community_hub_plugin/hub.rb index 24f10ed..d34eba7 100644 --- a/plugins/community_hub/lib/community_hub_plugin/hub.rb +++ b/plugins/community_hub/lib/community_hub_plugin/hub.rb @@ -1,7 +1,15 @@ +require File.dirname(__FILE__) + '/../../tweeter_stream/lib/twurl' + class CommunityHubPlugin::Hub < Folder settings_items :hashtags_twitter, :type => :string, :default => "" + settings_items :promoted_users, :type => Array, :default => [] + settings_items :pinned_posts, :type => Array, :default => [] + def initialize(my_var) + raise "ola".inspect + end + def self.icon_name(article = nil) 'community-hub' end diff --git a/plugins/community_hub/lib/community_hub_plugin/hub_helper.rb b/plugins/community_hub/lib/community_hub_plugin/hub_helper.rb new file mode 100644 index 0000000..3e7140b --- /dev/null +++ b/plugins/community_hub/lib/community_hub_plugin/hub_helper.rb @@ -0,0 +1,21 @@ +module CommunityHubPlugin::HubHelper + + def post_css_classes(post_id, latest_post_id, oldest_post_id) + classes = "post" + + if post_id == latest_post_id + classes += " latest" + end + + if post_id == oldest_post_id + classes += " oldest" + end + + classes + end + + def mediator?(hub) + true + end + +end \ No newline at end of file diff --git a/plugins/community_hub/public/javascripts/community_hub.js b/plugins/community_hub/public/javascripts/community_hub.js index 3b5f0aa..dd0bdd4 100644 --- a/plugins/community_hub/public/javascripts/community_hub.js +++ b/plugins/community_hub/public/javascripts/community_hub.js @@ -1,27 +1,36 @@ var $ = jQuery; +var latest_post_id = 0; +var oldest_post_id = 0; + + +function hub_open_loading() { + var html = '
' + + '' + + '
'; + + $('.hub .form .submit').after(html); + $('#hub-loading').fadeIn('slow'); +} + + +function hub_close_loading() { + $('#hub-loading').fadeOut('slow', function() { + $('#hub-loading').remove(); + }); +} + function send_message_for_stream(button) { - var $ = jQuery; - open_loading(DEFAULT_LOADING_MESSAGE); + var $button = $(button); var form = $button.parents("form"); - $button.addClass('stream-post-button-loading'); - $.post(form.attr("action"), form.serialize(), function(data) { - if(data.render_target == null) { - } - else if(data.render_target == 'form') { - } - else if($('#' + data.render_target).size() > 0) { - } - else { - form.find("input[type='text']").add('textarea').each(function() { - this.value = ''; - }); - } - close_loading(); - $button.removeClass('stream-post-button-loading'); - $button.enable(); + hub_open_loading(); + + $.post(form.attr("action"), form.serialize(), function(data) { + console.log(data); + $("#comment_body").val(''); + hub_close_loading(); }, 'json'); } @@ -31,19 +40,9 @@ function send_post_for_mediation(button) { var $button = $(button); var form = $button.parents("form"); $button.addClass('stream-post-button-loading'); + setMediationTimestamp(); $.post(form.attr("action"), form.serialize(), function(data) { - if(data.render_target == null) { - } - else if(data.render_target == 'form') { - } - else if($('#' + data.render_target).size() > 0) { - } - else { - form.find("input[type='text']").add('textarea').each(function() { - this.value = ''; - }); - } - + tinymce.get('article_body').setContent(''); close_loading(); $button.removeClass('stream-post-button-loading'); $button.enable(); @@ -57,7 +56,8 @@ function clearMediationForm(element) { function setMediationTimestamp() { var now = new Date().getTime(); var timestamp = 'hub-mediation-' + now.toString(); - $("article_name").value = timestamp; + $("#article_name").val(timestamp); + console.log('teste!!!!!!!!!!!'); } function loadPosts(section) { @@ -89,19 +89,115 @@ function loadPosts(section) { } function hub() { - loadPosts('live'); - loadPosts('mediation'); + //checkNewPosts('live'); + //checkNewPosts('mediation'); } -function checkNewPosts() { - var agora = new Date(); - console.log( 'checking news posts...' ); +function checkNewPosts(postType) { + + var url = ''; + var container; + + console.log(postType); + + switch (postType) { + case 'live': + url = '/plugin/community_hub/public/newer_comments'; + container = $("#live-posts"); + break; + case 'mediation': + url = '/plugin/community_hub/public/newer_articles'; + container = $("#mediation-posts"); + break; + } + + var hub_id = $(".hub").attr('id'); + var now = new Date(); + + $.ajax({ + url: url, + type: 'get', + data: { latest_post: latest_post_id, hub: hub_id }, + success: function(data) { + + if (data.trim().length > 0) { + container.prepend(data); + latest_post_id = $(".latest").attr("id"); + + } + else { + console.log('No more live posts!'); + } + + }, + error: function(ajax, stat, errorThrown) { + console.log(stat); + } + }); + } function toogleAutoScrolling() { alert($("#auto_scrolling").attr('checked')); } +function setHubView(role) { + + var hub_id = $(".hub").attr('id'); + + $.ajax({ + url: '/plugin/community_hub/public/set_hub_view', + type: 'get', + data: { hub: hub_id, role: role }, + success: function(data) { + $(".form").html(data); + console.log( data ); + }, + error: function(ajax, stat, errorThrown) { + console.log( 'ERRO ao processar requisição!'); + } + }); + +} + + +function checkUserLevel() { + + var hub_id = $(".hub").attr('id'); + + $.ajax({ + url: '/plugin/community_hub/public/check_user_level', + type: 'get', + dataType: 'json', + data: { hub: hub_id }, + success: function(data) { + + + switch (data.level) { + + case -1: + console.log( 'usuário não logado...' ); + setHubView('guest'); + break; + case 0: + console.log( 'usuário logado, visitante...'); + setHubView('visitor'); + break; + case 1: + console.log( 'usuário logado, mediador...'); + setHubView('mediator'); + break; + } + + + }, + error: function(ajax, stat, errorThrown) { + console.log( 'ERRO ao processar requisição!'); + } + }); + +} + $(document).ready(function(){ $("#auto_scrolling").click(function(){ @@ -109,7 +205,147 @@ $(document).ready(function(){ }); hub(); - checkNewPosts('live'); + + //checkUserLevel(); + + //setInterval(checkNewLivePosts, 10000); //10 seconds interval + setInterval(checkNewMediationPosts, 10000); //10 seconds interval + //setInterval(checkUserLevel, 10000); //10 seconds interval }); + +function checkNewLivePosts() { + checkNewPosts('live'); +} + + +function checkNewMediationPosts() { + checkNewPosts('mediation'); +} + + +function removeLivePost(post_id) { + + $.ajax({ + url: '/plugin/community_hub/public/remove_live_post', + type: 'get', + dataType: 'json', + data: { id: post_id }, + success: function(data) { + + if (data.ok) { + console.log( 'OK - Post removido!'); + } + else { + console.log( 'NOT OK - Post NÃO removido!'); + } + + }, + error: function(ajax, stat, errorThrown) { + console.log( 'ERRO ao processar requisição!'); + } + }); + +} + + +function promoteLivePost(post_id, user_id) { + + var hub_id = $(".hub").attr('id'); + + $.ajax({ + url: '/plugin/community_hub/public/promote_live_post', + type: 'get', + dataType: 'json', + data: { id: post_id, user: user_id, hub: hub_id }, + success: function(data) { + + if (data.ok) { + console.log( 'OK - Post promovido!'); + } + else { + console.log( 'NOT OK - Post NÃO promovido!'); + } + + }, + error: function(ajax, stat, errorThrown) { + console.log( 'ERRO ao processar requisição!'); + } + }); + +} + +function pinLivePost(post_id) { + + var hub_id = $(".hub").attr('id'); + + $.ajax({ + url: '/plugin/community_hub/public/pin_live_post', + type: 'get', + dataType: 'json', + data: { id: post_id, hub: hub_id }, + success: function(data) { + + if (data.ok) { + console.log( 'OK - Post fixado!'); + } + else { + console.log( 'NOT OK - Post NÃO fixado!'); + } + + }, + error: function(ajax, stat, errorThrown) { + console.log( 'ERRO ao processar requisição!'); + } + }); + +} + +function likeLivePost(post_id) { + + $.ajax({ + url: '/plugin/community_hub/public/like_live_post', + type: 'get', + dataType: 'json', + data: { id: post_id }, + success: function(data) { + + if (data.ok) { + console.log( 'OK - Post curtido!'); + } + else { + console.log( 'NOT OK - Post NÃO curtido!'); + } + + }, + error: function(ajax, stat, errorThrown) { + console.log( 'ERRO ao processar requisição!'); + } + }); + +} + +function dislikeLivePost(post_id) { + + $.ajax({ + url: '/plugin/community_hub/public/dislike_live_post', + type: 'get', + dataType: 'json', + data: { id: post_id }, + success: function(data) { + + if (data.ok) { + console.log( 'OK - Post descurtido!'); + } + else { + console.log( 'NOT OK - Post NÃO descurtido!'); + } + + }, + error: function(ajax, stat, errorThrown) { + console.log( 'ERRO ao processar requisição!'); + } + }); + +} \ No newline at end of file diff --git a/plugins/community_hub/public/style.css b/plugins/community_hub/public/style.css index 8308129..467832c 100644 --- a/plugins/community_hub/public/style.css +++ b/plugins/community_hub/public/style.css @@ -1,3 +1,8 @@ +#hub-loading { + margin-top: 18px; + float: right; +} + .icon-newcommunity-hub, .icon-community-hub { background-image: url(/plugins/community_hub/icons/community-hub.png) diff --git a/plugins/community_hub/tweeter_stream/lib/twurl/newjson.json b/plugins/community_hub/tweeter_stream/lib/twurl/newjson.json new file mode 100644 index 0000000..dbd23a1 --- /dev/null +++ b/plugins/community_hub/tweeter_stream/lib/twurl/newjson.json @@ -0,0 +1,3 @@ +{ + "name": "root" +} diff --git a/plugins/community_hub/tweeter_stream/lib/twurl/request_controller.rb b/plugins/community_hub/tweeter_stream/lib/twurl/request_controller.rb index 75ca11d..31837bb 100755 --- a/plugins/community_hub/tweeter_stream/lib/twurl/request_controller.rb +++ b/plugins/community_hub/tweeter_stream/lib/twurl/request_controller.rb @@ -13,8 +13,15 @@ module Twurl def perform_request client.perform_request_from_options(options) { |response| - response.read_body { |chunk| parsed = JSON.parse(chunk) - print parsed["text"] + "\n" + response.read_body { |chunk| +# print "#{chunk}\n" + #unless chunk.to_i.length = 0 + begin + parsed = JSON.parse(chunk) + print "@#{parsed["user"]["name"]} said: #{parsed["text"]} \n" + rescue + end + #end } } rescue URI::InvalidURIError diff --git a/plugins/community_hub/tweeter_stream/lib/twurl/stream.rb b/plugins/community_hub/tweeter_stream/lib/twurl/stream.rb index 1472fba..39f4fa6 100755 --- a/plugins/community_hub/tweeter_stream/lib/twurl/stream.rb +++ b/plugins/community_hub/tweeter_stream/lib/twurl/stream.rb @@ -20,9 +20,7 @@ module Twurl Twurl.options.command = 'request' # Not necessary anymore Twurl.options.data = {"track"=>tags} # Twurl.options.proxy = 'http://161.148.1.167:312' # Use for production mode at SERPRO - if !proxy.nil? - Twurl.options.proxy = proxy - end + Twurl.options.proxy = proxy unless proxy.nil? Twurl.options.trace = false Twurl.options.headers = {} Twurl.options.subcommands=[] diff --git a/plugins/community_hub/tweeter_stream/nbproject/private/private.xml b/plugins/community_hub/tweeter_stream/nbproject/private/private.xml new file mode 100644 index 0000000..4750962 --- /dev/null +++ b/plugins/community_hub/tweeter_stream/nbproject/private/private.xml @@ -0,0 +1,4 @@ + + + + diff --git a/plugins/community_hub/tweeter_stream/noosfero.rb b/plugins/community_hub/tweeter_stream/noosfero.rb index 982d674..ca9ed68 100755 --- a/plugins/community_hub/tweeter_stream/noosfero.rb +++ b/plugins/community_hub/tweeter_stream/noosfero.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +require 'rubygems' # if you use RubyGems + require File.dirname(__FILE__) + '/lib/twurl' -Twurl::Stream.run('nba', '/root/.twurlrc') +Twurl::Stream.run('popcorntime,time4popcorn', '/root/.twurlrc') diff --git a/plugins/community_hub/views/community_hub_plugin_public/_post.rhtml b/plugins/community_hub/views/community_hub_plugin_public/_post.rhtml index 8230b58..370ab20 100644 --- a/plugins/community_hub/views/community_hub_plugin_public/_post.rhtml +++ b/plugins/community_hub/views/community_hub_plugin_public/_post.rhtml @@ -1,39 +1,60 @@ -
  • +<% extend CommunityHubPlugin::HubHelper %> +
  • -