Commit a3c07c5d authored by Alex Castaño's avatar Alex Castaño

Fix some problems with phoenix

parent 75d9247f
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
import_deps: [:phoenix],
inputs: ["*.{ex,exs}", "{config,lib,priv,test}/**/*.{ex,exs}"]
]
......@@ -7,6 +7,9 @@
/test/uploads
/.elixir_ls
doc
/assets/node_modules
/priv/static
# Prevent committing custom emojis
/priv/static/emoji/custom/*
......
{
"presets": [
"@babel/preset-env"
]
}
nodejs 10.13.0
erlang 21.1
elixir 1.7.3
/* This file is for your main application css. */
@import "./phoenix.css";
This diff is collapsed.
// We need to import the CSS so that webpack will load it.
// The MiniCssExtractPlugin is used to separate it out into
// its own CSS file.
import css from "../css/app.scss"
// webpack automatically bundles all modules in your
// entry points. Those entry points can be configured
// in "webpack.config.js".
//
// Import dependencies
//
import "phoenix_html"
import 'bootstrap'
// Import local files
//
// Local files can be imported directly using relative paths, for example:
// import socket from "./socket"
// NOTE: The contents of this file will only be executed if
// you uncomment its entry in "assets/js/app.js".
// To use Phoenix channels, the first step is to import Socket,
// and connect at the socket path in "lib/web/endpoint.ex".
//
// Pass the token on params as below. Or remove it
// from the params if you are not using authentication.
import {Socket} from "phoenix"
let socket = new Socket("/socket", {params: {token: window.userToken}})
// When you connect, you'll often need to authenticate the client.
// For example, imagine you have an authentication plug, `MyAuth`,
// which authenticates the session and assigns a `:current_user`.
// If the current user exists you can assign the user's token in
// the connection for use in the layout.
//
// In your "lib/web/router.ex":
//
// pipeline :browser do
// ...
// plug MyAuth
// plug :put_user_token
// end
//
// defp put_user_token(conn, _) do
// if current_user = conn.assigns[:current_user] do
// token = Phoenix.Token.sign(conn, "user socket", current_user.id)
// assign(conn, :user_token, token)
// else
// conn
// end
// end
//
// Now you need to pass this token to JavaScript. You can do so
// inside a script tag in "lib/web/templates/layout/app.html.eex":
//
// <script>window.userToken = "<%= assigns[:user_token] %>";</script>
//
// You will need to verify the user token in the "connect/3" function
// in "lib/web/channels/user_socket.ex":
//
// def connect(%{"token" => token}, socket, _connect_info) do
// # max_age: 1209600 is equivalent to two weeks in seconds
// case Phoenix.Token.verify(socket, "user socket", token, max_age: 1209600) do
// {:ok, user_id} ->
// {:ok, assign(socket, :user, user_id)}
// {:error, reason} ->
// :error
// end
// end
//
// Finally, connect to the socket:
socket.connect()
// Now that you are connected, you can join channels with a topic:
let channel = socket.channel("topic:subtopic", {})
channel.join()
.receive("ok", resp => { console.log("Joined successfully", resp) })
.receive("error", resp => { console.log("Unable to join", resp) })
export default socket
This diff is collapsed.
{
"repository": {},
"license": "MIT",
"scripts": {
"deploy": "webpack --mode production",
"watch": "webpack --mode development --watch"
},
"dependencies": {
"bootstrap": "^4.1.3",
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-loader": "^8.0.0",
"copy-webpack-plugin": "^4.5.0",
"css-loader": "^0.28.10",
"mini-css-extract-plugin": "^0.4.0",
"node-sass": "^4.10.0",
"optimize-css-assets-webpack-plugin": "^4.0.0",
"sass-loader": "^7.1.0",
"uglifyjs-webpack-plugin": "^1.2.4",
"webpack": "4.4.0",
"webpack-cli": "^2.0.10"
}
}
# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
#
# To ban all spiders from the entire site uncomment the next two lines:
# User-agent: *
# Disallow: /
const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = (env, options) => ({
optimization: {
minimizer: [
new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
new OptimizeCSSAssetsPlugin({})
]
},
entry: {
'./js/app.js': ['./js/app.js'].concat(glob.sync('./vendor/**/*.js'))
},
output: {
filename: 'app.js',
path: path.resolve(__dirname, '../priv/static/js')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.s?css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
}
]
},
plugins: [
new MiniCssExtractPlugin({ filename: '../css/app.css' }),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
]
});
......@@ -108,9 +108,8 @@ config :moodle_net, :media_proxy,
# base_url: "https://cache.moodle_net.social"
config :ecto, json_library: Jason
config :phoenix, :format_encoders, json: Jason
config :phoenix, :json_library, Jason
config :moodle_net, :suggestions,
enabled: false,
......
......@@ -15,7 +15,15 @@ config :moodle_net, MoodleNetWeb.Endpoint,
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: []
watchers: [
node: [
"node_modules/webpack/bin/webpack.js",
"--mode",
"development",
"--watch-stdin",
cd: Path.expand("../assets", __DIR__)
]
]
# ## SSL Support
#
......
......@@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>ActivityPub · Phoenix Framework</title>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.scss") %>"/>
</head>
<body>
<header>
......
......@@ -4,24 +4,25 @@ defmodule Mix.Tasks.GeneratePasswordReset do
@shortdoc "Generate password reset link for user"
def run([nickname]) do
Mix.Task.run("app.start")
IO.puts("FIXME")
# Mix.Task.run("app.start")
with %User{local: true} = user <- User.get_by_nickname(nickname),
{:ok, token} <- MoodleNet.PasswordResetToken.create_token(user) do
IO.puts("Generated password reset token for #{user.nickname}")
# with %User{local: true} = user <- User.get_by_nickname(nickname),
# {:ok, token} <- MoodleNet.PasswordResetToken.create_token(user) do
# IO.puts("Generated password reset token for #{user.nickname}")
IO.puts(
"Url: #{
MoodleNetWeb.Router.Helpers.util_url(
MoodleNetWeb.Endpoint,
:show_password_reset,
token.token
)
}"
)
else
_ ->
IO.puts("No local user #{nickname}")
end
# IO.puts(
# "Url: #{
# MoodleNetWeb.Router.Helpers.util_url(
# MoodleNetWeb.Endpoint,
# :show_password_reset,
# token.token
# )
# }"
# )
# else
# _ ->
# IO.puts("No local user #{nickname}")
# end
end
end
defmodule MoodleNet.Repo do
use Ecto.Repo, otp_app: :moodle_net
use Ecto.Repo,
otp_app: :moodle_net,
adapter: Ecto.Adapters.Postgres
@doc """
Dynamically loads the repository url from the
......
......@@ -21,6 +21,7 @@ defmodule MoodleNetWeb do
use Phoenix.Controller, namespace: MoodleNetWeb
import Plug.Conn
import MoodleNetWeb.{Gettext, Router.Helpers}
alias AppWeb.Router.Helpers, as: Routes
end
end
......@@ -33,7 +34,9 @@ defmodule MoodleNetWeb do
# Import convenience functions from controllers
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
# Remove imports
import MoodleNetWeb.{ErrorHelpers, Gettext, Router.Helpers}
alias AppWeb.Router.Helpers, as: Routes
end
end
......
defmodule MoodleNetWeb.UserSocket do
use Phoenix.Socket
alias MoodleNet.User
## Channels
# channel "room:*", MoodleNetWeb.RoomChannel
## Transports
transport(:websocket, Phoenix.Transports.WebSocket)
# transport :longpoll, Phoenix.Transports.LongPoll
# channel "room:*", ActivityPubWeb.RoomChannel
# Socket params are passed from the client and can
# be used to verify and authenticate a user. After
......@@ -20,13 +15,8 @@ defmodule MoodleNetWeb.UserSocket do
#
# See `Phoenix.Token` documentation for examples in
# performing token verification on connect.
def connect(%{"token" => token}, socket) do
with {:ok, user_id} <- Phoenix.Token.verify(socket, "user socket", token, max_age: 84600),
%User{} = user <- MoodleNet.Repo.get(User, user_id) do
{:ok, assign(socket, :user_name, user.nickname)}
else
_e -> :error
end
def connect(_params, socket, _connect_info) do
{:ok, socket}
end
# Socket id's are topics that allow you to identify all sockets for a given user:
......@@ -36,7 +26,7 @@ defmodule MoodleNetWeb.UserSocket do
# Would allow you to broadcast a "disconnect" event and terminate
# all active sockets and channels for a given user:
#
# MoodleNetWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
# ActivityPubWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
#
# Returning `nil` makes this socket anonymous.
def id(_socket), do: nil
......
defmodule MoodleNetWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :moodle_net
# socket "/socket", ActivityPubWeb.UserSocket,
# websocket: true,
# longpoll: false
# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phoenix.digest
......
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset=utf-8 />
<title>Pub of the Commons</title>
<style>
body {
background-color: #282c37;
font-family: sans-serif;
color:white;
text-align: center;
}
.container {
margin: 50px auto;
max-width: 320px;
padding: 0;
padding: 40px 40px 40px 40px;
background-color: #313543;
border-radius: 4px;
}
h1 {
margin: 0;
}
h2 {
color: #9baec8;
font-weight: normal;
font-size: 20px;
margin-bottom: 40px;
}
form {
width: 100%;
}
input {
box-sizing: border-box;
width: 100%;
padding: 10px;
margin-top: 20px;
background-color: rgba(0,0,0,.1);
color: white;
border: 0;
border-bottom: 2px solid #9baec8;
font-size: 14px;
}
input:focus {
border-bottom: 2px solid #4b8ed8;
}
button {
box-sizing: border-box;
width: 100%;
color: white;
background-color: #419bdd;
border-radius: 4px;
border: none;
padding: 10px;
margin-top: 30px;
text-transform: uppercase;
font-weight: 500;
font-size: 16px;
}
</style>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>MoodleNet</title>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.scss") %>"/>
</head>
<body>
<div class="container">
<h1>Pub of the Commons</h1>
<header>
<section class="container">
<nav role="navigation">
<ul>
<li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
</ul>
</nav>
<a href="http://phoenixframework.org/" class="phx-logo">
<img src="<%= Routes.static_path(@conn, "/images/phoenix.png") %>" alt="Phoenix Framework Logo"/>
</a>
</section>
</header>
<main role="main" class="container">
<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
<p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
<%= render @view_module, @view_template, assigns %>
</div>
</main>
<script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
</body>
</html>
......@@ -40,6 +40,8 @@ defmodule MoodleNet.Mixfile do
{:jason, "~> 1.0"},
{:gettext, "~> 0.15"},
{:cowboy, "~> 2.5"},
{:plug_cowboy, "~> 2.0"},
{:plug, "~> 1.7"},
{:comeonin, "~> 4.1.1"},
{:pbkdf2_elixir, "~> 0.12.3"},
{:trailing_format_plug, "~> 0.0.7"},
......
......@@ -42,6 +42,7 @@
"phoenix_html": {:hex, :phoenix_html, "2.12.0", "1fb3c2e48b4b66d75564d8d63df6d53655469216d6b553e7e14ced2b46f97622", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.1", "6668d787e602981f24f17a5fbb69cc98f8ab085114ebfac6cc36e10a90c8e93c", [:mix], [], "hexpm"},
"plug": {:hex, :plug, "1.7.1", "8516d565fb84a6a8b2ca722e74e2cd25ca0fc9d64f364ec9dbec09d33eb78ccd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}], "hexpm"},
"plug_cowboy": {:hex, :plug_cowboy, "2.0.0", "ab0c92728f2ba43c544cce85f0f220d8d30fc0c90eaa1e6203683ab039655062", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"},
......
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