|
|
|
@ -14,15 +14,15 @@
|
|
|
|
|
/**
|
|
|
|
|
* Specifies custom tag added by Community maintainer or Community source.
|
|
|
|
|
*/
|
|
|
|
|
const USER_TAG = 0;
|
|
|
|
|
const USER_TAG = 'user';
|
|
|
|
|
/**
|
|
|
|
|
* Specifies basic type of tag reserved for assignment by our aggregator.
|
|
|
|
|
*/
|
|
|
|
|
const RESERVED_TAG = 1;
|
|
|
|
|
const RESERVED_TAG = 'reserved';
|
|
|
|
|
/**
|
|
|
|
|
* Specifies warning tag reserved for assignment by our aggregator.
|
|
|
|
|
*/
|
|
|
|
|
const WARNING_TAG = 2;
|
|
|
|
|
const WARNING_TAG = 'warning';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -32,25 +32,24 @@
|
|
|
|
|
/**
|
|
|
|
|
* Create a new CommunityTag instance.
|
|
|
|
|
* @param string $text Text the tag should read.
|
|
|
|
|
* @param int $tag_type Numeric {@link TagType} value.
|
|
|
|
|
* @param string $tag_type {@link TagType} enumeration value.
|
|
|
|
|
* @param string|null $description [optional] Brief explanation of tag.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
string $text,
|
|
|
|
|
int $tag_type = TagType::USER_TAG,
|
|
|
|
|
string $tag_type = TagType::USER_TAG,
|
|
|
|
|
?string $description = ""
|
|
|
|
|
) {
|
|
|
|
|
$this->text = $text;
|
|
|
|
|
$this->text = CommunityTag::preprocess_tag($text);
|
|
|
|
|
$this->type = $tag_type;
|
|
|
|
|
$this->description =
|
|
|
|
|
empty($description) ? "Tag: $text" : $description;
|
|
|
|
|
$this->description = $description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int $type
|
|
|
|
|
* @var string $type
|
|
|
|
|
* Tag type as given by a {@link TagType} value.
|
|
|
|
|
*/
|
|
|
|
|
public readonly int $type;
|
|
|
|
|
public readonly string $type;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string $text
|
|
|
|
@ -78,15 +77,30 @@
|
|
|
|
|
return strtolower($this->text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a lowercase text representation of the tag for use in HTML.
|
|
|
|
|
*/
|
|
|
|
|
public function get_text_sanitized(): string {
|
|
|
|
|
return html_sanitize($this->get_text());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return tag description.
|
|
|
|
|
*/
|
|
|
|
|
public function get_description_sanitized(): string {
|
|
|
|
|
return html_sanitize($this->description ?? "Tag: $this->text");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Produce data used to serialize the tag.
|
|
|
|
|
*/
|
|
|
|
|
public function jsonSerialize(): mixed {
|
|
|
|
|
// Only used for passing to DOM
|
|
|
|
|
$details = get_object_vars($this);
|
|
|
|
|
$details['text'] = html_sanitize($this->get_text());
|
|
|
|
|
$details['description'] = html_sanitize($details['description']);
|
|
|
|
|
$details = [];
|
|
|
|
|
$details['text'] = $this->get_text();
|
|
|
|
|
$details['type'] = $this->get_tag_type();
|
|
|
|
|
if (!empty($this->description)) {
|
|
|
|
|
$details['description'] = $this->description;
|
|
|
|
|
}
|
|
|
|
|
return $details;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -97,30 +111,28 @@
|
|
|
|
|
return $tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$tag = html_sanitize(html_entity_decode($tag));
|
|
|
|
|
$tag = html_entity_decode($tag);
|
|
|
|
|
|
|
|
|
|
if ($tag[0] == '#') {
|
|
|
|
|
return substr($tag, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $tag;
|
|
|
|
|
return strtolower($tag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string[] $tag_array
|
|
|
|
|
* @return CommunityTag[]
|
|
|
|
|
*/
|
|
|
|
|
private static function from_tag_array(array $tag_array) {
|
|
|
|
|
$tags = array_map(function(?string $tag) {
|
|
|
|
|
return CommunityTag::preprocess_tag($tag);
|
|
|
|
|
}, $tag_array);
|
|
|
|
|
|
|
|
|
|
private static function from_string_tags(array $tag_array) {
|
|
|
|
|
$tags = array_filter(
|
|
|
|
|
$tags, function(?string $tag) {
|
|
|
|
|
$tag_array, function(?string $tag) {
|
|
|
|
|
return strlen($tag) != 0;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$tags = CommunityTag::dedupe_tags($tags);
|
|
|
|
|
|
|
|
|
|
return array_map(function(string $tag) {
|
|
|
|
|
return new CommunityTag($tag);
|
|
|
|
|
}, $tags);
|
|
|
|
@ -129,38 +141,54 @@
|
|
|
|
|
/**
|
|
|
|
|
* Constructs the tags given, removing any reserved tags.
|
|
|
|
|
* @param string[] $tags
|
|
|
|
|
* @param bool $remove_redundant Removes duplicate and obvious tags.
|
|
|
|
|
* @param bool $remove_redundant Removes meaningless tags.
|
|
|
|
|
* @return CommunityTag[]
|
|
|
|
|
*/
|
|
|
|
|
public static function from_user_tags(
|
|
|
|
|
array $tags, bool $remove_redundant = false
|
|
|
|
|
array $tags,
|
|
|
|
|
bool $remove_redundant = true
|
|
|
|
|
): array {
|
|
|
|
|
$tags_user = array_filter(
|
|
|
|
|
$tags_user = array_values(array_filter(
|
|
|
|
|
$tags,
|
|
|
|
|
function($tag) {
|
|
|
|
|
return !CommunityTag::is_reserved_tag($tag);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$tags_built = CommunityTag::from_tag_array($tags_user);
|
|
|
|
|
$tags_built = CommunityTag::from_string_tags($tags_user);
|
|
|
|
|
|
|
|
|
|
if ($remove_redundant) {
|
|
|
|
|
$tags_built = CommunityTag::dedupe_tags($tags_built);
|
|
|
|
|
$tags_built = array_filter($tags_built, function(CommunityTag $tag) {
|
|
|
|
|
$text = strtolower($tag->text);
|
|
|
|
|
return !in_array($text, CommunityTag::REDUNDANT_TAGS);
|
|
|
|
|
});
|
|
|
|
|
$tags_built = array_values(
|
|
|
|
|
array_filter($tags_built, function(CommunityTag $tag) {
|
|
|
|
|
return !in_array($tag->get_text(), CommunityTag::REDUNDANT_TAGS);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $tags_built;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string[] $details_array Array of string tags.
|
|
|
|
|
* @param array $details Deserialized tag info.
|
|
|
|
|
* @return CommunityTag
|
|
|
|
|
*/
|
|
|
|
|
public static function from_details(array $details): CommunityTag {
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
$details['text'],
|
|
|
|
|
$details['type'],
|
|
|
|
|
$details['description'] ?? ''
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array[] $details_array Array of deserialized tag info.
|
|
|
|
|
* @return CommunityTag[]
|
|
|
|
|
*/
|
|
|
|
|
public static function from_details_array(array $details_array): array {
|
|
|
|
|
return CommunityTag::from_user_tags($details_array);
|
|
|
|
|
return array_map(function($details) {
|
|
|
|
|
return CommunityTag::from_details($details);
|
|
|
|
|
}, $details_array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -168,7 +196,7 @@
|
|
|
|
|
* @return CommunityTag[]
|
|
|
|
|
*/
|
|
|
|
|
public static function dedupe_tags(array $tags) {
|
|
|
|
|
return array_unique($tags);
|
|
|
|
|
return array_values(array_unique($tags));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -223,11 +251,6 @@
|
|
|
|
|
private const REDUNDANT_TAGS = ["session"];
|
|
|
|
|
|
|
|
|
|
public const NSFW_KEYWORDS = ["nsfw", "porn", "erotic", "18+", "sex"];
|
|
|
|
|
|
|
|
|
|
public const CHECK_MARK = "✅";
|
|
|
|
|
|
|
|
|
|
public const WARNING_ICON = "⚠️";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether the given user tag is reserved by our aggregator.
|
|
|
|
|
*/
|
|
|
|
@ -249,4 +272,87 @@
|
|
|
|
|
return in_array(strtolower($tag), CommunityTag::HIGHLIGHTED_TAGS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ReservedTags {
|
|
|
|
|
public static function official() {
|
|
|
|
|
$CHECK_MARK = "✅";
|
|
|
|
|
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"official",
|
|
|
|
|
TagType::RESERVED_TAG,
|
|
|
|
|
"This Community is maintained by the Session team. $CHECK_MARK"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function nsfw() {
|
|
|
|
|
$WARNING_ICON = "⚠️";
|
|
|
|
|
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"nsfw",
|
|
|
|
|
TagType::WARNING_TAG,
|
|
|
|
|
"This Community may contain adult material. $WARNING_ICON"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function moderated(int $users_per_staff) {
|
|
|
|
|
$CHECK_MARK = "✅";
|
|
|
|
|
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"moderated",
|
|
|
|
|
TagType::RESERVED_TAG,
|
|
|
|
|
"This Community has at least 1 staff per $users_per_staff active users. $CHECK_MARK"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function not_modded(int $users_per_staff) {
|
|
|
|
|
$WARNING_ICON = "⚠️";
|
|
|
|
|
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"not modded",
|
|
|
|
|
TagType::WARNING_TAG,
|
|
|
|
|
"This Community has less than 1 staff per $users_per_staff active users. $WARNING_ICON"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function read_only() {
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"read-only",
|
|
|
|
|
TagType::RESERVED_TAG,
|
|
|
|
|
"This Community is read-only."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function no_upload_permission() {
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"uploads off",
|
|
|
|
|
TagType::RESERVED_TAG,
|
|
|
|
|
"This Community does not support uploading files or link previews."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function recently_created() {
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"new",
|
|
|
|
|
TagType::RESERVED_TAG,
|
|
|
|
|
"This Community was created recently."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function used_by_project() {
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"we're here",
|
|
|
|
|
TagType::RESERVED_TAG,
|
|
|
|
|
"The sessioncommunities.online maintainer(s) can post updates "
|
|
|
|
|
. "or respond to feedback in this Community."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function testing() {
|
|
|
|
|
return new CommunityTag(
|
|
|
|
|
"test",
|
|
|
|
|
TagType::RESERVED_TAG,
|
|
|
|
|
"This Community is intended for testing only."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
?>
|
|
|
|
|