76 Commits

Author SHA1 Message Date
0ea953a34c fix tests
All checks were successful
continuous-integration/drone/push Build is passing
2023-02-05 00:48:47 -05:00
1284a0c4b1 improve oban logging 2023-02-05 00:48:47 -05:00
f10d061279 fix padding on chrome 2023-02-04 20:41:41 -05:00
ad7810a8ea use credo style 2023-02-04 17:36:27 -05:00
cd7220cea3 improve invites, record usage 2023-02-04 17:22:06 -05:00
eb75937587 improve templates 2023-02-04 13:02:09 -05:00
1726a1b72e fix bug with public registration 2023-02-04 10:31:59 -05:00
f990fafc6a add crypto to extra applications 2023-02-01 22:39:14 -05:00
2cb6aa8d33 add accounts doctests 2023-01-29 15:23:38 -05:00
24f608163f use atoms for role changeset
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-29 12:56:44 -05:00
40f301ca71 fix runtime config
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-26 19:45:51 -05:00
178111ce80 add qr code for invite link
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-26 00:46:27 -05:00
50c4878453 improve formatting 2023-01-26 00:36:32 -05:00
51e6f49d17 use list for classes 2023-01-25 23:11:31 -05:00
9289b79afa fix padding at bottom of page in chrome 2023-01-25 22:13:53 -05:00
87f4d18a6d add background to root 2023-01-25 22:06:24 -05:00
30bee92e37 improve locale 2023-01-25 21:53:58 -05:00
f32d52db26 fix webpack 2023-01-25 21:20:53 -05:00
d77421613c use topbar through npm 2023-01-25 21:19:41 -05:00
0d61e5ee96 bump version
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-23 00:06:25 -05:00
89413df8c4 fix gettext 2023-01-23 00:05:59 -05:00
8ccd94818c make loading screen less intrusive 2023-01-23 00:05:59 -05:00
482f339da2 show topbar in more situations 2023-01-23 00:05:59 -05:00
af911f211c update dependencies 2023-01-23 00:05:59 -05:00
7813738f91 add json data export
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-20 19:15:08 -05:00
c1337ebc10 fix formatting in note/context/step contents 2022-12-20 18:36:56 -05:00
59283a0217 fix overflow on note/contexts/step contents
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-19 23:16:12 -05:00
634891ee73 Added translation using Weblate (German)
Some checks are pending
continuous-integration/drone/push Build is running
2022-12-20 03:35:57 +00:00
571f6fffdb improve tagging logic
Some checks are pending
continuous-integration/drone/push Build is running
2022-12-19 22:34:00 -05:00
926805ba9b fix user invite page 2022-12-19 21:14:14 -05:00
220122dec6 add newlines to content components 2022-12-19 21:09:57 -05:00
de399b4819 fix broken docker-compose 2022-12-19 21:09:50 -05:00
c3ceb877b2 search tags when on click
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-15 22:52:42 -05:00
0b4449c8a8 backlink to other notes in notes 2022-12-15 22:45:48 -05:00
6452f64fec check for slug uniqueness before submitting
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-28 00:20:57 -05:00
c0afc96b8d add to faq 2022-11-28 00:14:16 -05:00
2cfecc54a0 fix more typos 2022-11-27 22:55:23 -05:00
501eb5d560 use project version on homepage
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-27 21:33:06 -05:00
16c4ae16d3 standardize changelog
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-27 21:25:17 -05:00
516fe4a9f7 include weblate changes 2022-11-27 21:24:47 -05:00
4a15f27923 fix some typos 2022-11-27 21:23:27 -05:00
dcfd1b87df improve table information 2022-11-27 21:18:35 -05:00
c0df3440f5 improve search 2022-11-27 21:10:54 -05:00
6f03bee761 fix gettext for emails 2022-11-27 19:58:56 -05:00
fe91d8de6d add german
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-27 13:02:32 -05:00
7fe636a99e restyle emails
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-27 12:59:16 -05:00
a94407a216 fix bug with email
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-27 12:06:26 -05:00
70de3f42a4 attempt to fix libcrypto error
Some checks are pending
continuous-integration/drone/push Build is running
2022-11-27 11:49:17 -05:00
03890d2431 update drone
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-27 01:18:04 -05:00
30535fe5e1 add german
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-27 00:10:40 -05:00
b7464cff38 update drone
Some checks failed
continuous-integration/drone/push Build is failing
2022-11-26 23:26:53 -05:00
769a7838a5 add screenshot
Some checks failed
continuous-integration/drone/push Build is failing
2022-11-26 23:24:23 -05:00
b8bb36bfd3 finish faq 2022-11-26 23:14:58 -05:00
8fcbb7aced add steps 2022-11-26 21:32:12 -05:00
1ba5b9ec41 remove step contexts and context notes 2022-11-26 21:32:12 -05:00
443ff86aee use private as default visibility 2022-11-26 21:32:12 -05:00
5ee7071dff rename to memEx 2022-11-26 21:32:12 -05:00
1802e54caf improve hr styling 2022-11-26 20:38:32 -05:00
3dceb17085 display note backlinks in contexts 2022-11-26 20:38:32 -05:00
ca5b29c914 fix home page title 2022-11-26 20:38:31 -05:00
264f13e523 use slugs 2022-11-26 20:38:31 -05:00
e9360fb3d5 fix page titles 2022-11-26 20:37:56 -05:00
44c8cf77bb only show action buttons when appropriate 2022-11-26 20:37:56 -05:00
632c2b3480 add Accounts.is_already_admin? check 2022-11-26 20:37:56 -05:00
852e60dd14 make topbar thinner 2022-11-26 20:37:56 -05:00
85c4559d1f work on steps 2022-11-26 20:37:56 -05:00
7195c7fba6 work on pipelines 2022-11-26 20:37:56 -05:00
ad457b428a improve placeholder text style 2022-11-26 20:37:56 -05:00
cc11491106 work on contexts 2022-11-26 20:37:55 -05:00
a0a0697f2d more changes to notes 2022-11-24 15:52:57 -05:00
2ce4fe3cc8 improve accounts context 2022-11-24 15:52:45 -05:00
f9be5229e7 add search to notes 2022-11-24 15:52:27 -05:00
b95d3039bb remove transition all on input 2022-11-24 15:52:09 -05:00
3c250e9064 alias endpoint 2022-11-24 15:51:58 -05:00
6f80ef35a1 display topbar when user is logged out 2022-11-24 15:51:43 -05:00
1a508a42ef implement notes 2022-11-24 15:51:06 -05:00
161 changed files with 9048 additions and 3485 deletions

View File

@ -157,17 +157,17 @@
# #
# Controversial and experimental checks (opt-in, just replace `false` with `[]`) # Controversial and experimental checks (opt-in, just replace `false` with `[]`)
# #
{Credo.Check.Consistency.MultiAliasImportRequireUse, false}, {Credo.Check.Consistency.MultiAliasImportRequireUse, []},
{Credo.Check.Consistency.UnusedVariableNames, false}, {Credo.Check.Consistency.UnusedVariableNames, [force: :meaningful]},
{Credo.Check.Design.DuplicatedCode, false}, {Credo.Check.Design.DuplicatedCode, false},
{Credo.Check.Readability.AliasAs, false}, {Credo.Check.Readability.AliasAs, false},
{Credo.Check.Readability.BlockPipe, false}, {Credo.Check.Readability.BlockPipe, false},
{Credo.Check.Readability.ImplTrue, false}, {Credo.Check.Readability.ImplTrue, false},
{Credo.Check.Readability.MultiAlias, false}, {Credo.Check.Readability.MultiAlias, false},
{Credo.Check.Readability.SeparateAliasRequire, false}, {Credo.Check.Readability.SeparateAliasRequire, []},
{Credo.Check.Readability.SinglePipe, false}, {Credo.Check.Readability.SinglePipe, false},
{Credo.Check.Readability.Specs, false}, {Credo.Check.Readability.Specs, false},
{Credo.Check.Readability.StrictModuleLayout, false}, {Credo.Check.Readability.StrictModuleLayout, []},
{Credo.Check.Readability.WithCustomTaggedTuple, false}, {Credo.Check.Readability.WithCustomTaggedTuple, false},
{Credo.Check.Refactor.ABCSize, false}, {Credo.Check.Refactor.ABCSize, false},
{Credo.Check.Refactor.AppendSingleItem, false}, {Credo.Check.Refactor.AppendSingleItem, false},
@ -176,9 +176,9 @@
{Credo.Check.Refactor.NegatedIsNil, false}, {Credo.Check.Refactor.NegatedIsNil, false},
{Credo.Check.Refactor.PipeChainStart, false}, {Credo.Check.Refactor.PipeChainStart, false},
{Credo.Check.Refactor.VariableRebinding, false}, {Credo.Check.Refactor.VariableRebinding, false},
{Credo.Check.Warning.LeakyEnvironment, false}, {Credo.Check.Warning.LeakyEnvironment, []},
{Credo.Check.Warning.MapGetUnsafePass, false}, {Credo.Check.Warning.MapGetUnsafePass, []},
{Credo.Check.Warning.UnsafeToAtom, false} {Credo.Check.Warning.UnsafeToAtom, []}
# #
# Custom checks can be created using `mix credo.gen.check`. # Custom checks can be created using `mix credo.gen.check`.

View File

@ -16,7 +16,7 @@ steps:
- assets/node_modules/ - assets/node_modules/
- name: test - name: test
image: elixir:1.13.4-alpine image: elixir:1.14.1-alpine
environment: environment:
TEST_DATABASE_URL: ecto://postgres:postgres@database/memex_test TEST_DATABASE_URL: ecto://postgres:postgres@database/memex_test
HOST: testing.example.tld HOST: testing.example.tld
@ -29,7 +29,7 @@ steps:
- npm --prefix ./assets ci --progress=false --no-audit --loglevel=error - npm --prefix ./assets ci --progress=false --no-audit --loglevel=error
- npm run --prefix ./assets deploy - npm run --prefix ./assets deploy
- mix do phx.digest, gettext.extract - mix do phx.digest, gettext.extract
- mix test - mix test.all
- name: build and publish stable - name: build and publish stable
image: thegeeklab/drone-docker-buildx image: thegeeklab/drone-docker-buildx
@ -38,7 +38,7 @@ steps:
repo: shibaobun/memex repo: shibaobun/memex
purge: true purge: true
compress: true compress: true
platforms: linux/amd64,linux/arm64,linux/arm/v7 platforms: linux/amd64,linux/arm/v7
username: username:
from_secret: docker_username from_secret: docker_username
password: password:
@ -55,7 +55,7 @@ steps:
repo: shibaobun/memex repo: shibaobun/memex
purge: true purge: true
compress: true compress: true
platforms: linux/amd64,linux/arm64,linux/arm/v7 platforms: linux/amd64,linux/arm/v7
username: username:
from_secret: docker_username from_secret: docker_username
password: password:

View File

@ -1,4 +1,4 @@
FROM elixir:1.13-alpine AS build FROM elixir:1.14.1-alpine AS build
# install build dependencies # install build dependencies
RUN apk add --no-cache build-base npm git python3 RUN apk add --no-cache build-base npm git python3
@ -37,7 +37,7 @@ RUN mix do compile, release
FROM alpine:latest AS app FROM alpine:latest AS app
RUN apk upgrade --no-cache && \ RUN apk upgrade --no-cache && \
apk add --no-cache bash openssl libgcc libstdc++ ncurses-libs apk add --no-cache bash openssl libssl1.1 libcrypto1.1 libgcc libstdc++ ncurses-libs
WORKDIR /app WORKDIR /app

View File

@ -25,12 +25,13 @@ $fa-font-path: "@fortawesome/fontawesome-free/webfonts";
100% { scale: 1.0; opacity: 1; } 100% { scale: 1.0; opacity: 1; }
} }
.phx-connected > #disconnect, #loading { // disconnect toast
.phx-connected > #disconnect {
opacity: 0 !important; opacity: 0 !important;
pointer-events: none; pointer-events: none;
} }
.phx-loading:not(.phx-error) > #loading, .phx-error > #disconnect { .phx-error > #disconnect {
opacity: 0.95 !important; opacity: 0.95 !important;
} }

View File

@ -2,13 +2,12 @@
.input { .input {
@apply rounded-lg px-4 py-2 border focus:outline-none; @apply rounded-lg px-4 py-2 border focus:outline-none;
@apply shadow-sm focus:shadow-lg; @apply shadow-sm focus:shadow-lg;
@apply transition-all duration-300 ease-in-out;
} }
.input-primary { .input-primary {
@apply bg-primary-900; @apply bg-primary-900;
@apply border-primary-900 hover:border-primary-800 active:border-primary-700; @apply border-primary-900 hover:border-primary-800 active:border-primary-700;
@apply text-primary-400; @apply text-primary-400 placeholder-primary-600;
} }
.checkbox { .checkbox {
@ -45,11 +44,7 @@
} }
.hr { .hr {
@apply border border-primary-400 w-full max-w-2xl; @apply mx-auto border border-primary-600 w-full max-w-2xl;
}
.hr-light {
@apply border border-primary-600 w-full max-w-2xl;
} }
.link { .link {
@ -79,7 +74,7 @@
} }
.alert p { .alert p {
margin-bottom: 0; @apply mb-0;
} }
.alert:empty { .alert:empty {

View File

@ -25,11 +25,13 @@ import 'phoenix_html'
// Establish Phoenix Socket and LiveView configuration. // Establish Phoenix Socket and LiveView configuration.
import { Socket } from 'phoenix' import { Socket } from 'phoenix'
import { LiveSocket } from 'phoenix_live_view' import { LiveSocket } from 'phoenix_live_view'
import topbar from '../vendor/topbar' import topbar from 'topbar'
import MaintainAttrs from './maintain_attrs' import MaintainAttrs from './maintain_attrs'
import Alpine from 'alpinejs' import Alpine from 'alpinejs'
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute('content') const csrfTokenElement = document.querySelector("meta[name='csrf-token']")
let csrfToken
if (csrfTokenElement) { csrfToken = csrfTokenElement.getAttribute('content') }
const liveSocket = new LiveSocket('/live', Socket, { const liveSocket = new LiveSocket('/live', Socket, {
dom: { dom: {
onBeforeElUpdated (from, to) { onBeforeElUpdated (from, to) {
@ -45,9 +47,11 @@ window.Alpine = Alpine
Alpine.start() Alpine.start()
// Show progress bar on live navigation and form submits // Show progress bar on live navigation and form submits
topbar.config({ barColors: { 0: '#29d' }, shadowColor: 'rgba(0, 0, 0, .3)' }) topbar.config({ barThickness: 1, barColors: { 0: '#fff' }, shadowColor: 'rgba(0, 0, 0, .3)' })
window.addEventListener('phx:page-loading-start', info => topbar.show()) window.addEventListener('phx:page-loading-start', info => topbar.show())
window.addEventListener('phx:page-loading-stop', info => topbar.hide()) window.addEventListener('phx:page-loading-stop', info => topbar.hide())
window.addEventListener('submit', info => topbar.show())
window.addEventListener('beforeunload', info => topbar.show())
// connect if there are any LiveViews on the page // connect if there are any LiveViews on the page
liveSocket.connect() liveSocket.connect()

View File

@ -2,7 +2,21 @@
// update. https://github.com/phoenixframework/phoenix_live_view/issues/1011 // update. https://github.com/phoenixframework/phoenix_live_view/issues/1011
export default { export default {
attrs () { return this.el.getAttribute('data-attrs').split(', ') }, attrs () {
beforeUpdate () { this.prevAttrs = this.attrs().map(name => [name, this.el.getAttribute(name)]) }, if (this.el && this.el.getAttribute('data-attrs')) {
updated () { this.prevAttrs.forEach(([name, val]) => this.el.setAttribute(name, val)) } return this.el.getAttribute('data-attrs').split(', ')
} else {
return []
}
},
beforeUpdate () {
if (this.el) {
this.prevAttrs = this.attrs().map(name => [name, this.el.getAttribute(name)])
}
},
updated () {
if (this.el) {
this.prevAttrs.forEach(([name, val]) => this.el.setAttribute(name, val))
}
}
} }

1482
assets/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,10 @@
"repository": {}, "repository": {},
"description": " ", "description": " ",
"license": "MIT", "license": "MIT",
"engines": {
"node": "18.12.1",
"npm": "8.19.2"
},
"scripts": { "scripts": {
"deploy": "NODE_ENV=production webpack --mode production", "deploy": "NODE_ENV=production webpack --mode production",
"watch": "webpack --mode development --watch --watch-options-stdin", "watch": "webpack --mode development --watch --watch-options-stdin",
@ -26,16 +30,14 @@
"css-loader": "^6.7.1", "css-loader": "^6.7.1",
"css-minimizer-webpack-plugin": "^3.4.1", "css-minimizer-webpack-plugin": "^3.4.1",
"file-loader": "^6.2.0", "file-loader": "^6.2.0",
"hard-source-webpack-plugin": "^0.13.1",
"mini-css-extract-plugin": "^2.6.0", "mini-css-extract-plugin": "^2.6.0",
"node-sass": "^7.0.1",
"postcss": "^8.4.13", "postcss": "^8.4.13",
"postcss-import": "^14.1.0", "postcss-import": "^14.1.0",
"postcss-loader": "^6.2.1", "postcss-loader": "^6.2.1",
"postcss-preset-env": "^7.5.0", "postcss-preset-env": "^7.5.0",
"sass": "^1.56.0",
"sass-loader": "^12.6.0", "sass-loader": "^12.6.0",
"standard": "^17.0.0", "standard": "^17.0.0",
"style-loader": "^3.3.1",
"tailwindcss": "^3.0.24", "tailwindcss": "^3.0.24",
"terser-webpack-plugin": "^5.3.1", "terser-webpack-plugin": "^5.3.1",
"webpack": "^5.72.0", "webpack": "^5.72.0",

View File

@ -1,157 +0,0 @@
/**
* @license MIT
* topbar 1.0.0, 2021-01-06
* https://buunguyen.github.io/topbar
* Copyright (c) 2021 Buu Nguyen
*/
(function (window, document) {
"use strict";
// https://gist.github.com/paulirish/1579671
(function () {
var lastTime = 0;
var vendors = ["ms", "moz", "webkit", "o"];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame =
window[vendors[x] + "RequestAnimationFrame"];
window.cancelAnimationFrame =
window[vendors[x] + "CancelAnimationFrame"] ||
window[vendors[x] + "CancelRequestAnimationFrame"];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
})();
var canvas,
progressTimerId,
fadeTimerId,
currentProgress,
showing,
addEvent = function (elem, type, handler) {
if (elem.addEventListener) elem.addEventListener(type, handler, false);
else if (elem.attachEvent) elem.attachEvent("on" + type, handler);
else elem["on" + type] = handler;
},
options = {
autoRun: true,
barThickness: 3,
barColors: {
0: "rgba(26, 188, 156, .9)",
".25": "rgba(52, 152, 219, .9)",
".50": "rgba(241, 196, 15, .9)",
".75": "rgba(230, 126, 34, .9)",
"1.0": "rgba(211, 84, 0, .9)",
},
shadowBlur: 10,
shadowColor: "rgba(0, 0, 0, .6)",
className: null,
},
repaint = function () {
canvas.width = window.innerWidth;
canvas.height = options.barThickness * 5; // need space for shadow
var ctx = canvas.getContext("2d");
ctx.shadowBlur = options.shadowBlur;
ctx.shadowColor = options.shadowColor;
var lineGradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
for (var stop in options.barColors)
lineGradient.addColorStop(stop, options.barColors[stop]);
ctx.lineWidth = options.barThickness;
ctx.beginPath();
ctx.moveTo(0, options.barThickness / 2);
ctx.lineTo(
Math.ceil(currentProgress * canvas.width),
options.barThickness / 2
);
ctx.strokeStyle = lineGradient;
ctx.stroke();
},
createCanvas = function () {
canvas = document.createElement("canvas");
var style = canvas.style;
style.position = "fixed";
style.top = style.left = style.right = style.margin = style.padding = 0;
style.zIndex = 100001;
style.display = "none";
if (options.className) canvas.classList.add(options.className);
document.body.appendChild(canvas);
addEvent(window, "resize", repaint);
},
topbar = {
config: function (opts) {
for (var key in opts)
if (options.hasOwnProperty(key)) options[key] = opts[key];
},
show: function () {
if (showing) return;
showing = true;
if (fadeTimerId !== null) window.cancelAnimationFrame(fadeTimerId);
if (!canvas) createCanvas();
canvas.style.opacity = 1;
canvas.style.display = "block";
topbar.progress(0);
if (options.autoRun) {
(function loop() {
progressTimerId = window.requestAnimationFrame(loop);
topbar.progress(
"+" + 0.05 * Math.pow(1 - Math.sqrt(currentProgress), 2)
);
})();
}
},
progress: function (to) {
if (typeof to === "undefined") return currentProgress;
if (typeof to === "string") {
to =
(to.indexOf("+") >= 0 || to.indexOf("-") >= 0
? currentProgress
: 0) + parseFloat(to);
}
currentProgress = to > 1 ? 1 : to;
repaint();
return currentProgress;
},
hide: function () {
if (!showing) return;
showing = false;
if (progressTimerId != null) {
window.cancelAnimationFrame(progressTimerId);
progressTimerId = null;
}
(function loop() {
if (topbar.progress("+.1") >= 1) {
canvas.style.opacity -= 0.05;
if (canvas.style.opacity <= 0.05) {
canvas.style.display = "none";
fadeTimerId = null;
return;
}
}
fadeTimerId = window.requestAnimationFrame(loop);
})();
},
};
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = topbar;
} else if (typeof define === "function" && define.amd) {
define(function () {
return topbar;
});
} else {
this.topbar = topbar;
}
}.call(this, window, document));

43
changelog.md Normal file
View File

@ -0,0 +1,43 @@
# v0.1.8
- Fix bug with public registration
- Improve templates
- Improve invites, record usage
- Fix padding on more pages when using chrome
- Add oban metrics to server log and live dashboard
# v0.1.7
- Update dependencies
- Show topbar on form submit/page refresh
- Make loading/reconnection less intrusive
- Add QR code for invite link
# v0.1.6
- fix formatting in note/context/step contents
- add json export for data
# v0.1.5
- fix overflow on note/contexts/step contents
# v0.1.4
- fix docker-compose
- fix newlines in note/context/step contents
- fix user invite page
- improve tagging logic
# v0.1.3
- backlink to other notes in notes
- search tags on click
# v0.1.2
- fix more typos
- add to faq
- check for slug uniqueness before submitting
# v0.1.1
- improve search a whole lot
- improve table information for notes and contexts
- fix some typos
- use project version on homepage
# v0.1.0
- initial release >:3c

View File

@ -11,6 +11,8 @@ config :memex,
ecto_repos: [Memex.Repo], ecto_repos: [Memex.Repo],
generators: [binary_id: true] generators: [binary_id: true]
config :memex, Memex.Accounts, registration: System.get_env("REGISTRATION", "invite")
# Configures the endpoint # Configures the endpoint
config :memex, MemexWeb.Endpoint, config :memex, MemexWeb.Endpoint,
url: [scheme: "https", host: System.get_env("HOST") || "localhost", port: "443"], url: [scheme: "https", host: System.get_env("HOST") || "localhost", port: "443"],
@ -18,8 +20,7 @@ config :memex, MemexWeb.Endpoint,
secret_key_base: "KH59P0iZixX5gP/u+zkxxG8vAAj6vgt0YqnwEB5JP5K+E567SsqkCz69uWShjE7I", secret_key_base: "KH59P0iZixX5gP/u+zkxxG8vAAj6vgt0YqnwEB5JP5K+E567SsqkCz69uWShjE7I",
render_errors: [view: MemexWeb.ErrorView, accepts: ~w(html json), layout: false], render_errors: [view: MemexWeb.ErrorView, accepts: ~w(html json), layout: false],
pubsub_server: Memex.PubSub, pubsub_server: Memex.PubSub,
live_view: [signing_salt: "zOLgd3lr"], live_view: [signing_salt: "zOLgd3lr"]
registration: System.get_env("REGISTRATION") || "invite"
config :memex, Memex.Application, automigrate: false config :memex, Memex.Application, automigrate: false

View File

@ -64,8 +64,9 @@ config :memex, MemexWeb.Endpoint,
] ]
] ]
# Do not include metadata nor timestamps in development logs config :logger, :console,
config :logger, :console, format: "[$level] $message\n" format: "[$level] $message $metadata\n\n",
metadata: [:data]
# Set a higher stacktrace during development. Avoid configuring such # Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive. # in production as building large stacktraces may be expensive.

View File

@ -13,17 +13,18 @@ if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do
end end
# Set default locale # Set default locale
config :gettext, :default_locale, System.get_env("LOCALE") || "en_US" config :gettext, :default_locale, System.get_env("LOCALE", "en_US")
maybe_ipv6 = if System.get_env("ECTO_IPV6") == "true", do: [:inet6], else: [] maybe_ipv6 = if System.get_env("ECTO_IPV6") == "true", do: [:inet6], else: []
database_url = database_url =
if config_env() == :test do if config_env() == :test do
System.get_env("TEST_DATABASE_URL") || System.get_env(
"TEST_DATABASE_URL",
"ecto://postgres:postgres@localhost/memex_test#{System.get_env("MIX_TEST_PARTITION")}" "ecto://postgres:postgres@localhost/memex_test#{System.get_env("MIX_TEST_PARTITION")}"
)
else else
System.get_env("DATABASE_URL") || System.get_env("DATABASE_URL", "ecto://postgres:postgres@memex-db/memex")
"ecto://postgres:postgres@memex-db/memex"
end end
host = host =
@ -38,7 +39,7 @@ interface =
config :memex, Memex.Repo, config :memex, Memex.Repo,
# ssl: true, # ssl: true,
url: database_url, url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"), pool_size: String.to_integer(System.get_env("POOL_SIZE", "10")),
socket_options: maybe_ipv6 socket_options: maybe_ipv6
config :memex, MemexWeb.Endpoint, config :memex, MemexWeb.Endpoint,
@ -47,10 +48,13 @@ config :memex, MemexWeb.Endpoint,
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
# for details about using IPv6 vs IPv4 and loopback vs public addresses. # for details about using IPv6 vs IPv4 and loopback vs public addresses.
ip: interface, ip: interface,
port: String.to_integer(System.get_env("PORT") || "4000") port: String.to_integer(System.get_env("PORT", "4000"))
], ],
server: true, server: true
registration: System.get_env("REGISTRATION") || "invite"
if config_env() in [:dev, :prod] do
config :memex, Memex.Accounts, registration: System.get_env("REGISTRATION", "invite")
end
if config_env() == :prod do if config_env() == :prod do
# The secret key base is used to sign/encrypt cookies and other secrets. # The secret key base is used to sign/encrypt cookies and other secrets.
@ -62,7 +66,7 @@ if config_env() == :prod do
System.get_env("SECRET_KEY_BASE") || System.get_env("SECRET_KEY_BASE") ||
raise """ raise """
environment variable SECRET_KEY_BASE is missing. environment variable SECRET_KEY_BASE is missing.
You can generate one by calling: mix phx.gen.secret You can generate one by running: mix phx.gen.secret
""" """
config :memex, MemexWeb.Endpoint, secret_key_base: secret_key_base config :memex, MemexWeb.Endpoint, secret_key_base: secret_key_base
@ -74,12 +78,12 @@ if config_env() == :prod do
config :memex, Memex.Mailer, config :memex, Memex.Mailer,
adapter: Swoosh.Adapters.SMTP, adapter: Swoosh.Adapters.SMTP,
relay: System.get_env("SMTP_HOST") || raise("No SMTP_HOST set!"), relay: System.get_env("SMTP_HOST") || raise("No SMTP_HOST set!"),
port: System.get_env("SMTP_PORT") || 587, port: System.get_env("SMTP_PORT", "587"),
username: System.get_env("SMTP_USERNAME") || raise("No SMTP_USERNAME set!"), username: System.get_env("SMTP_USERNAME") || raise("No SMTP_USERNAME set!"),
password: System.get_env("SMTP_PASSWORD") || raise("No SMTP_PASSWORD set!"), password: System.get_env("SMTP_PASSWORD") || raise("No SMTP_PASSWORD set!"),
ssl: System.get_env("SMTP_SSL") == "true", ssl: System.get_env("SMTP_SSL") == "true",
email_from: System.get_env("EMAIL_FROM") || "no-reply@#{System.get_env("HOST")}", email_from: System.get_env("EMAIL_FROM", "no-reply@#{System.get_env("HOST")}"),
email_name: System.get_env("EMAIL_NAME") || "Memex" email_name: System.get_env("EMAIL_NAME", "memEx")
# ## Using releases # ## Using releases
# #

View File

@ -22,6 +22,9 @@ config :memex, MemexWeb.Endpoint,
# In test we don't send emails. # In test we don't send emails.
config :memex, Memex.Mailer, adapter: Swoosh.Adapters.Test config :memex, Memex.Mailer, adapter: Swoosh.Adapters.Test
# Don't require invites for signups
config :memex, Memex.Accounts, registration: "public"
# Print only warnings and errors during test # Print only warnings and errors during test
config :logger, level: :warn config :logger, level: :warn

View File

@ -77,14 +77,14 @@ Check them out!
For development, I recommend setting environment variables with For development, I recommend setting environment variables with
[direnv](https://direnv.net). [direnv](https://direnv.net).
By default, Memex will always bind to all external IPv4 and IPv6 addresses in By default, memEx will always bind to all external IPv4 and IPv6 addresses in
`dev` and `prod` mode, respectively. If you would like to use different values, `dev` and `prod` mode, respectively. If you would like to use different values,
they will need to be overridden in `config/dev.exs` and `config/runtime.exs` for they will need to be overridden in `config/dev.exs` and `config/runtime.exs` for
`dev` and `prod` modes, respectively. `dev` and `prod` modes, respectively.
## `MIX_ENV=dev` ## `MIX_ENV=dev`
In `dev` mode, Memex will listen for these environment variables at runtime. In `dev` mode, memEx will listen for these environment variables at runtime.
- `HOST`: External url to generate links with. Set this especially if you're - `HOST`: External url to generate links with. Set this especially if you're
behind a reverse proxy. Defaults to `localhost`. External URLs will always be behind a reverse proxy. Defaults to `localhost`. External URLs will always be
@ -100,7 +100,7 @@ In `dev` mode, Memex will listen for these environment variables at runtime.
## `MIX_ENV=test` ## `MIX_ENV=test`
In `test` mode (or in the Docker container), Memex will listen for the same environment variables as dev mode, but also include the following at runtime: In `test` mode (or in the Docker container), memEx will listen for the same environment variables as dev mode, but also include the following at runtime:
- `TEST_DATABASE_URL`: REPLACES `DATABASE_URL`. Controls the database url to - `TEST_DATABASE_URL`: REPLACES `DATABASE_URL`. Controls the database url to
connect to. Defaults to `ecto://postgres:postgres@localhost/memex_test`. connect to. Defaults to `ecto://postgres:postgres@localhost/memex_test`.
@ -110,7 +110,7 @@ In `test` mode (or in the Docker container), Memex will listen for the same envi
## `MIX_ENV=prod` ## `MIX_ENV=prod`
In `prod` mode (or in the Docker container), Memex will listen for the same environment variables as dev mode, but also include the following at runtime: In `prod` mode (or in the Docker container), memEx will listen for the same environment variables as dev mode, but also include the following at runtime:
- `SECRET_KEY_BASE`: Secret key base used to sign cookies. Must be generated - `SECRET_KEY_BASE`: Secret key base used to sign cookies. Must be generated
with `docker run -it shibaobun/memex mix phx.gen.secret` and set for server to start. with `docker run -it shibaobun/memex mix phx.gen.secret` and set for server to start.
@ -121,4 +121,4 @@ In `prod` mode (or in the Docker container), Memex will listen for the same envi
- `SMTP_SSL`: Set to `true` to enable SSL for emails. Defaults to `false`. - `SMTP_SSL`: Set to `true` to enable SSL for emails. Defaults to `false`.
- `EMAIL_FROM`: Sets the sender email in sent emails. Defaults to - `EMAIL_FROM`: Sets the sender email in sent emails. Defaults to
`no-reply@HOST` where `HOST` was previously defined. `no-reply@HOST` where `HOST` was previously defined.
- `EMAIL_NAME`: Sets the sender name in sent emails. Defaults to "Memex". - `EMAIL_NAME`: Sets the sender name in sent emails. Defaults to "memEx".

10
de.tbx Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!DOCTYPE martif PUBLIC "ISO 12200:1999A//DTD MARTIF core (DXFcdV04)//EN" "TBXcdv04.dtd">
<martif type="TBX">
<martifHeader>
<fileDesc>
<sourceDesc><p>Translate Toolkit</p></sourceDesc>
</fileDesc>
</martifHeader>
<text><body></body></text>
</martif>

View File

@ -2,8 +2,7 @@ version: '3'
services: services:
memex: memex:
build: image: shibaobun/memex
context: .
container_name: memex container_name: memex
restart: always restart: always
environment: environment:
@ -25,8 +24,8 @@ services:
# - SMTP_SSL=false # - SMTP_SSL=false
# optional, default is format below # optional, default is format below
# - EMAIL_FROM=no-reply@memex.example.tld # - EMAIL_FROM=no-reply@memex.example.tld
# optional, default is "Memex" # optional, default is "memEx"
# - EMAIL_NAME=Memex # - EMAIL_NAME=memEx
expose: expose:
- "4000" - "4000"
depends_on: depends_on:

BIN
home.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 KiB

View File

@ -5,7 +5,7 @@ defmodule Memex.Accounts do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Memex.{Mailer, Repo} alias Memex.{Mailer, Repo}
alias Memex.Accounts.{User, UserToken} alias Memex.Accounts.{Invite, Invites, User, UserToken}
alias Ecto.{Changeset, Multi} alias Ecto.{Changeset, Multi}
alias Oban.Job alias Oban.Job
@ -16,29 +16,33 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> get_user_by_email("foo@example.com") iex> register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> with %User{} <- get_user_by_email("foo@example.com"), do: :passed
:passed
iex> get_user_by_email("unknown@example.com") iex> get_user_by_email("unknown@example.com")
nil nil
""" """
@spec get_user_by_email(String.t()) :: User.t() | nil @spec get_user_by_email(email :: String.t()) :: User.t() | nil
def get_user_by_email(email) when is_binary(email), do: Repo.get_by(User, email: email) def get_user_by_email(email) when is_binary(email) do
Repo.get_by(User, email: email)
end
@doc """ @doc """
Gets a user by email and password. Gets a user by email and password.
## Examples ## Examples
iex> get_user_by_email_and_password("foo@example.com", "correct_password") iex> register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> with %User{} <- get_user_by_email_and_password("foo@example.com", "valid_password"), do: :passed
:passed
iex> get_user_by_email_and_password("foo@example.com", "invalid_password") iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
nil nil
""" """
@spec get_user_by_email_and_password(String.t(), String.t()) :: @spec get_user_by_email_and_password(email :: String.t(), password :: String.t()) ::
User.t() | nil User.t() | nil
def get_user_by_email_and_password(email, password) def get_user_by_email_and_password(email, password)
when is_binary(email) and is_binary(password) do when is_binary(email) and is_binary(password) do
@ -53,28 +57,33 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> get_user!(123) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> get_user!(user.id)
user
iex> get_user!(456) > get_user!()
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
@spec get_user!(User.t()) :: User.t() @spec get_user!(User.t()) :: User.t()
def get_user!(id), do: Repo.get!(User, id) def get_user!(id) do
Repo.get!(User, id)
end
@doc """ @doc """
Returns all users grouped by role. Returns all users grouped by role.
## Examples ## Examples
iex> list_users_by_role(%User{id: 123, role: :admin}) iex> {:ok, user1} = register_user(%{email: "foo1@example.com", password: "valid_password"})
[admin: [%User{}], user: [%User{}, %User{}]] iex> {:ok, user2} = register_user(%{email: "foo2@example.com", password: "valid_password"})
iex> with %{admin: [^user1], user: [^user2]} <- list_all_users_by_role(user1), do: :passed
:passed
""" """
@spec list_all_users_by_role(User.t()) :: %{String.t() => [User.t()]} @spec list_all_users_by_role(User.t()) :: %{User.role() => [User.t()]}
def list_all_users_by_role(%User{role: :admin}) do def list_all_users_by_role(%User{role: :admin}) do
Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn user -> user.role end) Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn %{role: role} -> role end)
end end
@doc """ @doc """
@ -82,13 +91,13 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> list_users_by_role(%User{id: 123, role: :admin}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
[%User{}] iex> with [^user] <- list_users_by_role(:admin), do: :passed
:passed
""" """
@spec list_users_by_role(:admin | :user) :: [User.t()] @spec list_users_by_role(:admin) :: [User.t()]
def list_users_by_role(role) do def list_users_by_role(:admin = role) do
role = role |> to_string()
Repo.all(from u in User, where: u.role == ^role, order_by: u.email) Repo.all(from u in User, where: u.role == ^role, order_by: u.email)
end end
@ -99,22 +108,40 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> register_user(%{field: value}) iex> with {:ok, %User{email: "foo@example.com"}} <-
{:ok, %User{}} ...> register_user(%{email: "foo@example.com", password: "valid_password"}),
...> do: :passed
:passed
iex> register_user(%{field: bad_value}) iex> with {:error, %Changeset{}} <- register_user(%{email: "foo@example"}), do: :passed
{:error, %Changeset{}} :passed
""" """
@spec register_user(map()) :: {:ok, User.t()} | {:error, Changeset.t(User.new_user())} @spec register_user(attrs :: map()) ::
def register_user(attrs) do {:ok, User.t()} | {:error, :invalid_token | User.changeset()}
# if no registered users, make first user an admin @spec register_user(attrs :: map(), Invite.token() | nil) ::
role = {:ok, User.t()} | {:error, :invalid_token | User.changeset()}
if Repo.one!(from u in User, select: count(u.id), distinct: true) == 0, def register_user(attrs, invite_token \\ nil) do
do: "admin", Multi.new()
else: "user" |> Multi.one(:users_count, from(u in User, select: count(u.id), distinct: true))
|> Multi.run(:use_invite, fn _changes_so_far, _repo ->
%User{} |> User.registration_changeset(attrs |> Map.put("role", role)) |> Repo.insert() if allow_registration?() and invite_token |> is_nil() do
{:ok, nil}
else
Invites.use_invite(invite_token)
end
end)
|> Multi.insert(:add_user, fn %{users_count: count, use_invite: invite} ->
# if no registered users, make first user an admin
role = if count == 0, do: :admin, else: :user
User.registration_changeset(attrs, invite) |> User.role_changeset(role)
end)
|> Repo.transaction()
|> case do
{:ok, %{add_user: user}} -> {:ok, user}
{:error, :use_invite, :invalid_token, _changes_so_far} -> {:error, :invalid_token}
{:error, :add_user, changeset, _changes_so_far} -> {:error, changeset}
end
end end
@doc """ @doc """
@ -122,16 +149,18 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> change_user_registration(user) iex> with %Changeset{} <- change_user_registration(), do: :passed
%Changeset{data: %User{}} :passed
iex> with %Changeset{} <- change_user_registration(%{password: "hi"}), do: :passed
:passed
""" """
@spec change_user_registration(User.t() | User.new_user()) :: @spec change_user_registration() :: User.changeset()
Changeset.t(User.t() | User.new_user()) @spec change_user_registration(attrs :: map()) :: User.changeset()
@spec change_user_registration(User.t() | User.new_user(), map()) :: def change_user_registration(attrs \\ %{}) do
Changeset.t(User.t() | User.new_user()) User.registration_changeset(attrs, nil, hash_password: false)
def change_user_registration(user, attrs \\ %{}), end
do: User.registration_changeset(user, attrs, hash_password: false)
## Settings ## Settings
@ -140,24 +169,29 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> change_user_email(user) iex> with %Changeset{} <- change_user_email(%User{email: "foo@example.com"}), do: :passed
%Changeset{data: %User{}} :passed
""" """
@spec change_user_email(User.t(), map()) :: Changeset.t(User.t()) @spec change_user_email(User.t()) :: User.changeset()
def change_user_email(user, attrs \\ %{}), do: User.email_changeset(user, attrs) @spec change_user_email(User.t(), attrs :: map()) :: User.changeset()
def change_user_email(user, attrs \\ %{}) do
User.email_changeset(user, attrs)
end
@doc """ @doc """
Returns an `%Changeset{}` for changing the user role. Returns an `%Changeset{}` for changing the user role.
## Examples ## Examples
iex> change_user_role(user) iex> with %Changeset{} <- change_user_role(%User{}, :user), do: :passed
%Changeset{data: %User{}} :passed
""" """
@spec change_user_role(User.t(), atom()) :: Changeset.t(User.t()) @spec change_user_role(User.t(), User.role()) :: User.changeset()
def change_user_role(user, role), do: User.role_changeset(user, role) def change_user_role(user, role) do
User.role_changeset(user, role)
end
@doc """ @doc """
Emulates that the email will change without actually changing Emulates that the email will change without actually changing
@ -165,15 +199,21 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> apply_user_email(user, "valid password", %{email: ...}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %User{}} iex> with {:ok, %User{}} <-
...> apply_user_email(user, "valid_password", %{email: "new_email@account.com"}),
...> do: :passed
:passed
iex> apply_user_email(user, "invalid password", %{email: ...}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:error, %Changeset{}} iex> with {:error, %Changeset{}} <-
...> apply_user_email(user, "invalid password", %{email: "new_email@account"}),
...> do: :passed
:passed
""" """
@spec apply_user_email(User.t(), String.t(), map()) :: @spec apply_user_email(User.t(), email :: String.t(), attrs :: map()) ::
{:ok, User.t()} | {:error, Changeset.t(User.t())} {:ok, User.t()} | {:error, User.changeset()}
def apply_user_email(user, password, attrs) do def apply_user_email(user, password, attrs) do
user user
|> User.email_changeset(attrs) |> User.email_changeset(attrs)
@ -187,7 +227,7 @@ defmodule Memex.Accounts do
If the token matches, the user email is updated and the token is deleted. If the token matches, the user email is updated and the token is deleted.
The confirmed_at date is also updated to the current time. The confirmed_at date is also updated to the current time.
""" """
@spec update_user_email(User.t(), String.t()) :: :ok | :error @spec update_user_email(User.t(), token :: String.t()) :: :ok | :error
def update_user_email(user, token) do def update_user_email(user, token) do
context = "change:#{user.email}" context = "change:#{user.email}"
@ -196,11 +236,11 @@ defmodule Memex.Accounts do
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do {:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do
:ok :ok
else else
_ -> :error _error_tuple -> :error
end end
end end
@spec user_email_multi(User.t(), String.t(), String.t()) :: Multi.t() @spec user_email_multi(User.t(), email :: String.t(), context :: String.t()) :: Multi.t()
defp user_email_multi(user, email, context) do defp user_email_multi(user, email, context) do
changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset() changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset()
@ -214,11 +254,16 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> deliver_update_email_instructions(user, current_email, &Routes.user_update_email_url(conn, :edit, &1)) iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %{to: ..., body: ...}} iex> with %Oban.Job{
...> args: %{email: :update_email, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_update_email_instructions(user, "new_foo@example.com", fn _token -> "example url" end),
...> do: :passed
:passed
""" """
@spec deliver_update_email_instructions(User.t(), String.t(), function) :: Job.t() @spec deliver_update_email_instructions(User.t(), current_email :: String.t(), function) ::
Job.t()
def deliver_update_email_instructions(user, current_email, update_email_url_fun) def deliver_update_email_instructions(user, current_email, update_email_url_fun)
when is_function(update_email_url_fun, 1) do when is_function(update_email_url_fun, 1) do
{encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}") {encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}")
@ -231,28 +276,38 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> change_user_password(user) iex> with %Changeset{} <- change_user_password(%User{}), do: :passed
%Changeset{data: %User{}} :passed
""" """
@spec change_user_password(User.t(), map()) :: Changeset.t(User.t()) @spec change_user_password(User.t(), attrs :: map()) :: User.changeset()
def change_user_password(user, attrs \\ %{}), def change_user_password(user, attrs \\ %{}) do
do: User.password_changeset(user, attrs, hash_password: false) User.password_changeset(user, attrs, hash_password: false)
end
@doc """ @doc """
Updates the user password. Updates the user password.
## Examples ## Examples
iex> update_user_password(user, "valid password", %{password: ...}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %User{}} iex> with {:ok, %User{}} <-
...> reset_user_password(user, %{
...> password: "new password",
...> password_confirmation: "new password"
...> }),
...> do: :passed
:passed
iex> update_user_password(user, "invalid password", %{password: ...}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:error, %Changeset{}} iex> with {:error, %Changeset{}} <-
...> update_user_password(user, "invalid password", %{password: "123"}),
...> do: :passed
:passed
""" """
@spec update_user_password(User.t(), String.t(), map()) :: @spec update_user_password(User.t(), String.t(), attrs :: map()) ::
{:ok, User.t()} | {:error, Changeset.t(User.t())} {:ok, User.t()} | {:error, User.changeset()}
def update_user_password(user, password, attrs) do def update_user_password(user, password, attrs) do
changeset = changeset =
user user
@ -265,54 +320,62 @@ defmodule Memex.Accounts do
|> Repo.transaction() |> Repo.transaction()
|> case do |> case do
{:ok, %{user: user}} -> {:ok, user} {:ok, %{user: user}} -> {:ok, user}
{:error, :user, changeset, _} -> {:error, changeset} {:error, :user, changeset, _changes_so_far} -> {:error, changeset}
end end
end end
@doc """ @doc """
Returns an `%Changeset{}` for changing the user locale. Returns an `Ecto.Changeset.t()` for changing the user locale.
## Examples ## Examples
iex> change_user_locale(user) iex> with %Changeset{} <- change_user_locale(%User{}), do: :passed
%Changeset{data: %User{}} :passed
""" """
@spec change_user_locale(User.t()) :: Changeset.t(User.t()) @spec change_user_locale(User.t()) :: User.changeset()
def change_user_locale(%{locale: locale} = user), do: User.locale_changeset(user, locale) def change_user_locale(%{locale: locale} = user) do
User.locale_changeset(user, locale)
end
@doc """ @doc """
Updates the user locale. Updates the user locale.
## Examples ## Examples
iex> update_user_locale(user, "valid locale") iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %User{}} iex> with {:ok, %User{}} <- update_user_locale(user, "en_US"), do: :passed
:passed
iex> update_user_password(user, "invalid locale")
{:error, %Changeset{}}
""" """
@spec update_user_locale(User.t(), locale :: String.t()) :: @spec update_user_locale(User.t(), locale :: String.t()) ::
{:ok, User.t()} | {:error, Changeset.t(User.t())} {:ok, User.t()} | {:error, User.changeset()}
def update_user_locale(user, locale), def update_user_locale(user, locale) do
do: user |> User.locale_changeset(locale) |> Repo.update() user |> User.locale_changeset(locale) |> Repo.update()
end
@doc """ @doc """
Deletes a user. must be performed by an admin or the same user! Deletes a user. must be performed by an admin or the same user!
## Examples ## Examples
iex> delete_user!(user_to_delete, %User{id: 123, role: :admin}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> with %User{} <- delete_user!(user, %User{id: 123, role: :admin}), do: :passed
:passed
iex> delete_user!(%User{id: 123}, %User{id: 123}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> with %User{} <- delete_user!(user, user), do: :passed
:passed
""" """
@spec delete_user!(User.t(), User.t()) :: User.t() @spec delete_user!(user_to_delete :: User.t(), User.t()) :: User.t()
def delete_user!(user, %User{role: :admin}), do: user |> Repo.delete!() def delete_user!(user, %User{role: :admin}) do
def delete_user!(%User{id: user_id} = user, %User{id: user_id}), do: user |> Repo.delete!() user |> Repo.delete!()
end
def delete_user!(%User{id: user_id} = user, %User{id: user_id}) do
user |> Repo.delete!()
end
## Session ## Session
@ -329,7 +392,7 @@ defmodule Memex.Accounts do
@doc """ @doc """
Gets the user with the given signed token. Gets the user with the given signed token.
""" """
@spec get_user_by_session_token(String.t()) :: User.t() @spec get_user_by_session_token(token :: String.t()) :: User.t()
def get_user_by_session_token(token) do def get_user_by_session_token(token) do
{:ok, query} = UserToken.verify_session_token_query(token) {:ok, query} = UserToken.verify_session_token_query(token)
Repo.one(query) Repo.one(query)
@ -338,9 +401,9 @@ defmodule Memex.Accounts do
@doc """ @doc """
Deletes the signed token with the given context. Deletes the signed token with the given context.
""" """
@spec delete_session_token(String.t()) :: :ok @spec delete_session_token(token :: String.t()) :: :ok
def delete_session_token(token) do def delete_session_token(token) do
Repo.delete_all(UserToken.token_and_context_query(token, "session")) UserToken.token_and_context_query(token, "session") |> Repo.delete_all()
:ok :ok
end end
@ -349,19 +412,45 @@ defmodule Memex.Accounts do
""" """
@spec allow_registration?() :: boolean() @spec allow_registration?() :: boolean()
def allow_registration? do def allow_registration? do
Application.get_env(:memex, MemexWeb.Endpoint)[:registration] == "public" or Application.get_env(:memex, Memex.Accounts)[:registration] == "public" or
list_users_by_role(:admin) |> Enum.empty?() list_users_by_role(:admin) |> Enum.empty?()
end end
@doc """ @doc """
Checks if user is an admin Checks if user is an admin
## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> is_admin?(user)
true
iex> is_admin?(%User{id: Ecto.UUID.generate()})
false
""" """
@spec is_admin?(User.t()) :: boolean() @spec is_admin?(User.t()) :: boolean()
def is_admin?(%User{id: user_id}) do def is_admin?(%User{id: user_id}) do
Repo.one(from u in User, where: u.id == ^user_id and u.role == :admin) Repo.exists?(from u in User, where: u.id == ^user_id, where: u.role == :admin)
|> is_nil()
end end
@doc """
Checks to see if user has the admin role
## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> is_already_admin?(user)
true
iex> is_already_admin?(%User{})
false
"""
@spec is_already_admin?(User.t() | nil) :: boolean()
def is_already_admin?(%User{role: :admin}), do: true
def is_already_admin?(_invalid_user), do: false
## Confirmation ## Confirmation
@doc """ @doc """
@ -369,10 +458,16 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> deliver_user_confirmation_instructions(user, &Routes.user_confirmation_url(conn, :confirm, &1)) iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %{to: ..., body: ...}} iex> with %Oban.Job{
...> args: %{email: :welcome, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_user_confirmation_instructions(user, fn _token -> "example url" end),
...> do: :passed
:passed
iex> deliver_user_confirmation_instructions(confirmed_user, &Routes.user_confirmation_url(conn, :confirm, &1)) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> user = user |> User.confirm_changeset() |> Repo.update!()
iex> deliver_user_confirmation_instructions(user, fn _token -> "example url" end)
{:error, :already_confirmed} {:error, :already_confirmed}
""" """
@ -394,14 +489,14 @@ defmodule Memex.Accounts do
If the token matches, the user account is marked as confirmed If the token matches, the user account is marked as confirmed
and the token is deleted. and the token is deleted.
""" """
@spec confirm_user(String.t()) :: {:ok, User.t()} | atom() @spec confirm_user(token :: String.t()) :: {:ok, User.t()} | :error
def confirm_user(token) do def confirm_user(token) do
with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"), with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"),
%User{} = user <- Repo.one(query), %User{} = user <- Repo.one(query),
{:ok, %{user: user}} <- Repo.transaction(confirm_user_multi(user)) do {:ok, %{user: user}} <- Repo.transaction(confirm_user_multi(user)) do
{:ok, user} {:ok, user}
else else
_ -> :error _error_tuple -> :error
end end
end end
@ -419,8 +514,12 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1)) iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %{to: ..., body: ...}} iex> with %Oban.Job{args: %{
...> email: :reset_password, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_user_reset_password_instructions(user, fn _token -> "example url" end),
...> do: :passed
:passed
""" """
@spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t() @spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t()
@ -436,20 +535,23 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> get_user_by_reset_password_token("validtoken") iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> {encoded_token, user_token} = UserToken.build_email_token(user, "reset_password")
iex> Repo.insert!(user_token)
iex> with %User{} <- get_user_by_reset_password_token(encoded_token), do: :passed
:passed
iex> get_user_by_reset_password_token("invalidtoken") iex> get_user_by_reset_password_token("invalidtoken")
nil nil
""" """
@spec get_user_by_reset_password_token(String.t()) :: User.t() | nil @spec get_user_by_reset_password_token(token :: String.t()) :: User.t() | nil
def get_user_by_reset_password_token(token) do def get_user_by_reset_password_token(token) do
with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"), with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"),
%User{} = user <- Repo.one(query) do %User{} = user <- Repo.one(query) do
user user
else else
_ -> nil _error_tuple -> nil
end end
end end
@ -458,14 +560,24 @@ defmodule Memex.Accounts do
## Examples ## Examples
iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %User{}} iex> with {:ok, %User{}} <-
...> reset_user_password(user, %{
...> password: "new password",
...> password_confirmation: "new password"
...> }),
...> do: :passed
:passed
iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:error, %Changeset{}} iex> with {:error, %Changeset{}} <-
...> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}),
...> do: :passed
:passed
""" """
@spec reset_user_password(User.t(), map()) :: {:ok, User.t()} | {:error, Changeset.t(User.t())} @spec reset_user_password(User.t(), attrs :: map()) ::
{:ok, User.t()} | {:error, User.changeset()}
def reset_user_password(user, attrs) do def reset_user_password(user, attrs) do
Multi.new() Multi.new()
|> Multi.update(:user, User.password_changeset(user, attrs)) |> Multi.update(:user, User.password_changeset(user, attrs))
@ -473,7 +585,7 @@ defmodule Memex.Accounts do
|> Repo.transaction() |> Repo.transaction()
|> case do |> case do
{:ok, %{user: user}} -> {:ok, user} {:ok, %{user: user}} -> {:ok, user}
{:error, :user, changeset, _} -> {:error, changeset} {:error, :user, changeset, _changes_so_far} -> {:error, changeset}
end end
end end
end end

View File

@ -19,8 +19,8 @@ defmodule Memex.Email do
@spec base_email(User.t(), String.t()) :: t() @spec base_email(User.t(), String.t()) :: t()
defp base_email(%User{email: email}, subject) do defp base_email(%User{email: email}, subject) do
from = Application.get_env(:Memex, Memex.Mailer)[:email_from] || "noreply@localhost" from = Application.get_env(:memex, Memex.Mailer)[:email_from] || "noreply@localhost"
name = Application.get_env(:Memex, Memex.Mailer)[:email_name] name = Application.get_env(:memex, Memex.Mailer)[:email_name]
new() |> to(email) |> from({name, from}) |> subject(subject) new() |> to(email) |> from({name, from}) |> subject(subject)
end end

View File

@ -1,4 +1,4 @@
defmodule Memex.Invites.Invite do defmodule Memex.Accounts.Invite do
@moduledoc """ @moduledoc """
An invite, created by an admin to allow someone to join their instance. An An invite, created by an admin to allow someone to join their instance. An
invite can be enabled or disabled, and can have an optional number of uses if invite can be enabled or disabled, and can have an optional number of uses if
@ -7,8 +7,8 @@ defmodule Memex.Invites.Invite do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Ecto.{Changeset, UUID} alias Ecto.{Association, Changeset, UUID}
alias Memex.{Accounts.User, Invites.Invite} alias Memex.Accounts.User
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
@ -18,40 +18,46 @@ defmodule Memex.Invites.Invite do
field :uses_left, :integer, default: nil field :uses_left, :integer, default: nil
field :disabled_at, :naive_datetime field :disabled_at, :naive_datetime
belongs_to :user, User belongs_to :created_by, User
has_many :users, User
timestamps() timestamps()
end end
@type t :: %Invite{ @type t :: %__MODULE__{
id: id(), id: id(),
name: String.t(), name: String.t(),
token: String.t(), token: token(),
uses_left: integer() | nil, uses_left: integer() | nil,
disabled_at: NaiveDateTime.t(), disabled_at: NaiveDateTime.t(),
user: User.t(), created_by: User.t() | nil | Association.NotLoaded.t(),
user_id: User.id(), created_by_id: User.id() | nil,
users: [User.t()] | Association.NotLoaded.t(),
inserted_at: NaiveDateTime.t(), inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t() updated_at: NaiveDateTime.t()
} }
@type new_invite :: %Invite{} @type new_invite :: %__MODULE__{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_invite())
@type token :: String.t()
@doc false @doc false
@spec create_changeset(new_invite(), attrs :: map()) :: Changeset.t(new_invite()) @spec create_changeset(User.t(), token(), attrs :: map()) :: changeset()
def create_changeset(invite, attrs) do def create_changeset(%User{id: user_id}, token, attrs) do
invite %__MODULE__{}
|> cast(attrs, [:name, :token, :uses_left, :disabled_at, :user_id]) |> change(token: token, created_by_id: user_id)
|> validate_required([:name, :token, :user_id]) |> cast(attrs, [:name, :uses_left, :disabled_at])
|> validate_required([:name, :token, :created_by_id])
|> validate_number(:uses_left, greater_than_or_equal_to: 0) |> validate_number(:uses_left, greater_than_or_equal_to: 0)
end end
@doc false @doc false
@spec update_changeset(t() | new_invite(), attrs :: map()) :: Changeset.t(t() | new_invite()) @spec update_changeset(t() | new_invite(), attrs :: map()) :: changeset()
def update_changeset(invite, attrs) do def update_changeset(invite, attrs) do
invite invite
|> cast(attrs, [:name, :uses_left, :disabled_at]) |> cast(attrs, [:name, :uses_left, :disabled_at])
|> validate_required([:name, :token, :user_id]) |> validate_required([:name])
|> validate_number(:uses_left, greater_than_or_equal_to: 0) |> validate_number(:uses_left, greater_than_or_equal_to: 0)
end end
end end

View File

@ -0,0 +1,198 @@
defmodule Memex.Accounts.Invites do
@moduledoc """
The Invites context.
"""
import Ecto.Query, warn: false
alias Ecto.Multi
alias Memex.Accounts.{Invite, User}
alias Memex.Repo
@invite_token_length 20
@doc """
Returns the list of invites.
## Examples
iex> list_invites(%User{id: 123, role: :admin})
[%Invite{}, ...]
"""
@spec list_invites(User.t()) :: [Invite.t()]
def list_invites(%User{role: :admin}) do
Repo.all(from i in Invite, order_by: i.name)
end
@doc """
Gets a single invite for a user
Raises `Ecto.NoResultsError` if the Invite does not exist.
## Examples
iex> get_invite!(123, %User{id: 123, role: :admin})
%Invite{}
> get_invite!(456, %User{id: 123, role: :admin})
** (Ecto.NoResultsError)
"""
@spec get_invite!(Invite.id(), User.t()) :: Invite.t()
def get_invite!(id, %User{role: :admin}) do
Repo.get!(Invite, id)
end
@doc """
Returns if an invite token is still valid
## Examples
iex> valid_invite_token?("valid_token")
%Invite{}
iex> valid_invite_token?("invalid_token")
nil
"""
@spec valid_invite_token?(Invite.token() | nil) :: boolean()
def valid_invite_token?(token) when token in [nil, ""], do: false
def valid_invite_token?(token) do
Repo.exists?(
from i in Invite,
where: i.token == ^token,
where: i.disabled_at |> is_nil()
)
end
@doc """
Uses invite by decrementing uses_left, or marks invite invalid if it's been
completely used.
"""
@spec use_invite(Invite.token()) :: {:ok, Invite.t()} | {:error, :invalid_token}
def use_invite(invite_token) do
Multi.new()
|> Multi.run(:invite, fn _changes_so_far, _repo ->
invite_token |> get_invite_by_token()
end)
|> Multi.update(:decrement_invite, fn %{invite: invite} ->
decrement_invite_changeset(invite)
end)
|> Repo.transaction()
|> case do
{:ok, %{decrement_invite: invite}} -> {:ok, invite}
{:error, :invite, :invalid_token, _changes_so_far} -> {:error, :invalid_token}
end
end
@spec get_invite_by_token(Invite.token() | nil) :: {:ok, Invite.t()} | {:error, :invalid_token}
defp get_invite_by_token(token) when token in [nil, ""], do: {:error, :invalid_token}
defp get_invite_by_token(token) do
Repo.one(
from i in Invite,
where: i.token == ^token,
where: i.disabled_at |> is_nil()
)
|> case do
nil -> {:error, :invalid_token}
invite -> {:ok, invite}
end
end
@spec get_use_count(Invite.t(), User.t()) :: non_neg_integer()
def get_use_count(%Invite{id: invite_id}, %User{role: :admin}) do
Repo.one(
from u in User,
where: u.invite_id == ^invite_id,
select: count(u.id)
)
end
@spec decrement_invite_changeset(Invite.t()) :: Invite.changeset()
defp decrement_invite_changeset(%Invite{uses_left: nil} = invite) do
invite |> Invite.update_changeset(%{})
end
defp decrement_invite_changeset(%Invite{uses_left: 1} = invite) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
invite |> Invite.update_changeset(%{uses_left: 0, disabled_at: now})
end
defp decrement_invite_changeset(%Invite{uses_left: uses_left} = invite) do
invite |> Invite.update_changeset(%{uses_left: uses_left - 1})
end
@doc """
Creates a invite.
## Examples
iex> create_invite(%User{id: 123, role: :admin}, %{field: value})
{:ok, %Invite{}}
iex> create_invite(%User{id: 123, role: :admin}, %{field: bad_value})
{:error, %Changeset{}}
"""
@spec create_invite(User.t(), attrs :: map()) ::
{:ok, Invite.t()} | {:error, Invite.changeset()}
def create_invite(%User{role: :admin} = user, attrs) do
token =
:crypto.strong_rand_bytes(@invite_token_length)
|> Base.url_encode64()
|> binary_part(0, @invite_token_length)
Invite.create_changeset(user, token, attrs) |> Repo.insert()
end
@doc """
Updates a invite.
## Examples
iex> update_invite(invite, %{field: new_value}, %User{id: 123, role: :admin})
{:ok, %Invite{}}
iex> update_invite(invite, %{field: bad_value}, %User{id: 123, role: :admin})
{:error, %Changeset{}}
"""
@spec update_invite(Invite.t(), attrs :: map(), User.t()) ::
{:ok, Invite.t()} | {:error, Invite.changeset()}
def update_invite(invite, attrs, %User{role: :admin}) do
invite |> Invite.update_changeset(attrs) |> Repo.update()
end
@doc """
Deletes a invite.
## Examples
iex> delete_invite(invite, %User{id: 123, role: :admin})
{:ok, %Invite{}}
iex> delete_invite(invite, %User{id: 123, role: :admin})
{:error, %Changeset{}}
"""
@spec delete_invite(Invite.t(), User.t()) ::
{:ok, Invite.t()} | {:error, Invite.changeset()}
def delete_invite(invite, %User{role: :admin}) do
invite |> Repo.delete()
end
@doc """
Deletes a invite.
## Examples
iex> delete_invite(invite, %User{id: 123, role: :admin})
%Invite{}
"""
@spec delete_invite!(Invite.t(), User.t()) :: Invite.t()
def delete_invite!(invite, %User{role: :admin}) do
invite |> Repo.delete!()
end
end

View File

@ -6,9 +6,19 @@ defmodule Memex.Accounts.User do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import MemexWeb.Gettext import MemexWeb.Gettext
alias Ecto.{Changeset, UUID} alias Ecto.{Association, Changeset, UUID}
alias Memex.{Accounts.User, Invites.Invite} alias Memex.Accounts.{Invite, User}
@derive {Jason.Encoder,
only: [
:id,
:email,
:confirmed_at,
:role,
:locale,
:inserted_at,
:updated_at
]}
@derive {Inspect, except: [:password]} @derive {Inspect, except: [:password]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
@ -20,7 +30,9 @@ defmodule Memex.Accounts.User do
field :role, Ecto.Enum, values: [:admin, :user], default: :user field :role, Ecto.Enum, values: [:admin, :user], default: :user
field :locale, :string field :locale, :string
has_many :invites, Invite, on_delete: :delete_all has_many :created_invites, Invite, foreign_key: :created_by_id
belongs_to :invite, Invite
timestamps() timestamps()
end end
@ -31,14 +43,18 @@ defmodule Memex.Accounts.User do
password: String.t(), password: String.t(),
hashed_password: String.t(), hashed_password: String.t(),
confirmed_at: NaiveDateTime.t(), confirmed_at: NaiveDateTime.t(),
role: atom(), role: role(),
invites: [Invite.t()],
locale: String.t() | nil, locale: String.t() | nil,
created_invites: [Invite.t()] | Association.NotLoaded.t(),
invite: Invite.t() | nil | Association.NotLoaded.t(),
invite_id: Invite.id() | nil,
inserted_at: NaiveDateTime.t(), inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t() updated_at: NaiveDateTime.t()
} }
@type new_user :: %User{} @type new_user :: %User{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_user())
@type role :: :admin | :user
@doc """ @doc """
A user changeset for registration. A user changeset for registration.
@ -57,26 +73,25 @@ defmodule Memex.Accounts.User do
validations on a LiveView form), this option can be set to `false`. validations on a LiveView form), this option can be set to `false`.
Defaults to `true`. Defaults to `true`.
""" """
@spec registration_changeset(t() | new_user(), attrs :: map()) :: Changeset.t(t() | new_user()) @spec registration_changeset(attrs :: map(), Invite.t() | nil) :: changeset()
@spec registration_changeset(t() | new_user(), attrs :: map(), opts :: keyword()) :: @spec registration_changeset(attrs :: map(), Invite.t() | nil, opts :: keyword()) :: changeset()
Changeset.t(t() | new_user()) def registration_changeset(attrs, invite, opts \\ []) do
def registration_changeset(user, attrs, opts \\ []) do %User{}
user |> cast(attrs, [:email, :password, :locale])
|> cast(attrs, [:email, :password, :role, :locale]) |> put_change(:invite_id, if(invite, do: invite.id))
|> validate_email() |> validate_email()
|> validate_password(opts) |> validate_password(opts)
end end
@doc """ @doc """
A user changeset for role. A user changeset for role.
""" """
@spec role_changeset(t(), role :: atom()) :: Changeset.t(t()) @spec role_changeset(t() | new_user() | changeset(), role()) :: changeset()
def role_changeset(user, role) do def role_changeset(user, role) do
user |> cast(%{"role" => role}, [:role]) user |> change(role: role)
end end
@spec validate_email(Changeset.t(t() | new_user())) :: Changeset.t(t() | new_user()) @spec validate_email(changeset()) :: changeset()
defp validate_email(changeset) do defp validate_email(changeset) do
changeset changeset
|> validate_required([:email]) |> validate_required([:email])
@ -88,8 +103,8 @@ defmodule Memex.Accounts.User do
|> unique_constraint(:email) |> unique_constraint(:email)
end end
@spec validate_password(Changeset.t(t() | new_user()), opts :: keyword()) :: @spec validate_password(changeset(), opts :: keyword()) ::
Changeset.t(t() | new_user()) changeset()
defp validate_password(changeset, opts) do defp validate_password(changeset, opts) do
changeset changeset
|> validate_required([:password]) |> validate_required([:password])
@ -100,8 +115,7 @@ defmodule Memex.Accounts.User do
|> maybe_hash_password(opts) |> maybe_hash_password(opts)
end end
@spec maybe_hash_password(Changeset.t(t() | new_user()), opts :: keyword()) :: @spec maybe_hash_password(changeset(), opts :: keyword()) :: changeset()
Changeset.t(t() | new_user())
defp maybe_hash_password(changeset, opts) do defp maybe_hash_password(changeset, opts) do
hash_password? = Keyword.get(opts, :hash_password, true) hash_password? = Keyword.get(opts, :hash_password, true)
password = get_change(changeset, :password) password = get_change(changeset, :password)
@ -120,7 +134,7 @@ defmodule Memex.Accounts.User do
It requires the email to change otherwise an error is added. It requires the email to change otherwise an error is added.
""" """
@spec email_changeset(t(), attrs :: map()) :: Changeset.t(t()) @spec email_changeset(t(), attrs :: map()) :: changeset()
def email_changeset(user, attrs) do def email_changeset(user, attrs) do
user user
|> cast(attrs, [:email]) |> cast(attrs, [:email])
@ -143,8 +157,8 @@ defmodule Memex.Accounts.User do
validations on a LiveView form), this option can be set to `false`. validations on a LiveView form), this option can be set to `false`.
Defaults to `true`. Defaults to `true`.
""" """
@spec password_changeset(t(), attrs :: map()) :: Changeset.t(t()) @spec password_changeset(t(), attrs :: map()) :: changeset()
@spec password_changeset(t(), attrs :: map(), opts :: keyword()) :: Changeset.t(t()) @spec password_changeset(t(), attrs :: map(), opts :: keyword()) :: changeset()
def password_changeset(user, attrs, opts \\ []) do def password_changeset(user, attrs, opts \\ []) do
user user
|> cast(attrs, [:password]) |> cast(attrs, [:password])
@ -155,7 +169,7 @@ defmodule Memex.Accounts.User do
@doc """ @doc """
Confirms the account by setting `confirmed_at`. Confirms the account by setting `confirmed_at`.
""" """
@spec confirm_changeset(t() | Changeset.t(t())) :: Changeset.t(t()) @spec confirm_changeset(t() | changeset()) :: changeset()
def confirm_changeset(user_or_changeset) do def confirm_changeset(user_or_changeset) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
user_or_changeset |> change(confirmed_at: now) user_or_changeset |> change(confirmed_at: now)
@ -173,7 +187,7 @@ defmodule Memex.Accounts.User do
Bcrypt.verify_pass(password, hashed_password) Bcrypt.verify_pass(password, hashed_password)
end end
def valid_password?(_, _) do def valid_password?(_invalid_user, _invalid_password) do
Bcrypt.no_user_verify() Bcrypt.no_user_verify()
false false
end end
@ -181,7 +195,7 @@ defmodule Memex.Accounts.User do
@doc """ @doc """
Validates the current password otherwise adds an error to the changeset. Validates the current password otherwise adds an error to the changeset.
""" """
@spec validate_current_password(Changeset.t(t()), String.t()) :: Changeset.t(t()) @spec validate_current_password(changeset(), String.t()) :: changeset()
def validate_current_password(changeset, password) do def validate_current_password(changeset, password) do
if valid_password?(changeset.data, password), if valid_password?(changeset.data, password),
do: changeset, do: changeset,
@ -191,7 +205,7 @@ defmodule Memex.Accounts.User do
@doc """ @doc """
A changeset for changing the user's locale A changeset for changing the user's locale
""" """
@spec locale_changeset(t() | Changeset.t(t()), locale :: String.t() | nil) :: Changeset.t(t()) @spec locale_changeset(t() | changeset(), locale :: String.t() | nil) :: changeset()
def locale_changeset(user_or_changeset, locale) do def locale_changeset(user_or_changeset, locale) do
user_or_changeset user_or_changeset
|> cast(%{"locale" => locale}, [:locale]) |> cast(%{"locale" => locale}, [:locale])

View File

@ -5,6 +5,8 @@ defmodule Memex.Accounts.UserToken do
use Ecto.Schema use Ecto.Schema
import Ecto.Query import Ecto.Query
alias Ecto.{Association, UUID}
alias Memex.Accounts.User
@hash_algorithm :sha256 @hash_algorithm :sha256
@rand_size 32 @rand_size 32
@ -22,11 +24,25 @@ defmodule Memex.Accounts.UserToken do
field :token, :binary field :token, :binary
field :context, :string field :context, :string
field :sent_to, :string field :sent_to, :string
belongs_to :user, Memex.Accounts.User
belongs_to :user, User
timestamps(updated_at: false) timestamps(updated_at: false)
end end
@type t :: %__MODULE__{
id: id(),
token: token(),
context: String.t(),
sent_to: String.t(),
user: User.t() | Association.NotLoaded.t(),
user_id: User.id() | nil,
inserted_at: NaiveDateTime.t()
}
@type new_user_token :: %__MODULE__{}
@type id :: UUID.t()
@type token :: binary()
@doc """ @doc """
Generates a token that will be stored in a signed place, Generates a token that will be stored in a signed place,
such as session or cookie. As they are signed, those such as session or cookie. As they are signed, those
@ -34,7 +50,7 @@ defmodule Memex.Accounts.UserToken do
""" """
def build_session_token(user) do def build_session_token(user) do
token = :crypto.strong_rand_bytes(@rand_size) token = :crypto.strong_rand_bytes(@rand_size)
{token, %Memex.Accounts.UserToken{token: token, context: "session", user_id: user.id}} {token, %__MODULE__{token: token, context: "session", user_id: user.id}}
end end
@doc """ @doc """
@ -69,7 +85,7 @@ defmodule Memex.Accounts.UserToken do
hashed_token = :crypto.hash(@hash_algorithm, token) hashed_token = :crypto.hash(@hash_algorithm, token)
{Base.url_encode64(token, padding: false), {Base.url_encode64(token, padding: false),
%Memex.Accounts.UserToken{ %__MODULE__{
token: hashed_token, token: hashed_token,
context: context, context: context,
sent_to: sent_to, sent_to: sent_to,
@ -129,17 +145,17 @@ defmodule Memex.Accounts.UserToken do
Returns the given token with the given context. Returns the given token with the given context.
""" """
def token_and_context_query(token, context) do def token_and_context_query(token, context) do
from Memex.Accounts.UserToken, where: [token: ^token, context: ^context] from __MODULE__, where: [token: ^token, context: ^context]
end end
@doc """ @doc """
Gets all tokens for the given user for the given contexts. Gets all tokens for the given user for the given contexts.
""" """
def user_and_contexts_query(user, :all) do def user_and_contexts_query(user, :all) do
from t in Memex.Accounts.UserToken, where: t.user_id == ^user.id from t in __MODULE__, where: t.user_id == ^user.id
end end
def user_and_contexts_query(user, [_ | _] = contexts) do def user_and_contexts_query(user, [_ | _] = contexts) do
from t in Memex.Accounts.UserToken, where: t.user_id == ^user.id and t.context in ^contexts from t in __MODULE__, where: t.user_id == ^user.id and t.context in ^contexts
end end
end end

View File

@ -4,6 +4,7 @@ defmodule Memex.Application do
@moduledoc false @moduledoc false
use Application use Application
alias Memex.ErrorReporter
@impl true @impl true
def start(_type, _args) do def start(_type, _args) do
@ -17,16 +18,24 @@ defmodule Memex.Application do
# Start the Endpoint (http/https) # Start the Endpoint (http/https)
MemexWeb.Endpoint, MemexWeb.Endpoint,
# Add Oban # Add Oban
{Oban, oban_config()} {Oban, oban_config()},
Memex.Repo.Migrator
# Start a worker by calling: Memex.Worker.start_link(arg) # Start a worker by calling: Memex.Worker.start_link(arg)
# {Memex.Worker, arg} # {Memex.Worker, arg}
] ]
# Automatically migrate on start in prod # Oban events logging https://hexdocs.pm/oban/Oban.html#module-reporting-errors
children = :ok =
if Application.get_env(:memex, Memex.Application, automigrate: false)[:automigrate], :telemetry.attach_many(
do: children ++ [Memex.Repo.Migrator], "oban-logger",
else: children [
[:oban, :job, :exception],
[:oban, :job, :start],
[:oban, :job, :stop]
],
&ErrorReporter.handle_event/4,
[]
)
# See https://hexdocs.pm/elixir/Supervisor.html # See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options # for other strategies and supported options

View File

@ -4,21 +4,88 @@ defmodule Memex.Contexts do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Memex.Repo alias Memex.{Accounts.User, Contexts.Context, Repo}
alias Memex.Contexts.Context
@doc """ @doc """
Returns the list of contexts. Returns the list of contexts.
## Examples ## Examples
iex> list_contexts() iex> list_contexts(%User{id: 123})
[%Context{}, ...] [%Context{}, ...]
iex> list_contexts("my context", %User{id: 123})
[%Context{slug: "my context"}, ...]
""" """
def list_contexts do @spec list_contexts(User.t()) :: [Context.t()]
Repo.all(Context) @spec list_contexts(search :: String.t() | nil, User.t()) :: [Context.t()]
def list_contexts(search \\ nil, user)
def list_contexts(search, %{id: user_id}) when search |> is_nil() or search == "" do
Repo.all(from c in Context, where: c.user_id == ^user_id, order_by: c.slug)
end
def list_contexts(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from c in Context,
where: c.user_id == ^user_id,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end
@doc """
Returns the list of public contexts for viewing.
## Examples
iex> list_public_contexts()
[%Context{}, ...]
iex> list_public_contexts("my context")
[%Context{slug: "my context"}, ...]
"""
@spec list_public_contexts() :: [Context.t()]
@spec list_public_contexts(search :: String.t() | nil) :: [Context.t()]
def list_public_contexts(search \\ nil)
def list_public_contexts(search) when search |> is_nil() or search == "" do
Repo.all(from c in Context, where: c.visibility == :public, order_by: c.slug)
end
def list_public_contexts(search) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from c in Context,
where: c.visibility == :public,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end end
@doc """ @doc """
@ -28,31 +95,78 @@ defmodule Memex.Contexts do
## Examples ## Examples
iex> get_context!(123) iex> get_context!(123, %User{id: 123})
%Context{} %Context{}
iex> get_context!(456) iex> get_context!(456, %User{id: 123})
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
def get_context!(id), do: Repo.get!(Context, id) @spec get_context!(Context.id(), User.t()) :: Context.t()
def get_context!(id, %{id: user_id}) do
Repo.one!(
from c in Context,
where: c.id == ^id,
where: c.user_id == ^user_id or c.visibility in [:public, :unlisted]
)
end
def get_context!(id, _invalid_user) do
Repo.one!(
from c in Context,
where: c.id == ^id,
where: c.visibility in [:public, :unlisted]
)
end
@doc """
Gets a single context by a slug.
Raises `Ecto.NoResultsError` if the Context does not exist.
## Examples
iex> get_context_by_slug("my-context", %User{id: 123})
%Context{}
iex> get_context_by_slug("my-context", %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_context_by_slug(Context.slug(), User.t()) :: Context.t() | nil
def get_context_by_slug(slug, %{id: user_id}) do
Repo.one(
from c in Context,
where: c.slug == ^slug,
where: c.user_id == ^user_id or c.visibility in [:public, :unlisted]
)
end
def get_context_by_slug(slug, _invalid_user) do
Repo.one(
from c in Context,
where: c.slug == ^slug,
where: c.visibility in [:public, :unlisted]
)
end
@doc """ @doc """
Creates a context. Creates a context.
## Examples ## Examples
iex> create_context(%{field: value}) iex> create_context(%{field: value}, %User{id: 123})
{:ok, %Context{}} {:ok, %Context{}}
iex> create_context(%{field: bad_value}) iex> create_context(%{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_context(attrs \\ %{}) do @spec create_context(User.t()) :: {:ok, Context.t()} | {:error, Context.changeset()}
%Context{} @spec create_context(attrs :: map(), User.t()) ::
|> Context.changeset(attrs) {:ok, Context.t()} | {:error, Context.changeset()}
|> Repo.insert() def create_context(attrs \\ %{}, user) do
Context.create_changeset(attrs, user) |> Repo.insert()
end end
@doc """ @doc """
@ -60,16 +174,18 @@ defmodule Memex.Contexts do
## Examples ## Examples
iex> update_context(context, %{field: new_value}) iex> update_context(context, %{field: new_value}, %User{id: 123})
{:ok, %Context{}} {:ok, %Context{}}
iex> update_context(context, %{field: bad_value}) iex> update_context(context, %{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def update_context(%Context{} = context, attrs) do @spec update_context(Context.t(), attrs :: map(), User.t()) ::
{:ok, Context.t()} | {:error, Context.changeset()}
def update_context(%Context{} = context, attrs, user) do
context context
|> Context.changeset(attrs) |> Context.update_changeset(attrs, user)
|> Repo.update() |> Repo.update()
end end
@ -78,15 +194,24 @@ defmodule Memex.Contexts do
## Examples ## Examples
iex> delete_context(context) iex> delete_context(%Context{user_id: 123}, %User{id: 123})
{:ok, %Context{}} {:ok, %Context{}}
iex> delete_context(context) iex> delete_context(%Context{user_id: 123}, %User{role: :admin})
{:ok, %Context{}}
iex> delete_context(%Context{}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def delete_context(%Context{} = context) do @spec delete_context(Context.t(), User.t()) ::
Repo.delete(context) {:ok, Context.t()} | {:error, Context.changeset()}
def delete_context(%Context{user_id: user_id} = context, %{id: user_id}) do
context |> Repo.delete()
end
def delete_context(%Context{} = context, %{role: :admin}) do
context |> Repo.delete()
end end
@doc """ @doc """
@ -98,7 +223,9 @@ defmodule Memex.Contexts do
%Ecto.Changeset{data: %Context{}} %Ecto.Changeset{data: %Context{}}
""" """
def change_context(%Context{} = context, attrs \\ %{}) do @spec change_context(Context.t(), User.t()) :: Context.changeset()
Context.changeset(context, attrs) @spec change_context(Context.t(), attrs :: map(), User.t()) :: Context.changeset()
def change_context(%Context{} = context, attrs \\ %{}, user) do
context |> Context.update_changeset(attrs, user)
end end
end end

View File

@ -1,22 +1,112 @@
defmodule Memex.Contexts.Context do defmodule Memex.Contexts.Context do
@moduledoc """
Represents a document that synthesizes multiple concepts as defined by notes
into a single consideration
"""
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import MemexWeb.Gettext
alias Ecto.{Changeset, UUID}
alias Memex.{Accounts.User, Repo}
@derive {Jason.Encoder,
only: [
:slug,
:content,
:tags,
:visibility,
:inserted_at,
:updated_at
]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "contexts" do schema "contexts" do
field :slug, :string
field :content, :string field :content, :string
field :tag, {:array, :string} field :tags, {:array, :string}
field :title, :string field :tags_string, :string, virtual: true
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
belongs_to :user, User
timestamps() timestamps()
end end
@type t :: %__MODULE__{
slug: slug(),
content: String.t(),
tags: [String.t()] | nil,
tags_string: String.t() | nil,
visibility: :public | :private | :unlisted,
user: User.t() | Ecto.Association.NotLoaded.t(),
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type slug :: String.t()
@type changeset :: Changeset.t(t())
@doc false @doc false
def changeset(context, attrs) do @spec create_changeset(attrs :: map(), User.t()) :: changeset()
context def create_changeset(attrs, %User{id: user_id}) do
|> cast(attrs, [:title, :content, :tag, :visibility]) %__MODULE__{}
|> validate_required([:title, :content, :tag, :visibility]) |> cast(attrs, [:slug, :content, :tags, :visibility])
|> change(user_id: user_id)
|> cast_tags_string(attrs)
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :content, :user_id, :visibility])
|> unique_constraint(:slug)
|> unsafe_validate_unique(:slug, Repo)
end end
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = note, attrs, %User{id: user_id}) do
note
|> cast(attrs, [:slug, :content, :tags, :visibility])
|> cast_tags_string(attrs)
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :content, :visibility])
|> unique_constraint(:slug)
|> unsafe_validate_unique(:slug, Repo)
end
defp cast_tags_string(changeset, attrs) do
changeset
|> put_change(:tags_string, changeset |> get_field(:tags) |> get_tags_string())
|> cast(attrs, [:tags_string])
|> validate_format(:tags_string, ~r/^[\p{L}\p{N}\-\,]+$/,
message:
dgettext(
"errors",
"invalid format: only numbers, letters and hyphen are accepted. tags must be comma-delimited"
)
)
|> cast_tags()
end
defp cast_tags(%{valid?: false} = changeset), do: changeset
defp cast_tags(%{valid?: true} = changeset) do
tags = changeset |> get_field(:tags_string) |> process_tags()
changeset |> put_change(:tags, tags)
end
defp process_tags(tags_string) when tags_string |> is_binary() do
tags_string
|> String.split(",", trim: true)
|> Enum.map(fn str -> str |> String.trim() end)
|> Enum.reject(fn str -> str |> is_nil() end)
|> Enum.sort()
end
defp process_tags(_other_tags_string), do: []
@spec get_tags_string([String.t()] | nil) :: String.t()
def get_tags_string(nil), do: ""
def get_tags_string(tags) when tags |> is_list(), do: tags |> Enum.join(",")
end end

View File

@ -1,20 +0,0 @@
defmodule Memex.Contexts.ContextNote do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "context_notes" do
field :context_id, :binary_id
field :note_id, :binary_id
timestamps()
end
@doc false
def changeset(context_note, attrs) do
context_note
|> cast(attrs, [])
|> validate_required([])
end
end

View File

@ -0,0 +1,57 @@
defmodule Memex.ErrorReporter do
@moduledoc """
Custom logger for telemetry events
Oban implementation taken from
https://hexdocs.pm/oban/Oban.html#module-reporting-errors
"""
require Logger
def handle_event([:oban, :job, :exception], measure, %{stacktrace: stacktrace} = meta, _config) do
data =
get_oban_job_data(meta, measure)
|> Map.put(:stacktrace, Exception.format_stacktrace(stacktrace))
Logger.error(meta.reason, data: pretty_encode(data))
end
def handle_event([:oban, :job, :start], measure, meta, _config) do
Logger.info("Started oban job", data: get_oban_job_data(meta, measure) |> pretty_encode())
end
def handle_event([:oban, :job, :stop], measure, meta, _config) do
Logger.info("Finished oban job", data: get_oban_job_data(meta, measure) |> pretty_encode())
end
def handle_event([:oban, :job, unhandled_event], measure, meta, _config) do
data =
get_oban_job_data(meta, measure)
|> Map.put(:event, unhandled_event)
Logger.warning("Unhandled oban job event", data: pretty_encode(data))
end
def handle_event(unhandled_event, measure, meta, config) do
data = %{
event: unhandled_event,
meta: meta,
measurements: measure,
config: config
}
Logger.warning("Unhandled telemetry event", data: pretty_encode(data))
end
defp get_oban_job_data(%{job: job}, measure) do
job
|> Map.take([:id, :args, :meta, :queue, :worker])
|> Map.merge(measure)
end
defp pretty_encode(data) do
data
|> Jason.encode!()
|> Jason.Formatter.pretty_print()
end
end

View File

@ -1,173 +0,0 @@
defmodule Memex.Invites do
@moduledoc """
The Invites context.
"""
import Ecto.Query, warn: false
alias Ecto.Changeset
alias Memex.{Accounts.User, Invites.Invite, Repo}
@invite_token_length 20
@doc """
Returns the list of invites.
## Examples
iex> list_invites(%User{id: 123, role: :admin})
[%Invite{}, ...]
"""
@spec list_invites(User.t()) :: [Invite.t()]
def list_invites(%User{role: :admin}) do
Repo.all(from i in Invite, order_by: i.name)
end
@doc """
Gets a single invite.
Raises `Ecto.NoResultsError` if the Invite does not exist.
## Examples
iex> get_invite!(123, %User{id: 123, role: :admin})
%Invite{}
iex> get_invite!(456, %User{id: 123, role: :admin})
** (Ecto.NoResultsError)
"""
@spec get_invite!(Invite.id(), User.t()) :: Invite.t()
def get_invite!(id, %User{role: :admin}) do
Repo.get!(Invite, id)
end
@doc """
Returns a valid invite or nil based on the attempted token
## Examples
iex> get_invite_by_token("valid_token")
%Invite{}
iex> get_invite_by_token("invalid_token")
nil
"""
@spec get_invite_by_token(token :: String.t() | nil) :: Invite.t() | nil
def get_invite_by_token(nil), do: nil
def get_invite_by_token(""), do: nil
def get_invite_by_token(token) do
Repo.one(
from(i in Invite,
where: i.token == ^token and i.disabled_at |> is_nil()
)
)
end
@doc """
Uses invite by decrementing uses_left, or marks invite invalid if it's been
completely used.
"""
@spec use_invite!(Invite.t()) :: Invite.t()
def use_invite!(%Invite{uses_left: nil} = invite), do: invite
def use_invite!(%Invite{uses_left: uses_left} = invite) do
new_uses_left = uses_left - 1
attrs =
if new_uses_left <= 0 do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
%{"uses_left" => 0, "disabled_at" => now}
else
%{"uses_left" => new_uses_left}
end
invite |> Invite.update_changeset(attrs) |> Repo.update!()
end
@doc """
Creates a invite.
## Examples
iex> create_invite(%User{id: 123, role: :admin}, %{field: value})
{:ok, %Invite{}}
iex> create_invite(%User{id: 123, role: :admin}, %{field: bad_value})
{:error, %Changeset{}}
"""
@spec create_invite(User.t(), attrs :: map()) ::
{:ok, Invite.t()} | {:error, Changeset.t(Invite.new_invite())}
def create_invite(%User{id: user_id, role: :admin}, attrs) do
token =
:crypto.strong_rand_bytes(@invite_token_length)
|> Base.url_encode64()
|> binary_part(0, @invite_token_length)
attrs = attrs |> Map.merge(%{"user_id" => user_id, "token" => token})
%Invite{} |> Invite.create_changeset(attrs) |> Repo.insert()
end
@doc """
Updates a invite.
## Examples
iex> update_invite(invite, %{field: new_value}, %User{id: 123, role: :admin})
{:ok, %Invite{}}
iex> update_invite(invite, %{field: bad_value}, %User{id: 123, role: :admin})
{:error, %Changeset{}}
"""
@spec update_invite(Invite.t(), attrs :: map(), User.t()) ::
{:ok, Invite.t()} | {:error, Changeset.t(Invite.t())}
def update_invite(invite, attrs, %User{role: :admin}),
do: invite |> Invite.update_changeset(attrs) |> Repo.update()
@doc """
Deletes a invite.
## Examples
iex> delete_invite(invite, %User{id: 123, role: :admin})
{:ok, %Invite{}}
iex> delete_invite(invite, %User{id: 123, role: :admin})
{:error, %Changeset{}}
"""
@spec delete_invite(Invite.t(), User.t()) ::
{:ok, Invite.t()} | {:error, Changeset.t(Invite.t())}
def delete_invite(invite, %User{role: :admin}), do: invite |> Repo.delete()
@doc """
Deletes a invite.
## Examples
iex> delete_invite(invite, %User{id: 123, role: :admin})
%Invite{}
"""
@spec delete_invite!(Invite.t(), User.t()) :: Invite.t()
def delete_invite!(invite, %User{role: :admin}), do: invite |> Repo.delete!()
@doc """
Returns an `%Changeset{}` for tracking invite changes.
## Examples
iex> change_invite(invite)
%Changeset{data: %Invite{}}
"""
@spec change_invite(Invite.t() | Invite.new_invite()) ::
Changeset.t(Invite.t() | Invite.new_invite())
@spec change_invite(Invite.t() | Invite.new_invite(), attrs :: map()) ::
Changeset.t(Invite.t() | Invite.new_invite())
def change_invite(invite, attrs \\ %{}), do: invite |> Invite.update_changeset(attrs)
end

View File

@ -4,21 +4,87 @@ defmodule Memex.Notes do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Memex.Repo alias Memex.{Accounts.User, Notes.Note, Repo}
alias Memex.Notes.Note
@doc """ @doc """
Returns the list of notes. Returns the list of notes.
## Examples ## Examples
iex> list_notes() iex> list_notes(%User{id: 123})
[%Note{}, ...] [%Note{}, ...]
iex> list_notes("my note", %User{id: 123})
[%Note{slug: "my note"}, ...]
""" """
def list_notes do @spec list_notes(User.t()) :: [Note.t()]
Repo.all(Note) @spec list_notes(search :: String.t() | nil, User.t()) :: [Note.t()]
def list_notes(search \\ nil, user)
def list_notes(search, %{id: user_id}) when search |> is_nil() or search == "" do
Repo.all(from n in Note, where: n.user_id == ^user_id, order_by: n.slug)
end
def list_notes(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from n in Note,
where: n.user_id == ^user_id,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end
@doc """
Returns the list of public notes for viewing
## Examples
iex> list_public_notes()
[%Note{}, ...]
iex> list_public_notes("my note")
[%Note{slug: "my note"}, ...]
"""
@spec list_public_notes() :: [Note.t()]
@spec list_public_notes(search :: String.t() | nil) :: [Note.t()]
def list_public_notes(search \\ nil)
def list_public_notes(search) when search |> is_nil() or search == "" do
Repo.all(from n in Note, where: n.visibility == :public, order_by: n.slug)
end
def list_public_notes(search) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from n in Note,
where: n.visibility == :public,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end end
@doc """ @doc """
@ -28,31 +94,77 @@ defmodule Memex.Notes do
## Examples ## Examples
iex> get_note!(123) iex> get_note!(123, %User{id: 123})
%Note{} %Note{}
iex> get_note!(456) iex> get_note!(456, %User{id: 123})
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
def get_note!(id), do: Repo.get!(Note, id) @spec get_note!(Note.id(), User.t()) :: Note.t()
def get_note!(id, %{id: user_id}) do
Repo.one!(
from n in Note,
where: n.id == ^id,
where: n.user_id == ^user_id or n.visibility in [:public, :unlisted]
)
end
def get_note!(id, _invalid_user) do
Repo.one!(
from n in Note,
where: n.id == ^id,
where: n.visibility in [:public, :unlisted]
)
end
@doc """
Gets a single note by slug.
Raises `Ecto.NoResultsError` if the Note does not exist.
## Examples
iex> get_note_by_slug("my-note", %User{id: 123})
%Note{}
iex> get_note_by_slug("my-note", %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_note_by_slug(Note.slug(), User.t()) :: Note.t() | nil
def get_note_by_slug(slug, %{id: user_id}) do
Repo.one(
from n in Note,
where: n.slug == ^slug,
where: n.user_id == ^user_id or n.visibility in [:public, :unlisted]
)
end
def get_note_by_slug(slug, _invalid_user) do
Repo.one(
from n in Note,
where: n.slug == ^slug,
where: n.visibility in [:public, :unlisted]
)
end
@doc """ @doc """
Creates a note. Creates a note.
## Examples ## Examples
iex> create_note(%{field: value}) iex> create_note(%{field: value}, %User{id: 123})
{:ok, %Note{}} {:ok, %Note{}}
iex> create_note(%{field: bad_value}) iex> create_note(%{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_note(attrs \\ %{}) do @spec create_note(User.t()) :: {:ok, Note.t()} | {:error, Note.changeset()}
%Note{} @spec create_note(attrs :: map(), User.t()) :: {:ok, Note.t()} | {:error, Note.changeset()}
|> Note.changeset(attrs) def create_note(attrs \\ %{}, user) do
|> Repo.insert() Note.create_changeset(attrs, user) |> Repo.insert()
end end
@doc """ @doc """
@ -60,16 +172,18 @@ defmodule Memex.Notes do
## Examples ## Examples
iex> update_note(note, %{field: new_value}) iex> update_note(note, %{field: new_value}, %User{id: 123})
{:ok, %Note{}} {:ok, %Note{}}
iex> update_note(note, %{field: bad_value}) iex> update_note(note, %{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def update_note(%Note{} = note, attrs) do @spec update_note(Note.t(), attrs :: map(), User.t()) ::
{:ok, Note.t()} | {:error, Note.changeset()}
def update_note(%Note{} = note, attrs, user) do
note note
|> Note.changeset(attrs) |> Note.update_changeset(attrs, user)
|> Repo.update() |> Repo.update()
end end
@ -78,15 +192,23 @@ defmodule Memex.Notes do
## Examples ## Examples
iex> delete_note(note) iex> delete_note(%Note{user_id: 123}, %User{id: 123})
{:ok, %Note{}} {:ok, %Note{}}
iex> delete_note(note) iex> delete_note(%Note{}, %User{role: :admin})
{:ok, %Note{}}
iex> delete_note(%Note{}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def delete_note(%Note{} = note) do @spec delete_note(Note.t(), User.t()) :: {:ok, Note.t()} | {:error, Note.changeset()}
Repo.delete(note) def delete_note(%Note{user_id: user_id} = note, %{id: user_id}) do
note |> Repo.delete()
end
def delete_note(%Note{} = note, %{role: :admin}) do
note |> Repo.delete()
end end
@doc """ @doc """
@ -94,11 +216,16 @@ defmodule Memex.Notes do
## Examples ## Examples
iex> change_note(note) iex> change_note(note, %User{id: 123})
%Ecto.Changeset{data: %Note{}}
iex> change_note(note, %{slug: "new slug"}, %User{id: 123})
%Ecto.Changeset{data: %Note{}} %Ecto.Changeset{data: %Note{}}
""" """
def change_note(%Note{} = note, attrs \\ %{}) do @spec change_note(Note.t(), User.t()) :: Note.changeset()
Note.changeset(note, attrs) @spec change_note(Note.t(), attrs :: map(), User.t()) :: Note.changeset()
def change_note(%Note{} = note, attrs \\ %{}, user) do
note |> Note.update_changeset(attrs, user)
end end
end end

View File

@ -1,22 +1,111 @@
defmodule Memex.Notes.Note do defmodule Memex.Notes.Note do
@moduledoc """
Schema for a user-written note
"""
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import MemexWeb.Gettext
alias Ecto.{Changeset, UUID}
alias Memex.{Accounts.User, Repo}
@derive {Jason.Encoder,
only: [
:slug,
:content,
:tags,
:visibility,
:inserted_at,
:updated_at
]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "notes" do schema "notes" do
field :slug, :string
field :content, :string field :content, :string
field :tag, {:array, :string} field :tags, {:array, :string}
field :title, :string field :tags_string, :string, virtual: true
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
belongs_to :user, User
timestamps() timestamps()
end end
@type t :: %__MODULE__{
slug: slug(),
content: String.t(),
tags: [String.t()] | nil,
tags_string: String.t() | nil,
visibility: :public | :private | :unlisted,
user: User.t() | Ecto.Association.NotLoaded.t(),
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type slug :: String.t()
@type changeset :: Changeset.t(t())
@doc false @doc false
def changeset(note, attrs) do @spec create_changeset(attrs :: map(), User.t()) :: changeset()
note def create_changeset(attrs, %User{id: user_id}) do
|> cast(attrs, [:title, :content, :tag, :visibility]) %__MODULE__{}
|> validate_required([:title, :content, :tag, :visibility]) |> cast(attrs, [:slug, :content, :tags, :visibility])
|> change(user_id: user_id)
|> cast_tags_string(attrs)
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :content, :user_id, :visibility])
|> unique_constraint(:slug)
|> unsafe_validate_unique(:slug, Repo)
end end
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = note, attrs, %User{id: user_id}) do
note
|> cast(attrs, [:slug, :content, :tags, :visibility])
|> cast_tags_string(attrs)
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :content, :visibility])
|> unique_constraint(:slug)
|> unsafe_validate_unique(:slug, Repo)
end
defp cast_tags_string(changeset, attrs) do
changeset
|> put_change(:tags_string, changeset |> get_field(:tags) |> get_tags_string())
|> cast(attrs, [:tags_string])
|> validate_format(:tags_string, ~r/^[\p{L}\p{N}\-\,]+$/,
message:
dgettext(
"errors",
"invalid format: only numbers, letters and hyphen are accepted. tags must be comma-delimited"
)
)
|> cast_tags()
end
defp cast_tags(%{valid?: false} = changeset), do: changeset
defp cast_tags(%{valid?: true} = changeset) do
tags = changeset |> get_field(:tags_string) |> process_tags()
changeset |> put_change(:tags, tags)
end
defp process_tags(tags_string) when tags_string |> is_binary() do
tags_string
|> String.split(",", trim: true)
|> Enum.map(fn str -> str |> String.trim() end)
|> Enum.reject(fn str -> str |> is_nil() end)
|> Enum.sort()
end
defp process_tags(_other_tags_string), do: []
@spec get_tags_string([String.t()] | nil) :: String.t()
def get_tags_string(nil), do: ""
def get_tags_string(tags) when tags |> is_list(), do: tags |> Enum.join(",")
end end

View File

@ -4,21 +4,87 @@ defmodule Memex.Pipelines do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Memex.Repo alias Memex.{Accounts.User, Pipelines.Pipeline, Repo}
alias Memex.Pipelines.Pipeline
@doc """ @doc """
Returns the list of pipelines. Returns the list of pipelines.
## Examples ## Examples
iex> list_pipelines() iex> list_pipelines(%User{id: 123})
[%Pipeline{}, ...] [%Pipeline{}, ...]
iex> list_pipelines("my pipeline", %User{id: 123})
[%Pipeline{slug: "my pipeline"}, ...]
""" """
def list_pipelines do @spec list_pipelines(User.t()) :: [Pipeline.t()]
Repo.all(Pipeline) @spec list_pipelines(search :: String.t() | nil, User.t()) :: [Pipeline.t()]
def list_pipelines(search \\ nil, user)
def list_pipelines(search, %{id: user_id}) when search |> is_nil() or search == "" do
Repo.all(from p in Pipeline, where: p.user_id == ^user_id, order_by: p.slug)
end
def list_pipelines(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from p in Pipeline,
where: p.user_id == ^user_id,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end
@doc """
Returns the list of public pipelines for viewing
## Examples
iex> list_public_pipelines()
[%Pipeline{}, ...]
iex> list_public_pipelines("my pipeline")
[%Pipeline{slug: "my pipeline"}, ...]
"""
@spec list_public_pipelines() :: [Pipeline.t()]
@spec list_public_pipelines(search :: String.t() | nil) :: [Pipeline.t()]
def list_public_pipelines(search \\ nil)
def list_public_pipelines(search) when search |> is_nil() or search == "" do
Repo.all(from p in Pipeline, where: p.visibility == :public, order_by: p.slug)
end
def list_public_pipelines(search) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from p in Pipeline,
where: p.visibility == :public,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end end
@doc """ @doc """
@ -28,31 +94,78 @@ defmodule Memex.Pipelines do
## Examples ## Examples
iex> get_pipeline!(123) iex> get_pipeline!(123, %User{id: 123})
%Pipeline{} %Pipeline{}
iex> get_pipeline!(456) iex> get_pipeline!(456, %User{id: 123})
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
def get_pipeline!(id), do: Repo.get!(Pipeline, id) @spec get_pipeline!(Pipeline.id(), User.t()) :: Pipeline.t()
def get_pipeline!(id, %{id: user_id}) do
Repo.one!(
from p in Pipeline,
where: p.id == ^id,
where: p.user_id == ^user_id or p.visibility in [:public, :unlisted]
)
end
def get_pipeline!(id, _invalid_user) do
Repo.one!(
from p in Pipeline,
where: p.id == ^id,
where: p.visibility in [:public, :unlisted]
)
end
@doc """
Gets a single pipeline by it's slug.
Raises `Ecto.NoResultsError` if the Pipeline does not exist.
## Examples
iex> get_pipeline_by_slug("my-pipeline", %User{id: 123})
%Pipeline{}
iex> get_pipeline_by_slug("my-pipeline", %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_pipeline_by_slug(Pipeline.slug(), User.t()) :: Pipeline.t() | nil
def get_pipeline_by_slug(slug, %{id: user_id}) do
Repo.one(
from p in Pipeline,
where: p.slug == ^slug,
where: p.user_id == ^user_id or p.visibility in [:public, :unlisted]
)
end
def get_pipeline_by_slug(slug, _invalid_user) do
Repo.one(
from p in Pipeline,
where: p.slug == ^slug,
where: p.visibility in [:public, :unlisted]
)
end
@doc """ @doc """
Creates a pipeline. Creates a pipeline.
## Examples ## Examples
iex> create_pipeline(%{field: value}) iex> create_pipeline(%{field: value}, %User{id: 123})
{:ok, %Pipeline{}} {:ok, %Pipeline{}}
iex> create_pipeline(%{field: bad_value}) iex> create_pipeline(%{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_pipeline(attrs \\ %{}) do @spec create_pipeline(User.t()) :: {:ok, Pipeline.t()} | {:error, Pipeline.changeset()}
%Pipeline{} @spec create_pipeline(attrs :: map(), User.t()) ::
|> Pipeline.changeset(attrs) {:ok, Pipeline.t()} | {:error, Pipeline.changeset()}
|> Repo.insert() def create_pipeline(attrs \\ %{}, user) do
Pipeline.create_changeset(attrs, user) |> Repo.insert()
end end
@doc """ @doc """
@ -60,16 +173,18 @@ defmodule Memex.Pipelines do
## Examples ## Examples
iex> update_pipeline(pipeline, %{field: new_value}) iex> update_pipeline(pipeline, %{field: new_value}, %User{id: 123})
{:ok, %Pipeline{}} {:ok, %Pipeline{}}
iex> update_pipeline(pipeline, %{field: bad_value}) iex> update_pipeline(pipeline, %{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def update_pipeline(%Pipeline{} = pipeline, attrs) do @spec update_pipeline(Pipeline.t(), attrs :: map(), User.t()) ::
{:ok, Pipeline.t()} | {:error, Pipeline.changeset()}
def update_pipeline(%Pipeline{} = pipeline, attrs, user) do
pipeline pipeline
|> Pipeline.changeset(attrs) |> Pipeline.update_changeset(attrs, user)
|> Repo.update() |> Repo.update()
end end
@ -78,15 +193,24 @@ defmodule Memex.Pipelines do
## Examples ## Examples
iex> delete_pipeline(pipeline) iex> delete_pipeline(%Pipeline{user_id: 123}, %User{id: 123})
{:ok, %Pipeline{}} {:ok, %Pipeline{}}
iex> delete_pipeline(pipeline) iex> delete_pipeline(%Pipeline{}, %User{role: :admin})
{:ok, %Pipeline{}}
iex> delete_pipeline(%Pipeline{}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def delete_pipeline(%Pipeline{} = pipeline) do @spec delete_pipeline(Pipeline.t(), User.t()) ::
Repo.delete(pipeline) {:ok, Pipeline.t()} | {:error, Pipeline.changeset()}
def delete_pipeline(%Pipeline{user_id: user_id} = pipeline, %{id: user_id}) do
pipeline |> Repo.delete()
end
def delete_pipeline(%Pipeline{} = pipeline, %{role: :admin}) do
pipeline |> Repo.delete()
end end
@doc """ @doc """
@ -94,11 +218,16 @@ defmodule Memex.Pipelines do
## Examples ## Examples
iex> change_pipeline(pipeline) iex> change_pipeline(pipeline, %User{id: 123})
%Ecto.Changeset{data: %Pipeline{}}
iex> change_pipeline(pipeline, %{slug: "new slug"}, %User{id: 123})
%Ecto.Changeset{data: %Pipeline{}} %Ecto.Changeset{data: %Pipeline{}}
""" """
def change_pipeline(%Pipeline{} = pipeline, attrs \\ %{}) do @spec change_pipeline(Pipeline.t(), User.t()) :: Pipeline.changeset()
Pipeline.changeset(pipeline, attrs) @spec change_pipeline(Pipeline.t(), attrs :: map(), User.t()) :: Pipeline.changeset()
def change_pipeline(%Pipeline{} = pipeline, attrs \\ %{}, user) do
pipeline |> Pipeline.update_changeset(attrs, user)
end end
end end

View File

@ -1,21 +1,114 @@
defmodule Memex.Pipelines.Pipeline do defmodule Memex.Pipelines.Pipeline do
@moduledoc """
Represents a chain of considerations to take to accomplish a task
"""
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import MemexWeb.Gettext
alias Ecto.{Changeset, UUID}
alias Memex.{Accounts.User, Pipelines.Steps.Step, Repo}
@derive {Jason.Encoder,
only: [
:slug,
:description,
:tags,
:visibility,
:inserted_at,
:steps,
:updated_at
]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "pipelines" do schema "pipelines" do
field :slug, :string
field :description, :string field :description, :string
field :title, :string field :tags, {:array, :string}
field :tags_string, :string, virtual: true
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
belongs_to :user, User
has_many :steps, Step, preload_order: [asc: :position]
timestamps() timestamps()
end end
@type t :: %__MODULE__{
slug: slug(),
description: String.t(),
tags: [String.t()] | nil,
tags_string: String.t() | nil,
visibility: :public | :private | :unlisted,
user: User.t() | Ecto.Association.NotLoaded.t(),
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type slug :: String.t()
@type changeset :: Changeset.t(t())
@doc false @doc false
def changeset(pipeline, attrs) do @spec create_changeset(attrs :: map(), User.t()) :: changeset()
pipeline def create_changeset(attrs, %User{id: user_id}) do
|> cast(attrs, [:title, :description, :visibility]) %__MODULE__{}
|> validate_required([:title, :description, :visibility]) |> cast(attrs, [:slug, :description, :tags, :visibility])
|> change(user_id: user_id)
|> cast_tags_string(attrs)
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :user_id, :visibility])
|> unique_constraint(:slug)
|> unsafe_validate_unique(:slug, Repo)
end end
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = pipeline, attrs, %User{id: user_id}) do
pipeline
|> cast(attrs, [:slug, :description, :tags, :visibility])
|> cast_tags_string(attrs)
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
)
|> validate_required([:slug, :visibility])
|> unique_constraint(:slug)
|> unsafe_validate_unique(:slug, Repo)
end
defp cast_tags_string(changeset, attrs) do
changeset
|> put_change(:tags_string, changeset |> get_field(:tags) |> get_tags_string())
|> cast(attrs, [:tags_string])
|> validate_format(:tags_string, ~r/^[\p{L}\p{N}\-\,]+$/,
message:
dgettext(
"errors",
"invalid format: only numbers, letters and hyphen are accepted. tags must be comma-delimited"
)
)
|> cast_tags()
end
defp cast_tags(%{valid?: false} = changeset), do: changeset
defp cast_tags(%{valid?: true} = changeset) do
tags = changeset |> get_field(:tags_string) |> process_tags()
changeset |> put_change(:tags, tags)
end
defp process_tags(tags_string) when tags_string |> is_binary() do
tags_string
|> String.split(",", trim: true)
|> Enum.map(fn str -> str |> String.trim() end)
|> Enum.reject(fn str -> str |> is_nil() end)
|> Enum.sort()
end
defp process_tags(_other_tags_string), do: []
@spec get_tags_string([String.t()] | nil) :: String.t()
def get_tags_string(nil), do: ""
def get_tags_string(tags) when tags |> is_list(), do: tags |> Enum.join(",")
end end

View File

@ -0,0 +1,79 @@
defmodule Memex.Pipelines.Steps.Step do
@moduledoc """
Represents a step taken while executing a pipeline
"""
use Ecto.Schema
import Ecto.Changeset
alias Ecto.{Changeset, UUID}
alias Memex.{Accounts.User, Pipelines.Pipeline}
@derive {Jason.Encoder,
only: [
:title,
:content,
:position,
:inserted_at,
:updated_at
]}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "steps" do
field :title, :string
field :content, :string
field :position, :integer
belongs_to :pipeline, Pipeline
belongs_to :user, User
timestamps()
end
@type t :: %__MODULE__{
title: String.t(),
content: String.t(),
position: non_neg_integer(),
pipeline: Pipeline.t() | Ecto.Association.NotLoaded.t(),
pipeline_id: Pipeline.id(),
user: User.t() | Ecto.Association.NotLoaded.t(),
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type changeset :: Changeset.t(t())
@doc false
@spec create_changeset(attrs :: map(), position :: non_neg_integer(), Pipeline.t(), User.t()) ::
changeset()
def create_changeset(attrs, position, %Pipeline{id: pipeline_id, user_id: user_id}, %User{
id: user_id
}) do
%__MODULE__{}
|> cast(attrs, [:title, :content])
|> change(pipeline_id: pipeline_id, user_id: user_id, position: position)
|> validate_required([:title, :content, :user_id, :position])
end
@spec update_changeset(t(), attrs :: map(), User.t()) ::
changeset()
def update_changeset(
%{user_id: user_id} = step,
attrs,
%User{id: user_id}
) do
step
|> cast(attrs, [:title, :content])
|> validate_required([:title, :content, :user_id, :position])
end
@spec position_changeset(t(), position :: non_neg_integer(), User.t()) :: changeset()
def position_changeset(
%{user_id: user_id} = step,
position,
%User{id: user_id}
) do
step
|> change(position: position)
|> validate_required([:title, :content, :user_id, :position])
end
end

View File

@ -0,0 +1,238 @@
defmodule Memex.Pipelines.Steps do
@moduledoc """
The context for steps within a pipeline
"""
import Ecto.Query, warn: false
alias Ecto.Multi
alias Memex.{Accounts.User, Repo}
alias Memex.Pipelines.{Pipeline, Steps.Step}
@doc """
Returns the list of steps.
## Examples
iex> list_steps(%User{id: 123})
[%Step{}, ...]
iex> list_steps("my step", %User{id: 123})
[%Step{title: "my step"}, ...]
"""
@spec list_steps(Pipeline.t(), User.t()) :: [Step.t()]
def list_steps(%{id: pipeline_id}, %{id: user_id}) do
Repo.all(
from s in Step,
where: s.pipeline_id == ^pipeline_id,
where: s.user_id == ^user_id,
order_by: s.position
)
end
def list_steps(%{id: pipeline_id, visibility: visibility}, _invalid_user)
when visibility in [:unlisted, :public] do
Repo.all(
from s in Step,
where: s.pipeline_id == ^pipeline_id,
order_by: s.position
)
end
@doc """
Preloads the `:steps` field on a Memex.Pipelines.Pipeline
"""
@spec preload_steps(Pipeline.t(), User.t()) :: Pipeline.t()
def preload_steps(pipeline, user) do
%{pipeline | steps: list_steps(pipeline, user)}
end
@doc """
Gets a single step.
Raises `Ecto.NoResultsError` if the Step does not exist.
## Examples
iex> get_step!(123, %User{id: 123})
%Step{}
iex> get_step!(456, %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_step!(Step.id(), User.t()) :: Step.t()
def get_step!(id, %{id: user_id}) do
Repo.one!(from n in Step, where: n.id == ^id, where: n.user_id == ^user_id)
end
def get_step!(id, _invalid_user) do
Repo.one!(
from n in Step,
where: n.id == ^id,
where: n.visibility in [:public, :unlisted]
)
end
@doc """
Creates a step.
## Examples
iex> create_step(%{field: value}, %User{id: 123})
{:ok, %Step{}}
iex> create_step(%{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}}
"""
@spec create_step(position :: non_neg_integer(), Pipeline.t(), User.t()) ::
{:ok, Step.t()} | {:error, Step.changeset()}
@spec create_step(attrs :: map(), position :: non_neg_integer(), Pipeline.t(), User.t()) ::
{:ok, Step.t()} | {:error, Step.changeset()}
def create_step(attrs \\ %{}, position, pipeline, user) do
Step.create_changeset(attrs, position, pipeline, user) |> Repo.insert()
end
@doc """
Updates a step.
## Examples
iex> update_step(step, %{field: new_value}, %User{id: 123})
{:ok, %Step{}}
iex> update_step(step, %{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}}
"""
@spec update_step(Step.t(), attrs :: map(), User.t()) ::
{:ok, Step.t()} | {:error, Step.changeset()}
def update_step(%Step{} = step, attrs, user) do
step
|> Step.update_changeset(attrs, user)
|> Repo.update()
end
@doc """
Deletes a step.
## Examples
iex> delete_step(%Step{user_id: 123}, %User{id: 123})
{:ok, %Step{}}
iex> delete_step(%Step{}, %User{role: :admin})
{:ok, %Step{}}
iex> delete_step(%Step{}, %User{id: 123})
{:error, %Ecto.Changeset{}}
"""
@spec delete_step(Step.t(), User.t()) :: {:ok, Step.t()} | {:error, Step.changeset()}
def delete_step(%Step{user_id: user_id} = step, %{id: user_id}) do
delete_step(step)
end
def delete_step(%Step{} = step, %{role: :admin}) do
delete_step(step)
end
defp delete_step(step) do
Multi.new()
|> Multi.delete(:delete_step, step)
|> Multi.update_all(
:reorder_steps,
fn %{delete_step: %{position: position, pipeline_id: pipeline_id}} ->
from s in Step,
where: s.pipeline_id == ^pipeline_id,
where: s.position > ^position,
update: [set: [position: s.position - 1]]
end,
[]
)
|> Repo.transaction()
|> case do
{:ok, %{delete_step: step}} -> {:ok, step}
{:error, :delete_step, changeset, _changes_so_far} -> {:error, changeset}
end
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking step changes.
## Examples
iex> change_step(step, %User{id: 123})
%Ecto.Changeset{data: %Step{}}
iex> change_step(step, %{title: "new title"}, %User{id: 123})
%Ecto.Changeset{data: %Step{}}
"""
@spec change_step(Step.t(), User.t()) :: Step.changeset()
@spec change_step(Step.t(), attrs :: map(), User.t()) :: Step.changeset()
def change_step(%Step{} = step, attrs \\ %{}, user) do
step |> Step.update_changeset(attrs, user)
end
@spec reorder_step(Step.t(), :up | :down, User.t()) ::
{:ok, Step.t()} | {:error, Step.changeset()}
def reorder_step(%Step{position: 0} = step, :up, _user), do: {:error, step}
def reorder_step(
%Step{position: position, pipeline_id: pipeline_id, user_id: user_id} = step,
:up,
%{id: user_id} = user
) do
Multi.new()
|> Multi.update_all(
:reorder_steps,
from(s in Step,
where: s.pipeline_id == ^pipeline_id,
where: s.position == ^position - 1,
update: [set: [position: ^position]]
),
[]
)
|> Multi.update(
:update_step,
step |> Step.position_changeset(position - 1, user)
)
|> Repo.transaction()
|> case do
{:ok, %{update_step: step}} -> {:ok, step}
{:error, :update_step, changeset, _changes_so_far} -> {:error, changeset}
end
end
def reorder_step(
%Step{pipeline_id: pipeline_id, position: position, user_id: user_id} = step,
:down,
%{id: user_id} = user
) do
Multi.new()
|> Multi.one(
:step_count,
from(s in Step, where: s.pipeline_id == ^pipeline_id, distinct: true, select: count(s.id))
)
|> Multi.update_all(
:reorder_steps,
from(s in Step,
where: s.pipeline_id == ^pipeline_id,
where: s.position == ^position + 1,
update: [set: [position: ^position]]
),
[]
)
|> Multi.update(:update_step, fn %{step_count: step_count} ->
new_position = if position >= step_count - 1, do: position, else: position + 1
step |> Step.position_changeset(new_position, user)
end)
|> Repo.transaction()
|> case do
{:ok, %{update_step: step}} -> {:ok, step}
{:error, :update_step, changeset, _changes_so_far} -> {:error, changeset}
end
end
end

View File

@ -7,7 +7,9 @@ defmodule Memex.Release do
def rollback(repo, version) do def rollback(repo, version) do
load_app() load_app()
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
{:ok, _fun_return, _apps} =
Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end end
defp load_app do defp load_app do
@ -18,7 +20,8 @@ defmodule Memex.Release do
load_app() load_app()
for repo <- Application.fetch_env!(@app, :ecto_repos) do for repo <- Application.fetch_env!(@app, :ecto_repos) do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true)) {:ok, _fun_return, _apps} =
Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end end
end end
end end

View File

@ -6,17 +6,20 @@ defmodule Memex.Repo.Migrator do
use GenServer use GenServer
require Logger require Logger
def start_link(_) do def start_link(_opts) do
GenServer.start_link(__MODULE__, [], []) GenServer.start_link(__MODULE__, [], [])
end end
def init(_) do def init(_opts) do
migrate!() {:ok, if(automigrate_enabled?(), do: migrate!())}
{:ok, nil}
end end
def migrate! do def migrate! do
path = Application.app_dir(:memex, "priv/repo/migrations") path = Application.app_dir(:memex, "priv/repo/migrations")
Ecto.Migrator.run(Memex.Repo, path, :up, all: true) Ecto.Migrator.run(Memex.Repo, path, :up, all: true)
end end
defp automigrate_enabled? do
Application.get_env(:memex, Memex.Application, automigrate: false)[:automigrate]
end
end end

View File

@ -1,104 +0,0 @@
defmodule Memex.Steps do
@moduledoc """
The Steps context.
"""
import Ecto.Query, warn: false
alias Memex.Repo
alias Memex.Steps.Step
@doc """
Returns the list of steps.
## Examples
iex> list_steps()
[%Step{}, ...]
"""
def list_steps do
Repo.all(Step)
end
@doc """
Gets a single step.
Raises `Ecto.NoResultsError` if the Step does not exist.
## Examples
iex> get_step!(123)
%Step{}
iex> get_step!(456)
** (Ecto.NoResultsError)
"""
def get_step!(id), do: Repo.get!(Step, id)
@doc """
Creates a step.
## Examples
iex> create_step(%{field: value})
{:ok, %Step{}}
iex> create_step(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_step(attrs \\ %{}) do
%Step{}
|> Step.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a step.
## Examples
iex> update_step(step, %{field: new_value})
{:ok, %Step{}}
iex> update_step(step, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_step(%Step{} = step, attrs) do
step
|> Step.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a step.
## Examples
iex> delete_step(step)
{:ok, %Step{}}
iex> delete_step(step)
{:error, %Ecto.Changeset{}}
"""
def delete_step(%Step{} = step) do
Repo.delete(step)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking step changes.
## Examples
iex> change_step(step)
%Ecto.Changeset{data: %Step{}}
"""
def change_step(%Step{} = step, attrs \\ %{}) do
Step.changeset(step, attrs)
end
end

View File

@ -1,22 +0,0 @@
defmodule Memex.Steps.Step do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "steps" do
field :description, :string
field :position, :integer
field :title, :string
field :pipeline_id, :binary_id
timestamps()
end
@doc false
def changeset(step, attrs) do
step
|> cast(attrs, [:title, :description, :position])
|> validate_required([:title, :description, :position])
end
end

View File

@ -1,20 +0,0 @@
defmodule Memex.Steps.StepContext do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "step_contexts" do
field :step_id, :binary_id
field :context_id, :binary_id
timestamps()
end
@doc false
def changeset(step_context, attrs) do
step_context
|> cast(attrs, [])
|> validate_required([])
end
end

View File

@ -23,6 +23,7 @@ defmodule MemexWeb do
import Plug.Conn import Plug.Conn
import MemexWeb.Gettext import MemexWeb.Gettext
alias MemexWeb.Endpoint
alias MemexWeb.Router.Helpers, as: Routes alias MemexWeb.Router.Helpers, as: Routes
end end
end end
@ -45,7 +46,7 @@ defmodule MemexWeb do
def live_view do def live_view do
quote do quote do
use Phoenix.LiveView, use Phoenix.LiveView,
layout: {MemexWeb.LayoutView, :live} layout: {MemexWeb.LayoutView, "live.html"}
on_mount MemexWeb.InitAssigns on_mount MemexWeb.InitAssigns
unquote(view_helpers()) unquote(view_helpers())
@ -72,15 +73,16 @@ defmodule MemexWeb do
quote do quote do
use Phoenix.Router use Phoenix.Router
import Phoenix.{Controller, LiveView.Router}
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import Plug.Conn import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
end end
end end
def channel do def channel do
quote do quote do
use Phoenix.Channel use Phoenix.Channel
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import MemexWeb.Gettext import MemexWeb.Gettext
end end
end end
@ -88,16 +90,16 @@ defmodule MemexWeb do
defp view_helpers do defp view_helpers do
quote do quote do
# Use all HTML functionality (forms, tags, etc) # Use all HTML functionality (forms, tags, etc)
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
use Phoenix.HTML use Phoenix.HTML
# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc) # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
import Phoenix.Component
import MemexWeb.LiveHelpers
# Import basic rendering functionality (render, render_layout, etc) # Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View import Phoenix.{Component, View}
import MemexWeb.{ErrorHelpers, Gettext, LiveHelpers, ViewHelpers} import MemexWeb.{ErrorHelpers, Gettext, LiveHelpers, ViewHelpers}
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
alias MemexWeb.Endpoint
alias MemexWeb.Router.Helpers, as: Routes alias MemexWeb.Router.Helpers, as: Routes
end end
end end

View File

@ -0,0 +1,44 @@
defmodule MemexWeb.Components.ContextContent do
@moduledoc """
Display the content for a context
"""
use MemexWeb, :component
alias Memex.Contexts.Context
alias Phoenix.HTML
attr :context, Context, required: true
def context_content(assigns) do
~H"""
<div
id={"show-context-content-#{@context.id}"}
class="input input-primary h-128 min-h-128 inline-block whitespace-pre-wrap overflow-x-hidden overflow-y-auto"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><p class="inline"><%= add_links_to_content(@context.content) %></p></div>
"""
end
defp add_links_to_content(content) do
Regex.replace(
~r/\[\[([\p{L}\p{N}\-]+)\]\]/,
content,
fn _whole_match, slug ->
link =
HTML.Link.link(
"[[#{slug}]]",
to: Routes.note_show_path(Endpoint, :show, slug),
class: "link inline",
data: [qa: "context-note-#{slug}"]
)
|> HTML.Safe.to_iodata()
|> IO.iodata_to_binary()
"</p>#{link}<p class=\"inline\">"
end
)
|> HTML.raw()
end
end

View File

@ -0,0 +1,134 @@
defmodule MemexWeb.Components.ContextsTableComponent do
@moduledoc """
A component that displays a list of contexts
"""
use MemexWeb, :live_component
alias Ecto.UUID
alias Memex.{Accounts.User, Contexts.Context}
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
required(:contexts) => [Context.t()],
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, contexts: _contexts, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> display_contexts()
{:ok, socket}
end
defp display_contexts(
%{
assigns: %{
contexts: contexts,
current_user: current_user,
actions: actions
}
} = socket
) do
columns =
if actions == [] or current_user |> is_nil() do
[]
else
[%{label: nil, key: :actions, sortable: false}]
end
columns = [
%{label: gettext("slug"), key: :slug},
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
| columns
]
rows =
contexts
|> Enum.map(fn context ->
context
|> get_row_data_for_context(%{
columns: columns,
current_user: current_user,
actions: actions
})
end)
socket |> assign(columns: columns, rows: rows)
end
@impl true
def render(assigns) do
~H"""
<div class="w-full">
<.live_component
module={MemexWeb.Components.TableComponent}
id={@id}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
@spec get_row_data_for_context(Context.t(), additional_data :: map()) :: map()
defp get_row_data_for_context(context, %{columns: columns} = additional_data) do
columns
|> Map.new(fn %{key: key} ->
{key, get_value_for_key(key, context, additional_data)}
end)
end
@spec get_value_for_key(atom(), Context.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(:slug, %{slug: slug}, _additional_data) do
assigns = %{slug: slug}
slug_block = ~H"""
<.link
navigate={Routes.context_show_path(Endpoint, :show, @slug)}
class="link"
data-qa={"context-show-#{@slug}"}
>
<%= @slug %>
</.link>
"""
{slug, slug_block}
end
defp get_value_for_key(:tags, %{tags: tags}, _additional_data) do
assigns = %{tags: tags}
~H"""
<div class="flex flex-wrap justify-center space-x-1">
<.link
:for={tag <- @tags}
patch={Routes.context_index_path(Endpoint, :search, tag)}
class="link"
>
<%= tag %>
</.link>
</div>
"""
end
defp get_value_for_key(:actions, context, %{actions: actions}) do
assigns = %{actions: actions, context: context}
~H"""
<div class="flex justify-center items-center space-x-4">
<%= render_slot(@actions, @context) %>
</div>
"""
end
defp get_value_for_key(key, context, _additional_data), do: context |> Map.get(key)
end

View File

@ -4,9 +4,20 @@ defmodule MemexWeb.Components.InviteCard do
""" """
use MemexWeb, :component use MemexWeb, :component
alias Memex.Accounts.{Invite, Invites, User}
alias MemexWeb.Endpoint alias MemexWeb.Endpoint
def invite_card(assigns) do attr :invite, Invite, required: true
attr :current_user, User, required: true
slot(:inner_block)
slot(:code_actions)
def invite_card(%{invite: invite, current_user: current_user} = assigns) do
assigns =
assigns
|> assign(:use_count, Invites.get_use_count(invite, current_user))
|> assign_new(:code_actions, fn -> [] end)
~H""" ~H"""
<div class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center space-y-4 <div class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center space-y-4
border border-gray-400 rounded-lg shadow-lg hover:shadow-md border border-gray-400 rounded-lg shadow-lg hover:shadow-md
@ -17,8 +28,14 @@ defmodule MemexWeb.Components.InviteCard do
<%= if @invite.disabled_at |> is_nil() do %> <%= if @invite.disabled_at |> is_nil() do %>
<h2 class="title text-md"> <h2 class="title text-md">
<%= gettext("Uses Left:") %> <%= if @invite.uses_left do %>
<%= @invite.uses_left || gettext("unlimited") %> <%= gettext(
"uses left: %{uses_left_count}",
uses_left_count: @invite.uses_left
) %>
<% else %>
<%= gettext("uses left: unlimited") %>
<% end %>
</h2> </h2>
<% else %> <% else %>
<h2 class="title text-md"> <h2 class="title text-md">
@ -26,24 +43,27 @@ defmodule MemexWeb.Components.InviteCard do
</h2> </h2>
<% end %> <% end %>
<.qr_code
content={Routes.user_registration_url(Endpoint, :new, invite: @invite.token)}
filename={@invite.name}
/>
<h2 :if={@use_count != 0} class="title text-md">
<%= gettext("uses: %{uses_count}", uses_count: @use_count) %>
</h2>
<div class="flex flex-row flex-wrap justify-center items-center"> <div class="flex flex-row flex-wrap justify-center items-center">
<code <code
id={"code-#{@invite.id}"} id={"code-#{@invite.id}"}
class="mx-2 my-1 text-xs px-4 py-2 rounded-lg text-center break-all text-gray-100 bg-primary-800" class="mx-2 my-1 text-xs px-4 py-2 rounded-lg text-center break-all text-gray-100 bg-primary-800"
> phx-no-format
<%= Routes.user_registration_url(Endpoint, :new, invite: @invite.token) %> ><%= Routes.user_registration_url(Endpoint, :new, invite: @invite.token) %></code>
</code> <%= render_slot(@code_actions) %>
<%= if @code_actions do %>
<%= render_slot(@code_actions) %>
<% end %>
</div> </div>
<%= if @inner_block do %> <div :if={@inner_block} class="flex space-x-4 justify-center items-center">
<div class="flex space-x-4 justify-center items-center"> <%= render_slot(@inner_block) %>
<%= render_slot(@inner_block) %> </div>
</div>
<% end %>
</div> </div>
""" """
end end

View File

@ -0,0 +1,44 @@
defmodule MemexWeb.Components.NoteContent do
@moduledoc """
Display the content for a note
"""
use MemexWeb, :component
alias Memex.Notes.Note
alias Phoenix.HTML
attr :note, Note, required: true
def note_content(assigns) do
~H"""
<div
id={"show-note-content-#{@note.id}"}
class="input input-primary h-128 min-h-128 inline-block whitespace-pre-wrap overflow-x-hidden overflow-y-auto"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><p class="inline"><%= add_links_to_content(@note.content) %></p></div>
"""
end
defp add_links_to_content(content) do
Regex.replace(
~r/\[\[([\p{L}\p{N}\-]+)\]\]/,
content,
fn _whole_match, slug ->
link =
HTML.Link.link(
"[[#{slug}]]",
to: Routes.note_show_path(Endpoint, :show, slug),
class: "link inline",
data: [qa: "note-link-#{slug}"]
)
|> HTML.Safe.to_iodata()
|> IO.iodata_to_binary()
"</p>#{link}<p class=\"inline\">"
end
)
|> HTML.raw()
end
end

View File

@ -0,0 +1,130 @@
defmodule MemexWeb.Components.NotesTableComponent do
@moduledoc """
A component that displays a list of notes
"""
use MemexWeb, :live_component
alias Ecto.UUID
alias Memex.{Accounts.User, Notes.Note}
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
required(:notes) => [Note.t()],
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, notes: _notes, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> display_notes()
{:ok, socket}
end
defp display_notes(
%{
assigns: %{
notes: notes,
current_user: current_user,
actions: actions
}
} = socket
) do
columns =
if actions == [] or current_user |> is_nil() do
[]
else
[%{label: nil, key: :actions, sortable: false}]
end
columns = [
%{label: gettext("slug"), key: :slug},
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
| columns
]
rows =
notes
|> Enum.map(fn note ->
note
|> get_row_data_for_note(%{
columns: columns,
current_user: current_user,
actions: actions
})
end)
socket |> assign(columns: columns, rows: rows)
end
@impl true
def render(assigns) do
~H"""
<div class="w-full">
<.live_component
module={MemexWeb.Components.TableComponent}
id={@id}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
@spec get_row_data_for_note(Note.t(), additional_data :: map()) :: map()
defp get_row_data_for_note(note, %{columns: columns} = additional_data) do
columns
|> Map.new(fn %{key: key} ->
{key, get_value_for_key(key, note, additional_data)}
end)
end
@spec get_value_for_key(atom(), Note.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(:slug, %{slug: slug}, _additional_data) do
assigns = %{slug: slug}
slug_block = ~H"""
<.link
navigate={Routes.note_show_path(Endpoint, :show, @slug)}
class="link"
data-qa={"note-show-#{@slug}"}
>
<%= @slug %>
</.link>
"""
{slug, slug_block}
end
defp get_value_for_key(:tags, %{tags: tags}, _additional_data) do
assigns = %{tags: tags}
~H"""
<div class="flex flex-wrap justify-center space-x-1">
<.link :for={tag <- @tags} patch={Routes.note_index_path(Endpoint, :search, tag)} class="link">
<%= tag %>
</.link>
</div>
"""
end
defp get_value_for_key(:actions, note, %{actions: actions}) do
assigns = %{actions: actions, note: note}
~H"""
<div class="flex justify-center items-center space-x-4">
<%= render_slot(@actions, @note) %>
</div>
"""
end
defp get_value_for_key(key, note, _additional_data), do: note |> Map.get(key)
end

View File

@ -0,0 +1,147 @@
defmodule MemexWeb.Components.PipelinesTableComponent do
@moduledoc """
A component that displays a list of pipelines
"""
use MemexWeb, :live_component
alias Ecto.UUID
alias Memex.{Accounts.User, Pipelines.Pipeline}
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
required(:pipelines) => [Pipeline.t()],
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, pipelines: _pipelines, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> display_pipelines()
{:ok, socket}
end
defp display_pipelines(
%{
assigns: %{
pipelines: pipelines,
current_user: current_user,
actions: actions
}
} = socket
) do
columns =
if actions == [] or current_user |> is_nil() do
[]
else
[%{label: nil, key: :actions, sortable: false}]
end
columns = [
%{label: gettext("slug"), key: :slug},
%{label: gettext("description"), key: :description},
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
| columns
]
rows =
pipelines
|> Enum.map(fn pipeline ->
pipeline
|> get_row_data_for_pipeline(%{
columns: columns,
current_user: current_user,
actions: actions
})
end)
socket |> assign(columns: columns, rows: rows)
end
@impl true
def render(assigns) do
~H"""
<div class="w-full">
<.live_component
module={MemexWeb.Components.TableComponent}
id={@id}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
@spec get_row_data_for_pipeline(Pipeline.t(), additional_data :: map()) :: map()
defp get_row_data_for_pipeline(pipeline, %{columns: columns} = additional_data) do
columns
|> Map.new(fn %{key: key} ->
{key, get_value_for_key(key, pipeline, additional_data)}
end)
end
@spec get_value_for_key(atom(), Pipeline.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(:slug, %{slug: slug}, _additional_data) do
assigns = %{slug: slug}
slug_block = ~H"""
<.link
navigate={Routes.pipeline_show_path(Endpoint, :show, @slug)}
class="link"
data-qa={"pipeline-show-#{@slug}"}
>
<%= @slug %>
</.link>
"""
{slug, slug_block}
end
defp get_value_for_key(:description, %{description: description}, _additional_data) do
assigns = %{description: description}
description_block = ~H"""
<div class="truncate max-w-sm">
<%= @description %>
</div>
"""
{description, description_block}
end
defp get_value_for_key(:tags, %{tags: tags}, _additional_data) do
assigns = %{tags: tags}
~H"""
<div class="flex flex-wrap justify-center space-x-1">
<.link
:for={tag <- @tags}
patch={Routes.pipeline_index_path(Endpoint, :search, tag)}
class="link"
>
<%= tag %>
</.link>
</div>
"""
end
defp get_value_for_key(:actions, pipeline, %{actions: actions}) do
assigns = %{actions: actions, pipeline: pipeline}
~H"""
<div class="flex justify-center items-center space-x-4">
<%= render_slot(@actions, @pipeline) %>
</div>
"""
end
defp get_value_for_key(key, pipeline, _additional_data), do: pipeline |> Map.get(key)
end

View File

@ -0,0 +1,44 @@
defmodule MemexWeb.Components.StepContent do
@moduledoc """
Display the content for a step
"""
use MemexWeb, :component
alias Memex.Pipelines.Steps.Step
alias Phoenix.HTML
attr :step, Step, required: true
def step_content(assigns) do
~H"""
<div
id={"show-step-content-#{@step.id}"}
class="input input-primary h-32 min-h-32 inline-block whitespace-pre-wrap overflow-x-hidden overflow-y-auto"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><p class="inline"><%= add_links_to_content(@step.content) %></p></div>
"""
end
defp add_links_to_content(content) do
Regex.replace(
~r/\[\[([\p{L}\p{N}\-]+)\]\]/,
content,
fn _whole_match, slug ->
link =
HTML.Link.link(
"[[#{slug}]]",
to: Routes.context_show_path(Endpoint, :show, slug),
class: "link inline",
data: [qa: "step-context-#{slug}"]
)
|> HTML.Safe.to_iodata()
|> IO.iodata_to_binary()
"</p>#{link}<p class=\"inline\">"
end
)
|> HTML.raw()
end
end

View File

@ -4,7 +4,7 @@
<tr> <tr>
<%= for %{key: key, label: label} = column <- @columns do %> <%= for %{key: key, label: label} = column <- @columns do %>
<%= if column |> Map.get(:sortable, true) do %> <%= if column |> Map.get(:sortable, true) do %>
<th class={"p-2 #{column[:class]}"}> <th class={["p-2", column[:class]]}>
<span <span
class="cursor-pointer flex justify-center items-center space-x-2" class="cursor-pointer flex justify-center items-center space-x-2"
phx-click="sort_by" phx-click="sort_by"
@ -25,7 +25,7 @@
</span> </span>
</th> </th>
<% else %> <% else %>
<th class={"p-2 #{column[:class]}"}> <th class={["p-2", column[:class]]}>
<%= label %> <%= label %>
</th> </th>
<% end %> <% end %>
@ -33,20 +33,19 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<%= for {values, i} <- @rows |> Enum.with_index() do %> <tr
<tr class={if i |> Integer.is_even(), do: @row_class, else: @alternate_row_class}> :for={{values, i} <- @rows |> Enum.with_index()}
<%= for %{key: key} = value <- @columns do %> class={if i |> Integer.is_even(), do: @row_class, else: @alternate_row_class}
<td class={"p-2 #{value[:class]}"}> >
<%= case values |> Map.get(key) do %> <td :for={%{key: key} = value <- @columns} class={["p-2", value[:class]]}>
<% {_custom_sort_value, value} -> %> <%= case values |> Map.get(key) do %>
<%= value %> <% {_custom_sort_value, value} -> %>
<% value -> %> <%= value %>
<%= value %> <% value -> %>
<% end %> <%= value %>
</td>
<% end %> <% end %>
</tr> </td>
<% end %> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -6,7 +6,7 @@ defmodule MemexWeb.Components.Topbar do
use MemexWeb, :component use MemexWeb, :component
alias Memex.Accounts alias Memex.Accounts
alias MemexWeb.{Endpoint, HomeLive} alias MemexWeb.HomeLive
def topbar(assigns) do def topbar(assigns) do
assigns = assigns =
@ -20,7 +20,7 @@ defmodule MemexWeb.Components.Topbar do
navigate={Routes.live_path(Endpoint, HomeLive)} navigate={Routes.live_path(Endpoint, HomeLive)}
class="mx-2 my-1 leading-5 text-xl text-primary-400 hover:underline" class="mx-2 my-1 leading-5 text-xl text-primary-400 hover:underline"
> >
<%= gettext("memex") %> <%= gettext("memEx") %>
</.link> </.link>
<%= if @title_content do %> <%= if @title_content do %>
@ -35,47 +35,45 @@ defmodule MemexWeb.Components.Topbar do
<ul class="flex flex-row flex-wrap justify-center items-center <ul class="flex flex-row flex-wrap justify-center items-center
text-lg text-primary-400 text-ellipsis"> text-lg text-primary-400 text-ellipsis">
<li class="mx-2 my-1">
<.link
navigate={Routes.note_index_path(Endpoint, :index)}
class="text-primary-400 text-primary-400 hover:underline truncate"
>
<%= gettext("notes") %>
</.link>
</li>
<li class="mx-2 my-1">
<.link
navigate={Routes.context_index_path(Endpoint, :index)}
class="text-primary-400 text-primary-400 hover:underline truncate"
>
<%= gettext("contexts") %>
</.link>
</li>
<li class="mx-2 my-1">
<.link
navigate={Routes.pipeline_index_path(Endpoint, :index)}
class="text-primary-400 text-primary-400 hover:underline truncate"
>
<%= gettext("pipelines") %>
</.link>
</li>
<li class="mx-2 my-1 border-left border border-primary-700"></li>
<%= if @current_user do %> <%= if @current_user do %>
<li class="mx-2 my-1"> <li :if={@current_user |> Accounts.is_already_admin?()} class="mx-2 my-1">
<.link <.link
navigate={Routes.note_index_path(Endpoint, :index)} navigate={Routes.invite_index_path(Endpoint, :index)}
class="text-primary-400 text-primary-400 hover:underline truncate" class="text-primary-400 text-primary-400 hover:underline"
> >
<%= gettext("notes") %> <%= gettext("invites") %>
</.link> </.link>
</li> </li>
<li class="mx-2 my-1">
<.link
navigate={Routes.context_index_path(Endpoint, :index)}
class="text-primary-400 text-primary-400 hover:underline truncate"
>
<%= gettext("contexts") %>
</.link>
</li>
<li class="mx-2 my-1">
<.link
navigate={Routes.pipeline_index_path(Endpoint, :index)}
class="text-primary-400 text-primary-400 hover:underline truncate"
>
<%= gettext("pipelines") %>
</.link>
</li>
<li class="mx-2 my-1 border-left border border-primary-700"></li>
<%= if @current_user.role == :admin do %>
<li class="mx-2 my-1">
<.link
navigate={Routes.invite_index_path(Endpoint, :index)}
class="text-primary-400 text-primary-400 hover:underline"
>
<%= gettext("invites") %>
</.link>
</li>
<% end %>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <.link
navigate={Routes.user_settings_path(Endpoint, :edit)} navigate={Routes.user_settings_path(Endpoint, :edit)}
@ -95,27 +93,28 @@ defmodule MemexWeb.Components.Topbar do
</.link> </.link>
</li> </li>
<%= if @current_user.role == :admin and function_exported?(Routes, :live_dashboard_path, 2) do %> <li
<li class="mx-2 my-1"> :if={
<.link @current_user.role == :admin and function_exported?(Routes, :live_dashboard_path, 2)
navigate={Routes.live_dashboard_path(Endpoint, :home)} }
class="text-primary-400 text-primary-400 hover:underline" class="mx-2 my-1"
> >
<i class="fas fa-gauge"></i> <.link
</.link> navigate={Routes.live_dashboard_path(Endpoint, :home)}
</li> class="text-primary-400 text-primary-400 hover:underline"
<% end %> >
<i class="fas fa-gauge"></i>
</.link>
</li>
<% else %> <% else %>
<%= if Accounts.allow_registration?() do %> <li :if={Accounts.allow_registration?()} class="mx-2 my-1">
<li class="mx-2 my-1"> <.link
<.link navigate={Routes.user_registration_path(Endpoint, :new)}
navigate={Routes.user_registration_path(Endpoint, :new)} class="text-primary-400 text-primary-400 hover:underline truncate"
class="text-primary-400 text-primary-400 hover:underline truncate" >
> <%= dgettext("actions", "register") %>
<%= dgettext("actions", "register") %> </.link>
</.link> </li>
</li>
<% end %>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <.link

View File

@ -10,8 +10,8 @@ defmodule MemexWeb.Components.UserCard do
<div <div
id={"user-#{@user.id}"} id={"user-#{@user.id}"}
class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center text-center class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center text-center
border border-gray-400 rounded-lg shadow-lg hover:shadow-md border border-gray-400 rounded-lg shadow-lg hover:shadow-md
transition-all duration-300 ease-in-out" transition-all duration-300 ease-in-out"
> >
<h1 class="px-4 py-2 rounded-lg title text-xl break-all"> <h1 class="px-4 py-2 rounded-lg title text-xl break-all">
<%= @user.email %> <%= @user.email %>
@ -19,27 +19,29 @@ defmodule MemexWeb.Components.UserCard do
<h3 class="px-4 py-2 rounded-lg title text-lg"> <h3 class="px-4 py-2 rounded-lg title text-lg">
<p> <p>
<%= if @user.confirmed_at |> is_nil() do %> <%= if @user.confirmed_at do %>
<%= gettext("email unconfirmed") %>
<% else %>
<%= gettext( <%= gettext(
"user was confirmed at %{relative_datetime}", "user confirmed on%{confirmed_datetime}",
relative_datetime: @user.confirmed_at |> display_datetime() confirmed_datetime: ""
) %> ) %>
<.datetime datetime={@user.confirmed_at} />
<% else %>
<%= gettext("email unconfirmed") %>
<% end %> <% end %>
</p> </p>
<p> <p>
<%= gettext("User registered on") %> <%= gettext(
<%= @user.inserted_at |> display_datetime() %> "user registered on%{registered_datetime}",
registered_datetime: ""
) %>
<.datetime datetime={@user.inserted_at} />
</p> </p>
</h3> </h3>
<%= if @inner_block do %> <div :if={@inner_block} class="px-4 py-2 flex space-x-4 justify-center items-center">
<div class="px-4 py-2 flex space-x-4 justify-center items-center"> <%= render_slot(@inner_block) %>
<%= render_slot(@inner_block) %> </div>
</div>
<% end %>
</div> </div>
""" """
end end

View File

@ -0,0 +1,17 @@
defmodule MemexWeb.ExportController do
use MemexWeb, :controller
alias Memex.{Contexts, Notes, Pipelines, Pipelines.Steps}
def export(%{assigns: %{current_user: current_user}} = conn, %{"mode" => "json"}) do
pipelines =
Pipelines.list_pipelines(current_user)
|> Enum.map(fn pipeline -> Steps.preload_steps(pipeline, current_user) end)
json(conn, %{
user: current_user,
notes: Notes.list_notes(current_user),
contexts: Contexts.list_contexts(current_user),
pipelines: pipelines
})
end
end

View File

@ -1,15 +1,12 @@
defmodule MemexWeb.UserRegistrationController do defmodule MemexWeb.UserRegistrationController do
use MemexWeb, :controller use MemexWeb, :controller
import MemexWeb.Gettext import MemexWeb.Gettext
alias Memex.{Accounts, Invites} alias Memex.{Accounts, Accounts.Invites}
alias Memex.Accounts.User
alias MemexWeb.{Endpoint, HomeLive} alias MemexWeb.{Endpoint, HomeLive}
def new(conn, %{"invite" => invite_token}) do def new(conn, %{"invite" => invite_token}) do
invite = Invites.get_invite_by_token(invite_token) if Invites.valid_invite_token?(invite_token) do
conn |> render_new(invite_token)
if invite do
conn |> render_new(invite)
else else
conn conn
|> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired")) |> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired"))
@ -28,19 +25,17 @@ defmodule MemexWeb.UserRegistrationController do
end end
# renders new user registration page # renders new user registration page
defp render_new(conn, invite \\ nil) do defp render_new(conn, invite_token \\ nil) do
render(conn, "new.html", render(conn, "new.html",
changeset: Accounts.change_user_registration(%User{}), changeset: Accounts.change_user_registration(),
invite: invite, invite_token: invite_token,
page_title: gettext("register") page_title: gettext("register")
) )
end end
def create(conn, %{"user" => %{"invite_token" => invite_token}} = attrs) do def create(conn, %{"user" => %{"invite_token" => invite_token}} = attrs) do
invite = Invites.get_invite_by_token(invite_token) if Invites.valid_invite_token?(invite_token) do
conn |> create_user(attrs, invite_token)
if invite do
conn |> create_user(attrs, invite)
else else
conn conn
|> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired")) |> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired"))
@ -58,24 +53,25 @@ defmodule MemexWeb.UserRegistrationController do
end end
end end
defp create_user(conn, %{"user" => user_params}, invite \\ nil) do defp create_user(conn, %{"user" => user_params}, invite_token \\ nil) do
case Accounts.register_user(user_params) do case Accounts.register_user(user_params, invite_token) do
{:ok, user} -> {:ok, user} ->
unless invite |> is_nil() do
invite |> Invites.use_invite!()
end
Accounts.deliver_user_confirmation_instructions( Accounts.deliver_user_confirmation_instructions(
user, user,
&Routes.user_confirmation_url(conn, :confirm, &1) &Routes.user_confirmation_url(conn, :confirm, &1)
) )
conn conn
|> put_flash(:info, dgettext("prompts", "Please check your email to verify your account")) |> put_flash(:info, dgettext("prompts", "please check your email to verify your account"))
|> redirect(to: Routes.user_session_path(Endpoint, :new)) |> redirect(to: Routes.user_session_path(Endpoint, :new))
{:error, :invalid_token} ->
conn
|> put_flash(:error, dgettext("errors", "sorry, this invite was not found or expired"))
|> redirect(to: Routes.live_path(Endpoint, HomeLive))
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
conn |> render("new.html", changeset: changeset, invite: invite) conn |> render("new.html", changeset: changeset, invite_token: invite_token)
end end
end end
end end

View File

@ -6,7 +6,7 @@ defmodule MemexWeb.UserResetPasswordController do
plug :get_user_by_reset_password_token when action in [:edit, :update] plug :get_user_by_reset_password_token when action in [:edit, :update]
def new(conn, _params) do def new(conn, _params) do
render(conn, "new.html", page_title: gettext("Forgot your password?")) render(conn, "new.html", page_title: gettext("forgot your password?"))
end end
def create(conn, %{"user" => %{"email" => email}}) do def create(conn, %{"user" => %{"email" => email}}) do

View File

@ -20,7 +20,7 @@ defmodule MemexWeb.UserSessionController do
def delete(conn, _params) do def delete(conn, _params) do
conn conn
|> put_flash(:info, dgettext("prompts", "Logged out successfully.")) |> put_flash(:info, dgettext("prompts", "logged out successfully."))
|> UserAuth.log_out_user() |> UserAuth.log_out_user()
end end
end end

View File

@ -7,7 +7,7 @@ defmodule MemexWeb.UserSettingsController do
plug :assign_email_and_password_changesets plug :assign_email_and_password_changesets
def edit(conn, _params) do def edit(conn, _params) do
render(conn, "edit.html", page_title: gettext("Settings")) render(conn, "edit.html", page_title: gettext("settings"))
end end
def update(%{assigns: %{current_user: user}} = conn, %{ def update(%{assigns: %{current_user: user}} = conn, %{
@ -28,7 +28,7 @@ defmodule MemexWeb.UserSettingsController do
:info, :info,
dgettext( dgettext(
"prompts", "prompts",
"A link to confirm your email change has been sent to the new address." "a link to confirm your email change has been sent to the new address."
) )
) )
|> redirect(to: Routes.user_settings_path(conn, :edit)) |> redirect(to: Routes.user_settings_path(conn, :edit))
@ -46,7 +46,7 @@ defmodule MemexWeb.UserSettingsController do
case Accounts.update_user_password(user, password, user_params) do case Accounts.update_user_password(user, password, user_params) do
{:ok, user} -> {:ok, user} ->
conn conn
|> put_flash(:info, dgettext("prompts", "Password updated successfully.")) |> put_flash(:info, dgettext("prompts", "password updated successfully."))
|> put_session(:user_return_to, Routes.user_settings_path(conn, :edit)) |> put_session(:user_return_to, Routes.user_settings_path(conn, :edit))
|> UserAuth.log_in_user(user) |> UserAuth.log_in_user(user)
@ -62,7 +62,7 @@ defmodule MemexWeb.UserSettingsController do
case Accounts.update_user_locale(user, locale) do case Accounts.update_user_locale(user, locale) do
{:ok, _user} -> {:ok, _user} ->
conn conn
|> put_flash(:info, dgettext("prompts", "Language updated successfully.")) |> put_flash(:info, dgettext("prompts", "language updated successfully."))
|> redirect(to: Routes.user_settings_path(conn, :edit)) |> redirect(to: Routes.user_settings_path(conn, :edit))
{:error, changeset} -> {:error, changeset} ->
@ -74,14 +74,14 @@ defmodule MemexWeb.UserSettingsController do
case Accounts.update_user_email(user, token) do case Accounts.update_user_email(user, token) do
:ok -> :ok ->
conn conn
|> put_flash(:info, dgettext("prompts", "Email changed successfully.")) |> put_flash(:info, dgettext("prompts", "email changed successfully."))
|> redirect(to: Routes.user_settings_path(conn, :edit)) |> redirect(to: Routes.user_settings_path(conn, :edit))
:error -> :error ->
conn conn
|> put_flash( |> put_flash(
:error, :error,
dgettext("errors", "Email change link is invalid or it has expired.") dgettext("errors", "email change link is invalid or it has expired.")
) )
|> redirect(to: Routes.user_settings_path(conn, :edit)) |> redirect(to: Routes.user_settings_path(conn, :edit))
end end
@ -92,11 +92,11 @@ defmodule MemexWeb.UserSettingsController do
current_user |> Accounts.delete_user!(current_user) current_user |> Accounts.delete_user!(current_user)
conn conn
|> put_flash(:error, dgettext("prompts", "Your account has been deleted")) |> put_flash(:error, dgettext("prompts", "your account has been deleted"))
|> redirect(to: Routes.live_path(conn, HomeLive)) |> redirect(to: Routes.live_path(conn, HomeLive))
else else
conn conn
|> put_flash(:error, dgettext("errors", "Unable to delete user")) |> put_flash(:error, dgettext("errors", "unable to delete user"))
|> redirect(to: Routes.user_settings_path(conn, :edit)) |> redirect(to: Routes.user_settings_path(conn, :edit))
end end
end end

View File

@ -4,8 +4,8 @@ defmodule MemexWeb.ContextLive.FormComponent do
alias Memex.Contexts alias Memex.Contexts
@impl true @impl true
def update(%{context: context} = assigns, socket) do def update(%{context: context, current_user: current_user} = assigns, socket) do
changeset = Contexts.change_context(context) changeset = Contexts.change_context(context, current_user)
{:ok, {:ok,
socket socket
@ -14,39 +14,52 @@ defmodule MemexWeb.ContextLive.FormComponent do
end end
@impl true @impl true
def handle_event("validate", %{"context" => context_params}, socket) do def handle_event(
"validate",
%{"context" => context_params},
%{assigns: %{context: context, current_user: current_user}} = socket
) do
changeset = changeset =
socket.assigns.context context
|> Contexts.change_context(context_params) |> Contexts.change_context(context_params, current_user)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
end end
def handle_event("save", %{"context" => context_params}, socket) do def handle_event("save", %{"context" => context_params}, %{assigns: %{action: action}} = socket) do
save_context(socket, socket.assigns.action, context_params) save_context(socket, action, context_params)
end end
defp save_context(socket, :edit, context_params) do defp save_context(
case Contexts.update_context(socket.assigns.context, context_params) do %{assigns: %{context: context, return_to: return_to, current_user: current_user}} =
{:ok, _context} -> socket,
:edit,
context_params
) do
case Contexts.update_context(context, context_params, current_user) do
{:ok, %{slug: slug}} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, "context updated successfully") |> put_flash(:info, gettext("%{slug} saved", slug: slug))
|> push_navigate(to: socket.assigns.return_to)} |> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
end end
end end
defp save_context(socket, :new, context_params) do defp save_context(
case Contexts.create_context(context_params) do %{assigns: %{return_to: return_to, current_user: current_user}} = socket,
{:ok, _context} -> :new,
context_params
) do
case Contexts.create_context(context_params, current_user) do
{:ok, %{slug: slug}} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, "context created successfully") |> put_flash(:info, gettext("%{slug} created", slug: slug))
|> push_navigate(to: socket.assigns.return_to)} |> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)} {:noreply, assign(socket, changeset: changeset)}

View File

@ -1,6 +1,4 @@
<div> <div class="h-full flex flex-col justify-start items-stretch space-y-4">
<h2><%= @title %></h2>
<.form <.form
:let={f} :let={f}
for={@changeset} for={@changeset}
@ -8,27 +6,42 @@
phx-target={@myself} phx-target={@myself}
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
> >
<%= label(f, :title) %> <%= text_input(f, :slug,
<%= text_input(f, :title) %> class: "input input-primary",
<%= error_tag(f, :title) %> placeholder: gettext("slug")
) %>
<%= error_tag(f, :slug) %>
<%= label(f, :content) %> <%= textarea(f, :content,
<%= textarea(f, :content) %> id: "context-form-content",
class: "input input-primary h-64 min-h-64",
phx_hook: "MaintainAttrs",
phx_update: "ignore",
placeholder: gettext("use [[note-slug]] to link to a note")
) %>
<%= error_tag(f, :content) %> <%= error_tag(f, :content) %>
<%= label(f, :tag) %> <%= text_input(f, :tags_string,
<%= multiple_select(f, :tag, "Option 1": "option1", "Option 2": "option2") %> id: "tags-input",
<%= error_tag(f, :tag) %> class: "input input-primary",
placeholder: gettext("tag1,tag2")
<%= label(f, :visibility) %>
<%= select(f, :visibility, Ecto.Enum.values(Memex.Contexts.Context, :visibility),
prompt: "Choose a value"
) %> ) %>
<%= error_tag(f, :visibility) %> <%= error_tag(f, :tags_string) %>
<div> <div class="flex justify-center items-stretch space-x-4">
<%= submit("Save", phx_disable_with: "Saving...") %> <%= select(f, :visibility, Ecto.Enum.values(Memex.Contexts.Context, :visibility),
class: "grow input input-primary",
prompt: gettext("select privacy")
) %>
<%= submit(dgettext("actions", "save"),
phx_disable_with: gettext("saving..."),
class: "mx-auto btn btn-primary"
) %>
</div> </div>
<%= error_tag(f, :visibility) %>
</.form> </.form>
</div> </div>

View File

@ -1,46 +1,89 @@
defmodule MemexWeb.ContextLive.Index do defmodule MemexWeb.ContextLive.Index do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.{Accounts.User, Contexts, Contexts.Context}
alias Memex.Contexts
alias Memex.Contexts.Context
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(search: search) |> display_contexts()}
end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, assign(socket, :contexts, list_contexts())} {:ok, socket |> assign(search: nil) |> display_contexts()}
end end
@impl true @impl true
def handle_params(params, _url, socket) do def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)} {:noreply, apply_action(socket, live_action, params)}
end end
defp apply_action(socket, :edit, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"slug" => slug}) do
%{slug: slug} = context = Contexts.get_context_by_slug(slug, current_user)
socket socket
|> assign(:page_title, "edit context") |> assign(page_title: gettext("edit %{slug}", slug: slug))
|> assign(:context, Contexts.get_context!(id)) |> assign(context: context)
end end
defp apply_action(socket, :new, _params) do defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
socket socket
|> assign(:page_title, "new context") |> assign(page_title: gettext("new context"))
|> assign(:context, %Context{}) |> assign(context: %Context{visibility: :private, user_id: current_user_id})
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket
|> assign(:page_title, "listing contexts") |> assign(page_title: gettext("contexts"))
|> assign(:context, nil) |> assign(search: nil)
|> assign(context: nil)
|> display_contexts()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(page_title: gettext("contexts"))
|> assign(search: search)
|> assign(context: nil)
|> display_contexts()
end end
@impl true @impl true
def handle_event("delete", %{"id" => id}, socket) do def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
context = Contexts.get_context!(id) context = Contexts.get_context!(id, current_user)
{:ok, _} = Contexts.delete_context(context) {:ok, %{slug: slug}} = Contexts.delete_context(context, current_user)
{:noreply, assign(socket, :contexts, list_contexts())} socket =
socket
|> assign(contexts: Contexts.list_contexts(current_user))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
{:noreply, socket}
end end
defp list_contexts do @impl true
Contexts.list_contexts() def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.context_index_path(Endpoint, :index))}
end end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply,
socket |> push_patch(to: Routes.context_index_path(Endpoint, :search, search_term))}
end
defp display_contexts(%{assigns: %{current_user: current_user, search: search}} = socket)
when not (current_user |> is_nil()) do
socket |> assign(contexts: Contexts.list_contexts(search, current_user))
end
defp display_contexts(%{assigns: %{search: search}} = socket) do
socket |> assign(contexts: Contexts.list_public_contexts(search))
end
@spec is_owner_or_admin?(Context.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner_or_admin?(_context, %{role: :admin}), do: true
defp is_owner_or_admin?(_context, _other_user), do: false
@spec is_owner?(Context.t(), User.t()) :: boolean()
defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner?(_context, _other_user), do: false
end end

View File

@ -1,66 +1,73 @@
<h1>listing contexts</h1> <div class="mx-auto flex flex-col justify-center items-start space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= gettext("contexts") %>
</h1>
<%= if @live_action in [:new, :edit] do %> <.form
<.modal return_to={Routes.context_index_path(@socket, :index)}> :let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="self-stretch flex flex-col items-stretch"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("search")
) %>
</.form>
<%= if @contexts |> Enum.empty?() do %>
<h1 class="self-center text-primary-500">
<%= gettext("no contexts found") %>
</h1>
<% else %>
<.live_component <.live_component
module={MemexWeb.ContextLive.FormComponent} module={MemexWeb.Components.ContextsTableComponent}
id={@context.id || :new} id="contexts-index-table"
title={@page_title} current_user={@current_user}
action={@live_action} contexts={@contexts}
context={@context} >
return_to={Routes.context_index_path(@socket, :index)} <:actions :let={context}>
/> <.link
</.modal> :if={is_owner?(context, @current_user)}
<% end %> patch={Routes.context_index_path(@socket, :edit, context.slug)}
data-qa={"context-edit-#{context.id}"}
>
<%= dgettext("actions", "edit") %>
</.link>
<.link
:if={is_owner_or_admin?(context, @current_user)}
href="#"
phx-click="delete"
phx-value-id={context.id}
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-context-#{context.id}"}
>
<%= dgettext("actions", "delete") %>
</.link>
</:actions>
</.live_component>
<% end %>
<table> <.link
<thead> :if={@current_user}
<tr> patch={Routes.context_index_path(@socket, :new)}
<th>Title</th> class="self-end btn btn-primary"
<th>Content</th> >
<th>Tag</th>
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody id="contexts">
<%= for context <- @contexts do %>
<tr id={"context-#{context.id}"}>
<td><%= context.title %></td>
<td><%= context.content %></td>
<td><%= context.tag %></td>
<td><%= context.visibility %></td>
<td>
<span>
<.link navigate={Routes.context_show_path(@socket, :show, context)}>
<%= dgettext("actions", "show") %>
</.link>
</span>
<span>
<.link patch={Routes.context_index_path(@socket, :edit, context)}>
<%= dgettext("actions", "edit") %>
</.link>
</span>
<span>
<.link
href="#"
phx-click="delete"
phx-value-id={context.id}
data-confirm={dgettext("prompts", "are you sure?")}
>
<%= dgettext("actions", "delete") %>
</.link>
</span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span>
<.link patch={Routes.context_index_path(@socket, :new)}>
<%= dgettext("actions", "new context") %> <%= dgettext("actions", "new context") %>
</.link> </.link>
</span> </div>
<.modal :if={@live_action in [:new, :edit]} return_to={Routes.context_index_path(@socket, :index)}>
<.live_component
module={MemexWeb.ContextLive.FormComponent}
id={@context.id || :new}
current_user={@current_user}
title={@page_title}
action={@live_action}
context={@context}
return_to={Routes.context_index_path(@socket, :index)}
/>
</.modal>

View File

@ -1,7 +1,7 @@
defmodule MemexWeb.ContextLive.Show do defmodule MemexWeb.ContextLive.Show do
use MemexWeb, :live_view use MemexWeb, :live_view
import MemexWeb.Components.ContextContent
alias Memex.Contexts alias Memex.{Accounts.User, Contexts, Contexts.Context}
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
@ -9,13 +9,50 @@ defmodule MemexWeb.ContextLive.Show do
end end
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(
{:noreply, %{"slug" => slug},
socket _params,
|> assign(:page_title, page_title(socket.assigns.live_action)) %{assigns: %{live_action: live_action, current_user: current_user}} = socket
|> assign(:context, Contexts.get_context!(id))} ) do
context =
case Contexts.get_context_by_slug(slug, current_user) do
nil -> raise MemexWeb.NotFoundError, gettext("%{slug} could not be found", slug: slug)
context -> context
end
socket =
socket
|> assign(:page_title, page_title(live_action, context))
|> assign(:context, context)
{:noreply, socket}
end end
defp page_title(:show), do: "show context" @impl true
defp page_title(:edit), do: "edit context" def handle_event(
"delete",
_params,
%{assigns: %{context: context, current_user: current_user}} = socket
) do
{:ok, %{slug: slug}} = Contexts.delete_context(context, current_user)
socket =
socket
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|> push_navigate(to: Routes.context_index_path(Endpoint, :index))
{:noreply, socket}
end
defp page_title(:show, %{slug: slug}), do: slug
defp page_title(:edit, %{slug: slug}), do: gettext("edit %{slug}", slug: slug)
@spec is_owner_or_admin?(Context.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner_or_admin?(_context, %{role: :admin}), do: true
defp is_owner_or_admin?(_context, _other_user), do: false
@spec is_owner?(Context.t(), User.t()) :: boolean()
defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner?(_context, _other_user), do: false
end end

View File

@ -1,48 +1,59 @@
<h1>show context</h1> <div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @context.slug %>
</h1>
<%= if @live_action in [:edit] do %> <div class="flex flex-wrap space-x-1">
<.modal return_to={Routes.context_show_path(@socket, :show, @context)}> <.link
<.live_component :for={tag <- @context.tags}
module={MemexWeb.ContextLive.FormComponent} navigate={Routes.context_index_path(Endpoint, :search, tag)}
id={@context.id} class="link"
title={@page_title} >
action={@live_action} <%= tag %>
context={@context} </.link>
return_to={Routes.context_show_path(@socket, :show, @context)} </div>
/>
</.modal>
<% end %>
<ul> <.context_content context={@context} />
<li>
<strong>Title:</strong>
<%= @context.title %>
</li>
<li> <p class="self-end">
<strong>Content:</strong> <%= gettext("Visibility: %{visibility}", visibility: @context.visibility) %>
<%= @context.content %> </p>
</li>
<li> <div class="self-end flex space-x-4">
<strong>Tag:</strong> <.link class="btn btn-primary" navigate={Routes.context_index_path(@socket, :index)}>
<%= @context.tag %> <%= dgettext("actions", "back") %>
</li> </.link>
<.link
:if={is_owner?(@context, @current_user)}
class="btn btn-primary"
patch={Routes.context_show_path(@socket, :edit, @context.slug)}
>
<%= dgettext("actions", "edit") %>
</.link>
<button
:if={is_owner_or_admin?(@context, @current_user)}
type="button"
class="btn btn-primary"
phx-click="delete"
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-context-#{@context.id}"}
>
<%= dgettext("actions", "delete") %>
</button>
</div>
</div>
<li> <.modal
<strong>Visibility:</strong> :if={@live_action == :edit}
<%= @context.visibility %> return_to={Routes.context_show_path(@socket, :show, @context.slug)}
</li> >
</ul> <.live_component
module={MemexWeb.ContextLive.FormComponent}
<span> id={@context.id}
<.link patch={Routes.context_show_path(@socket, :edit, @context)} class="button"> current_user={@current_user}
<%= dgettext("actions", "edit") %> title={@page_title}
</.link> action={@live_action}
</span> context={@context}
| return_to={Routes.context_show_path(@socket, :show, @context.slug)}
<span> />
<.link navigate={Routes.context_index_path(@socket, :index)}> </.modal>
<%= dgettext("actions", "Back") %>
</.link>
</span>

View File

@ -0,0 +1,12 @@
defmodule MemexWeb.FaqLive do
@moduledoc """
Liveview for the faq page
"""
use MemexWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(page_title: gettext("faq"))}
end
end

View File

@ -0,0 +1,145 @@
<div class="mx-auto flex flex-col justify-center items-stretch space-y-8 text-center max-w-3xl">
<h1 class="title text-primary-400 text-2xl">
<%= gettext("faq") %>
</h1>
<hr class="hr" />
<ul class="flex flex-col justify-center items-stretch space-y-8">
<li class="flex flex-col justify-center items-stretch space-y-2">
<b class="whitespace-nowrap">
<%= gettext("what is this?") %>
</b>
<p>
<%= gettext(
"this is a memex, used to document not just your notes, but also your perspectives and processes."
) %>
</p>
<p>
<%= gettext("some things that this memex is very loosely inspired by:") %>
</p>
<ul class="list-disc flex flex-col justify-center items-center space-y-2">
<li>
<.link
href="https://en.wikipedia.org/wiki/Memex"
class="flex flex-row justify-center items-center space-x-2 link"
target="_blank"
rel="noopener noreferrer"
>
<%= gettext("memex") %>
</.link>
</li>
<li>
<.link
href="https://en.wikipedia.org/wiki/Zettelkasten"
class="flex flex-row justify-center items-center space-x-2 link"
target="_blank"
rel="noopener noreferrer"
>
<%= gettext("zettelkasten") %>
</.link>
</li>
<li>
<.link
href="https://en.wikipedia.org/wiki/Org-mode"
class="flex flex-row justify-center items-center space-x-2 link"
target="_blank"
rel="noopener noreferrer"
>
<%= gettext("org-mode") %>
</.link>
</li>
</ul>
</li>
<li class="flex flex-col justify-center items-stretch space-y-2">
<b class="whitespace-nowrap">
<%= gettext("why split up into notes, contexts and pipelines?") %>
</b>
<p>
<%= gettext(
"i really admired the idea of a zettelkasten, especially with org-mode backlinks, however I felt like my notes would immediately become too messy by just putting everything into a single hierarchy."
) %>
</p>
<p>
<%= gettext(
"i wanted to separate between a personal dictionary of concepts and then my thought processes that are built off of my experiences and life lessons. these are notes, and contexts, respectively."
) %>
</p>
<p>
<%= gettext(
"finally, i wanted to externalize the processes for common situations that use these thought processes at discrete steps. these are pipelines!"
) %>
</p>
</li>
<li class="flex flex-col justify-center items-stretch space-y-2">
<b class="whitespace-nowrap">
<%= gettext("what should my notes be like?") %>
</b>
<p>
<%= gettext(
"in my opinion, notes should be written by any of the discrete objects or concepts that are meaningful to you in your life."
) %>
</p>
<p>
<%= gettext(
"spoons? probably not. a particular brand of spoons that you really like? why not :)"
) %>
</p>
</li>
<li class="flex flex-col justify-center items-stretch space-y-2">
<b class="whitespace-nowrap">
<%= gettext("what should my contexts be like?") %>
</b>
<p>
<%= gettext("in my opinion, contexts should be like single-topic blog posts.") %>
</p>
<p>
<%= gettext(
"for instance, a good context could be what makes some physical designs spark joy for you, and in that context you could backlink to the spoon note as an example of how it fits nicely into your hand."
) %>
</p>
</li>
<li class="flex flex-col justify-center items-stretch space-y-2">
<b class="whitespace-nowrap">
<%= gettext("what should my pipelines be like?") %>
</b>
<p>
<%= gettext(
"in my opinion, pipelines should be pretty lightweight, and just backlink to contexts to provide most of the heavy lifting."
) %>
</p>
<p>
<%= gettext(
"for instance, a pipeline for buying an object could have a step where you consider how much it sparks joy, and it could backlink to the physical designs context, maybe with some notes about how it applies in this case."
) %>
</p>
</li>
<li class="flex flex-col justify-center items-stretch space-y-2">
<b class="whitespace-nowrap">
<%= gettext("how many people should i invite?") %>
</b>
<p>
<%= gettext(
"while memEx fully supports multiple users, each memEx instance should be treated as a single cohesive and collaborative document."
) %>
</p>
<p>
<%= gettext(
"note, context and pipeline slugs must be unique, and you are free to backlink to notes not written by you."
) %>
</p>
<p>
<%= gettext(
"so, i'd recommend inviting anyone you'd like to work on your collective memEx. however, when in doubt, hopefully setting up a new instance is easy enough. if it isn't, then feel free to let me know :)"
) %>
</p>
</li>
</ul>
</div>

View File

@ -5,41 +5,13 @@ defmodule MemexWeb.HomeLive do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.Accounts alias Memex.Accounts
alias MemexWeb.{Endpoint, FaqLive}
@version Mix.Project.config()[:version]
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
admins = Accounts.list_users_by_role(:admin) admins = Accounts.list_users_by_role(:admin)
{:ok, socket |> assign(page_title: gettext("Home"), query: "", results: %{}, admins: admins)} {:ok, socket |> assign(page_title: gettext("home"), admins: admins, version: @version)}
end
@impl true
def handle_event("suggest", %{"q" => query}, socket) do
{:noreply, socket |> assign(results: search(query), query: query)}
end
@impl true
def handle_event("search", %{"q" => query}, socket) do
case search(query) do
%{^query => vsn} ->
{:noreply, socket |> redirect(external: "https://hexdocs.pm/#{query}/#{vsn}")}
_ ->
{:noreply,
socket
|> put_flash(:error, "No dependencies found matching \"#{query}\"")
|> assign(results: %{}, query: query)}
end
end
defp search(query) do
if not MemexWeb.Endpoint.config(:code_reloader) do
raise "action disabled when not in development"
end
for {app, desc, vsn} <- Application.started_applications(),
app = to_string(app),
String.starts_with?(app, query) and not List.starts_with?(desc, ~c"ERTS"),
into: %{},
do: {app, vsn}
end end
end end

View File

@ -1,13 +1,12 @@
<div class="flex flex-col justify-center items-center text-center space-y-4"> <div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="title text-primary-400 text-2xl"> <h1 class="title text-primary-400 text-2xl text-center">
<%= gettext("memex") %> <%= gettext("memEx") %>
</h1> </h1>
<hr class="hr" /> <hr class="hr" />
<ul class="flex flex-col space-y-4 text-center"> <ul class="flex flex-col space-y-4 text-center">
<li class="flex flex-col justify-center items-center <li class="flex flex-col justify-center items-center space-y-2">
space-y-2">
<b class="whitespace-nowrap"> <b class="whitespace-nowrap">
<%= gettext("notes:") %> <%= gettext("notes:") %>
</b> </b>
@ -16,8 +15,7 @@
</p> </p>
</li> </li>
<li class="flex flex-col justify-center items-center <li class="flex flex-col justify-center items-center space-y-2">
space-y-2">
<b class="whitespace-nowrap"> <b class="whitespace-nowrap">
<%= gettext("contexts:") %> <%= gettext("contexts:") %>
</b> </b>
@ -26,8 +24,7 @@
</p> </p>
</li> </li>
<li class="flex flex-col justify-center items-center <li class="flex flex-col justify-center items-center space-y-2">
space-y-2">
<b class="whitespace-nowrap"> <b class="whitespace-nowrap">
<%= gettext("pipelines:") %> <%= gettext("pipelines:") %>
</b> </b>
@ -35,6 +32,15 @@
<%= gettext("document your processes, attaching contexts to each step") %> <%= gettext("document your processes, attaching contexts to each step") %>
</p> </p>
</li> </li>
<li class="flex flex-col justify-center items-center space-y-2">
<.link
navigate={Routes.live_path(Endpoint, FaqLive)}
class="link title text-primary-400 text-lg"
>
<%= gettext("read more on how to use memEx") %>
</.link>
</li>
</ul> </ul>
<hr class="hr" /> <hr class="hr" />
@ -44,8 +50,7 @@
<%= gettext("features") %> <%= gettext("features") %>
</h2> </h2>
<li class="flex flex-col justify-center items-center <li class="flex flex-col justify-center items-center space-y-2">
space-y-2">
<b class="whitespace-nowrap"> <b class="whitespace-nowrap">
<%= gettext("multi-user:") %> <%= gettext("multi-user:") %>
</b> </b>
@ -54,8 +59,7 @@
</p> </p>
</li> </li>
<li class="flex flex-col justify-center items-center <li class="flex flex-col justify-center items-center space-y-2">
space-y-2">
<b class="whitespace-nowrap"> <b class="whitespace-nowrap">
<%= gettext("privacy:") %> <%= gettext("privacy:") %>
</b> </b>
@ -64,8 +68,7 @@
</p> </p>
</li> </li>
<li class="flex flex-col justify-center items-center <li class="flex flex-col justify-center items-center space-y-2">
space-y-2">
<b class="whitespace-nowrap"> <b class="whitespace-nowrap">
<%= gettext("convenient:") %> <%= gettext("convenient:") %>
</b> </b>
@ -88,19 +91,14 @@
</b> </b>
<p> <p>
<%= if @admins |> Enum.empty?() do %> <%= if @admins |> Enum.empty?() do %>
<.link <.link href={Routes.user_registration_path(Endpoint, :new)} class="link">
href={Routes.user_registration_path(MemexWeb.Endpoint, :new)} <%= dgettext("prompts", "register to setup memEx") %>
class="hover:underline"
>
<%= dgettext("prompts", "register to setup %{name}", name: "memex") %>
</.link> </.link>
<% else %> <% else %>
<div class="flex flex-wrap justify-center space-x-2"> <div class="flex flex-wrap justify-center space-x-2">
<%= for admin <- @admins do %> <a :for={%{email: email} <- @admins} class="link" href={"mailto:#{email}"}>
<a class="hover:underline" href={"mailto:#{admin.email}"}> <%= email %>
<%= admin.email %> </a>
</a>
<% end %>
</div> </div>
<% end %> <% end %>
</p> </p>
@ -109,8 +107,7 @@
<li class="flex flex-row justify-center space-x-2"> <li class="flex flex-row justify-center space-x-2">
<b><%= gettext("registration:") %></b> <b><%= gettext("registration:") %></b>
<p> <p>
<%= Application.get_env(:memex, MemexWeb.Endpoint)[:registration] <%= case Application.get_env(:memex, Memex.Accounts)[:registration] do
|> case do
"public" -> gettext("public signups") "public" -> gettext("public signups")
_ -> gettext("invite only") _ -> gettext("invite only")
end %> end %>
@ -120,12 +117,12 @@
<li class="flex flex-row justify-center items-center space-x-2"> <li class="flex flex-row justify-center items-center space-x-2">
<b><%= gettext("version:") %></b> <b><%= gettext("version:") %></b>
<.link <.link
href="https://gitea.bubbletea.dev/shibao/memex/src/branch/stable/CHANGELOG.md" href="https://gitea.bubbletea.dev/shibao/memEx/src/branch/stable/changelog.md"
class="flex flex-row justify-center items-center space-x-2 hover:underline" class="flex flex-row justify-center items-center space-x-2 link"
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
> >
<p>0.1.0</p> <p><%= @version %></p>
<i class="fas fa-md fa-info-circle"></i> <i class="fas fa-md fa-info-circle"></i>
</.link> </.link>
</li> </li>
@ -140,8 +137,8 @@
<li class="flex flex-col justify-center space-x-2"> <li class="flex flex-col justify-center space-x-2">
<.link <.link
href="https://gitea.bubbletea.dev/shibao/memex" href="https://gitea.bubbletea.dev/shibao/memEx"
class="flex flex-row justify-center items-center space-x-2 hover:underline" class="flex flex-row justify-center items-center space-x-2 link"
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
> >
@ -151,8 +148,8 @@
</li> </li>
<li class="flex flex-col justify-center space-x-2"> <li class="flex flex-col justify-center space-x-2">
<.link <.link
href="https://weblate.bubbletea.dev/engage/memex" href="https://weblate.bubbletea.dev/engage/memEx"
class="flex flex-row justify-center items-center space-x-2 hover:underline" class="flex flex-row justify-center items-center space-x-2 link"
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
> >
@ -162,8 +159,8 @@
</li> </li>
<li class="flex flex-col justify-center space-x-2"> <li class="flex flex-col justify-center space-x-2">
<.link <.link
href="https://gitea.bubbletea.dev/shibao/memex/issues/new" href="https://gitea.bubbletea.dev/shibao/memEx/issues/new"
class="flex flex-row justify-center items-center space-x-2 hover:underline" class="flex flex-row justify-center items-center space-x-2 link"
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
> >

View File

@ -1,11 +1,11 @@
defmodule MemexWeb.InviteLive.FormComponent do defmodule MemexWeb.InviteLive.FormComponent do
@moduledoc """ @moduledoc """
Livecomponent that can update or create an Memex.Invites.Invite Livecomponent that can update or create an Memex.Accounts.Invite
""" """
use MemexWeb, :live_component use MemexWeb, :live_component
alias Ecto.Changeset alias Ecto.Changeset
alias Memex.{Accounts.User, Invites, Invites.Invite} alias Memex.Accounts.{Invite, Invites, User}
alias Phoenix.LiveView.Socket alias Phoenix.LiveView.Socket
@impl true @impl true
@ -13,23 +13,44 @@ defmodule MemexWeb.InviteLive.FormComponent do
%{:invite => Invite.t(), :current_user => User.t(), optional(any) => any}, %{:invite => Invite.t(), :current_user => User.t(), optional(any) => any},
Socket.t() Socket.t()
) :: {:ok, Socket.t()} ) :: {:ok, Socket.t()}
def update(%{invite: invite} = assigns, socket) do def update(%{invite: _invite} = assigns, socket) do
{:ok, socket |> assign(assigns) |> assign(:changeset, Invites.change_invite(invite))} {:ok, socket |> assign(assigns) |> assign_changeset(%{})}
end end
@impl true @impl true
def handle_event( def handle_event("validate", %{"invite" => invite_params}, socket) do
"validate", {:noreply, socket |> assign_changeset(invite_params)}
%{"invite" => invite_params},
%{assigns: %{invite: invite}} = socket
) do
{:noreply, socket |> assign(:changeset, invite |> Invites.change_invite(invite_params))}
end end
def handle_event("save", %{"invite" => invite_params}, %{assigns: %{action: action}} = socket) do def handle_event("save", %{"invite" => invite_params}, %{assigns: %{action: action}} = socket) do
save_invite(socket, action, invite_params) save_invite(socket, action, invite_params)
end end
defp assign_changeset(
%{assigns: %{action: action, current_user: user, invite: invite}} = socket,
invite_params
) do
changeset_action =
case action do
:new -> :insert
:edit -> :update
end
changeset =
case action do
:new -> Invite.create_changeset(user, "example_token", invite_params)
:edit -> invite |> Invite.update_changeset(invite_params)
end
changeset =
case changeset |> Changeset.apply_action(changeset_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
socket |> assign(:changeset, changeset)
end
defp save_invite( defp save_invite(
%{assigns: %{current_user: current_user, invite: invite, return_to: return_to}} = socket, %{assigns: %{current_user: current_user, invite: invite, return_to: return_to}} = socket,
:edit, :edit,
@ -38,9 +59,7 @@ defmodule MemexWeb.InviteLive.FormComponent do
socket = socket =
case invite |> Invites.update_invite(invite_params, current_user) do case invite |> Invites.update_invite(invite_params, current_user) do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = prompt = dgettext("prompts", "%{name} updated successfully", name: invite_name)
dgettext("prompts", "%{invite_name} updated successfully", invite_name: invite_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
@ -58,9 +77,7 @@ defmodule MemexWeb.InviteLive.FormComponent do
socket = socket =
case current_user |> Invites.create_invite(invite_params) do case current_user |> Invites.create_invite(invite_params) do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = prompt = dgettext("prompts", "%{name} created successfully", name: invite_name)
dgettext("prompts", "%{invite_name} created successfully", invite_name: invite_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->

View File

@ -11,11 +11,12 @@
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
> >
<%= if @changeset.action && not @changeset.valid? do %> <div
<div class="invalid-feedback col-span-3 text-center"> :if={@changeset.action && not @changeset.valid?()}
<%= changeset_errors(@changeset) %> class="invalid-feedback col-span-3 text-center"
</div> >
<% end %> <%= changeset_errors(@changeset) %>
</div>
<%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-400") %> <%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-400") %>
<%= text_input(f, :name, class: "input input-primary col-span-2") %> <%= text_input(f, :name, class: "input input-primary col-span-2") %>

View File

@ -1,11 +1,12 @@
defmodule MemexWeb.InviteLive.Index do defmodule MemexWeb.InviteLive.Index do
@moduledoc """ @moduledoc """
Liveview to show a Memex.Invites.Invite index Liveview to show a Memex.Accounts.Invite index
""" """
use MemexWeb, :live_view use MemexWeb, :live_view
import MemexWeb.Components.{InviteCard, UserCard} import MemexWeb.Components.{InviteCard, UserCard}
alias Memex.{Accounts, Invites, Invites.Invite} alias Memex.Accounts
alias Memex.Accounts.{Invite, Invites}
alias MemexWeb.{Endpoint, HomeLive} alias MemexWeb.{Endpoint, HomeLive}
alias Phoenix.LiveView.JS alias Phoenix.LiveView.JS
@ -15,9 +16,9 @@ defmodule MemexWeb.InviteLive.Index do
if current_user |> Map.get(:role) == :admin do if current_user |> Map.get(:role) == :admin do
socket |> display_invites() socket |> display_invites()
else else
prompt = dgettext("errors", "You are not authorized to view this page") prompt = dgettext("errors", "you are not authorized to view this page")
return_to = Routes.live_path(Endpoint, HomeLive) return_to = Routes.live_path(Endpoint, HomeLive)
socket |> put_flash(:error, prompt) |> push_navigate(to: return_to) socket |> put_flash(:error, prompt) |> push_redirect(to: return_to)
end end
{:ok, socket} {:ok, socket}
@ -61,7 +62,7 @@ defmodule MemexWeb.InviteLive.Index do
) do ) do
socket = socket =
Invites.get_invite!(id, current_user) Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{"uses_left" => nil}, current_user) |> Invites.update_invite(%{uses_left: nil}, current_user)
|> case do |> case do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = prompt =
@ -83,7 +84,7 @@ defmodule MemexWeb.InviteLive.Index do
) do ) do
socket = socket =
Invites.get_invite!(id, current_user) Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{"uses_left" => nil, "disabled_at" => nil}, current_user) |> Invites.update_invite(%{uses_left: nil, disabled_at: nil}, current_user)
|> case do |> case do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = prompt =
@ -107,7 +108,7 @@ defmodule MemexWeb.InviteLive.Index do
socket = socket =
Invites.get_invite!(id, current_user) Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{"uses_left" => 0, "disabled_at" => now}, current_user) |> Invites.update_invite(%{uses_left: 0, disabled_at: now}, current_user)
|> case do |> case do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = prompt =
@ -123,8 +124,8 @@ defmodule MemexWeb.InviteLive.Index do
end end
@impl true @impl true
def handle_event("copy_to_clipboard", _, socket) do def handle_event("copy_to_clipboard", _params, socket) do
prompt = dgettext("prompts", "Copied to clipboard") prompt = dgettext("prompts", "copied to clipboard")
{:noreply, socket |> put_flash(:info, prompt)} {:noreply, socket |> put_flash(:info, prompt)}
end end

View File

@ -18,69 +18,65 @@
<% end %> <% end %>
<div class="w-full flex flex-row flex-wrap justify-center items-center"> <div class="w-full flex flex-row flex-wrap justify-center items-center">
<%= for invite <- @invites do %> <.invite_card :for={invite <- @invites} invite={invite} current_user={@current_user}>
<.invite_card invite={invite}> <:code_actions>
<:code_actions> <form phx-submit="copy_to_clipboard">
<form phx-submit="copy_to_clipboard"> <button
<button type="submit"
type="submit" class="mx-2 my-1 btn btn-primary"
class="mx-2 my-1 btn btn-primary" phx-click={JS.dispatch("memex:clipcopy", to: "#code-#{invite.id}")}
phx-click={JS.dispatch("memex:clipcopy", to: "#code-#{invite.id}")}
>
<%= dgettext("actions", "Copy to clipboard") %>
</button>
</form>
</:code_actions>
<.link
patch={Routes.invite_index_path(Endpoint, :edit, invite)}
class="text-primary-400 link"
data-qa={"edit-#{invite.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
href="#"
class="text-primary-400 link"
phx-click="delete_invite"
phx-value-id={invite.id}
data-confirm={
dgettext("prompts", "are you sure you want to delete the invite for %{invite_name}?",
invite_name: invite.name
)
}
data-qa={"delete-#{invite.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
<%= if invite.disabled_at |> is_nil() do %>
<a href="#" class="btn btn-primary" phx-click="disable_invite" phx-value-id={invite.id}>
<%= gettext("disable") %>
</a>
<% else %>
<a href="#" class="btn btn-primary" phx-click="enable_invite" phx-value-id={invite.id}>
<%= gettext("enable") %>
</a>
<% end %>
<%= if invite.disabled_at |> is_nil() and not (invite.uses_left |> is_nil()) do %>
<a
href="#"
class="btn btn-primary"
phx-click="set_unlimited"
phx-value-id={invite.id}
data-confirm={
dgettext("prompts", "are you sure you want to make %{invite_name} unlimited?",
invite_name: invite.name
)
}
> >
<%= gettext("set unlimited") %> <%= dgettext("actions", "Copy to clipboard") %>
</a> </button>
<% end %> </form>
</.invite_card> </:code_actions>
<% end %> <.link
patch={Routes.invite_index_path(Endpoint, :edit, invite)}
class="text-primary-400 link"
data-qa={"edit-#{invite.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
href="#"
class="text-primary-400 link"
phx-click="delete_invite"
phx-value-id={invite.id}
data-confirm={
dgettext("prompts", "are you sure you want to delete the invite for %{invite_name}?",
invite_name: invite.name
)
}
data-qa={"delete-#{invite.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
<a
href="#"
class="btn btn-primary"
phx-click={if invite.disabled_at, do: "enable_invite", else: "disable_invite"}
phx-value-id={invite.id}
>
<%= if invite.disabled_at, do: gettext("enable"), else: gettext("disable") %>
</a>
<a
:if={invite.disabled_at |> is_nil() and not (invite.uses_left |> is_nil())}
href="#"
class="btn btn-primary"
phx-click="set_unlimited"
phx-value-id={invite.id}
data-confirm={
dgettext("prompts", "are you sure you want to make %{invite_name} unlimited?",
invite_name: invite.name
)
}
>
<%= gettext("set unlimited") %>
</a>
</.invite_card>
</div> </div>
<%= unless @admins |> Enum.empty?() do %> <%= unless @admins |> Enum.empty?() do %>
@ -91,25 +87,23 @@
</h1> </h1>
<div class="w-full flex flex-row flex-wrap justify-center items-center"> <div class="w-full flex flex-row flex-wrap justify-center items-center">
<%= for admin <- @admins do %> <.user_card :for={admin <- @admins} user={admin}>
<.user_card user={admin}> <.link
<.link href="#"
href="#" class="text-primary-400 link"
class="text-primary-400 link" phx-click="delete_user"
phx-click="delete_user" phx-value-id={admin.id}
phx-value-id={admin.id} data-confirm={
data-confirm={ dgettext(
dgettext( "prompts",
"prompts", "are you sure you want to delete %{email}? This action is permanent!",
"are you sure you want to delete %{email}? This action is permanent!", email: admin.email
email: admin.email )
) }
} >
> <i class="fa-fw fa-lg fas fa-trash"></i>
<i class="fa-fw fa-lg fas fa-trash"></i> </.link>
</.link> </.user_card>
</.user_card>
<% end %>
</div> </div>
<% end %> <% end %>
@ -121,39 +115,35 @@
</h1> </h1>
<div class="w-full flex flex-row flex-wrap justify-center items-center"> <div class="w-full flex flex-row flex-wrap justify-center items-center">
<%= for user <- @users do %> <.user_card :for={user <- @users} user={user}>
<.user_card user={user}> <.link
<.link href="#"
href="#" class="text-primary-400 link"
class="text-primary-400 link" phx-click="delete_user"
phx-click="delete_user" phx-value-id={user.id}
phx-value-id={user.id} data-confirm={
data-confirm={ dgettext(
dgettext( "prompts",
"prompts", "are you sure you want to delete %{email}? This action is permanent!",
"are you sure you want to delete %{email}? This action is permanent!", email: user.email
email: user.email )
) }
} >
> <i class="fa-fw fa-lg fas fa-trash"></i>
<i class="fa-fw fa-lg fas fa-trash"></i> </.link>
</.link> </.user_card>
</.user_card>
<% end %>
</div> </div>
<% end %> <% end %>
</div> </div>
<%= if @live_action in [:new, :edit] do %> <.modal :if={@live_action in [:new, :edit]} return_to={Routes.invite_index_path(Endpoint, :index)}>
<.modal return_to={Routes.invite_index_path(Endpoint, :index)}> <.live_component
<.live_component module={MemexWeb.InviteLive.FormComponent}
module={MemexWeb.InviteLive.FormComponent} id={@invite.id || :new}
id={@invite.id || :new} title={@page_title}
title={@page_title} action={@live_action}
action={@live_action} invite={@invite}
invite={@invite} return_to={Routes.invite_index_path(Endpoint, :index)}
return_to={Routes.invite_index_path(Endpoint, :index)} current_user={@current_user}
current_user={@current_user} />
/> </.modal>
</.modal>
<% end %>

View File

@ -1,6 +1,6 @@
defmodule MemexWeb.LiveHelpers do defmodule MemexWeb.LiveHelpers do
@moduledoc """ @moduledoc """
Contains resuable methods for all liveviews Contains common helper functions for liveviews
""" """
import Phoenix.Component import Phoenix.Component
@ -31,8 +31,8 @@ defmodule MemexWeb.LiveHelpers do
patch={@return_to} patch={@return_to}
id="modal-bg" id="modal-bg"
class="fade-in fixed z-10 left-0 top-0 class="fade-in fixed z-10 left-0 top-0
w-screen h-screen overflow-hidden w-full h-full overflow-hidden
p-8 flex flex-col justify-center items-center cursor-auto" p-8 flex flex-col justify-center items-center cursor-auto"
style="background-color: rgba(0,0,0,0.4);" style="background-color: rgba(0,0,0,0.4);"
phx-remove={hide_modal()} phx-remove={hide_modal()}
> >
@ -48,22 +48,22 @@ defmodule MemexWeb.LiveHelpers do
<div <div
id="modal-content" id="modal-content"
class="fade-in-scale max-w-3xl max-h-3xl relative w-full class="fade-in-scale max-w-3xl max-h-3xl relative w-full
pointer-events-auto overflow-hidden pointer-events-auto overflow-hidden
px-8 py-4 sm:py-8 flex flex-col justify-start items-stretch px-8 py-4 sm:py-8 flex flex-col justify-start items-stretch
bg-primary-800 text-primary-400 border-primary-900 border-2 rounded-lg" bg-primary-800 text-primary-400 border-primary-900 border-2 rounded-lg"
> >
<.link <.link
patch={@return_to} patch={@return_to}
id="close" id="close"
class="absolute top-8 right-10 class="absolute top-8 right-10
text-gray-500 hover:text-gray-800 text-gray-500 hover:text-gray-800
transition-all duration-500 ease-in-out" transition-all duration-500 ease-in-out"
phx-remove={hide_modal()} phx-remove={hide_modal()}
> >
<i class="fa-fw fa-lg fas fa-times"></i> <i class="fa-fw fa-lg fas fa-times"></i>
</.link> </.link>
<div class="overflow-x-hidden overflow-y-visible p-8 flex flex-col space-y-4 justify-start items-stretch"> <div class="overflow-x-hidden overflow-y-auto w-full p-8 flex flex-col space-y-4 justify-start items-stretch">
<%= render_slot(@inner_block) %> <%= render_slot(@inner_block) %>
</div> </div>
</div> </div>
@ -71,10 +71,57 @@ defmodule MemexWeb.LiveHelpers do
""" """
end end
def hide_modal(js \\ %JS{}) do defp hide_modal(js \\ %JS{}) do
js js
|> JS.hide(to: "#modal", transition: "fade-out") |> JS.hide(to: "#modal", transition: "fade-out")
|> JS.hide(to: "#modal-bg", transition: "fade-out") |> JS.hide(to: "#modal-bg", transition: "fade-out")
|> JS.hide(to: "#modal-content", transition: "fade-out-scale") |> JS.hide(to: "#modal-content", transition: "fade-out-scale")
end end
@doc """
A toggle button element that can be directed to a liveview or a
live_component's `handle_event/3`.
## Examples
<.toggle_button action="my_liveview_action" value={@some_value}>
<span>Toggle me!</span>
</.toggle_button>
<.toggle_button action="my_live_component_action" target={@myself} value={@some_value}>
<span>Whatever you want</span>
</.toggle_button>
"""
def toggle_button(assigns) do
assigns = assigns |> assign_new(:id, fn -> assigns.action end)
~H"""
<label for={@id} class="inline-flex relative items-center cursor-pointer">
<input
id={@id}
type="checkbox"
value={@value}
checked={@value}
class="sr-only peer"
data-qa={@id}
{
if assigns |> Map.has_key?(:target),
do: %{"phx-click": @action, "phx-value-value": @value, "phx-target": @target},
else: %{"phx-click": @action, "phx-value-value": @value}
}
/>
<div class="w-11 h-6 bg-gray-300 rounded-full peer
peer-focus:ring-4 peer-focus:ring-teal-300 dark:peer-focus:ring-teal-800
peer-checked:bg-gray-600
peer-checked:after:translate-x-full peer-checked:after:border-white
after:content-[''] after:absolute after:top-1 after:left-[2px] after:bg-white after:border-gray-300
after:border after:rounded-full after:h-5 after:w-5
after:transition-all after:duration-250 after:ease-in-out
transition-colors duration-250 ease-in-out">
</div>
<span class="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300">
<%= render_slot(@inner_block) %>
</span>
</label>
"""
end
end end

View File

@ -4,8 +4,8 @@ defmodule MemexWeb.NoteLive.FormComponent do
alias Memex.Notes alias Memex.Notes
@impl true @impl true
def update(%{note: note} = assigns, socket) do def update(%{note: note, current_user: current_user} = assigns, socket) do
changeset = Notes.change_note(note) changeset = Notes.change_note(note, current_user)
{:ok, {:ok,
socket socket
@ -14,39 +14,51 @@ defmodule MemexWeb.NoteLive.FormComponent do
end end
@impl true @impl true
def handle_event("validate", %{"note" => note_params}, socket) do def handle_event(
"validate",
%{"note" => note_params},
%{assigns: %{note: note, current_user: current_user}} = socket
) do
changeset = changeset =
socket.assigns.note note
|> Notes.change_note(note_params) |> Notes.change_note(note_params, current_user)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
end end
def handle_event("save", %{"note" => note_params}, socket) do def handle_event("save", %{"note" => note_params}, %{assigns: %{action: action}} = socket) do
save_note(socket, socket.assigns.action, note_params) save_note(socket, action, note_params)
end end
defp save_note(socket, :edit, note_params) do defp save_note(
case Notes.update_note(socket.assigns.note, note_params) do %{assigns: %{note: note, return_to: return_to, current_user: current_user}} = socket,
{:ok, _note} -> :edit,
note_params
) do
case Notes.update_note(note, note_params, current_user) do
{:ok, %{slug: slug}} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, gettext("%{title} saved", title: title)) |> put_flash(:info, gettext("%{slug} saved", slug: slug))
|> push_navigate(to: socket.assigns.return_to)} |> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
end end
end end
defp save_note(socket, :new, note_params) do defp save_note(
case Notes.create_note(note_params) do %{assigns: %{return_to: return_to, current_user: current_user}} = socket,
{:ok, _note} -> :new,
note_params
) do
case Notes.create_note(note_params, current_user) do
{:ok, %{slug: slug}} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, gettext("%{title} created", title: title)) |> put_flash(:info, gettext("%{slug} created", slug: slug))
|> push_navigate(to: socket.assigns.return_to)} |> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)} {:noreply, assign(socket, changeset: changeset)}

View File

@ -1,6 +1,4 @@
<div> <div class="h-full flex flex-col justify-start items-stretch space-y-4">
<h2><%= @title %></h2>
<.form <.form
:let={f} :let={f}
for={@changeset} for={@changeset}
@ -8,27 +6,42 @@
phx-target={@myself} phx-target={@myself}
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
> >
<%= label(f, :title) %> <%= text_input(f, :slug,
<%= text_input(f, :title) %> class: "input input-primary",
<%= error_tag(f, :title) %> placeholder: gettext("slug")
) %>
<%= error_tag(f, :slug) %>
<%= label(f, :content) %> <%= textarea(f, :content,
<%= textarea(f, :content) %> id: "note-form-content",
class: "input input-primary h-64 min-h-64",
phx_hook: "MaintainAttrs",
phx_update: "ignore",
placeholder: gettext("content")
) %>
<%= error_tag(f, :content) %> <%= error_tag(f, :content) %>
<%= label(f, :tag) %> <%= text_input(f, :tags_string,
<%= multiple_select(f, :tag, "Option 1": "option1", "Option 2": "option2") %> id: "tags-input",
<%= error_tag(f, :tag) %> class: "input input-primary",
placeholder: gettext("tag1,tag2")
<%= label(f, :visibility) %>
<%= select(f, :visibility, Ecto.Enum.values(Memex.Notes.Note, :visibility),
prompt: "Choose a value"
) %> ) %>
<%= error_tag(f, :visibility) %> <%= error_tag(f, :tags_string) %>
<div> <div class="flex justify-center items-stretch space-x-4">
<%= submit("Save", phx_disable_with: "Saving...") %> <%= select(f, :visibility, Ecto.Enum.values(Memex.Notes.Note, :visibility),
class: "grow input input-primary",
prompt: gettext("select privacy")
) %>
<%= submit(dgettext("actions", "save"),
phx_disable_with: gettext("saving..."),
class: "mx-auto btn btn-primary"
) %>
</div> </div>
<%= error_tag(f, :visibility) %>
</.form> </.form>
</div> </div>

View File

@ -1,45 +1,88 @@
defmodule MemexWeb.NoteLive.Index do defmodule MemexWeb.NoteLive.Index do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.{Accounts.User, Notes, Notes.Note}
alias Memex.Notes
alias Memex.Notes.Note
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(search: search) |> display_notes()}
end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, assign(socket, :notes, list_notes())} {:ok, socket |> assign(search: nil) |> display_notes()}
end end
@impl true @impl true
def handle_params(params, _url, socket) do def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)} {:noreply, apply_action(socket, live_action, params)}
end end
defp apply_action(socket, :edit, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"slug" => slug}) do
%{slug: slug} = note = Notes.get_note_by_slug(slug, current_user)
socket socket
|> assign(page_title: gettext("edit %{title}", title: title)) |> assign(page_title: gettext("edit %{slug}", slug: slug))
|> assign(:note, Notes.get_note!(id)) |> assign(note: note)
end end
defp apply_action(socket, :new, _params) do defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
socket socket
|> assign(page_title: "new note") |> assign(page_title: gettext("new note"))
|> assign(:note, %Note{}) |> assign(note: %Note{visibility: :private, user_id: current_user_id})
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket
|> assign(page_title: "notes") |> assign(page_title: gettext("notes"))
|> assign(:note, nil) |> assign(search: nil)
|> assign(note: nil)
|> display_notes()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(page_title: gettext("notes"))
|> assign(search: search)
|> assign(note: nil)
|> display_notes()
end end
@impl true @impl true
def handle_event("delete", %{"id" => id}, socket) do def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
note = Notes.get_note!(id) note = Notes.get_note!(id, current_user)
{:ok, _} = Notes.delete_note(note) {:ok, %{slug: slug}} = Notes.delete_note(note, current_user)
|> put_flash(:info, gettext("%{title} deleted", title: title)) socket =
socket
|> assign(notes: Notes.list_notes(current_user))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
defp list_notes do {:noreply, socket}
Notes.list_notes()
end end
@impl true
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.note_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply, socket |> push_patch(to: Routes.note_index_path(Endpoint, :search, search_term))}
end
defp display_notes(%{assigns: %{current_user: current_user, search: search}} = socket)
when not (current_user |> is_nil()) do
socket |> assign(notes: Notes.list_notes(search, current_user))
end
defp display_notes(%{assigns: %{search: search}} = socket) do
socket |> assign(notes: Notes.list_public_notes(search))
end
@spec is_owner_or_admin?(Note.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner_or_admin?(_context, %{role: :admin}), do: true
defp is_owner_or_admin?(_context, _other_user), do: false
@spec is_owner?(Note.t(), User.t()) :: boolean()
defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner?(_context, _other_user), do: false
end end

View File

@ -1,66 +1,73 @@
<h1>Listing Notes</h1> <div class="mx-auto flex flex-col justify-center items-start space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= gettext("notes") %>
</h1>
<%= if @live_action in [:new, :edit] do %> <.form
<.modal return_to={Routes.note_index_path(@socket, :index)}> :let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="self-stretch flex flex-col items-stretch"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("search")
) %>
</.form>
<%= if @notes |> Enum.empty?() do %>
<h1 class="self-center text-primary-500">
<%= gettext("no notes found") %>
</h1>
<% else %>
<.live_component <.live_component
module={MemexWeb.NoteLive.FormComponent} module={MemexWeb.Components.NotesTableComponent}
id={@note.id || :new} id="notes-index-table"
title={@page_title} current_user={@current_user}
action={@live_action} notes={@notes}
note={@note} >
return_to={Routes.note_index_path(@socket, :index)} <:actions :let={note}>
/> <.link
</.modal> :if={is_owner?(note, @current_user)}
<% end %> patch={Routes.note_index_path(@socket, :edit, note.slug)}
data-qa={"note-edit-#{note.id}"}
>
<%= dgettext("actions", "edit") %>
</.link>
<.link
:if={is_owner_or_admin?(note, @current_user)}
href="#"
phx-click="delete"
phx-value-id={note.id}
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-note-#{note.id}"}
>
<%= dgettext("actions", "delete") %>
</.link>
</:actions>
</.live_component>
<% end %>
<table> <.link
<thead> :if={@current_user}
<tr> patch={Routes.note_index_path(@socket, :new)}
<th>Title</th> class="self-end btn btn-primary"
<th>Content</th> >
<th>Tag</th> <%= dgettext("actions", "new note") %>
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody id="notes">
<%= for note <- @notes do %>
<tr id={"note-#{note.id}"}>
<td><%= note.title %></td>
<td><%= note.content %></td>
<td><%= note.tag %></td>
<td><%= note.visibility %></td>
<td>
<span>
<.link navigate={Routes.note_show_path(@socket, :show, note)}>
<%= dgettext("actions", "Show") %>
</.link>
</span>
<span>
<.link patch={Routes.note_index_path(@socket, :edit, note)}>
<%= dgettext("actions", "Edit") %>
</.link>
</span>
<span>
<.link
href="#"
phx-click="delete"
phx-value-id={note.id}
data-confirm={dgettext("prompts", "Are you sure?")}
>
<%= dgettext("actions", "Delete") %>
</.link>
</span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span>
<.link patch={Routes.note_index_path(@socket, :new)}>
<%= dgettext("actions", "New Note") %>
</.link> </.link>
</span> </div>
<.modal :if={@live_action in [:new, :edit]} return_to={Routes.note_index_path(@socket, :index)}>
<.live_component
module={MemexWeb.NoteLive.FormComponent}
id={@note.id || :new}
current_user={@current_user}
title={@page_title}
action={@live_action}
note={@note}
return_to={Routes.note_index_path(@socket, :index)}
/>
</.modal>

View File

@ -1,7 +1,7 @@
defmodule MemexWeb.NoteLive.Show do defmodule MemexWeb.NoteLive.Show do
use MemexWeb, :live_view use MemexWeb, :live_view
import MemexWeb.Components.NoteContent
alias Memex.Notes alias Memex.{Accounts.User, Notes, Notes.Note}
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
@ -9,13 +9,50 @@ defmodule MemexWeb.NoteLive.Show do
end end
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(
{:noreply, %{"slug" => slug},
socket _params,
|> assign(:page_title, page_title(socket.assigns.live_action)) %{assigns: %{live_action: live_action, current_user: current_user}} = socket
|> assign(:note, Notes.get_note!(id))} ) do
note =
case Notes.get_note_by_slug(slug, current_user) do
nil -> raise MemexWeb.NotFoundError, gettext("%{slug} could not be found", slug: slug)
note -> note
end
socket =
socket
|> assign(:page_title, page_title(live_action, note))
|> assign(:note, note)
{:noreply, socket}
end end
defp page_title(:show), do: "show note" @impl true
defp page_title(:edit), do: "edit note" def handle_event(
"delete",
_params,
%{assigns: %{note: note, current_user: current_user}} = socket
) do
{:ok, %{slug: slug}} = Notes.delete_note(note, current_user)
socket =
socket
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|> push_navigate(to: Routes.note_index_path(Endpoint, :index))
{:noreply, socket}
end
defp page_title(:show, %{slug: slug}), do: slug
defp page_title(:edit, %{slug: slug}), do: gettext("edit %{slug}", slug: slug)
@spec is_owner_or_admin?(Note.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner_or_admin?(_context, %{role: :admin}), do: true
defp is_owner_or_admin?(_context, _other_user), do: false
@spec is_owner?(Note.t(), User.t()) :: boolean()
defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner?(_context, _other_user), do: false
end end

View File

@ -1,48 +1,56 @@
<h1>Show Note</h1> <div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @note.slug %>
</h1>
<%= if @live_action in [:edit] do %> <div class="flex flex-wrap space-x-1">
<.modal return_to={Routes.note_show_path(@socket, :show, @note)}> <.link
<.live_component :for={tag <- @note.tags}
module={MemexWeb.NoteLive.FormComponent} navigate={Routes.note_index_path(Endpoint, :search, tag)}
id={@note.id} class="link"
title={@page_title} >
action={@live_action} <%= tag %>
note={@note} </.link>
return_to={Routes.note_show_path(@socket, :show, @note)} </div>
/>
</.modal>
<% end %>
<ul> <.note_content note={@note} />
<li>
<strong>Title:</strong>
<%= @note.title %>
</li>
<li> <p class="self-end">
<strong>Content:</strong> <%= gettext("Visibility: %{visibility}", visibility: @note.visibility) %>
<%= @note.content %> </p>
</li>
<li> <div class="self-end flex space-x-4">
<strong>Tag:</strong> <.link class="btn btn-primary" navigate={Routes.note_index_path(@socket, :index)}>
<%= @note.tag %> <%= dgettext("actions", "back") %>
</li> </.link>
<.link
:if={is_owner?(@note, @current_user)}
class="btn btn-primary"
patch={Routes.note_show_path(@socket, :edit, @note.slug)}
>
<%= dgettext("actions", "edit") %>
</.link>
<button
:if={is_owner_or_admin?(@note, @current_user)}
type="button"
class="btn btn-primary"
phx-click="delete"
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-note-#{@note.id}"}
>
<%= dgettext("actions", "delete") %>
</button>
</div>
</div>
<li> <.modal :if={@live_action == :edit} return_to={Routes.note_show_path(@socket, :show, @note.slug)}>
<strong>Visibility:</strong> <.live_component
<%= @note.visibility %> module={MemexWeb.NoteLive.FormComponent}
</li> id={@note.id}
</ul> current_user={@current_user}
title={@page_title}
<span> action={@live_action}
<.link patch={Routes.note_show_path(@socket, :edit, @note)} class="button"> note={@note}
<%= dgettext("actions", "Edit") %> return_to={Routes.note_show_path(@socket, :show, @note.slug)}
</.link> />
</span> </.modal>
|
<span>
<.link patch={Routes.note_index_path(@socket, :index)}>
<%= dgettext("actions", "Back") %>
</.link>
</span>

View File

@ -4,8 +4,8 @@ defmodule MemexWeb.PipelineLive.FormComponent do
alias Memex.Pipelines alias Memex.Pipelines
@impl true @impl true
def update(%{pipeline: pipeline} = assigns, socket) do def update(%{pipeline: pipeline, current_user: current_user} = assigns, socket) do
changeset = Pipelines.change_pipeline(pipeline) changeset = Pipelines.change_pipeline(pipeline, current_user)
{:ok, {:ok,
socket socket
@ -14,39 +14,56 @@ defmodule MemexWeb.PipelineLive.FormComponent do
end end
@impl true @impl true
def handle_event("validate", %{"pipeline" => pipeline_params}, socket) do def handle_event(
"validate",
%{"pipeline" => pipeline_params},
%{assigns: %{pipeline: pipeline, current_user: current_user}} = socket
) do
changeset = changeset =
socket.assigns.pipeline pipeline
|> Pipelines.change_pipeline(pipeline_params) |> Pipelines.change_pipeline(pipeline_params, current_user)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
end end
def handle_event("save", %{"pipeline" => pipeline_params}, socket) do def handle_event(
save_pipeline(socket, socket.assigns.action, pipeline_params) "save",
%{"pipeline" => pipeline_params},
%{assigns: %{action: action}} = socket
) do
save_pipeline(socket, action, pipeline_params)
end end
defp save_pipeline(socket, :edit, pipeline_params) do defp save_pipeline(
case Pipelines.update_pipeline(socket.assigns.pipeline, pipeline_params) do %{assigns: %{pipeline: pipeline, return_to: return_to, current_user: current_user}} =
{:ok, _pipeline} -> socket,
:edit,
pipeline_params
) do
case Pipelines.update_pipeline(pipeline, pipeline_params, current_user) do
{:ok, %{slug: slug}} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, "pipeline updated successfully") |> put_flash(:info, gettext("%{slug} saved", slug: slug))
|> push_navigate(to: socket.assigns.return_to)} |> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
end end
end end
defp save_pipeline(socket, :new, pipeline_params) do defp save_pipeline(
case Pipelines.create_pipeline(pipeline_params) do %{assigns: %{return_to: return_to, current_user: current_user}} = socket,
{:ok, _pipeline} -> :new,
pipeline_params
) do
case Pipelines.create_pipeline(pipeline_params, current_user) do
{:ok, %{slug: slug}} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, "pipeline created successfully") |> put_flash(:info, gettext("%{slug} created", slug: slug))
|> push_navigate(to: socket.assigns.return_to)} |> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)} {:noreply, assign(socket, changeset: changeset)}

View File

@ -1,6 +1,4 @@
<div> <div class="h-full flex flex-col justify-start items-stretch space-y-4">
<h2><%= @title %></h2>
<.form <.form
:let={f} :let={f}
for={@changeset} for={@changeset}
@ -8,23 +6,42 @@
phx-target={@myself} phx-target={@myself}
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
> >
<%= label(f, :title) %> <%= text_input(f, :slug,
<%= text_input(f, :title) %> class: "input input-primary",
<%= error_tag(f, :title) %> placeholder: gettext("slug")
) %>
<%= error_tag(f, :slug) %>
<%= label(f, :description) %> <%= textarea(f, :description,
<%= textarea(f, :description) %> id: "pipeline-form-description",
class: "input input-primary h-64 min-h-64",
phx_hook: "MaintainAttrs",
phx_update: "ignore",
placeholder: gettext("description")
) %>
<%= error_tag(f, :description) %> <%= error_tag(f, :description) %>
<%= label(f, :visibility) %> <%= text_input(f, :tags_string,
<%= select(f, :visibility, Ecto.Enum.values(Memex.Pipelines.Pipeline, :visibility), id: "tags-input",
prompt: "Choose a value" class: "input input-primary",
placeholder: gettext("tag1,tag2")
) %> ) %>
<%= error_tag(f, :visibility) %> <%= error_tag(f, :tags_string) %>
<div> <div class="flex justify-center items-stretch space-x-4">
<%= submit("Save", phx_disable_with: "Saving...") %> <%= select(f, :visibility, Ecto.Enum.values(Memex.Pipelines.Pipeline, :visibility),
class: "grow input input-primary",
prompt: gettext("select privacy")
) %>
<%= submit(dgettext("actions", "save"),
phx_disable_with: gettext("saving..."),
class: "mx-auto btn btn-primary"
) %>
</div> </div>
<%= error_tag(f, :visibility) %>
</.form> </.form>
</div> </div>

View File

@ -1,46 +1,89 @@
defmodule MemexWeb.PipelineLive.Index do defmodule MemexWeb.PipelineLive.Index do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.{Accounts.User, Pipelines, Pipelines.Pipeline}
alias Memex.Pipelines
alias Memex.Pipelines.Pipeline
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(search: search) |> display_pipelines()}
end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, assign(socket, :pipelines, list_pipelines())} {:ok, socket |> assign(search: nil) |> display_pipelines()}
end end
@impl true @impl true
def handle_params(params, _url, socket) do def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)} {:noreply, apply_action(socket, live_action, params)}
end end
defp apply_action(socket, :edit, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"slug" => slug}) do
%{slug: slug} = pipeline = Pipelines.get_pipeline_by_slug(slug, current_user)
socket socket
|> assign(:page_title, "edit pipeline") |> assign(page_title: gettext("edit %{slug}", slug: slug))
|> assign(:pipeline, Pipelines.get_pipeline!(id)) |> assign(pipeline: pipeline)
end end
defp apply_action(socket, :new, _params) do defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
socket socket
|> assign(:page_title, "new Pipeline") |> assign(page_title: gettext("new pipeline"))
|> assign(:pipeline, %Pipeline{}) |> assign(pipeline: %Pipeline{visibility: :private, user_id: current_user_id})
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket
|> assign(:page_title, "listing pipelines") |> assign(page_title: gettext("pipelines"))
|> assign(:pipeline, nil) |> assign(search: nil)
|> assign(pipeline: nil)
|> display_pipelines()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(page_title: gettext("pipelines"))
|> assign(search: search)
|> assign(pipeline: nil)
|> display_pipelines()
end end
@impl true @impl true
def handle_event("delete", %{"id" => id}, socket) do def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
pipeline = Pipelines.get_pipeline!(id) pipeline = Pipelines.get_pipeline!(id, current_user)
{:ok, _} = Pipelines.delete_pipeline(pipeline) {:ok, %{slug: slug}} = Pipelines.delete_pipeline(pipeline, current_user)
{:noreply, assign(socket, :pipelines, list_pipelines())} socket =
socket
|> assign(pipelines: Pipelines.list_pipelines(current_user))
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
{:noreply, socket}
end end
defp list_pipelines do @impl true
Pipelines.list_pipelines() def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.pipeline_index_path(Endpoint, :index))}
end end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply,
socket |> push_patch(to: Routes.pipeline_index_path(Endpoint, :search, search_term))}
end
defp display_pipelines(%{assigns: %{current_user: current_user, search: search}} = socket)
when not (current_user |> is_nil()) do
socket |> assign(pipelines: Pipelines.list_pipelines(search, current_user))
end
defp display_pipelines(%{assigns: %{search: search}} = socket) do
socket |> assign(pipelines: Pipelines.list_public_pipelines(search))
end
@spec is_owner_or_admin?(Pipeline.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner_or_admin?(_context, %{role: :admin}), do: true
defp is_owner_or_admin?(_context, _other_user), do: false
@spec is_owner?(Pipeline.t(), User.t()) :: boolean()
defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner?(_context, _other_user), do: false
end end

View File

@ -1,64 +1,76 @@
<h1>listing pipelines</h1> <div class="mx-auto flex flex-col justify-center items-start space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= gettext("pipelines") %>
</h1>
<%= if @live_action in [:new, :edit] do %> <.form
<.modal return_to={Routes.pipeline_index_path(@socket, :index)}> :let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="self-stretch flex flex-col items-stretch"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("search")
) %>
</.form>
<%= if @pipelines |> Enum.empty?() do %>
<h1 class="self-center text-primary-500">
<%= gettext("no pipelines found") %>
</h1>
<% else %>
<.live_component <.live_component
module={MemexWeb.PipelineLive.FormComponent} module={MemexWeb.Components.PipelinesTableComponent}
id={@pipeline.id || :new} id="pipelines-index-table"
title={@page_title} current_user={@current_user}
action={@live_action} pipelines={@pipelines}
pipeline={@pipeline} >
return_to={Routes.pipeline_index_path(@socket, :index)} <:actions :let={pipeline}>
/> <.link
</.modal> :if={is_owner?(pipeline, @current_user)}
<% end %> patch={Routes.pipeline_index_path(@socket, :edit, pipeline.slug)}
data-qa={"pipeline-edit-#{pipeline.id}"}
>
<%= dgettext("actions", "edit") %>
</.link>
<.link
:if={is_owner_or_admin?(pipeline, @current_user)}
href="#"
phx-click="delete"
phx-value-id={pipeline.id}
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-pipeline-#{pipeline.id}"}
>
<%= dgettext("actions", "delete") %>
</.link>
</:actions>
</.live_component>
<% end %>
<table> <.link
<thead> :if={@current_user}
<tr> patch={Routes.pipeline_index_path(@socket, :new)}
<th>Title</th> class="self-end btn btn-primary"
<th>Description</th> >
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody id="pipelines">
<%= for pipeline <- @pipelines do %>
<tr id={"pipeline-#{pipeline.id}"}>
<td><%= pipeline.title %></td>
<td><%= pipeline.description %></td>
<td><%= pipeline.visibility %></td>
<td>
<span>
<.link navigate={Routes.pipeline_show_path(@socket, :show, pipeline)}>
<%= dgettext("actions", "show") %>
</.link>
</span>
<span>
<.link patch={Routes.pipeline_index_path(@socket, :edit, pipeline)}>
<%= dgettext("actions", "edit") %>
</.link>
</span>
<span>
<.link
href="#"
phx-click="delete"
phx-value-id={pipeline.id}
data-confirm={dgettext("prompts", "are you sure?")}
>
<%= dgettext("actions", "delete") %>
</.link>
</span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span>
<.link patch={Routes.pipeline_index_path(@socket, :new)}>
<%= dgettext("actions", "new pipeline") %> <%= dgettext("actions", "new pipeline") %>
</.link> </.link>
</span> </div>
<.modal
:if={@live_action in [:new, :edit]}
return_to={Routes.pipeline_index_path(@socket, :index)}
>
<.live_component
module={MemexWeb.PipelineLive.FormComponent}
id={@pipeline.id || :new}
current_user={@current_user}
title={@page_title}
action={@live_action}
pipeline={@pipeline}
return_to={Routes.pipeline_index_path(@socket, :index)}
/>
</.modal>

View File

@ -1,7 +1,8 @@
defmodule MemexWeb.PipelineLive.Show do defmodule MemexWeb.PipelineLive.Show do
use MemexWeb, :live_view use MemexWeb, :live_view
import MemexWeb.Components.StepContent
alias Memex.Pipelines alias Memex.{Accounts.User, Pipelines}
alias Memex.Pipelines.{Pipeline, Steps, Steps.Step}
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
@ -9,13 +10,128 @@ defmodule MemexWeb.PipelineLive.Show do
end end
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(
{:noreply, %{"slug" => slug} = params,
socket _url,
|> assign(:page_title, page_title(socket.assigns.live_action)) %{assigns: %{current_user: current_user, live_action: live_action}} = socket
|> assign(:pipeline, Pipelines.get_pipeline!(id))} ) do
pipeline =
case Pipelines.get_pipeline_by_slug(slug, current_user) do
nil -> raise MemexWeb.NotFoundError, gettext("%{slug} could not be found", slug: slug)
pipeline -> pipeline
end
socket =
socket
|> assign(:page_title, page_title(live_action, pipeline))
|> assign(:pipeline, pipeline)
|> assign(:steps, pipeline |> Steps.list_steps(current_user))
|> apply_action(live_action, params)
{:noreply, socket}
end end
defp page_title(:show), do: "show pipeline" defp apply_action(socket, live_action, _params) when live_action in [:show, :edit] do
defp page_title(:edit), do: "edit pipeline" socket
end
defp apply_action(
%{
assigns: %{
steps: steps,
pipeline: %{id: pipeline_id},
current_user: %{id: current_user_id}
}
} = socket,
:add_step,
_params
) do
socket
|> assign(
step: %Step{
position: steps |> Enum.count(),
pipeline_id: pipeline_id,
user_id: current_user_id
}
)
end
defp apply_action(
%{assigns: %{current_user: current_user}} = socket,
:edit_step,
%{"step_id" => step_id}
) do
socket |> assign(step: step_id |> Steps.get_step!(current_user))
end
@impl true
def handle_event(
"delete",
_params,
%{assigns: %{pipeline: pipeline, current_user: current_user}} = socket
) do
{:ok, %{slug: slug}} = Pipelines.delete_pipeline(pipeline, current_user)
socket =
socket
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|> push_navigate(to: Routes.pipeline_index_path(Endpoint, :index))
{:noreply, socket}
end
@impl true
def handle_event(
"delete_step",
%{"step-id" => step_id},
%{assigns: %{pipeline: %{slug: pipeline_slug}, current_user: current_user}} = socket
) do
{:ok, %{title: title}} =
step_id
|> Steps.get_step!(current_user)
|> Steps.delete_step(current_user)
socket =
socket
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> push_patch(to: Routes.pipeline_show_path(Endpoint, :show, pipeline_slug))
{:noreply, socket}
end
@impl true
def handle_event(
"reorder_step",
%{"step-id" => step_id, "direction" => direction},
%{assigns: %{pipeline: %{slug: pipeline_slug}, current_user: current_user}} = socket
) do
direction = if direction == "up", do: :up, else: :down
{:ok, _step} =
step_id
|> Steps.get_step!(current_user)
|> Steps.reorder_step(direction, current_user)
socket =
socket
|> push_patch(to: Routes.pipeline_show_path(Endpoint, :show, pipeline_slug))
{:noreply, socket}
end
defp page_title(:show, %{slug: slug}), do: slug
defp page_title(live_action, %{slug: slug}) when live_action in [:edit, :edit_step],
do: gettext("edit %{slug}", slug: slug)
defp page_title(:add_step, %{slug: slug}), do: gettext("add step to %{slug}", slug: slug)
@spec is_owner_or_admin?(Pipeline.t(), User.t()) :: boolean()
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner_or_admin?(_context, %{role: :admin}), do: true
defp is_owner_or_admin?(_context, _other_user), do: false
@spec is_owner?(Pipeline.t(), User.t()) :: boolean()
defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
defp is_owner?(_context, _other_user), do: false
end end

View File

@ -1,43 +1,165 @@
<h1>show pipeline</h1> <div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @pipeline.slug %>
</h1>
<%= if @live_action in [:edit] do %> <div class="flex flex-wrap space-x-1">
<.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)}> <.link
<.live_component :for={tag <- @pipeline.tags}
module={MemexWeb.PipelineLive.FormComponent} navigate={Routes.pipeline_index_path(Endpoint, :search, tag)}
id={@pipeline.id} class="link"
title={@page_title} >
action={@live_action} <%= tag %>
pipeline={@pipeline} </.link>
return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)} </div>
/>
</.modal> <textarea
:if={@pipeline.description}
id="show-pipeline-description"
class="input input-primary h-32 min-h-32"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><%= @pipeline.description %></textarea>
<p class="self-end">
<%= gettext("Visibility: %{visibility}", visibility: @pipeline.visibility) %>
</p>
<div class="pb-4 self-end flex space-x-4">
<.link class="btn btn-primary" navigate={Routes.pipeline_index_path(@socket, :index)}>
<%= dgettext("actions", "back") %>
</.link>
<.link
:if={is_owner?(@pipeline, @current_user)}
class="btn btn-primary"
patch={Routes.pipeline_show_path(@socket, :edit, @pipeline.slug)}
>
<%= dgettext("actions", "edit") %>
</.link>
<button
:if={is_owner_or_admin?(@pipeline, @current_user)}
type="button"
class="btn btn-primary"
phx-click="delete"
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-pipeline-#{@pipeline.id}"}
>
<%= dgettext("actions", "delete") %>
</button>
</div>
<hr class="hr" />
<h2 class="pt-2 self-center text-lg">
<%= gettext("steps:") %>
</h2>
<%= if @steps |> Enum.empty?() do %>
<h3 class="self-center text-md text-primary-600">
<%= gettext("no steps") %>
</h3>
<% else %>
<%= for %{id: step_id, position: position, title: title} = step <- @steps do %>
<div class="flex justify-between items-center space-x-4">
<h3 class="text-md">
<%= gettext("%{position}. %{title}", position: position + 1, title: title) %>
</h3>
<%= if is_owner?(@pipeline, @current_user) do %>
<div class="flex justify-between items-center space-x-4">
<%= if position <= 0 do %>
<i class="fas text-xl fa-chevron-up cursor-not-allowed opacity-25"></i>
<% else %>
<button
type="button"
class="cursor-pointer flex justify-center items-center"
phx-click="reorder_step"
phx-value-direction="up"
phx-value-step-id={step_id}
data-qa={"move-step-up-#{step_id}"}
>
<i class="fas text-xl fa-chevron-up"></i>
</button>
<% end %>
<%= if position >= length(@steps) - 1 do %>
<i class="fas text-xl fa-chevron-down cursor-not-allowed opacity-25"></i>
<% else %>
<button
type="button"
class="cursor-pointer flex justify-center items-center"
phx-click="reorder_step"
phx-value-direction="down"
phx-value-step-id={step_id}
data-qa={"move-step-down-#{step_id}"}
>
<i class="fas text-xl fa-chevron-down"></i>
</button>
<% end %>
<.link
class="self-end btn btn-primary"
patch={Routes.pipeline_show_path(@socket, :edit_step, @pipeline.slug, step_id)}
data-qa={"edit-step-#{step_id}"}
>
<%= dgettext("actions", "edit") %>
</.link>
<button
type="button"
class="btn btn-primary"
phx-click="delete_step"
phx-value-step-id={step_id}
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-step-#{step_id}"}
>
<%= dgettext("actions", "delete") %>
</button>
</div>
<% end %>
</div>
<.step_content step={step} />
<% end %>
<% end %>
<.link
:if={is_owner?(@pipeline, @current_user)}
class="self-end btn btn-primary"
patch={Routes.pipeline_show_path(@socket, :add_step, @pipeline.slug)}
data-qa={"add-step-#{@pipeline.id}"}
>
<%= dgettext("actions", "add step") %>
</.link>
</div>
<%= case @live_action do %>
<% :edit -> %>
<.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline.slug)}>
<.live_component
module={MemexWeb.PipelineLive.FormComponent}
id={@pipeline.id}
current_user={@current_user}
title={@page_title}
action={@live_action}
pipeline={@pipeline}
return_to={Routes.pipeline_show_path(@socket, :show, @pipeline.slug)}
/>
</.modal>
<% action when action in [:add_step, :edit_step] -> %>
<.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline.slug)}>
<.live_component
module={MemexWeb.StepLive.FormComponent}
id={@pipeline.id || :new}
current_user={@current_user}
title={@page_title}
action={@live_action}
pipeline={@pipeline}
step={@step}
return_to={Routes.pipeline_show_path(@socket, :show, @pipeline.slug)}
/>
</.modal>
<% _ -> %>
<% end %> <% end %>
<ul>
<li>
<strong>Title:</strong>
<%= @pipeline.title %>
</li>
<li>
<strong>Description:</strong>
<%= @pipeline.description %>
</li>
<li>
<strong>Visibility:</strong>
<%= @pipeline.visibility %>
</li>
</ul>
<span>
<.link patch={Routes.pipeline_show_path(@socket, :edit, @pipeline)} class="button">
<%= dgettext("actions", "edit") %>
</.link>
</span>
|
<span>
<.link patch={Routes.pipeline_index_path(@socket, :index)}>
<%= dgettext("actions", "Back") %>
</.link>
</span>

View File

@ -0,0 +1,74 @@
defmodule MemexWeb.StepLive.FormComponent do
use MemexWeb, :live_component
alias Memex.Pipelines.Steps
@impl true
def update(%{step: step, current_user: current_user, pipeline: _pipeline} = assigns, socket) do
changeset = Steps.change_step(step, current_user)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event(
"validate",
%{"step" => step_params},
%{assigns: %{step: step, current_user: current_user}} = socket
) do
changeset =
step
|> Steps.change_step(step_params, current_user)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"step" => step_params}, %{assigns: %{action: action}} = socket) do
save_step(socket, action, step_params)
end
defp save_step(
%{assigns: %{step: step, return_to: return_to, current_user: current_user}} = socket,
:edit_step,
step_params
) do
case Steps.update_step(step, step_params, current_user) do
{:ok, %{title: title}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} saved", title: title))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_step(
%{
assigns: %{
step: %{position: position},
return_to: return_to,
current_user: current_user,
pipeline: pipeline
}
} = socket,
:add_step,
step_params
) do
case Steps.create_step(step_params, position, pipeline, current_user) do
{:ok, %{title: title}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} created", title: title))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View File

@ -0,0 +1,34 @@
<div class="h-full flex flex-col justify-start items-stretch space-y-4">
<.form
:let={f}
for={@changeset}
id="step-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
>
<%= text_input(f, :title,
class: "input input-primary",
placeholder: gettext("title")
) %>
<%= error_tag(f, :title) %>
<%= textarea(f, :content,
id: "step-form-content",
class: "input input-primary h-64 min-h-64",
phx_hook: "MaintainAttrs",
phx_update: "ignore",
placeholder: gettext("use [[context-slug]] to link to a context")
) %>
<%= error_tag(f, :content) %>
<div class="flex justify-center items-stretch space-x-4">
<%= submit(dgettext("actions", "save"),
phx_disable_with: gettext("saving..."),
class: "mx-auto btn btn-primary"
) %>
</div>
</.form>
</div>

View File

@ -0,0 +1,3 @@
defmodule MemexWeb.NotFoundError do
defexception [:message, plug_status: 404]
end

View File

@ -11,15 +11,17 @@ defmodule MemexWeb.Router do
plug :protect_from_forgery plug :protect_from_forgery
plug :put_secure_browser_headers plug :put_secure_browser_headers
plug :fetch_current_user plug :fetch_current_user
plug :put_user_locale, default: Application.compile_env(:gettext, :default_locale, "en_US") plug :put_user_locale
end end
defp put_user_locale(%{assigns: %{current_user: %{locale: locale}}} = conn, default: default) do defp put_user_locale(%{assigns: %{current_user: %{locale: locale}}} = conn, _opts) do
default = Application.fetch_env!(:gettext, :default_locale)
Gettext.put_locale(locale || default) Gettext.put_locale(locale || default)
conn |> put_session(:locale, locale || default) conn |> put_session(:locale, locale || default)
end end
defp put_user_locale(conn, default: default) do defp put_user_locale(conn, _opts) do
default = Application.fetch_env!(:gettext, :default_locale)
Gettext.put_locale(default) Gettext.put_locale(default)
conn |> put_session(:locale, default) conn |> put_session(:locale, default)
end end
@ -36,6 +38,7 @@ defmodule MemexWeb.Router do
pipe_through :browser pipe_through :browser
live "/", HomeLive live "/", HomeLive
live "/faq", FaqLive
end end
## Authentication routes ## Authentication routes
@ -58,34 +61,40 @@ defmodule MemexWeb.Router do
pipe_through [:browser, :require_authenticated_user] pipe_through [:browser, :require_authenticated_user]
live "/notes/new", NoteLive.Index, :new live "/notes/new", NoteLive.Index, :new
live "/notes/:id/edit", NoteLive.Index, :edit live "/notes/:slug/edit", NoteLive.Index, :edit
live "/notes/:id/show/edit", NoteLive.Show, :edit live "/note/:slug/edit", NoteLive.Show, :edit
live "/contexts/new", ContextLive.Index, :new live "/contexts/new", ContextLive.Index, :new
live "/contexts/:id/edit", ContextLive.Index, :edit live "/contexts/:slug/edit", ContextLive.Index, :edit
live "/contexts/:id/show/edit", ContextLive.Show, :edit live "/context/:slug/edit", ContextLive.Show, :edit
live "/pipelines/new", PipelineLive.Index, :new live "/pipelines/new", PipelineLive.Index, :new
live "/pipelines/:id/edit", PipelineLive.Index, :edit live "/pipelines/:slug/edit", PipelineLive.Index, :edit
live "/pipelines/:id/show/edit", PipelineLive.Show, :edit live "/pipeline/:slug/edit", PipelineLive.Show, :edit
live "/pipeline/:slug/add_step", PipelineLive.Show, :add_step
live "/pipeline/:slug/:step_id", PipelineLive.Show, :edit_step
get "/users/settings", UserSettingsController, :edit get "/users/settings", UserSettingsController, :edit
put "/users/settings", UserSettingsController, :update put "/users/settings", UserSettingsController, :update
delete "/users/settings/:id", UserSettingsController, :delete delete "/users/settings/:id", UserSettingsController, :delete
get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
get "/export/:mode", ExportController, :export
end end
scope "/", MemexWeb do scope "/", MemexWeb do
pipe_through [:browser] pipe_through [:browser]
live "/notes", NoteLive.Index, :index live "/notes", NoteLive.Index, :index
live "/notes/:id", NoteLive.Show, :show live "/notes/:search", NoteLive.Index, :search
live "/note/:slug", NoteLive.Show, :show
live "/contexts", ContextLive.Index, :index live "/contexts", ContextLive.Index, :index
live "/contexts/:id", ContextLive.Show, :show live "/contexts/:search", ContextLive.Index, :search
live "/context/:slug", ContextLive.Show, :show
live "/pipelines", PipelineLive.Index, :index live "/pipelines", PipelineLive.Index, :index
live "/pipelines/:id", PipelineLive.Show, :show live "/pipelines/:search", PipelineLive.Index, :search
live "/pipeline/:slug", PipelineLive.Show, :show
end end
end end

View File

@ -57,6 +57,30 @@ defmodule MemexWeb.Telemetry do
"The time the connection spent waiting before being checked out for the query" "The time the connection spent waiting before being checked out for the query"
), ),
# Oban Metrics
counter("oban.job.exception",
tags: [:queue, :worker],
event_name: [:oban, :job, :exception],
measurement: :duration,
description: "Number of oban jobs that raised an exception"
),
counter("oban.job.start",
tags: [:queue, :worker],
event_name: [:oban, :job, :start],
measurement: :system_time,
description: "Number of oban jobs started"
),
summary("oban.job.stop.duration",
tags: [:queue, :worker],
unit: {:native, :millisecond},
description: "Length of time spent processing the oban job"
),
summary("oban.job.stop.queue_time",
tags: [:queue, :worker],
unit: {:native, :millisecond},
description: "Time the oban job spent waiting in milliseconds"
),
# VM Metrics # VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}), summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"), summary("vm.total_run_queue_lengths.total"),

View File

@ -6,7 +6,7 @@
<br /> <br />
<span style="margin-bottom: 1em; font-size: 1.25em;"> <span style="margin-bottom: 1em; font-size: 1.25em;">
<%= dgettext("emails", "Welcome to Memex") %> <%= dgettext("emails", "Welcome to memEx") %>
</span> </span>
<br /> <br />
@ -19,5 +19,5 @@
<br /> <br />
<%= dgettext("emails", "If you didn't create an account at Memex, please ignore this.") %> <%= dgettext("emails", "If you didn't create an account at memEx, please ignore this.") %>
</div> </div>

View File

@ -1,7 +1,7 @@
<%= dgettext("emails", "Hi %{email},", email: @user.email) %> <%= dgettext("emails", "Hi %{email},", email: @user.email) %>
<%= dgettext("emails", "Welcome to Memex") %> <%= dgettext("emails", "Welcome to memEx") %>
<%= dgettext("emails", "You can confirm your account by visiting the URL below:") %> <%= dgettext("emails", "You can confirm your account by visiting the URL below:") %>

View File

@ -13,5 +13,5 @@
<br /> <br />
<%= dgettext("emails", "If you didn't request this change from Memex, please ignore this.") %> <%= dgettext("emails", "If you didn't request this change from memEx, please ignore this.") %>
</div> </div>

View File

@ -15,6 +15,6 @@
<%= dgettext( <%= dgettext(
"emails", "emails",
"If you didn't request this change from Memex, please ignore this." "If you didn't request this change from memEx, please ignore this."
) %> ) %>
</div> </div>

View File

@ -5,13 +5,13 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title> <title>
<%= dgettext("errors", "Error") %>| Memex <%= dgettext("errors", "Error") %> | <%= gettext("memEx") %>
</title> </title>
<link rel="stylesheet" href="/css/app.css" /> <link rel="stylesheet" href="/css/app.css" />
<script defer type="text/javascript" src="/js/app.js"> <script defer type="text/javascript" src="/js/app.js">
</script> </script>
</head> </head>
<body class="pb-8 m-0 p-0 w-full h-full"> <body class="m-0 p-0 w-full h-full bg-primary-800 text-primary-400 subpixel-antialiased">
<header> <header>
<.topbar current_user={assigns[:current_user]}></.topbar> <.topbar current_user={assigns[:current_user]}></.topbar>
</header> </header>
@ -25,7 +25,7 @@
<hr class="w-full hr" /> <hr class="w-full hr" />
<a href={Routes.live_path(Endpoint, HomeLive)} class="link title text-primary-400 text-lg"> <a href={Routes.live_path(Endpoint, HomeLive)} class="link title text-primary-400 text-lg">
<%= dgettext("errors", "Go back home") %> <%= dgettext("errors", "go back home") %>
</a> </a>
</div> </div>
</div> </div>

View File

@ -3,16 +3,12 @@
<.topbar current_user={assigns[:current_user]}></.topbar> <.topbar current_user={assigns[:current_user]}></.topbar>
<div class="mx-8 my-2 flex flex-col space-y-4 text-center"> <div class="mx-8 my-2 flex flex-col space-y-4 text-center">
<%= if get_flash(@conn, :info) do %> <p :if={get_flash(@conn, :info)} class="alert alert-info" role="alert">
<p class="alert alert-info" role="alert"> <%= get_flash(@conn, :info) %>
<%= get_flash(@conn, :info) %> </p>
</p> <p :if={get_flash(@conn, :error)} class="alert alert-danger" role="alert">
<% end %> <%= get_flash(@conn, :error) %>
<%= if get_flash(@conn, :error) do %> </p>
<p class="alert alert-danger" role="alert">
<%= get_flash(@conn, :error) %>
</p>
<% end %>
</div> </div>
</header> </header>

View File

@ -4,15 +4,15 @@
<%= @email.subject %> <%= @email.subject %>
</title> </title>
</head> </head>
<body style="padding: 2em; color: rgb(31, 31, 31); background-color: rgb(220, 220, 228); font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; text-align: center;"> <body style="padding: 2em; color: rgb(161, 161, 170); background-color: rgb(39, 39, 42); font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; text-align: center;">
<%= @inner_content %> <%= @inner_content %>
<hr style="margin: 2em auto; border-width: 1px; border-color: rgb(212, 212, 216); width: 100%; max-width: 42rem;" /> <hr style="margin: 2em auto; border-width: 1px; border-color: rgb(161, 161, 170); width: 100%; max-width: 42rem;" />
<a style="color: rgb(31, 31, 31);" href={Routes.live_url(Endpoint, HomeLive)}> <a style="color: rgb(161, 161, 170);" href={Routes.live_url(Endpoint, HomeLive)}>
<%= dgettext( <%= dgettext(
"emails", "emails",
"This email was sent from Memex, the self-hosted firearm tracker website." "This email was sent from memEx"
) %> ) %>
</a> </a>
</body> </body>

View File

@ -7,5 +7,5 @@
===================== =====================
<%= dgettext("emails", <%= dgettext("emails",
"This email was sent from Memex at %{url}, the self-hosted firearm tracker website.", "This email was sent from memEx at %{url}",
url: Routes.live_url(Endpoint, HomeLive)) %> url: Routes.live_url(Endpoint, HomeLive)) %>

View File

@ -1,24 +1,27 @@
<main class="mb-8 min-w-full"> <main class="pb-8 min-w-full">
<header> <header>
<.topbar current_user={assigns[:current_user]}></.topbar> <.topbar current_user={assigns[:current_user]}></.topbar>
<div class="mx-8 my-2 flex flex-col space-y-4 text-center"> <div class="mx-8 my-2 flex flex-col space-y-4 text-center">
<%= if @flash && @flash |> Map.has_key?("info") do %> <p
<p class="alert alert-info" role="alert" phx-click="lv:clear-flash" phx-value-key="info"> :if={@flash && @flash |> Map.has_key?("info")}
<%= live_flash(@flash, "info") %> class="alert alert-info"
</p> role="alert"
<% end %> phx-click="lv:clear-flash"
phx-value-key="info"
>
<%= live_flash(@flash, "info") %>
</p>
<%= if @flash && @flash |> Map.has_key?("error") do %> <p
<p :if={@flash && @flash |> Map.has_key?("error")}
class="alert alert-danger" class="alert alert-danger"
role="alert" role="alert"
phx-click="lv:clear-flash" phx-click="lv:clear-flash"
phx-value-key="error" phx-value-key="error"
> >
<%= live_flash(@flash, "error") %> <%= live_flash(@flash, "error") %>
</p> </p>
<% end %>
</div> </div>
</header> </header>
@ -27,28 +30,16 @@
</div> </div>
</main> </main>
<div
id="loading"
class="fixed opacity-0 top-0 left-0 w-screen h-screen bg-primary-900 z-50
flex flex-col justify-center items-center space-y-4
transition-opacity ease-in-out duration-500"
>
<h1 class="title text-2xl title-primary-500 text-primary-400">
<%= gettext("Loading...") %>
</h1>
<i class="fas fa-3x fa-spin fa-gear text-primary-400"></i>
</div>
<div <div
id="disconnect" id="disconnect"
class="fixed opacity-0 top-0 left-0 w-screen h-screen bg-primary-900 z-50 class="z-50 fixed opacity-0 bottom-12 right-12 px-8 py-4 w-max h-max
flex flex-col justify-center items-center space-y-4 border border-primary-400 shadow-lg rounded-lg bg-primary-800 text-primary-400
transition-opacity ease-in-out duration-500" flex justify-center items-center space-x-4
transition-opacity ease-in-out duration-500 delay-[2000ms]"
> >
<h1 class="title text-2xl title-primary-500 text-primary-400"> <i class="fas fa-fade text-md fa-satellite-dish"></i>
<h1 class="title text-md">
<%= gettext("Reconnecting...") %> <%= gettext("Reconnecting...") %>
</h1> </h1>
<i class="fas fa-3x fa-fade fa-satellite-dish text-primary-400"></i>
</div> </div>

View File

@ -1,12 +1,12 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" class="m-0 p-0 w-full h-full"> <html lang="en" class="m-0 p-0 w-full h-full bg-primary-800">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<%= csrf_meta_tag() %> <%= csrf_meta_tag() %>
<.live_title suffix={" | #{gettext("memex")}"}> <.live_title suffix={" | #{gettext("memEx")}"}>
<%= assigns[:page_title] || gettext("memex") %> <%= assigns[:page_title] || gettext("memEx") %>
</.live_title> </.live_title>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/css/app.css")} /> <link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/css/app.css")} />
<script <script
@ -18,7 +18,7 @@
</script> </script>
</head> </head>
<body class="m-0 p-0 w-full h-full bg-primary-800 text-primary-400 subpixel-antialiased"> <body class="m-0 p-0 w-full h-full text-primary-400 subpixel-antialiased">
<%= @inner_content %> <%= @inner_content %>
</body> </body>
</html> </html>

Some files were not shown because too many files have changed in this diff Show More