Skip to content

Commit

Permalink
Comment attachment editing nits.
Browse files Browse the repository at this point in the history
- Allow attaching on first edit.
- Saving an empty comment != deleting.
  • Loading branch information
kohler committed Aug 22, 2018
1 parent 0b2d5c8 commit e36722c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 39 deletions.
4 changes: 2 additions & 2 deletions comment.php
Expand Up @@ -68,7 +68,7 @@ function save_comment($qreq, $text, $is_response, $roundnum) {
"text" => $text,
"tags" => $qreq->commenttags,
"blind" => $qreq->blind,
"docs" => $crow->attachments()];
"docs" => $crow ? $crow->attachments() : []];
$ok = true;
$msg = false;

Expand Down Expand Up @@ -181,7 +181,7 @@ function handle_response($qreq) {
if (($whyNot = $Me->perm_submit_comment($prow, $crow)))
Conf::msg_error(whyNotText($whyNot));
else
save_comment($Qreq, "", ($crow->commentType & COMMENTTYPE_RESPONSE) != 0, $crow->commentRound);
save_comment($Qreq, false, ($crow->commentType & COMMENTTYPE_RESPONSE) != 0, $crow->commentRound);
if ($Qreq->ajax)
json_exit(["ok" => false]);
} else if ($Qreq->cancel && $crow)
Expand Down
3 changes: 1 addition & 2 deletions doc.php
Expand Up @@ -224,7 +224,6 @@ function document_download($qreq) {
$doc_crow = $cmtid = null;
if ($dr->linkid[0] === "x" || $dr->linkid[0] === "X")
$cmtid = (int) substr($dr->linkid, 1);
error_log("got cmtid $dr->linkid");
foreach ($prow->viewable_comment_skeletons($Me) as $crow)
if ($crow->unparse_html_id() === $dr->linkid
|| $crow->commentId === $cmtid) {
Expand All @@ -233,7 +232,7 @@ function document_download($qreq) {
}
if (!$doc_crow)
document_error("404 Not Found", "No such document “" . htmlspecialchars($dr->req_filename) . "”.");
foreach ($prow->comment_linked_documents($doc_crow) as $xdoc)
foreach ($doc_crow->attachments() as $xdoc)
if ($xdoc->unique_filename === $dr->attachment) {
$request_docid = $want_docid = $xdoc->paperStorageId;
break;
Expand Down
6 changes: 3 additions & 3 deletions scripts/script.js
Expand Up @@ -3412,9 +3412,9 @@ function activate_editing($c, cj) {
if (cj.response && resp_rounds[cj.response].words > 0)
make_update_words($c, resp_rounds[cj.response].words);

$c.find("form").on("submit", submit_editor);
$c.on("click", "button", buttonclick_editor);
hiliter_children($c);
var $f = $c.find("form");
$f.on("submit", submit_editor).on("click", "button", buttonclick_editor);
hiliter_children($f);
$c.find(".need-tooltip").each(tooltip);
}

Expand Down
56 changes: 29 additions & 27 deletions src/commentinfo.php
Expand Up @@ -245,8 +245,7 @@ function has_tag($tag) {

function attachments() {
return $this->commentType & COMMENTTYPE_HASDOC
? $this->prow->comment_linked_documents($this)
: [];
? $this->prow->linked_documents($this->commentId, 0, 1024) : [];
}

function unparse_json(Contact $contact) {
Expand Down Expand Up @@ -456,10 +455,10 @@ function save($req, Contact $contact) {
&& !$contact->act_author_view($this->prow)) {
$tagger = new Tagger($contact);
$ctags = array();
foreach ($m[0] as $text)
if (($text = $tagger->check($text, Tagger::NOVALUE))
&& !stri_ends_with($text, "response"))
$ctags[strtolower($text)] = $text;
foreach ($m[0] as $tt)
if (($tt = $tagger->check($tt, Tagger::NOVALUE))
&& !stri_ends_with($tt, "response"))
$ctags[strtolower($tt)] = $tt;
$tagger->sort($ctags);
$ctags = count($ctags) ? " " . join(" ", $ctags) . " " : null;
} else
Expand All @@ -478,13 +477,15 @@ function save($req, Contact $contact) {
$displayed = !($ctype & COMMENTTYPE_DRAFT);

// query
$text = get_s($req, "text");
if (($text = get($req, "text")) !== false)
$text = (string) $text;
$q = "";
$qv = array();
if ($text === "" && $this->commentId) {
if ($text === false && $this->commentId) {
$change = true;
$q = "delete from $Table where commentId=$this->commentId";
} else if ($text === "")
$docids = [];
} else if ($text === false)
/* do nothing */;
else if (!$this->commentId) {
$change = true;
Expand Down Expand Up @@ -542,34 +543,20 @@ function save($req, Contact $contact) {
$result = $this->conf->qe_apply($q, $qv);
if (!$result)
return false;

$cmtid = $this->commentId ? : $result->insert_id;
if (!$cmtid)
return false;

// document links
if ($docids !== $old_docids) {
if ($old_docids)
$this->conf->qe("delete from DocumentLink where paperId=? and linkId=? and linkType>=? and linkType<?", $this->prow->paperId, $this->commentId, 0, 1024);
if ($docids) {
$qv = [];
foreach ($docids as $i => $did)
$qv[] = [$this->prow->paperId, $this->commentId, $i, $did];
$this->conf->qe("insert into DocumentLink (paperId,linkId,linkType,documentId) values ?v", $qv);
}
if ($old_docids)
$this->prow->mark_inactive_linked_documents();
$this->prow->invalidate_linked_documents();
}

// log
$contact->log_activity("Comment $cmtid " . ($text !== "" ? "saved" : "deleted"), $this->prow->$LinkColumn);
$contact->log_activity("Comment $cmtid " . ($text !== false ? "saved" : "deleted"), $this->prow->$LinkColumn);

// ordinal
if ($text !== "" && $this->ordinal_missing($ctype))
if ($text !== false && $this->ordinal_missing($ctype))
$this->save_ordinal($cmtid, $ctype, $Table, $LinkTable, $LinkColumn);

// reload
if ($text !== "") {
if ($text !== false) {
$comments = $this->prow->fetch_comments("commentId=$cmtid");
$this->merge($comments[$cmtid], $this->prow);
if ($this->timeNotified == $this->timeModified)
Expand All @@ -580,6 +567,21 @@ function save($req, Contact $contact) {
$this->commentTags = null;
}

// document links
if ($docids !== $old_docids) {
if ($old_docids)
$this->conf->qe("delete from DocumentLink where paperId=? and linkId=? and linkType>=? and linkType<?", $this->prow->paperId, $this->commentId, 0, 1024);
if ($docids) {
$qv = [];
foreach ($docids as $i => $did)
$qv[] = [$this->prow->paperId, $this->commentId, $i, $did];
$this->conf->qe("insert into DocumentLink (paperId,linkId,linkType,documentId) values ?v", $qv);
}
if ($old_docids)
$this->prow->mark_inactive_linked_documents();
$this->prow->invalidate_linked_documents();
}

return true;
}

Expand Down
7 changes: 2 additions & 5 deletions src/paperinfo.php
Expand Up @@ -1185,18 +1185,15 @@ private function doclink_array() {
}
return $this->_doclink_array;
}
private function linked_documents($linkid, $min) {
function linked_documents($linkid, $min, $max) {
$docs = [];
foreach (get($this->doclink_array(), $linkid, []) as $lt => $docid)
if ($lt >= $min && $lt < $min + 1024)
if ($lt >= $min && $lt < $max)
$docs[] = $this->document(-2, $docid);
if (!empty($docs))
DocumentInfo::assign_unique_filenames($docs);
return $docs;
}
function comment_linked_documents(CommentInfo $cinfo) {
return $this->linked_documents($cinfo->commentId, 0);
}
function invalidate_linked_documents() {
$this->_doclink_array = null;
}
Expand Down

0 comments on commit e36722c

Please sign in to comment.