-
Notifications
You must be signed in to change notification settings - Fork 785
Link unfurling without SignalID #1196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
monorkin
wants to merge
9
commits into
main
Choose a base branch
from
link-unfurling-without-signalid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+935
−15
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2352b47
Create Integrations and add link unfurling
monorkin 02bf13c
Copy and style for prompt and popup
jzimdars 11f79a2
Copy edit
jzimdars 6660643
Copy and style for done and already
jzimdars 8240a68
Lose the parens
jzimdars 3122011
Move Basecamp unfurling to Fizzy SAAS
monorkin 98f6b13
Generalize unfurler setup
monorkin 23623ac
Fix migrations after rebase
monorkin 33effe6
Remove reference to the basecamp unfurler
monorkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,22 @@ | ||
| class UnfurlLinksController < ApplicationController | ||
| rate_limit to: 50, within: 1.hour, by: -> { Current.user.id } | ||
|
|
||
| def create | ||
| link = Link.unfurl(url_param) | ||
|
|
||
| if link.unfurler.requires_setup? | ||
| render \ | ||
| json: { error: :unfurler_requires_setup, config: link.unfurler.setup_config }, | ||
| status: :unprocessable_entity | ||
| elsif link.metadata | ||
| render json: link.metadata | ||
| else | ||
| head :no_content | ||
| end | ||
| end | ||
|
|
||
| private | ||
| def url_param | ||
| params.require(:url) | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| export default class extends Controller { | ||
| close() { | ||
| window.close() | ||
| } | ||
| } |
This file contains hidden or 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,109 @@ | ||
| import { Controller } from "@hotwired/stimulus"; | ||
| import { post } from "@rails/request.js" | ||
| import Cookie from "models/cookie" | ||
|
|
||
| export default class extends Controller { | ||
| static targets = [ "linkAccountsPrompt" ] | ||
| static values = { | ||
| url: String, | ||
| setUpBasecampIntegrationUrl: String | ||
| } | ||
|
|
||
| static MAX_DISMISSAL_COUNT = 3 | ||
| static DISMISSAL_COUNTER_KEY = "basecamp_integration_dimissal_count" | ||
|
|
||
| #accountLinkingDismissed | ||
|
|
||
| unfurl(event) { | ||
| this.#unfurlLink(event.detail.url, event.detail) | ||
| } | ||
|
|
||
| setUpBasecampIntegration() { | ||
| this.#openPopup(this.setUpBasecampIntegrationUrlValue, { width: 400, height: 600 }) | ||
| } | ||
|
|
||
| closePrompt(event) { | ||
| this.linkAccountsPromptTarget.hidden = true | ||
|
|
||
| if (event.params.intent === "dismiss") { | ||
| this.#incrementDismissalCounter() | ||
| } | ||
| } | ||
|
|
||
| async #unfurlLink(url, callbacks) { | ||
| const { response } = await post( | ||
| this.urlValue, | ||
| { | ||
| body: JSON.stringify({ url }), | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "Accept": "application/json" | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| let metadata = null | ||
|
|
||
| if (response.status !== 204) { | ||
| metadata = await response.json() | ||
| } | ||
|
|
||
| if (metadata?.error) { | ||
| this.#handleError(metadata) | ||
| } else if (metadata) { | ||
| this.#insertUnfurledLink(metadata, callbacks) | ||
| } | ||
| } | ||
|
|
||
| #insertUnfurledLink(metadata, callbacks) { | ||
| callbacks.replaceLinkWith(this.#renderUnfurledLinkHTML(metadata)) | ||
| } | ||
|
|
||
| #renderUnfurledLinkHTML(metadata) { | ||
| return `<a href="${metadata.canonical_url}">${metadata.title}</a>` | ||
| } | ||
|
|
||
| #handleError({ error, ...data }) { | ||
| switch (error) { | ||
| case "unfurler_requires_setup": | ||
| if (this.#shouldShowUnfurlerSetupPrompt()) { | ||
| this.#promptUnfurlerSetUp(data.config) | ||
| } | ||
| break; | ||
| default: | ||
| throw new Error(`Unknown API error: ${error}`) | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| #promptUnfurlerSetUp() { | ||
| this.linkAccountsPromptTarget.hidden = false | ||
| } | ||
|
|
||
| #openPopup(url, options, onClose) { | ||
| const { width, height } = options | ||
| const left = (window.screen.width - width) / 2 | ||
| const top = (window.screen.height - height) / 2 | ||
|
|
||
| window.open( | ||
| url, | ||
| "_blank", | ||
| `width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,status=no` | ||
| ) | ||
| } | ||
|
|
||
| #incrementDismissalCounter() { | ||
| this.#cookie.increment(this.constructor.DISMISSAL_COUNTER_KEY) | ||
| this.#accountLinkingDismissed = true | ||
| } | ||
|
|
||
| #shouldShowUnfurlerSetupPrompt() { | ||
| const dismissalCount = this.#cookie.get(this.constructor.DISMISSAL_COUNTER_KEY) || 0 | ||
| return !this.#accountLinkingDismissed && (dismissalCount < this.constructor.MAX_DISMISSAL_COUNT) | ||
| } | ||
|
|
||
| get #cookie() { | ||
| const name = `link-unfurler-setup-prompt` | ||
| return Cookie.find(name) || new Cookie(name) | ||
| } | ||
| } | ||
This file contains hidden or 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 @@ | ||
| export default class Cookie { | ||
| static DEFAULT_EXPIRATION = 20 * 365 * 24 * 60 * 60 * 1000 | ||
|
|
||
| static find(name) { | ||
| const value = document.cookie | ||
| .split("; ") | ||
| .find(row => row.startsWith(`${name}=`)) | ||
| ?.split("=")[1] | ||
|
|
||
| if (!value) return null | ||
|
|
||
| try { | ||
| const data = JSON.parse(decodeURIComponent(value)) | ||
| return new Cookie(name, data) | ||
| } catch { | ||
| return new Cookie(name, { value: decodeURIComponent(value) }) | ||
| } | ||
| } | ||
|
|
||
| constructor(name, data = {}, options = {}) { | ||
| this.name = name | ||
| this.data = data | ||
| this.options = options | ||
| } | ||
|
|
||
| get(key) { | ||
| return this.data[key] | ||
| } | ||
|
|
||
| set(key, value) { | ||
| this.data[key] = value | ||
| this.save() | ||
| } | ||
|
|
||
| increment(key, amount = 1) { | ||
| const currentValue = this.data[key] || 0 | ||
|
|
||
| try { | ||
| this.set(key, currentValue + amount) | ||
| } catch { | ||
| this.set(key, amount) | ||
| } | ||
| } | ||
|
|
||
| save() { | ||
| const value = encodeURIComponent(JSON.stringify(this.data)) | ||
| const defaultExpires = new Date(Date.now() + this.constructor.DEFAULT_EXPIRATION) | ||
| const expires = `; expires=${(this.options.expires || defaultExpires).toUTCString()}` | ||
| const path = `; path=${this.options.path || "/"}` | ||
| const sameSite = this.options.sameSite ? `; SameSite=${this.options.sameSite}` : "; SameSite=Lax" | ||
|
|
||
| document.cookie = `${this.name}=${value}${expires}${path}${sameSite}` | ||
| } | ||
|
|
||
| delete() { | ||
| document.cookie = `${this.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/` | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| class Integration::Basecamp::SetUpJob < ApplicationJob | ||
| def perform(code:, state:) | ||
| Integration::Basecamp.set_up(code: code, state: state) | ||
| end | ||
| end |
This file contains hidden or 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 @@ | ||
| class Integration < ApplicationRecord | ||
| belongs_to :owner, class_name: "User" | ||
|
|
||
| store :data, coder: JSON | ||
| end |
This file contains hidden or 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,32 @@ | ||
| class Link | ||
| UNFURLERS = [ | ||
| FizzyUnfurler, | ||
| ] | ||
|
|
||
| attr_reader :uri, :metadata, :user | ||
|
|
||
| def self.unfurl(url, **options) | ||
| new(url).unfurl(**options) | ||
| end | ||
|
|
||
| def initialize(url, user: Current.user) | ||
| @uri = URI.parse(url) | ||
| @metadata = nil | ||
| @user = user | ||
| end | ||
|
|
||
| def unfurl | ||
| if unfurler&.setup? | ||
| @metadata = unfurler.unfurl | ||
| end | ||
|
|
||
| self | ||
| end | ||
|
|
||
| def unfurler | ||
| @unfurler ||= begin | ||
| unfurler = UNFURLERS.find { |unfurler| unfurler.unfurls?(uri) } | ||
| unfurler&.new(uri, user: user) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.