|
| 1 | +defmodule GroupherServerWeb.Controller.OG do |
| 2 | + @moduledoc """ |
| 3 | + handle open-graph info |
| 4 | + """ |
| 5 | + use GroupherServerWeb, :controller |
| 6 | + # alias Todos.Todo |
| 7 | + # plug(:action) |
| 8 | + |
| 9 | + def index(conn, %{"url" => url}) do |
| 10 | + fetch_opengraph_info(conn, url) |
| 11 | + end |
| 12 | + |
| 13 | + # return editor-js flavor fmt |
| 14 | + # see https://github.com/editor-js/link |
| 15 | + defp fetch_opengraph_info(conn, url) do |
| 16 | + case OpenGraph.fetch(url) do |
| 17 | + {:ok, info} -> |
| 18 | + ok_response(conn, url, info) |
| 19 | + |
| 20 | + {:error, reason} -> |
| 21 | + error_response(conn, url, reason) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + defp ok_response(conn, url, %OpenGraph{title: nil, description: nil}) do |
| 26 | + error_response(conn, url) |
| 27 | + end |
| 28 | + |
| 29 | + defp ok_response(conn, url, %OpenGraph{title: nil, description: description} = info) |
| 30 | + when not is_nil(description) do |
| 31 | + json(conn, %{ |
| 32 | + success: 1, |
| 33 | + meta: %{ |
| 34 | + title: info.description |> String.slice(0, 8), |
| 35 | + description: info.description, |
| 36 | + image: %{ |
| 37 | + url: nil |
| 38 | + } |
| 39 | + } |
| 40 | + }) |
| 41 | + end |
| 42 | + |
| 43 | + defp ok_response(conn, url, info) do |
| 44 | + json(conn, %{ |
| 45 | + success: 1, |
| 46 | + meta: %{ |
| 47 | + title: info.title, |
| 48 | + description: info.description, |
| 49 | + image: %{ |
| 50 | + url: info.image |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + end |
| 55 | + |
| 56 | + defp error_response(conn, url) do |
| 57 | + json(conn, %{ |
| 58 | + success: 1, |
| 59 | + meta: %{ |
| 60 | + title: url, |
| 61 | + description: url, |
| 62 | + image: %{ |
| 63 | + url: nil |
| 64 | + } |
| 65 | + } |
| 66 | + }) |
| 67 | + end |
| 68 | + |
| 69 | + defp error_response(conn, _url, :nxdomain) do |
| 70 | + json(conn, %{ |
| 71 | + success: 0, |
| 72 | + meta: %{ |
| 73 | + title: "domain-not-exsit", |
| 74 | + description: "--", |
| 75 | + image: %{ |
| 76 | + url: nil |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + end |
| 81 | + |
| 82 | + defp error_response(conn, _url, :timeout) do |
| 83 | + json(conn, %{ |
| 84 | + success: 0, |
| 85 | + meta: %{ |
| 86 | + title: "timeout", |
| 87 | + description: "--", |
| 88 | + image: %{ |
| 89 | + url: nil |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + end |
| 94 | + |
| 95 | + defp error_response(conn, url, "Not found :(") do |
| 96 | + json(conn, %{ |
| 97 | + success: 1, |
| 98 | + meta: %{ |
| 99 | + title: url, |
| 100 | + description: "--", |
| 101 | + image: %{ |
| 102 | + url: nil |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + end |
| 107 | + |
| 108 | + defp error_response(conn, _url, _reason) do |
| 109 | + json(conn, %{ |
| 110 | + success: 0, |
| 111 | + meta: %{ |
| 112 | + title: "unknown-error", |
| 113 | + description: nil, |
| 114 | + image: %{ |
| 115 | + url: nil |
| 116 | + } |
| 117 | + } |
| 118 | + }) |
| 119 | + end |
| 120 | +end |
0 commit comments