Skip to content

Commit

Permalink
Report when anonymization occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 25, 2024
1 parent da7e602 commit 0e8151a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
14 changes: 10 additions & 4 deletions src/api/api_comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,21 @@ private function run_post(Qrequest $qreq, $rrd, $crow) {
$this->ms->append_item(self::save_success_message($xcrow));

$aunames = $mentions = [];
$mentions_missing = false;
$mentions_missing = $mentions_censored = false;
foreach ($xcrow->notifications ?? [] as $n) {
if (($n->types & NotificationInfo::CONTACT) !== 0 && $n->sent) {
if ($n->has(NotificationInfo::CONTACT | NotificationInfo::SENT)) {
$aunames[] = $n->user->name_h(NAME_EB);
}
if (($n->types & NotificationInfo::MENTION) !== 0) {
if ($n->sent) {
if ($n->has(NotificationInfo::MENTION)) {
if ($n->sent()) {
$mentions[] = $n->user_html ?? $suser->reviewer_html_for($n->user);
} else if ($xcrow->timeNotified === $xcrow->timeModified) {
$mentions_missing = true;
}
}
if ($n->has(NotificationInfo::CENSORED)) {
$mentions_censored = true;
}
}
if ($aunames && !$this->prow->has_author($suser)) {
if ($this->user->allow_view_authors($this->prow)) {
Expand All @@ -181,6 +184,9 @@ private function run_post(Qrequest $qreq, $rrd, $crow) {
if ($mentions_missing) {
$this->ms->msg_at(null, $this->conf->_("<0>Some mentioned users cannot currently see this comment, so they were not notified."), MessageSet::WARNING_NOTE);
}
if ($mentions_censored) {
$this->ms->msg_at(null, $this->conf->_("<0>Some notifications were censored to anonymize mentioned users."), MessageSet::WARNING_NOTE);
}
return $xcrow;
}

Expand Down
27 changes: 16 additions & 11 deletions src/commentinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class CommentInfo {
public $message_list;
/** @var ?Contact */
private $_commenter;
/** @var ?bool */
private $_recently_censored;

const CT_DRAFT = 0x01;
const CT_BLIND = 0x02;
Expand Down Expand Up @@ -319,12 +321,6 @@ function mtime(Contact $viewer) {
}
}

/** @return string
* @deprecated */
function raw_contents() {
return $this->raw_content();
}

/** @return string */
function raw_content() {
return $this->commentOverflow ?? $this->comment ?? "";
Expand Down Expand Up @@ -394,6 +390,7 @@ function content($viewer = null, $censor_until = null) {
$r = $this->prow->unparse_pseudonym($viewer, $mn[0]) ?? "Anonymous";
$t = substr_replace($t, "@{$r}", $mn[1] + $delta, $mn[2] - $mn[1]);
$delta += strlen($r) - ($mn[2] - $mn[1] - 1);
$this->_recently_censored = true;
}
}
return $t;
Expand Down Expand Up @@ -1170,7 +1167,7 @@ function save_comment($req, Contact $acting_user) {
private function notification($user, $types) {
foreach ($this->notifications as $n) {
if ($n->user->contactId === $user->contactId) {
$n->types |= $types;
$n->flags |= $types;
return $n;
}
}
Expand Down Expand Up @@ -1213,20 +1210,24 @@ private function inform_mentions($mentions) {
continue;
}
$notification = $this->notification($mentionee, NotificationInfo::MENTION);
if ($notification->sent
if ($notification->sent()
|| $mentionee->is_dormant()
|| !$mentionee->can_view_comment($this->prow, $this)) {
continue;
}
$this->_recently_censored = false;
HotCRPMailer::send_to($mentionee, "@mentionnotify", [
"prow" => $this->prow,
"comment_row" => $this
]);
$notification->sent = true;
if (!$mxm[3]) {
$n = substr($this->raw_content(), $mxm[1] + 1, $mxm[2] - $mxm[1] - 1);
$notification->user_html = htmlspecialchars($n);
}
$notification->flags |= NotificationInfo::SENT;
if ($this->_recently_censored) {
$notification->flags |= NotificationInfo::CENSORED;
}
}
}

Expand Down Expand Up @@ -1286,10 +1287,11 @@ function notify($user) {
}
$is_author = $this->prow->has_author($minic);
$notification = $this->notification($minic, $is_author ? NotificationInfo::CONTACT : NotificationInfo::FOLLOW);
if ($notification->sent) {
if ($notification->sent()) {
continue;
}
// prepare mail
$this->_recently_censored = false;
$p = HotCRPMailer::prepare_to($minic, $tmpl, $info);
if (!$p) {
continue;
Expand All @@ -1302,7 +1304,10 @@ function notify($user) {
$p->unique_preparation = true;
}
$preps[] = $p;
$notification->sent = true;
$notification->flags |= NotificationInfo::SENT;
if ($this->_recently_censored) {
$notification->flags |= NotificationInfo::CENSORED;
}
}
HotCRPMailer::send_combined_preparations($preps);
}
Expand Down
23 changes: 17 additions & 6 deletions src/notificationinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@ class NotificationInfo {
/** @var Contact */
public $user;
/** @var int */
public $types = 0;
/** @var bool */
public $sent = false;
public $flags = 0;
/** @var ?string */
public $user_html;

const CONTACT = 1;
const FOLLOW = 2;
const MENTION = 4;
const SENT = 8;
const CENSORED = 16;

/** @param Contact $user
* @param 0|1|2|4 $types */
function __construct($user, $types) {
* @param int $flags */
function __construct($user, $flags) {
$this->user = $user;
$this->types = $types;
$this->flags = $flags;
}

/** @param int $flags
* @return bool */
function has($flags) {
return ($this->flags & $flags) === $flags;
}

/** @return bool */
function sent() {
return ($this->flags & self::SENT) !== 0;
}
}

0 comments on commit 0e8151a

Please sign in to comment.