Skip to content

Commit

Permalink
feat: sms list in AndroidController
Browse files Browse the repository at this point in the history
  • Loading branch information
Chisw committed Feb 17, 2024
1 parent 8ac5f93 commit bc71c07
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 1 deletion.
12 changes: 12 additions & 0 deletions gagu-back-end/src/modules/termux/termux.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,16 @@ export class TermuxController {
return respond(null, ServerMessage.ERROR_NO_RESPONSE)
}
}

@Get('sms-list')
@Permission(UserPermission.administer)
async querySMSList() {
try {
const data = await this.termuxService.getSMSList()
return respond(data)
} catch (error) {
catchError(error)
return respond(null, ServerMessage.ERROR_NO_RESPONSE)
}
}
}
33 changes: 33 additions & 0 deletions gagu-back-end/src/modules/termux/termux.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IDownloadForm,
IInfraredTransmitForm,
ILocationForm,
ISMSQuery,
MediaPlayerStateType,
} from 'src/types'

Expand Down Expand Up @@ -236,4 +237,36 @@ export class TermuxService {
})
return out
}

async getSMSList(form?: ISMSQuery) {
const { limit = 200, offset = 0 } = form || {}
const params = [
'-l',
String(limit),
'-o',
String(offset),
'-t',
'all',
'-d',
'-n',
]

const out = await new Promise((resolve, reject) => {
const stream = spawn('termux-sms-list', params)

let allData = ''

stream.stdout.on('data', (buffer) => {
allData += buffer.toString('utf8')
})

stream.stdout.on('close', () => {
resolve(allData)
})

stream.stderr.on('error', reject)
})

return out ? JSON.parse(out as string) : out
}
}
15 changes: 15 additions & 0 deletions gagu-back-end/src/types/termux.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ export interface ILocation {
}

export type MediaPlayerStateType = 'play' | 'pause' | 'stop'

export interface ISMSQuery {
limit?: number
offset?: number
}

export interface ISMS {
_id: number
threadid: number
type: 'inbox' | 'outbox' | 'sent' | 'draft'
received: string
read: boolean
number: number
body: string
}
6 changes: 6 additions & 0 deletions gagu-front-end/src/api/termux.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
IInfraredTransmitForm,
ILocation,
ILocationForm,
ISMS,
MediaPlayerStateType,
} from '../types/termux.type'
import { getPathParam } from '../utils'
Expand Down Expand Up @@ -100,4 +101,9 @@ export class TermuxApi {
const { data } = await service.put<IResponse>(`/api/termux/media-player/${state}`)
return data
}

static querySMSList = async () => {
const { data } = await service.get<IResponse<ISMS[]>>('/api/termux/sms-list')
return data
}
}
54 changes: 54 additions & 0 deletions gagu-front-end/src/apps/AndroidController/SMSList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useEffect, useMemo } from 'react'
import { TermuxApi } from '../../api'
import { useRequest } from '../../hooks'
import { Button } from '@douyinfe/semi-ui'
import { SvgIcon } from '../../components/common'
import { line } from '../../utils'

export default function SMSList() {
const { request: querySMSList, loading, response } = useRequest(TermuxApi.querySMSList)

const list = useMemo(() => (response?.data || []).reverse(), [response])

useEffect(() => {
querySMSList()
}, [querySMSList])

return (
<>
<div className="relative h-72 overflow-y-auto">
<Button
icon={<SvgIcon.Refresh />}
loading={loading}
onClick={querySMSList}
/>
<div className="pr-2">
{list.map(({ _id, type, received, number, read, body }) => (
<div
key={_id}
className="py-1"
>
<div
className={line(`
mt-1 px-3 py-2 rounded-lg text-justify
${type === 'inbox'
? 'mr-12 bg-zinc-100 dark:bg-zinc-500'
: 'ml-12 bg-blue-600 text-white dark:bg-blue-700'
}
`)}
>
<div className="flex justify-between font-din text-xs opacity-50">
<span>{number}</span>
<span>{received}</span>
</div>
<div className="mt-1">
{body}
</div>
</div>
</div>
))}
</div>
</div>
</>
)
}
3 changes: 2 additions & 1 deletion gagu-front-end/src/apps/AndroidController/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import InfraredControl from './InfraredControl'
import Dialog from './Dialog'
import Location from './Location'
import MediaPlayer from './MediaPlayer'
import SMSList from './SMSList'

export default function AndroidController() {

Expand All @@ -36,7 +37,7 @@ export default function AndroidController() {
{ key: 'notifiction', title: 'Notification', icon: <SvgIcon.Notification />, component: <></> },
{ key: 'sensor', title: 'Sensor', icon: <SvgIcon.Sensor />, component: <></> },
{ key: 'share', title: 'Share', icon: <SvgIcon.Share />, component: <></> },
{ key: 'sms', title: 'SMS', icon: <SvgIcon.SMS />, component: <></> },
{ key: 'sms', title: 'SMS', icon: <SvgIcon.SMS />, component: <SMSList /> },
{ key: 'toast', title: 'Toast', icon: <SvgIcon.Toast />, component: <></> },
{ key: 'torch', title: 'Torch', icon: <SvgIcon.BulbFlash />, component: <></> },
{ key: 'tts', title: 'TTS', icon: <SvgIcon.Speak />, component: <></> },
Expand Down
15 changes: 15 additions & 0 deletions gagu-front-end/src/types/termux.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ export interface ILocation {
}

export type MediaPlayerStateType = 'play' | 'pause' | 'stop'

export interface ISMSQuery {
limit?: number
offset?: number
}

export interface ISMS {
_id: number
threadid: number
type: 'inbox' | 'outbox' | 'sent' | 'draft'
received: string
read: boolean
number: number
body: string
}

0 comments on commit bc71c07

Please sign in to comment.