From ce08f919bfab73178b2f8c584f34fd8849834365 Mon Sep 17 00:00:00 2001
From: Douwe Maan <douwe@gitlab.com>
Date: Fri, 13 Feb 2015 12:02:17 +0100
Subject: [PATCH] Add link to share via twitter to "created project" event.

---
 app/assets/stylesheets/sections/events.scss   |  4 +++
 .../admin/application_settings_controller.rb  |  1 +
 app/helpers/application_settings_helper.rb    |  4 +++
 app/models/application_setting.rb             |  2 ++
 .../application_settings/_form.html.haml      |  5 ++++
 app/views/events/_event.html.haml             |  6 +++--
 .../events/event/_created_project.html.haml   | 27 +++++++++++++++++++
 config/initializers/1_settings.rb             |  1 +
 ...sharing_enabled_to_application_settings.rb |  5 ++++
 db/schema.rb                                  |  7 ++---
 10 files changed, 57 insertions(+), 5 deletions(-)
 create mode 100644 app/views/events/event/_created_project.html.haml
 create mode 100644 db/migrate/20150213104043_add_twitter_sharing_enabled_to_application_settings.rb

diff --git a/app/assets/stylesheets/sections/events.scss b/app/assets/stylesheets/sections/events.scss
index 9582c995980..b7614513216 100644
--- a/app/assets/stylesheets/sections/events.scss
+++ b/app/assets/stylesheets/sections/events.scss
@@ -64,6 +64,10 @@
 
       .md {
         font-size: 13px;
+
+        iframe.twitter-share-button {
+          vertical-align: bottom;
+        }
       }
 
       pre {
diff --git a/app/controllers/admin/application_settings_controller.rb b/app/controllers/admin/application_settings_controller.rb
index 7458542fc73..2b0c500e97a 100644
--- a/app/controllers/admin/application_settings_controller.rb
+++ b/app/controllers/admin/application_settings_controller.rb
@@ -26,6 +26,7 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
       :signup_enabled,
       :signin_enabled,
       :gravatar_enabled,
+      :twitter_sharing_enabled,
       :sign_in_text,
       :home_page_url
     )
diff --git a/app/helpers/application_settings_helper.rb b/app/helpers/application_settings_helper.rb
index 04299316102..1ee086da997 100644
--- a/app/helpers/application_settings_helper.rb
+++ b/app/helpers/application_settings_helper.rb
@@ -3,6 +3,10 @@ module ApplicationSettingsHelper
     current_application_settings.gravatar_enabled?
   end
 
+  def twitter_sharing_enabled?
+    current_application_settings.twitter_sharing_enabled?
+  end
+
   def signup_enabled?
     current_application_settings.signup_enabled?
   end
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb
index 6d4e220b16c..f1d918e5457 100644
--- a/app/models/application_setting.rb
+++ b/app/models/application_setting.rb
@@ -8,6 +8,7 @@
 #  signup_enabled         :boolean
 #  signin_enabled         :boolean
 #  gravatar_enabled       :boolean
+#  twitter_sharing_enabled  :boolean
 #  sign_in_text           :text
 #  created_at             :datetime
 #  updated_at             :datetime
@@ -30,6 +31,7 @@ class ApplicationSetting < ActiveRecord::Base
       default_branch_protection: Settings.gitlab['default_branch_protection'],
       signup_enabled: Settings.gitlab['signup_enabled'],
       signin_enabled: Settings.gitlab['signin_enabled'],
+      twitter_sharing_enabled: Settings.gitlab['twitter_sharing_enabled'],
       gravatar_enabled: Settings.gravatar['enabled'],
       sign_in_text: Settings.extra['sign_in_text'],
     )
diff --git a/app/views/admin/application_settings/_form.html.haml b/app/views/admin/application_settings/_form.html.haml
index ae0c70a79c7..f528d69f431 100644
--- a/app/views/admin/application_settings/_form.html.haml
+++ b/app/views/admin/application_settings/_form.html.haml
@@ -19,6 +19,11 @@
       = f.label :gravatar_enabled, class: 'control-label'
       .col-sm-10
         = f.check_box :gravatar_enabled, class: 'checkbox'
+    .form-group
+      = f.label :twitter_sharing_enabled, "Twitter enabled", class: 'control-label'
+      .col-sm-10
+        = f.check_box :twitter_sharing_enabled, class: 'checkbox'
+        %span.help-block Show users button to share their newly created public or internal projects on twitter
   %fieldset
     %legend Misc
     .form-group
diff --git a/app/views/events/_event.html.haml b/app/views/events/_event.html.haml
index c7976ba564f..02b1dec753c 100644
--- a/app/views/events/_event.html.haml
+++ b/app/views/events/_event.html.haml
@@ -3,12 +3,14 @@
     .event-item-timestamp
       #{time_ago_with_tooltip(event.created_at)}
 
-    = cache event do
+    = cache [event, current_user] do
       = image_tag avatar_icon(event.author_email, 24), class: "avatar s24", alt:''
 
       - if event.push?
         = render "events/event/push", event: event
-      - elsif event.note?
+      - elsif event.commented?
         = render "events/event/note", event: event
+      - elsif event.created_project?
+        = render "events/event/created_project", event: event
       - else
         = render "events/event/common", event: event
\ No newline at end of file
diff --git a/app/views/events/event/_created_project.html.haml b/app/views/events/event/_created_project.html.haml
new file mode 100644
index 00000000000..0ebbb841cca
--- /dev/null
+++ b/app/views/events/event/_created_project.html.haml
@@ -0,0 +1,27 @@
+.event-title
+  %span.author_name= link_to_author event
+  %span.event_label{class: event.action_name}
+    = event_action_name(event)
+
+  - if event.project
+    = link_to_project event.project
+  - else
+    = event.project_name
+
+- if current_user == event.author && !event.project.private? && twitter_sharing_enabled?
+  .event-body
+    .event-note
+      .md
+        %p
+          Congratulations! Why not share your accomplishment with the world?
+
+          %a.twitter-share-button{ |
+            href: "https://twitter.com/share", |
+            class: "twitter-share-button", |
+            "data-url" => event.project.web_url, |
+            "data-text" => "I just created a new project in GitLab! GitLab is version control on your server, like GitHub but better.", |
+            "data-size" => "medium", |
+            "data-related" => "gitlab", |
+            "data-count" => "none"}
+            Tweet
+          <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
\ No newline at end of file
diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb
index d7c1a8428ac..6a8bbb80b9c 100644
--- a/config/initializers/1_settings.rb
+++ b/config/initializers/1_settings.rb
@@ -112,6 +112,7 @@ end
 Settings.gitlab['time_zone']  ||= nil
 Settings.gitlab['signup_enabled'] ||= true if Settings.gitlab['signup_enabled'].nil?
 Settings.gitlab['signin_enabled'] ||= true if Settings.gitlab['signin_enabled'].nil?
+Settings.gitlab['twitter_sharing_enabled'] ||= true if Settings.gitlab['twitter_sharing_enabled'].nil?
 Settings.gitlab['restricted_visibility_levels'] = Settings.send(:verify_constant_array, Gitlab::VisibilityLevel, Settings.gitlab['restricted_visibility_levels'], [])
 Settings.gitlab['username_changing_enabled'] = true if Settings.gitlab['username_changing_enabled'].nil?
 Settings.gitlab['issue_closing_pattern'] = '((?:[Cc]los(?:e[sd]?|ing)|[Ff]ix(?:e[sd]|ing)?|[Rr]esolv(?:e[sd]?|ing)) +(?:(?:issues? +)?#\d+(?:(?:, *| +and +)?))+)' if Settings.gitlab['issue_closing_pattern'].nil?
diff --git a/db/migrate/20150213104043_add_twitter_sharing_enabled_to_application_settings.rb b/db/migrate/20150213104043_add_twitter_sharing_enabled_to_application_settings.rb
new file mode 100644
index 00000000000..a0439172391
--- /dev/null
+++ b/db/migrate/20150213104043_add_twitter_sharing_enabled_to_application_settings.rb
@@ -0,0 +1,5 @@
+class AddTwitterSharingEnabledToApplicationSettings < ActiveRecord::Migration
+  def change
+    add_column :application_settings, :twitter_sharing_enabled, :boolean, default: true
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index f33766a1fe8..15c89a14c02 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20150211174341) do
+ActiveRecord::Schema.define(version: 20150213104043) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -26,6 +26,7 @@ ActiveRecord::Schema.define(version: 20150211174341) do
     t.datetime "updated_at"
     t.string   "home_page_url"
     t.integer  "default_branch_protection", default: 2
+    t.boolean  "twitter_sharing_enabled",   default: true
   end
 
   create_table "broadcast_messages", force: true do |t|
@@ -333,10 +334,10 @@ ActiveRecord::Schema.define(version: 20150211174341) do
     t.string   "import_url"
     t.integer  "visibility_level",       default: 0,        null: false
     t.boolean  "archived",               default: false,    null: false
+    t.string   "avatar"
     t.string   "import_status"
     t.float    "repository_size",        default: 0.0
     t.integer  "star_count",             default: 0,        null: false
-    t.string   "avatar"
     t.string   "import_type"
     t.string   "import_source"
   end
@@ -440,6 +441,7 @@ ActiveRecord::Schema.define(version: 20150211174341) do
     t.integer  "notification_level",       default: 1,     null: false
     t.datetime "password_expires_at"
     t.integer  "created_by_id"
+    t.datetime "last_credential_check_at"
     t.string   "avatar"
     t.string   "confirmation_token"
     t.datetime "confirmed_at"
@@ -447,7 +449,6 @@ ActiveRecord::Schema.define(version: 20150211174341) do
     t.string   "unconfirmed_email"
     t.boolean  "hide_no_ssh_key",          default: false
     t.string   "website_url",              default: "",    null: false
-    t.datetime "last_credential_check_at"
     t.string   "github_access_token"
     t.string   "gitlab_access_token"
     t.string   "notification_email"
-- 
GitLab