Notifications
Clear all

Bug in UserFileActivity.php  

(@shiltin)
Active Member Registered

public static function Load ($idFile = 0, $idAccount = 0, $returnArray = false) {
$where = "Likes > 0 "; ///fixed this line, otherwise if you register a view first you can vote multiple times
ReplyQuote
Topic starter Posted : 14/07/2020 9:21 pm
(@skaredcreations)
Honorable Member Admin

As I see in the action "like" of user_files webservice it's checking if the existing UserFileActivity has "Likes > 0" and eventually sends back the error ERROR_USERFILE_VOTED, apparently it should already prevent multiple likes by the same user. I will test it better to see if there's a bug in the code, but the change you're suggesting is not logically correct.

EDIT: ok I saw the bug, I probably need to fix this in UserFile::AddLike and add a check if an activity is already existing instead of just adding a new activity, just like it does in UserFile::AddView. It will be fixed in the incoming update during this month.

This should be the correct code in UserFile::AddLike (inside the statement "if ($Database->Query($query)) {"), if you want to test it:

// Add an activity log of the logged user
if ($LoggedAccount->IsLogged()) {
    $activities = UserFileActivity::Load($this->Id, $LoggedAccount->Id);
    if (count($activities) == 0) {
        $activity = new UserFileActivity();
        $activity->IdFile = $this->Id;
        $activity->IdAccount = $LoggedAccount->Id;
    } else {
        $activity = $activities[0];
    }
    $activity->Likes = 1;
    $activity->Save();
}

FRANCESCO CROCETTI @ SKARED CREATIONS

ReplyQuote
Posted : 15/07/2020 9:24 am
(@shiltin)
Active Member Registered

@skaredcreations The new code didn't fix it for me.  If I register a view on the file first, I can vote on the file multiple times.

ReplyQuote
Topic starter Posted : 15/07/2020 4:25 pm
(@skaredcreations)
Honorable Member Admin

Oh may be that it's still increasing the Likes count in the UserFile record, but you have only one record in UserActivityLog for the user, right? Then I have to move the check for UserFileActivity::Load before the UPDATE of the UserFile record.

Let's try this code else I'll get deeper into this:

public function AddLike () {
        global $LoggedAccount$Database;
        if ($this->Id > 0) {
            $activity = NULL;
            if ($LoggedAccount->IsLogged()) {
                $activities = UserFileActivity::Load($this->Id$LoggedAccount->Id);
                if (count($activities) > 0) {
                    $activity = $activities[0];
                    if ($activity->Likes > 0) {
                        return FALSE;
                    }
                }
            }
            // Try to save the new count
            $this->Likes++;
            $query = sprintf("UPDATE %s SET Likes = %d WHERE Id = %d",
                    self::GetTableName(__CLASS__),
                    $this->Likes,
                    $this->Id);
            if ($Database->Query($query)) {
                // Add an activity log of the logged user
                if ($LoggedAccount->IsLogged()) {
                    if (is_null($activity)) {
                        $activity = new UserFileActivity();
                        $activity->IdFile = $this->Id;
                        $activity->IdAccount = $LoggedAccount->Id;
                    }
                    $activity->Likes = 1;
                    $activity->Save();
                }
                return TRUE;
            }
            // If the SQL statement failed then bring back the old count
            $this->Likes--;
        }
        return FALSE;
    }

FRANCESCO CROCETTI @ SKARED CREATIONS

ReplyQuote
Posted : 15/07/2020 4:32 pm
(@shiltin)
Active Member Registered

@skaredcreations That fixed it, thanks

ReplyQuote
Topic starter Posted : 15/07/2020 5:53 pm
Share: