Skip to content

Commit

Permalink
support delete
Browse files Browse the repository at this point in the history
  • Loading branch information
smpallen99 committed Apr 11, 2018
1 parent f561ba6 commit f92ef7f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 47 deletions.
11 changes: 2 additions & 9 deletions plugins/one_chat/lib/one_chat_web/views/message_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ defmodule OneChatWeb.MessageView do
|> encode_mentions(user, require_space)
|> encode_room_links
|> EmojiOne.shortname_to_image(single_class: "big")
|> IO.inspect(label: "before mrp")
|> run_message_replacement_patterns()
|> autolink()
end
Expand Down Expand Up @@ -449,7 +448,8 @@ defmodule OneChatWeb.MessageView do
[first, markdown | rest] ->
markdown = String.trim_leading(markdown, "\n")
first <> ~s|<div class="markdown-body">| <>
Earmark.as_html!(markdown, %Earmark.Options{gfm: true, plugins: %{"" => InfinityOne.EarmarkPlugin.Task}})
Earmark.as_html!(markdown, %Earmark.Options{gfm: true, plugins:
%{"" => InfinityOne.EarmarkPlugin.Task}})
<> "</div>" <> String.trim_leading(Enum.join(rest, ""))
_ ->
body
Expand All @@ -468,15 +468,8 @@ defmodule OneChatWeb.MessageView do

def encode_users(body, username, require_space) do
sp = if require_space, do: "(?:^|\s)", else: "(?:^|\s|>)"
Logger.warn "sp: " <> sp
# Regex.replace ~r/(^|\s)@([\.a-zA-Z0-9-_]+)/, body, fn x, y ->
Regex.replace ~r/#{sp}@([\.a-zA-Z0-9-_]+)/, body, fn x, y ->
# Regex.replace ~r/(?:^|\s|>)@([\.a-zA-Z0-9-_]+)/, body, fn x, y ->
Logger.warn "{x,y}: " <> inspect({x,y})
x = String.trim_trailing(x, "@" <> y)
# y = String.trim_trailing(y, ">")
# ~s'#{y}<a rebel-channel="user" rebel-click="flex_call" data-id="members-list"' <>
# ~s' data-fun="flex_user_open" class="mention-link#{get_own_class(username, x)}" data-username="#{x}">#{x}</a>'
~s'#{x}<a rebel-channel="user" rebel-click="flex_call" data-id="members-list"' <>
~s' data-fun="flex_user_open" class="mention-link#{get_own_class(username, y)}" data-username="#{y}">@#{y}</a>'
end
Expand Down
92 changes: 71 additions & 21 deletions plugins/one_wiki/lib/one_wiki/models/one_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,80 @@ defmodule OneWiki.Page do
"""
use OneModel, schema: OneWiki.Schema.Page
alias Ecto.Multi
alias InfinityOne.Accounts.User

require Logger

def changeset(user, params) do
def changeset(%User{} = user, params) do
changeset %@schema{}, user, params
end

def changeset(%@schema{} = struct, %User{} = user) do
changeset(struct, user, %{})
end

def changeset(struct, user, params) do
struct
|> @schema.changeset(params)
|> validate_permission(user)
end

# def create(user, params) do
# user
# |> changeset(params)
# |> create
# end
@doc """
Create a new page.
Creates the database entry for the page. Also creates a file with the page
body in `priv/static/uploads/pages` directory. This directory is a git repo.
After writing the file, it is committed to the git repo, providing version
control of each page revision.
"""
def create(user, params) do
case transaction(user, params) |> @repo.transaction() do
case create_transaction(user, params) |> @repo.transaction() do
{:ok, %{insert: insert}} -> {:ok, insert}
{:error, _, reason} -> {:error, reason}
{:error, _, reason, _} -> {:error, reason}
end
|> IO.inspect(label: "transaction result")
end

def transaction(user, params) do
defp create_transaction(user, params) do
Multi.new()
|> Multi.run(:insert, fn _ -> insert(user, params) end)
|> Multi.run(:insert, fn _ -> run_insert(user, params) end)
|> Multi.run(:create_file, &create_file(user, &1.insert))
end

defp insert(user, params) do
defp run_insert(user, params) do
user
|> changeset(params)
|> create
end

@doc """
Updates an existing page.
Updates the database record with the modified page. Runs the same git commands
as create/2 to commit the revisions in git.
"""
def update(user, page, params) do
case update_transaction(user, page, params) |> @repo.transaction() do
{:ok, %{update: update}} -> {:ok, update}
{:error, _, reason} -> {:error, reason}
{:error, _, reason, _} -> {:error, reason}
end
end

defp update_transaction(user, page, params) do
Multi.new()
|> Multi.run(:update, fn _ -> run_update(user, page, params) end)
|> Multi.run(:create_file, &create_file(user, &1.update, :updated))
end

defp run_update(user, page, params) do
page
|> changeset(user, params)
|> __MODULE__.update()
end

defp create_file(user, page, action \\ :added) do
Logger.warn "page: " <> inspect(page)
contents = page.body
# contents = "Title: " <> page.title <> "\n" <> page.body <> "\n"
repo = Git.new OneWiki.pages_path()
message = "'#{page.title}' #{action} by @#{user.username}"
path = Path.join(OneWiki.pages_path(), page.id)
Expand All @@ -60,22 +91,41 @@ defmodule OneWiki.Page do
end
end

def update(user, page, params) do
case update_transaction(user, page, params) |> @repo.transaction() do
{:ok, %{update: update}} -> {:ok, update}
def delete(user, id) do
case delete_transaction(user, id) |> @repo.transaction() do
{:ok, %{delete: delete}} -> {:ok, delete}
{:error, _, reason} -> {:error, reason}
{:error, _, reason, _} -> {:error, reason}
end
|> IO.inspect(label: "transaction result")
end

defp update_transaction(user, page, params) do
defp delete_transaction(user, id) do
Multi.new()
|> Multi.run(:update, fn _ -> __MODULE__.update(page, params) end)
|> Multi.run(:create_file, &create_file(user, &1.update, :updated))
|> Multi.run(:delete, fn _ -> run_delete(user, id) end)
|> Multi.run(:delete_file, &delete_file(user, &1.delete))
end

defp run_delete(user, page_id) do
page_id
|> get()
|> changeset(user)
|> delete()
end

defp delete_file(user, page) do
repo = Git.new OneWiki.pages_path()
message = "'#{page.title}' delete by @#{user.username}"
path = Path.join(OneWiki.pages_path(), page.id)
with :ok <- File.rm(path),
{:ok, _} <- Git.commit(repo, ["-am", message]) do
{:ok, path}
else
{:error, error} -> {:error, error}
other -> {:error, other}
end
end

def validate_permission(changeset, _user) do
defp validate_permission(changeset, _user) do
changeset
end

Expand Down
46 changes: 29 additions & 17 deletions plugins/one_wiki/lib/one_wiki_web/channels/wiki_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,55 +103,55 @@ defmodule OneWikiWeb.WikiChannel do
end

def create_page(socket, %{"form" => form} = _sender) do
# Logger.warn "sender: " <> inspect(sender)
# Logger.warn "assigns: " <> inspect(socket.assigns)
# sidenav(socket)
user = get_user(socket)

resource_params = Helpers.normalize_params(form)["wiki"]
# |> IO.inspect(label: "resource_params")

case Page.create(user, resource_params) do
{:ok, page} ->
Client.toastr(socket, :success, ~g(Page created successfully!))
OnePubSub.broadcast("wiki:all", "create:page", %{page: page})
render_page(page, socket)
{:error, changeset} ->
errors = SharedView.format_errors(changeset)
Client.toastr(socket, :error, gettext("Problem creating page: %{errors}", errors: errors))
page_action_error(socket, changeset, :creating)
end
end

def edit_page(socket, sender) do
# Logger.warn "sender: " <> inspect(sender)
# Logger.warn "assigns: " <> inspect(socket.assigns)
# user = Accounts.get_user socket.assigns.user_id, default_preload: true

page = Page.get(sender["dataset"]["id"])

# html = Phoenix.View.render_to_string(PageView, "edit.html", title: page.title, body: page.body, id: page.id)
# Rebel.Query.update(socket, :html, set: html, on: ".main-content")
render_to_string({
[title: page.title, body: page.body, id: page.id],
get_user(socket), page, "edit.html", socket})
end

def update_page(socket, %{"form" => form} = _sender) do
# Logger.warn "sender: " <> inspect(sender)
# Logger.warn "assigns: " <> inspect(socket.assigns)

resource_params = (Helpers.normalize_params(form)["wiki"])
case Page.update(get_user(socket), Page.get(form["id"]), resource_params) do
{:ok, page} ->
Client.toastr(socket, :success, ~g(Page updated successfully.))
OnePubSub.broadcast("wiki:all", "update:page", %{page: page})
render_page(page, socket)
{:error, changeset} ->
errors = SharedView.format_errors(changeset)
Client.toastr(socket, :error, gettext("Problem updating page: %{errors}", errors: errors))
page_action_error(socket, changeset, :updating)
end
end

def delete_page(socket, sender) do
id = sender["dataset"]["id"]
user = get_user(socket)
Logger.warn "delete_page #{id}"
case Page.delete(user, id) do
{:ok, page} ->
Client.toastr(socket, :success, ~g(Page deleted successfully.))
OnePubSub.broadcast("wiki:all", "page:delete", %{page: page})
socket |> update_users_page_list() |> close_sidenav()
{:error, changeset} ->
page_action_error(socket, changeset, :deleting)
end

end

def cancel_edit(socket, %{"form" => form} = sender) do
# Logger.warn "sender: " <> inspect(sender)
# Logger.warn "assigns: " <> inspect(socket.assigns)
Expand Down Expand Up @@ -376,4 +376,16 @@ defmodule OneWikiWeb.WikiChannel do
|> Rebel.Core.async_js(~s/$('h3.add-room.wiki .room-count-small').text(#{count})/)
end

defp page_action_error(socket, changeset, action) do
errors =
case changeset do
%Ecto.Changeset{} = changeset ->
SharedView.format_errors(changeset)
string when is_binary(string) ->
string
end
Client.toastr(socket, :error, gettext("Problem %{action} page: %{errors}",
errors: errors, action: to_string(action)))
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ section.page-container.page-home.page-static
.submit
button.button.primary.save rebel-channel="wiki" rebel-click="edit_page" data-id="#{@id}"
span = ~g(Edit Page)
button.button.danger.delete rebel-channel="wiki" rebel-click="delete_page" data-id="#{@id}"
span = ~g(Delete Page)

.content.wiki.markdown-body.message
= raw @body

0 comments on commit f92ef7f

Please sign in to comment.