-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from kufu/features/friends
Friends
- Loading branch information
Showing
22 changed files
with
294 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
class FriendsController < ApplicationController | ||
include QRcode | ||
|
||
before_action :make_sure_user_logged_in | ||
|
||
def new | ||
@trigger = Trigger.find_by(description: trigger_description) | ||
|
||
if @trigger | ||
@trigger.key = SecureRandom.uuid | ||
@trigger.expires_at = Time.zone.now + 30.seconds | ||
@trigger.amount = 1 | ||
else | ||
@trigger = new_trigger | ||
end | ||
|
||
raise unless @trigger.save | ||
|
||
@qrcode = url_to_svg_qrcode(url: trigger_url(@trigger, key: @trigger.key)) | ||
end | ||
|
||
private | ||
|
||
def trigger_description | ||
"friends:#{@user.profile.uid}" | ||
end | ||
|
||
def new_trigger | ||
Trigger.build( | ||
description: trigger_description, | ||
key: SecureRandom.uuid, | ||
action: [ | ||
{ | ||
model: 'Friend', | ||
target: 'Profile', | ||
props: { | ||
from: @user.profile.id, | ||
to: :target | ||
}, | ||
action: :craete | ||
}, | ||
{ | ||
model: 'Friend', | ||
target: 'Profile', | ||
props: { | ||
from: :target, | ||
to: @user.profile.id | ||
}, | ||
action: :craete | ||
} | ||
], | ||
expires_at: Time.zone.now + 30.seconds, | ||
amount: 1 | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
module FriendsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
// Connects to data-controller="friend-code" | ||
export default class extends Controller { | ||
static targets = ['counter'] | ||
static values = { expiresAt: String } | ||
|
||
connect() { | ||
this.calc(); | ||
this.timer = setInterval(() => { | ||
this.calc(); | ||
}, 1000); | ||
} | ||
|
||
disconnect() { | ||
clearInterval(this.timer); | ||
} | ||
|
||
calc () { | ||
const expiresAt = Date.parse(this.expiresAtValue); | ||
const now = Date.now(); | ||
|
||
if (now < expiresAt) { | ||
this.counterTarget.innerHTML = Math.trunc((expiresAt - now) / 1000); | ||
} else { | ||
this.counterTarget.innerHTML = 0; | ||
|
||
fetch('/profile/friends/new', { headers: { Accept: "text/vnd.turbo-stream.html" } }) | ||
.then(r => r.text()) | ||
.then(html => Turbo.renderStreamMessage(html)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class Friend < ApplicationRecord | ||
belongs_to :from_profile, class_name: 'Profile', foreign_key: :from | ||
belongs_to :to_profile, class_name: 'Profile', foreign_key: :to | ||
|
||
validates :from, presence: true | ||
validates :to, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="w-full"> | ||
<%= @qrcode.html_safe %> | ||
<div class="flex flex-col w-full mt-4" data-controller="friend-code" data-friend-code-expires-at-value="<%= @trigger.expires_at %>"> | ||
<p>expires in <span data-friend-code-target="counter"></span> seconds</p> | ||
<a href="<%= new_profile_friend_path %>" class="normal-button mr-2"><%= I18n.t('button.reload') %></a> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= turbo_frame_tag 'friend-qr' do %> | ||
<div id="friend-qr-code"> | ||
<%= render 'qrcode' %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= turbo_stream.update 'friend-qr-code' do %> | ||
<%= render 'qrcode' %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.