Skip to content

Commit

Permalink
Fix calculation of visitor's messages for old threads
Browse files Browse the repository at this point in the history
  • Loading branch information
faf committed Nov 2, 2021
1 parent 57eacfb commit 1a137bb
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/mibew/libs/classes/Mibew/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ public static function closeOldThreads()
$thread->modified = $now;
$thread->closed = $now;
$thread->state = self::STATE_CLOSED;
$thread->messageCount = $thread->getMessageCount();
$thread->save(false);
unset($thread);
}
Expand Down Expand Up @@ -955,27 +956,13 @@ public function close($is_user)
}
}

// Get messages count
$db = Database::getInstance();

list($message_count) = $db->query(
("SELECT COUNT(*) FROM {message} "
. "WHERE {message}.threadid = :threadid AND ikind = :kind_user"),
array(
':threadid' => $this->id,
':kind_user' => Thread::KIND_USER,
),
array(
'return_rows' => Database::RETURN_ONE_ROW,
'fetch_type' => Database::FETCH_NUM,
)
);

// Close thread if it's not already closed
if ($this->state != self::STATE_CLOSED) {
$this->state = self::STATE_CLOSED;
$this->closed = time();
$this->messageCount = $message_count;
$this->messageCount = $this->getMessageCount();
$this->save();

$args = array('thread' => $this);
Expand Down Expand Up @@ -1091,6 +1078,32 @@ public function renameUser($new_name)
}
}

/**
* Get actual count of visitor's messages
*
* @return int Count
*/
public function getMessageCount()
{
// Get messages count
$db = Database::getInstance();

list($message_count) = $db->query(
("SELECT COUNT(*) FROM {message} "
. "WHERE {message}.threadid = :threadid AND ikind = :kind_user"),
array(
':threadid' => $this->id,
':kind_user' => Thread::KIND_USER,
),
array(
'return_rows' => Database::RETURN_ONE_ROW,
'fetch_type' => Database::FETCH_NUM,
)
);

return $message_count;
}

/**
* Sets thread's fields according to the fields from Database.
*
Expand Down

0 comments on commit 1a137bb

Please sign in to comment.