liquid_feedback_frontend
view app/main/draft/diff.lua @ 1765:8d809c49c48a
Fixed syntax error
| author | bsw | 
|---|---|
| date | Mon Oct 18 16:01:55 2021 +0200 (2021-10-18) | 
| parents | ed3c40911ae1 | 
| children | 
 line source
     1 local old_draft_id = param.get("old_draft_id", atom.integer)
     2 local new_draft_id = param.get("new_draft_id", atom.integer)
     3 local initiative_id = param.get("initiative_id", atom.number)
     5 if not old_draft_id 
     6   or not new_draft_id 
     7   or old_draft_id == new_draft_id
     8 then
     9   slot.reset_all()
    10   slot.select("error", function()
    11     ui.tag{ content = _"Please choose two different versions of the draft to compare" }
    12   end )
    13   request.redirect{
    14     module = "initiative", view = "history", id = initiative_id
    15   }
    16   return
    17 end
    19 if old_draft_id > new_draft_id then
    20   old_draft_id, new_draft_id = new_draft_id, old_draft_id
    21 end
    23 local old_draft = Draft:by_id(old_draft_id)
    24 local new_draft = Draft:by_id(new_draft_id)
    26 local initiative = new_draft.initiative
    28 if app.session.member then
    29   initiative:load_everything_for_member_id(app.session.member_id)
    30   initiative.issue:load_everything_for_member_id(app.session.member_id)
    31 end
    34 local old_draft_content = string.gsub(string.gsub(util.html_to_text(old_draft.content), "\n", " ###ENTER###\n"), " ", "\n")
    35 local new_draft_content = string.gsub(string.gsub(util.html_to_text(new_draft.content), "\n", " ###ENTER###\n"), " ", "\n")
    37 local key = multirand.string(24, "0123456789abcdefghijklmnopqrstuvwxyz")
    39 local old_draft_filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "diff-" .. key .. "-old.tmp")
    40 local new_draft_filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "diff-" .. key .. "-new.tmp")
    42 local old_draft_file = assert(io.open(old_draft_filename, "w"))
    43 old_draft_file:write(old_draft_content)
    44 old_draft_file:write("\n")
    45 old_draft_file:close()
    47 local new_draft_file = assert(io.open(new_draft_filename, "w"))
    48 new_draft_file:write(new_draft_content)
    49 new_draft_file:write("\n")
    50 new_draft_file:close()
    52 local output, err, status = extos.pfilter(nil, "sh", "-c", "diff -a -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep --binary-files=text -v ^--- | grep --binary-files=text -v ^+++ | grep --binary-files=text -v ^@")
    54 os.remove(old_draft_filename)
    55 os.remove(new_draft_filename)
    57 local last_state = "first_run"
    59 local function process_line(line)
    60   local state_char = string.sub(line, 1, 1)
    61   local state
    62   if state_char == "+" then
    63     state = "added"
    64   elseif state_char == "-" then
    65     state = "removed"
    66   elseif state_char == " " then
    67     state = "unchanged"
    68   end
    69   local state_changed = false
    70   if state ~= last_state then
    71     if last_state ~= "first_run" then
    72       slot.put("</span> ")
    73     end
    74     last_state = state
    75     state_changed = true
    76     slot.put("<span class=\"diff_" .. tostring(state) .. "\">")
    77   end
    79   line = string.sub(line, 2, #line)
    80   if line ~= "###ENTER###" then
    81     if not state_changed then
    82       slot.put(" ")
    83     end
    84     --slot.put(encode.html(line))
    85     slot.put(line)
    86   else
    87     slot.put("<br />")
    88   end
    89 end
    91 execute.view{ module = "issue", view = "_head", params = { issue = initiative.issue } }
    93 ui.grid{ content = function()
    94   ui.cell_main{ content = function()
    95     ui.container{ attr = { class = "mdl-card mdl-card__fullwidth mdl-shadow--2dp" }, content = function()
    97       ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function ()
    98         ui.heading { 
    99           attr = { class = "mdl-card__title-text" },
   100           content = function()
   101             ui.link{
   102               module = "initiative", view = "show", id = initiative.id,
   103               content = initiative.display_name
   104             }
   105           end
   106         }
   107         ui.container{ content = _("Comparision of revisions #{id1} and #{id2}", {
   108           id1 = old_draft.id,
   109           id2 = new_draft.id 
   110         } ) }
   111       end }
   113       if app.session.member_id and not new_draft.initiative.revoked then
   114         local supporter = app.session.member:get_reference_selector("supporters")
   115           :add_where{ "initiative_id = ?", new_draft.initiative_id }
   116           :optional_object_mode()
   117           :exec()
   118         if supporter and supporter.draft_id == old_draft.id and new_draft.id == initiative.current_draft.id then
   119           ui.container {
   120             attr = { class = "mdl-card__content mdl-card--no-bottom-pad mdl-card--notice" },
   121             content = _"The draft of this initiative has been updated!"
   122           }
   123           ui.container {
   124             attr = { class = "mdl-card__actions mdl-card--action-border  mdl-card--notice" },
   125             content = function ()
   126               ui.link{
   127                 attr = { class = "mdl-button mdl-js-button mdl-button--raised" },
   128                 text   = _"refresh my support",
   129                 module = "initiative",
   130                 action = "add_support",
   131                 id     = new_draft.initiative.id,
   132                 params = { draft_id = new_draft.id },
   133                 routing = {
   134                   default = {
   135                     mode = "redirect",
   136                     module = "initiative",
   137                     view = "show",
   138                     id = new_draft.initiative.id
   139                   }
   140                 }
   141               }
   143               slot.put("   ")
   145               ui.link{
   146                 attr = { class = "mdl-button mdl-js-button mdl-button--raised" },
   147                 text   = _"remove my support",
   148                 module = "initiative",
   149                 action = "remove_support",
   150                 id     = new_draft.initiative.id,
   151                 routing = {
   152                   default = {
   153                     mode = "redirect",
   154                     module = "initiative",
   155                     view = "show",
   156                     id = new_draft.initiative.id
   157                   }
   158                 }
   159               }        
   161               slot.put("   ")
   163               ui.link{
   164                 attr = { class = "mdl-button mdl-js-button" },
   165                 text   = _"cancel",
   166                 module = "initiative",
   167                 view   = "show",
   168                 id     = new_draft.initiative.id,
   169               }        
   170             end
   171           }
   172         end
   173       end
   175       ui.container {
   176         attr = { class = "draft mdl-card__content mdl-card--border" },
   177         content = function ()
   178           if not status then
   179             ui.field.text{ value = _"The drafts do not differ" }
   180           else
   181             ui.container{
   182               tag = "div",
   183               attr = { class = "diff" },
   184               content = function()
   185                 output = output:gsub("[^\n\r]+", function(line)
   186                   process_line(line)
   187                 end)
   188               end
   189             }
   190           end 
   191         end
   192       }
   194       local old_files = File:new_selector()
   195         :left_join("draft_attachment", nil, "draft_attachment.file_id = file.id")
   196         :add_where{ "draft_attachment.draft_id = ?", old_draft.id }
   197         :reset_fields()
   198         :add_field("file.id")
   199         :add_field("draft_attachment.title")
   200         :add_field("draft_attachment.description")
   201         :add_order_by("draft_attachment.id")
   202         :exec()
   204       local new_files = File:new_selector()
   205         :left_join("draft_attachment", nil, "draft_attachment.file_id = file.id")
   206         :add_where{ "draft_attachment.draft_id = ?", new_draft.id }
   207         :reset_fields()
   208         :add_field("file.id")
   209         :add_field("draft_attachment.title")
   210         :add_field("draft_attachment.description")
   211         :add_order_by("draft_attachment.id")
   212         :exec()
   214       local added_files = {}
   215       for i, new_file in ipairs(new_files) do
   216         local added = true
   217         for j, old_file in ipairs(old_files) do
   218           if 
   219             old_file.file_id == new_file.file_id 
   220             and old_file.title == new_file.title
   221             and old_file.description == new_file.description
   222           then
   223             added = false
   224           end
   225         end
   226         if added then
   227           table.insert(added_files, new_file)
   228         end
   229       end
   231       if #added_files > 0 then
   232         ui.container {
   233           attr = { class = "mdl-card__content mdl-card--border" },
   234           content = function()
   235             ui.container{ content = _"Added attachments" }
   236             for i, file in ipairs(added_files) do
   237               ui.image{ module = "file", view = "show.jpg", id = file.id, params = { preview = true } }
   238               ui.container{ content = file.title or "" }
   239               ui.container{ content = file.description or "" }
   240               slot.put("<br /><br />")
   241             end
   242           end
   243         }
   244       end
   246       local removed_files = {}      
   247       for i, old_file in ipairs(old_files) do
   248         local removed = true
   249         for j, new_file in ipairs(new_files) do
   250           if 
   251             old_file.file_id == new_file.file_id 
   252             and old_file.title == new_file.title
   253             and old_file.description == new_file.description
   254           then
   255             removed = false
   256           end
   257         end
   258         if removed then
   259           table.insert(removed_files, old_file)
   260         end
   261       end
   264       if #removed_files > 0 then
   265         ui.container {
   266           attr = { class = "mdl-card__content mdl-card--border" },
   267           content = function()
   268             ui.container{ content = _"Removed attachments" }
   269             for i, file in ipairs(removed_files) do
   270               ui.image{ module = "file", view = "show.jpg", id = file.id, params = { preview = true } }
   271               ui.container{ content = file.title or "" }
   272               ui.container{ content = file.description or "" }
   273               slot.put("<br /><br />")
   274             end
   275           end
   276         }
   277       end
   279       ui.container {
   280         attr = { class = "draft mdl-card__content mdl-card--border" },
   281         content = function ()
   282         end
   283       }
   285     end }
   286   end }
   287   ui.cell_sidebar{ content = function()
   289     execute.view{ module = "issue", view = "_sidebar", params = {
   290       initiative = initiative,
   291       issue = initiative.issue
   292     } }
   294     execute.view {
   295       module = "issue", view = "_sidebar_whatcanido",
   296       params = { initiative = initiative }
   297     }
   299     execute.view { 
   300       module = "issue", view = "_sidebar_members", params = {
   301         issue = initiative.issue, initiative = initiative
   302       }
   303     }
   304   end }
   305 end }
