Commit 9dc66aca authored by Eugen Rochko's avatar Eugen Rochko

Improve design of site, show followers, link to profiles

parent 27be061a
// Place all the styles related to the friends controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the home controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
@import url(https://fonts.googleapis.com/css?family=Roboto:400,400i,700);
body {
background: #fff;
color: #282c37;
font-family: 'Roboto', sans-serif;
font-size: 13px;
line-height: 18px;
font-weight: 400;
max-width: 600px;
margin: 0 auto;
padding: 30px 10px;
}
a {
color: #2b90d9;
&:visited {
color: darken(#2b90d9, 15%);
}
}
hr {
border: 0;
background: none;
border-bottom: 1px solid #9baec8;
margin: 30px 0;
}
// Place all the styles related to the Users::OmniauthCallbacks controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
......@@ -5,6 +5,7 @@ class FriendsController < ApplicationController
def index
fetch_twitter_followees
fetch_twitter_followers
fetch_related_mastodons
end
......@@ -18,13 +19,23 @@ class FriendsController < ApplicationController
private
def fetch_twitter_followees
@twitter_friend_ids = Rails.cache.fetch("#{current_user.id}/twitter-friends", expires_in: 1.minute) do
@twitter_friend_ids = Rails.cache.fetch("#{current_user.id}/twitter-friends", expires_in: 15.minutes) do
twitter_client.friend_ids
end
end
def fetch_twitter_followers
@twitter_follower_ids = Rails.cache.fetch("#{current_user.id}/twitter-followers", expires_in: 15.minutes) do
twitter_client.follower_ids
end
end
def fetch_related_mastodons
@friends = User.where(id: Authorization.where(provider: :twitter, uid: @twitter_friend_ids.to_a).pluck(:user_id)).includes(:authorizations)
found_ids1 = Authorization.where(provider: :twitter, uid: @twitter_friend_ids.to_a)
found_ids2 = Authorization.where(provider: :twitter, uid: @twitter_follower_ids.to_a)
@name_map = twitter_client.users((found_ids1 + found_ids2).map(&:uid).map(&:to_i)).map { |u| [u.id.to_s, u] }.to_h
@friends = User.where(id: found_ids1.map(&:user_id)).includes(:authorizations)
@followers = User.where(id: found_ids2.map(&:user_id)).includes(:authorizations)
end
def twitter_client
......
module FriendsHelper
def mastodon_profile_url(uid)
username, domain = uid.split('@')
"https://#{domain}/users/#{username}"
end
end
......@@ -3,6 +3,14 @@ class User < ApplicationRecord
has_many :authorizations
def twitter
@twitter ||= authorizations.find_by(provider: :twitter)
end
def mastodon
@mastodon ||= authorizations.find_by(provider: :mastodon)
end
class << self
def from_omniauth(auth, current_user)
authorization = Authorization.where(provider: auth.provider, uid: auth.uid.to_s).first_or_initialize
......
<table>
<thead>
<tr>
<th>Twitter user</th>
<th>Mastodon user</th>
<th></th>
</tr>
</thead>
<tbody>
<% collection.each do |user| %>
<tr>
<td><%= link_to "@#{@name_map[user.twitter.uid].screen_name}", "https://twitter.com/#{@name_map[user.twitter.uid].screen_name}" %></td>
<% if user.mastodon %>
<td><%= link_to user.mastodon.uid, mastodon_profile_url(user.mastodon.uid) %></td>
<td><%= link_to "Follow", follow_friend_path(user) %></td>
<% else %>
<td colspan="2">Hasn't signed in with Mastodon yet</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
......@@ -4,27 +4,13 @@
<a class="twitter-share-button" href="https://twitter.com/intent/tweet?text=<%= URI.encode('Find your friends on #Mastodon') %>&url=<%= root_url %>" data-size="large">Tweet</a>
</p>
<table>
<thead>
<tr>
<th>Twitter user</th>
<th>Mastodon user</th>
<th></th>
</tr>
</thead>
<tbody>
<% @friends.each do |user| %>
<tr>
<td><%= user.authorizations.find_by(provider: :twitter).uid %></td>
<td><%= user.authorizations.find_by(provider: :mastodon).try(:uid) %></td>
<td><%= link_to "Follow", follow_friend_path(user) %></td>
</tr>
<% end %>
</tbody>
</table>
<p><small>Results are cached for 1 minute</small></p>
<h4>Friends</h4>
<%= render 'table', collection: @friends %>
<h4>Followers</h4>
<%= render 'table', collection: @followers %>
<script>window.twttr = (function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
......
......@@ -10,7 +10,7 @@
<ul>
<li><%= link_to "Sign in with Twitter", user_twitter_omniauth_authorize_path %></li>
<li><%= link_to "Sign in with Mastodon", user_mastodon_omniauth_authorize_path %></li>
</li>
</ul>
<% if user_signed_in? %>
<p>You are signed in:</p>
......@@ -19,12 +19,12 @@
<tbody>
<tr>
<th>Twitter</th>
<td><%= current_user.authorizations.find_by(provider: :twitter).try(:uid) %></td>
<td><%= current_user.twitter.try(:uid) || 'Please sign in' %></td>
</tr>
<tr>
<th>Mastodon</th>
<td><%= current_user.authorizations.find_by(provider: :mastodon).try(:uid) %></td>
<td><%= current_user.mastodon.try(:uid) || 'Please sign in' %></td>
</tr>
</tbody>
</table>
......
......@@ -16,5 +16,9 @@
<% end %>
<%= yield %>
<hr />
<p>This website is run by <a href='https://mastodon.social/users/Gargron'>Gargron@mastodon.social</a> / <a href='https://twitter.com/Gargron'>@Gargron</a></p>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment