-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdshow.go
87 lines (79 loc) · 1.98 KB
/
cmdshow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"context"
"strings"
"time"
"github.com/rs/zerolog"
"maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/util"
)
const showBotMessage = `Bot ´%s´ info:
* Created on %s
* Device ID: ´%s´
* Last seen %s
`
func getBotMeta(ctx context.Context, username string) *Bot {
bot, err := db.GetBot(ctx, id.NewUserID(strings.ToLower(username), cli.UserID.Homeserver()))
if err != nil {
replyErr(ctx, err, "Failed to get bot info")
} else if bot == nil {
reply(ctx, "That bot doesn't exist")
} else if bot.OwnerMXID != getEvent(ctx).Sender {
reply(ctx, "That's not your bot")
} else {
return bot
}
return nil
}
func cmdShow(ctx context.Context, args []string) {
if len(args) < 1 {
reply(ctx, "**Usage:** `show <username>`")
return
}
bot := getBotMeta(ctx, args[0])
if bot == nil {
return
}
userInfo, err := synadm.GetUserInfo(ctx, bot.MXID)
if err != nil {
replyErr(ctx, err, "Failed to get bot user info")
return
}
devices, err := synadm.ListDevices(ctx, bot.MXID)
if err != nil {
replyErr(ctx, err, "Failed to get bot device info")
return
}
if len(devices.Devices) > 1 {
zerolog.Ctx(ctx).Warn().Msg("Bot has multiple devices")
}
var deviceID, lastSeen string
if len(devices.Devices) > 0 {
deviceInfo := devices.Devices[0]
deviceID = deviceInfo.DeviceID.String()
lastSeenTS := time.UnixMilli(deviceInfo.LastSeenTS)
if deviceInfo.LastSeenTS == 0 {
lastSeen = "never"
} else if seenAgo := time.Since(lastSeenTS); seenAgo < time.Second {
lastSeen = "now"
} else if seenAgo >= 1*util.Week {
lastSeen = "at " + lastSeenTS.UTC().Format(time.UnixDate)
} else {
lastSeen = util.FormatDuration(seenAgo) + " ago"
}
if deviceInfo.LastSeenIP != "" {
lastSeen += " from " + deviceInfo.LastSeenIP
}
} else {
zerolog.Ctx(ctx).Warn().Msg("Bot has no devices")
deviceID = "<none>"
lastSeen = "N/A"
}
reply(
ctx, showBotMessage,
userInfo.UserID,
userInfo.CreationTS.UTC().Format(time.UnixDate),
deviceID,
lastSeen,
)
}