Skip to content

Commit

Permalink
Display user AKAs
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaserlang committed Jun 7, 2024
1 parent 3d7d5d3 commit ce286ef
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
8 changes: 6 additions & 2 deletions tbot/web/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from functools import partial
import logging, os
import asyncio, aioredis, aiohttp
import logging
import os
import asyncio
import aioredis
import aiohttp
import signal
from tornado import web
from tbot import config, db
Expand Down Expand Up @@ -28,6 +31,7 @@ def App():
(r'/api/twitch/channels/([0-9]+)/chatlog', handlers.api.twitch.chatlog.Handler),
(r'/api/twitch/channels/([0-9]+)/user-chatstats', handlers.api.twitch.chatlog.User_stats_handler),
(r'/api/twitch/channels/([0-9]+)/user-streams-watched', handlers.api.twitch.chatlog.User_streams_watched_handler),
(r'/api/twitch/channels/([0-9]+)/user-akas', handlers.api.twitch.chatlog.User_akas_handler),
(r'/api/twitch/channels/([0-9]+)/users', handlers.api.twitch.channel_users.Handler),
(r'/api/twitch/channels/([0-9]+)/bot-join', handlers.api.twitch.control_bot.Join_handler),
(r'/api/twitch/channels/([0-9]+)/bot-mute', handlers.api.twitch.control_bot.Mute_handler),
Expand Down
29 changes: 28 additions & 1 deletion tbot/web/handlers/api/twitch/chatlog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from tornado import web
from ..base import Api_handler
from dateutil.parser import parse
Expand Down Expand Up @@ -135,6 +134,34 @@ async def get(self, channel_id):
self.write_object(list(streams))


class User_akas_handler(Api_handler):

async def get(self, channel_id):
await has_mod(self, channel_id)
user = self.get_argument('user')
u = await self.db.fetchone('SELECT user_id FROM twitch_usernames WHERE user=%s', [user])
user_id: str = ''
if not u:
user_id = await utils.twitch_lookup_user_id(self.ahttp, self.db, user)
if not user_id:
self.set_status(204)
return
else:
user_id = u['user_id']
args = [user_id]
sql = '''
select
user
from
twitch_usernames
where
user_id=%s
ORDER BY expires DESC
'''
akas = await self.db.fetchall(sql, args)
self.write_object(list(akas))


async def has_mod(handler, channel_id):
# TODO: Add setting to allow or disallow non mods to view the logs
# For the moment only mods or admins are allowed to view them
Expand Down
5 changes: 4 additions & 1 deletion tbot/web/ui/twitch/logviewer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Loading from 'tbot/components/loading'
import UserInput from './userinput'
import UserStats from './user_stats'
import UserStreamsWatched from './user_streams_watched'
import UserAKAs from './user_akas'
import './logviewer.scss'
import '../dashboard/components/topbar.scss'

Expand Down Expand Up @@ -107,7 +108,7 @@ class Logviewer extends React.Component {

return <div className="chatlog">
<h3>Chat logs</h3>
<table className="table table-dark table-striped table-sm table-hover">
<table className="table table-dark table-sm table-hover">
<tbody>
{this.state.showLoadBefore?
<tr><td colSpan="3" style={{textAlign: 'center'}}>
Expand Down Expand Up @@ -219,6 +220,8 @@ class Logviewer extends React.Component {

{this.query.user?<UserStreamsWatched channelId={this.state.channel.id} user={this.query.user} />: null}

{this.query.user?<UserAKAs channelId={this.state.channel.id} user={this.query.user} />: null}

</div>;
}

Expand Down
51 changes: 51 additions & 0 deletions tbot/web/ui/twitch/logviewer/user_akas.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useEffect, useState } from "react"

import api from 'tbot/twitch/api'

export default function({ channelId, user }) {
const [data, setData] = useState('')
const [loading, setLoading] = useState(true)
const [error, setError] = useState(false)

useEffect(() => {
setLoading(false)
api.get(`/api/twitch/channels/${channelId}/user-akas`, {params: {
user: user,
}}).then(r => {
setData(r.data)
setError(false)
}).catch((e) => {
console.log(e)
setData(null)
setError(true)
}).finally(() => {
setLoading(false)
})
}, [channelId, user])

if (loading)
return <div></div>

if (error)
return <div></div>

if (data?.length < 2)
return <div></div>

return <div style={{marginTop: '1rem'}}>
<h3>AKAs</h3>
<table className="table table-dark table-sm table-hover">
<tbody>
{data.map(s => (
<tr key={s.stream_id}>
<td
className="fit-content"
>
{data['user']}
</td>
</tr>
))}
</tbody>
</table>
</div>
}
10 changes: 5 additions & 5 deletions tbot/web/ui/twitch/logviewer/user_streams_watched.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export default function({ channelId, user }) {
}, [channelId, user])

if (loading)
return <div style={{marginTop: '2rem'}}><h3>Streams watched</h3> <div className="spacing">Loading streams watched...</div></div>
return <div style={{marginTop: '1rem'}}><h3>Streams watched</h3> <div className="spacing">Loading streams watched...</div></div>

if (error)
return <div style={{marginTop: '2rem'}}><h3>Streams watched</h3> <div className="spacing">Failed to load streams watched, try again.</div></div>
return <div style={{marginTop: '1rem'}}><h3>Streams watched</h3> <div className="spacing">Failed to load streams watched, try again.</div></div>

if (!data || (data.length === 0))
return <div style={{marginTop: '2rem'}}><h3>Streams watched</h3> <div className="spacing">Didn't find any streams watched for user.</div></div>
return <div style={{marginTop: '1rem'}}><h3>Streams watched</h3> <div className="spacing">Didn't find any streams watched for user.</div></div>

const loadMore = (e) => {
e.preventDefault()
Expand All @@ -55,9 +55,9 @@ export default function({ channelId, user }) {
))
}

return <div style={{marginTop: '2rem'}}>
return <div style={{marginTop: '1rem'}}>
<h3>Streams watched</h3>
<table className="table table-dark table-striped table-sm table-hover">
<table className="table table-dark table-sm table-hover">
<thead>
<tr>
<th className="fit-content">Stream date</th>
Expand Down

0 comments on commit ce286ef

Please sign in to comment.