Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(torrent/tags): Add tags support of torrent upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Jul 19, 2019
1 parent 675fd0c commit e198704
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
### Fix
- **Cookies:** Fix session sep from `%` to `_`
- **Database:** Fix table `links` miss
- **Env:** Exit when parse env file failed
- **Form:** Miss use flag since namespace change
- **Form:** Link\EditForm Update diff
- **Requests:** Fix fullUrl() may add unnecessary `?`
Expand Down
10 changes: 10 additions & 0 deletions apps/controllers/ManageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ public function actionCategories()

return $this->render('manage/categories', ['parent_id' => $parent_id, 'parent_category' => $parent_category, 'categories' => $categories]);
}

public function actionQualities()
{
// TODO
}

public function actionTags()
{
// TODO
}
}
68 changes: 66 additions & 2 deletions apps/models/form/TorrentUploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ class TorrentUploadForm extends Validator

public $category;
public $title;
public $subtitle = "";
public $subtitle = '';
public $descr;
public $uplver = "no"; // If user upload this torrent Anonymous
public $uplver = 'no'; // If user upload this torrent Anonymous
public $hr = 'no'; // TODO This torrent require hr check

// Quality
public $audio = 0; /* 0 is default value. */
public $codec = 0;
public $medium = 0;
public $resolution = 0;

public $tags;

private $info_hash; // the value of sha1($this->$torrent_dict['info'])

private $status = 'confirmed';
Expand Down Expand Up @@ -80,6 +83,16 @@ public static function ruleQuality($quality)
return $quality_table;
}

public static function rulePinnedTags()
{
$tags = app()->redis->get('site:pinned_tags');
if (false === $tags) {
$tags = app()->pdo->createCommand('SELECT * FROM `tags` WHERE `pinned` = 1')->queryAll();
app()->redis->set('site:pinned_tags', $tags, 86400);
}
return $tags;
}

// 规则
public static function inputRules()
{
Expand Down Expand Up @@ -218,6 +231,7 @@ public function makePrivateTorrent()
public function flush()
{
$this->makePrivateTorrent();
$this->rewriteFlags();

// Check if this torrent is exist or not before insert.
$count = app()->pdo->createCommand('SELECT COUNT(*) FROM torrents WHERE info_hash = :info_hash')->bindParams([
Expand Down Expand Up @@ -250,6 +264,7 @@ public function flush()
])->execute();
$this->id = app()->pdo->getLastInsertId();

$this->insertTags();
$this->setBuff();

// Save this torrent
Expand All @@ -271,6 +286,55 @@ public function flush()
}
}

// TODO Check and rewrite torrent flags if user don't reach flags privilege
private function rewriteFlags()
{

}

private function insertTags()
{
$tags = str_replace(',', ' ', $this->tags);
$tags_list = explode(' ', $tags);

// Get and cache the exist tags
if (!app()->redis->exists('site:torrents_all_tags')) {
$exist_tags = app()->pdo->createCommand('SELECT id,tag FROM tags')->queryAll();
foreach ($exist_tags as $exist_tag) {
app()->redis->zAdd('site:torrents_all_tags', $exist_tag['id'], $exist_tag['tag']);
}
app()->redis->expire('site:torrents_all_tags', 43200);
}

$tag_id_list = [];
for ($i = 0; $i < min(count($tags_list), 10); $i++) {
$tag = trim($tags_list[$i]);
if (strlen($tag) > 0) {
$tag_id = app()->redis->zScore('site:torrents_all_tags', $tag); // check if it is exist tag in cache
if ($tag_id == 0) { // un-exist tag
// TODO Check if allow user to add un-exist tag
try { // insert tag to database and cache
app()->pdo->createCommand('INSERT INTO `tags`(`tag`) VALUES (:tag)')->bindParams([
'tag' => $tag
])->execute();
$tag_id = app()->pdo->getLastInsertId();
app()->redis->zAdd('site:torrents_all_tags', $tag_id, $tag);
$tag_id_list[] = ['tag_id' => $tag_id, 'torrent_id' => $this->id];
} catch (\Exception $e) {
continue;
}
} else {
$tag_id_list[] = ['tag_id' => $tag_id, 'torrent_id' => $this->id];
}
}
}

// batchInsert into map
if (count($tag_id_list) > 0) {
app()->pdo->batchInsert('map_torrents_tags', $tag_id_list)->execute();
}
}

private function setBuff()
{
// Add Large Buff and Random Buff
Expand Down
17 changes: 17 additions & 0 deletions apps/public/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,21 @@ jQuery(document).ready(function () {
}
});
}

// Help to insert tags in `/torrent/upload`
if ($('div.tag-help-block').length) {
let tags_input = $('input[name="tags"]');
$('a.add-tag').click(function (e) {
e.preventDefault();
let add_tag = $(this).text();
let exist_tag_value = tags_input.val();
let exist_tag_set = new Set(exist_tag_value.split(' '));
if (exist_tag_set.has(add_tag)) {
exist_tag_set.delete(add_tag);
} else {
exist_tag_set.add(add_tag);
}
tags_input.val(Array.from(exist_tag_set).join(' '));
})
}
});
41 changes: 27 additions & 14 deletions apps/views/torrent/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div class="row">
<div class="col-md-3">
<select id="category" name="category" class="form-control">
<option value="0" selected>Please choose one Category</option>
<option value="0" selected>[Select a category]</option>
<?php foreach (TorrentUploadForm::ruleCategory() as $category) : ?>
<option value="<?= $category['id'] ?>"><?= $category['full_path'] ?></option>
<?php endforeach; ?>
Expand All @@ -52,14 +52,14 @@
<td><input id="title" name="title" class="form-control" type="text"
placeholder="The main title of Your upload torrent"
required="required">
<small>You should obey our upload rules. **LINK**</small>
<div class="help-block">You should obey our upload rules. **LINK**</div>
</td> <!-- FIXME link url -->
</tr>
<tr>
<td class="nowrap"><label for="subtitle">Sub Title</label></td>
<td><input id="subtitle" name="subtitle" class="form-control" type="text"
placeholder="The subtitle of Your upload torrent">
<small>You should obey our upload rules. **LINK**</small>
<div class="help-block">You should obey our upload rules. **LINK**</div>
</td> <!-- FIXME link url -->
</tr>
<tr>
Expand All @@ -83,7 +83,6 @@
</div>
<?php endforeach; ?>
</div>

</td> <!-- FIXME link url -->
</tr>
<tr>
Expand All @@ -94,18 +93,32 @@
</td>
</tr>
<tr>
<td class="nowrap"><label for="descr">Other</label></td>
<td>
<div class="checkbox-primary">
<input type="checkbox" id="uplver" name="uplver" value="yes"
<?= app()->user->getClass(true) > config('authority.upload_anonymous') ? '' : ' disabled' ?>
><label for="uplver">Anonymous Upload</label>
<td class="nowrap"><label for="tags">Tags</label></td>
<td><input id="tags" name="tags" class="form-control" type="text">
<div class="tag-help-block" style="margin-top: 4px">
Pinned Tags:
<?php foreach (TorrentUploadForm::rulePinnedTags() as $tag): ?>
<a href="javascript:" class="add-tag label label-outline <?= $tag['class_name'] ?>"><?= $tag['tag'] ?></a>
<?php endforeach; ?>
</div>
<div class="checkbox-primary">
<input type="checkbox" id="hr" name="hr" value="yes"
<?= app()->user->getClass(true) > config('authority.upload_anonymous') ? '' : ' disabled' // FIXME ?>
><label for="hr">H&R</label>
</td> <!-- FIXME link url -->
</tr>
<tr>
<td class="nowrap"><label for="descr">Flags</label></td>
<td>
<div class="row">
<div class="col-md-3">
<div class="switch<?= app()->user->getClass(true) > config('authority.upload_anonymous') ? '' : ' disabled' ?>">
<input type="checkbox" id="uplver" name="uplver" value="yes"><label for="uplver">Anonymous Upload</label>
</div>
</div>
<div class="col-md-3">
<div class="switch<?= app()->user->getClass(true) > config('authority.upload_anonymous') ? '' : ' disabled' // FIXME Config key ?>">
<input type="checkbox" id="hr" name="hr" value="yes"><label for="hr">H&R</label>
</div>
</div>
</div>

</td>
</tr>
</tbody>
Expand Down
70 changes: 69 additions & 1 deletion migration/ridpt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jul 18, 2019 at 10:16 PM
-- Generation Time: Jul 19, 2019 at 05:34 PM
-- Server version: 8.0.16
-- PHP Version: 7.3.7

Expand Down Expand Up @@ -261,6 +261,30 @@ CREATE TABLE IF NOT EXISTS `links` (

-- --------------------------------------------------------

--
-- Table structure for table `map_torrents_tags`
--

DROP TABLE IF EXISTS `map_torrents_tags`;
CREATE TABLE IF NOT EXISTS `map_torrents_tags` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`torrent_id` int(10) UNSIGNED NOT NULL,
`tag_id` int(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_map_tt_to_torrents` (`torrent_id`),
KEY `FK_map_tt_to_tags` (`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

--
-- RELATIONSHIPS FOR TABLE `map_torrents_tags`:
-- `tag_id`
-- `tags` -> `id`
-- `torrent_id`
-- `torrents` -> `id`
--

-- --------------------------------------------------------

--
-- Table structure for table `messages`
--
Expand Down Expand Up @@ -722,6 +746,43 @@ CREATE TABLE IF NOT EXISTS `snatched` (

-- --------------------------------------------------------

--
-- Table structure for table `tags`
--

DROP TABLE IF EXISTS `tags`;
CREATE TABLE IF NOT EXISTS `tags` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`tag` varchar(64) NOT NULL DEFAULT '',
`class_name` varchar(64) NOT NULL DEFAULT '',
`pinned` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `tag` (`tag`),
KEY `pinned` (`pinned`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

--
-- RELATIONSHIPS FOR TABLE `tags`:
--

--
-- Truncate table before insert `tags`
--

TRUNCATE TABLE `tags`;
--
-- Dumping data for table `tags`
--

INSERT INTO `tags` (`id`, `tag`, `class_name`, `pinned`) VALUES
(1, 'Internal', 'label-primary', 1),
(2, 'DIY', 'label-primary', 1),
(3, 'Premiere', 'label-primary', 1),
(4, 'Exclusive', 'label-primary', 1),
(5, 'Request', 'label-primary', 1);

-- --------------------------------------------------------

--
-- Table structure for table `torrents`
--
Expand Down Expand Up @@ -1010,6 +1071,13 @@ ALTER TABLE `invite`
ALTER TABLE `ip_bans`
ADD CONSTRAINT `FK_ip_ban_operator` FOREIGN KEY (`add_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `map_torrents_tags`
--
ALTER TABLE `map_torrents_tags`
ADD CONSTRAINT `FK_map_tt_to_tags` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_map_tt_to_torrents` FOREIGN KEY (`torrent_id`) REFERENCES `torrents` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `news`
--
Expand Down

0 comments on commit e198704

Please sign in to comment.