tags_assign: user_id instead of prefix

This commit is contained in:
Flummi 2021-12-23 10:07:12 +01:00
parent b2f45dc70e
commit bad5a5f7f0
9 changed files with 23 additions and 25 deletions

View File

@ -89,7 +89,7 @@ const flash = ({ type, msg }) => {
const span = document.createElement("span"); const span = document.createElement("span");
span.classList.add("badge", "badge-light", "mr-2"); span.classList.add("badge", "badge-light", "mr-2");
span.title = tag.prefix; span.title = tag.user;
if(tag.tag == "sfw") { if(tag.tag == "sfw") {
span.classList.remove("badge-light"); span.classList.remove("badge-light");
span.classList.add("badge-success"); span.classList.add("badge-success");

View File

@ -99,7 +99,9 @@ export default new class {
}; };
async getTags(itemid) { async getTags(itemid) {
const tags = await sql("tags_assign") const tags = await sql("tags_assign")
.select("tags.id", "tags.tag", "user.user")
.leftJoin("tags", "tags.id", "tags_assign.tag_id") .leftJoin("tags", "tags.id", "tags_assign.tag_id")
.leftJoin("user", "user.id", "tags_assign.user_id")
.where("tags_assign.item_id", itemid); .where("tags_assign.item_id", itemid);
for(let t = 0; t < tags.length; t++) for(let t = 0; t < tags.length; t++)
tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';'); tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';');

View File

@ -1,4 +1,5 @@
import sql from "../sql.mjs"; import sql from "../sql.mjs";
import lib from "../lib.mjs";
const allowedMimes = [ "audio", "image", "video", "%" ]; const allowedMimes = [ "audio", "image", "video", "%" ];
const auth = async (req, res, next) => { const auth = async (req, res, next) => {
@ -11,11 +12,6 @@ const auth = async (req, res, next) => {
return next(); return next();
}; };
const getTags = async itemid => await sql("tags_assign")
.leftJoin("tags", "tags.id", "tags_assign.tag_id")
.where("tags_assign.item_id", itemid)
.select("tags.id", "tags.tag", "tags_assign.prefix");
export default (router, tpl) => { export default (router, tpl) => {
router.group(/^\/api\/v2/, group => { router.group(/^\/api\/v2/, group => {
@ -121,13 +117,13 @@ export default (router, tpl) => {
await sql("tags_assign").insert({ await sql("tags_assign").insert({
tag_id: tagid, tag_id: tagid,
item_id: postid, item_id: postid,
prefix: `${req.session.user}@webinterface` user_id: req.session.id
}); });
} catch(err) { } catch(err) {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
success: false, success: false,
msg: err.message, msg: err.message,
tags: await getTags(postid) tags: await lib.getTags(postid)
})}); })});
} }
@ -135,7 +131,7 @@ export default (router, tpl) => {
success: true, success: true,
postid: req.post.postid, postid: req.post.postid,
tag: req.post.tag, tag: req.post.tag,
tags: await getTags(postid) tags: await lib.getTags(postid)
})}); })});
}); });
@ -150,7 +146,7 @@ export default (router, tpl) => {
const postid = +req.post.postid; const postid = +req.post.postid;
const tag = req.post.tag; const tag = req.post.tag;
const tags = await getTags(postid); const tags = await lib.getTags(postid);
const tagid = tags.filter(t => t.tag === tag)[0]?.id ?? null; const tagid = tags.filter(t => t.tag === tag)[0]?.id ?? null;
if(!tagid || tagid?.length === 0) { if(!tagid || tagid?.length === 0) {
@ -158,28 +154,26 @@ export default (router, tpl) => {
success: false, success: false,
tag: tag, tag: tag,
msg: "tag is not assigned", msg: "tag is not assigned",
tags: await getTags(postid) tags: await lib.getTags(postid)
})}); })});
} }
let q = sql("tags_assign").where("tag_id", tagid).andWhere("item_id", postid).del(); let q = sql("tags_assign").where("tag_id", tagid).andWhere("item_id", postid).del();
if(req.session.level < 50) if(req.session.level < 50)
q = q.andWhere("prefix", `${req.session.user}@webinterface`); q = q.andWhere("user_id", req.session.id);
const reply = !!(await q); const reply = !!(await q);
//await cleanTags();
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
success: reply, success: reply,
tag: tag, tag: tag,
tagid: tagid, tagid: tagid,
tags: await getTags(postid) tags: await lib.getTags(postid)
})}); })});
}); });
group.get(/\/admin\/tags\/get\/\d+$/, auth, async (req, res) => { group.get(/\/admin\/tags\/get\/\d+$/, auth, async (req, res) => {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
tags: await getTags(+req.url.split[5]) tags: await lib.getTags(+req.url.split[5])
})}); })});
}); });

View File

@ -231,8 +231,9 @@ export default (router, tpl) => {
router.get(/^\/ranking/, async (req, res) => { router.get(/^\/ranking/, async (req, res) => {
try { try {
const list = await sql('tags_assign') const list = await sql('tags_assign')
.select('prefix', sql.raw('count(distinct tag_id, item_id) count')) .select('user.user', sql.raw('count(distinct tag_id, item_id) count'))
.groupBy('prefix') .leftJoin('user', 'user.id', 'tags_assign.user_id')
.groupBy('user.user')
.orderBy('count', 'desc'); .orderBy('count', 'desc');
const stats = await lib.countf0cks(); const stats = await lib.countf0cks();

View File

@ -165,7 +165,7 @@ export default async bot => {
await sql("tags_assign").insert({ await sql("tags_assign").insert({
tag_id: tag === "sfw" ? 1 : 2, tag_id: tag === "sfw" ? 1 : 2,
item_id: insertq, item_id: insertq,
prefix: `autotagger` user_id: 7 // user: autotagger
}); });
} }
} }

View File

@ -25,7 +25,7 @@ export default async bot => {
}, { }, {
name: "tags add", name: "tags add",
call: /^\!tags add \d+ .*/i, call: /^\!tags add \d+ .*/i,
active: true, active: false,
level: 100, level: 100,
f: async e => { f: async e => {
const id = +e.args[1]; const id = +e.args[1];
@ -71,7 +71,7 @@ export default async bot => {
}, { }, {
name: "tags remove", name: "tags remove",
call: /^\!tags remove \d+ .*/i, call: /^\!tags remove \d+ .*/i,
active: true, active: false,
level: 100, level: 100,
f: async e => { f: async e => {
const id = +e.args[1]; const id = +e.args[1];

View File

@ -63,7 +63,7 @@
<span class="badge badge-dark" id="tags"> <span class="badge badge-dark" id="tags">
@if(typeof item.tags !== "undefined") @if(typeof item.tags !== "undefined")
@each(item.tags as tag) @each(item.tags as tag)
<span @if(session)title="{{ tag.prefix }}"@endif class="badge @if(tag.tag[0] == "&")badge-greentext@endif badge-{{ (tag.tag === "nsfw" ? "danger" : tag.tag === "sfw" ? "success" : "light") }} mr-2"> <span @if(session)title="{{ tag.user }}"@endif class="badge @if(tag.tag[0] == "&")badge-greentext@endif badge-{{ (tag.tag === "nsfw" ? "danger" : tag.tag === "sfw" ? "success" : "light") }} mr-2">
<a href="/tag/{{ tag.tag.replace(/\s/g, "%20") }}" style="color: inherit !important;">{{ tag.tag }}</a>@if(session)&nbsp;<a href="#">&#215;</a>@endif <a href="/tag/{{ tag.tag.replace(/\s/g, "%20") }}" style="color: inherit !important;">{{ tag.tag }}</a>@if(session)&nbsp;<a href="#">&#215;</a>@endif
</span> </span>
@endeach @endeach

View File

@ -3,12 +3,12 @@
<tr> <tr>
<td>rank</td> <td>rank</td>
<td>username</td> <td>username</td>
<td># tagged f0cks</td> <td># tags placed</td>
</tr> </tr>
@for(let i = 0; i < list.length; i++) @for(let i = 0; i < list.length; i++)
<tr> <tr>
<td>{{ i + 1 }}</td> <td>{{ i + 1 }}</td>
<td>{!! list[i].prefix !!}</td> <td>{!! list[i].user !!}</td>
<td>{{ list[i].count }}</td> <td>{{ list[i].count }}</td>
</tr> </tr>
@endfor @endfor

View File

@ -12,6 +12,7 @@
<li><a href="/user/{{ session.user.toLowerCase() }}">my f0cks</a></li> <li><a href="/user/{{ session.user.toLowerCase() }}">my f0cks</a></li>
<li><a href="/settings">settings</a></li> <li><a href="/settings">settings</a></li>
<li><a href="/about">About</a></li> <li><a href="/about">About</a></li>
<li><a href="/ranking">Ranking</a></li>
<li><a href="/logout">logout</a></li> <li><a href="/logout">logout</a></li>
</ul> </ul>
@else @else