1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 17:10:10 +00:00

Matrix: Connect on accounts tab, customizable chat topic

This commit is contained in:
Harvey Tindall 2021-05-30 11:47:41 +01:00
parent 59ebf52fe2
commit 75fdf6ec3d
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
7 changed files with 102 additions and 7 deletions

36
api.go
View File

@ -1540,6 +1540,8 @@ func (app *appContext) GetConfig(gc *gin.Context) {
resp.Sections["email"].Settings["language"] = el resp.Sections["email"].Settings["language"] = el
resp.Sections["password_resets"].Settings["language"] = pl resp.Sections["password_resets"].Settings["language"] = pl
resp.Sections["telegram"].Settings["language"] = tl resp.Sections["telegram"].Settings["language"] = tl
resp.Sections["discord"].Settings["language"] = tl
resp.Sections["matrix"].Settings["language"] = tl
gc.JSON(200, resp) gc.JSON(200, resp)
} }
@ -2365,6 +2367,40 @@ func (app *appContext) MatrixCheckPIN(gc *gin.Context) {
respondBool(200, true, gc) respondBool(200, true, gc)
} }
// @Summary Links a Matrix user to a Jellyfin account via user IDs. Notifications are turned on by default.
// @Produce json
// @Success 200 {object} boolResponse
// @Failure 400 {object} boolResponse
// @Failure 500 {object} boolResponse
// @Param MatrixConnectUserDTO body MatrixConnectUserDTO true "User's Jellyfin ID & Matrix user ID."
// @Router /users/matrix [post]
// @tags Other
func (app *appContext) MatrixConnect(gc *gin.Context) {
var req MatrixConnectUserDTO
gc.BindJSON(&req)
if app.storage.matrix == nil {
app.storage.matrix = map[string]MatrixUser{}
}
roomID, err := app.matrix.CreateRoom(req.UserID)
if err != nil {
app.err.Printf("Matrix: Failed to create room: %v", err)
respondBool(500, false, gc)
return
}
app.storage.matrix[req.JellyfinID] = MatrixUser{
UserID: req.UserID,
RoomID: roomID,
Lang: "en-us",
Contact: true,
}
if err := app.storage.storeMatrixUsers(); err != nil {
app.err.Printf("Failed to store Matrix users: %v", err)
respondBool(500, false, gc)
return
}
respondBool(200, true, gc)
}
// @Summary Returns a list of matching users from a Discord guild, given a username (discriminator optional). // @Summary Returns a list of matching users from a Discord guild, given a username (discriminator optional).
// @Produce json // @Produce json
// @Success 200 {object} DiscordUsersDTO // @Success 200 {object} DiscordUsersDTO

View File

@ -84,6 +84,8 @@ func (app *appContext) loadConfig() error {
app.MustSetValue("user_expiry", "email_html", "jfa-go:"+"user-expired.html") app.MustSetValue("user_expiry", "email_html", "jfa-go:"+"user-expired.html")
app.MustSetValue("user_expiry", "email_text", "jfa-go:"+"user-expired.txt") app.MustSetValue("user_expiry", "email_text", "jfa-go:"+"user-expired.txt")
app.MustSetValue("matrix", "topic", "Jellyfin notifications")
app.config.Section("jellyfin").Key("version").SetValue(version) app.config.Section("jellyfin").Key("version").SetValue(version)
app.config.Section("jellyfin").Key("device").SetValue("jfa-go") app.config.Section("jellyfin").Key("device").SetValue("jfa-go")
app.config.Section("jellyfin").Key("device_id").SetValue(fmt.Sprintf("jfa-go-%s-%s", version, commit)) app.config.Section("jellyfin").Key("device_id").SetValue(fmt.Sprintf("jfa-go-%s-%s", version, commit))

View File

@ -727,6 +727,15 @@
"value": "", "value": "",
"description": "User ID of bot account (Example: @jfa-bot:riot.im)" "description": "User ID of bot account (Example: @jfa-bot:riot.im)"
}, },
"topic": {
"name": "Chat topic",
"required": false,
"requires_restart": true,
"depends_true": "enabled",
"type": "text",
"value": "Jellyfin notifications",
"description": "Topic of Matrix private chats."
},
"language": { "language": {
"name": "Language", "name": "Language",
"required": false, "required": false,

View File

@ -145,12 +145,20 @@ func (d *MatrixDaemon) commandLang(event *gomatrix.Event, code, lang string) {
} }
} }
func (d *MatrixDaemon) SendStart(userID string) (ok bool) { func (d *MatrixDaemon) CreateRoom(userID string) (string, error) {
room, err := d.bot.CreateRoom(&gomatrix.ReqCreateRoom{ room, err := d.bot.CreateRoom(&gomatrix.ReqCreateRoom{
Visibility: "private", Visibility: "private",
Invite: []string{userID}, Invite: []string{userID},
Topic: "jfa-go", Topic: d.app.config.Section("matrix").Key("topic").String(),
}) })
if err != nil {
return "", err
}
return room.RoomID, nil
}
func (d *MatrixDaemon) SendStart(userID string) (ok bool) {
roomID, err := d.CreateRoom(userID)
if err != nil { if err != nil {
d.app.err.Printf("Failed to create room for user \"%s\": %v", userID, err) d.app.err.Printf("Failed to create room for user \"%s\": %v", userID, err)
return return
@ -160,13 +168,13 @@ func (d *MatrixDaemon) SendStart(userID string) (ok bool) {
d.tokens[pin] = UnverifiedUser{ d.tokens[pin] = UnverifiedUser{
false, false,
&MatrixUser{ &MatrixUser{
RoomID: room.RoomID, RoomID: roomID,
UserID: userID, UserID: userID,
Lang: lang, Lang: lang,
}, },
} }
_, err = d.bot.SendText( _, err = d.bot.SendText(
room.RoomID, roomID,
d.app.storage.lang.Telegram[lang].Strings.get("matrixStartMessage")+"\n\n"+pin+"\n\n"+ d.app.storage.lang.Telegram[lang].Strings.get("matrixStartMessage")+"\n\n"+pin+"\n\n"+
d.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "!lang"}), d.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "!lang"}),
) )

View File

@ -290,6 +290,12 @@ type DiscordInviteDTO struct {
type MatrixSendPINDTO struct { type MatrixSendPINDTO struct {
UserID string `json:"user_id"` UserID string `json:"user_id"`
} }
type MatrixCheckPINDTO struct { type MatrixCheckPINDTO struct {
PIN string `json:"pin"` PIN string `json:"pin"`
} }
type MatrixConnectUserDTO struct {
JellyfinID string `json:"jf_id"`
UserID string `json:"user_id"`
}

View File

@ -130,6 +130,7 @@ func (app *appContext) loadRoutes(router *gin.Engine) {
if matrixEnabled { if matrixEnabled {
router.GET(p+"/invite/:invCode/matrix/verified/:userID/:pin", app.MatrixCheckPIN) router.GET(p+"/invite/:invCode/matrix/verified/:userID/:pin", app.MatrixCheckPIN)
router.POST(p+"/invite/:invCode/matrix/user", app.MatrixSendPIN) router.POST(p+"/invite/:invCode/matrix/user", app.MatrixSendPIN)
router.POST(p+"/users/matrix", app.MatrixConnect)
} }
} }
if *SWAGGER { if *SWAGGER {

View File

@ -186,8 +186,11 @@ class user implements User {
this._matrixID = u; this._matrixID = u;
if (!u) { if (!u) {
this._notifyDropdown.querySelector(".accounts-area-matrix").classList.add("unfocused"); this._notifyDropdown.querySelector(".accounts-area-matrix").classList.add("unfocused");
this._matrix.innerHTML = `<span class="chip btn !low">${window.lang.strings("add")}</span>`; this._matrix.innerHTML = `
// (this._matrix.querySelector("span") as HTMLSpanElement).onclick = this._addMatrix; <span class="chip btn !low">${window.lang.strings("add")}</span>
<input type="text" class="input ~neutral !normal stealth-input unfocused" placeholder="@user:riot.im">
`;
(this._matrix.querySelector("span") as HTMLSpanElement).onclick = this._addMatrix;
} else { } else {
this._notifyDropdown.querySelector(".accounts-area-matrix").classList.remove("unfocused"); this._notifyDropdown.querySelector(".accounts-area-matrix").classList.remove("unfocused");
this._matrix.innerHTML = ` this._matrix.innerHTML = `
@ -201,6 +204,36 @@ class user implements User {
} }
} }
private _addMatrix = () => {
const addButton = this._matrix.querySelector(".btn") as HTMLSpanElement;
const icon = this._matrix.querySelector("i");
const input = this._matrix.querySelector("input.stealth-input") as HTMLInputElement;
if (addButton.classList.contains("chip")) {
input.classList.remove("unfocused");
addButton.innerHTML = `<i class="ri-check-line"></i>`;
addButton.classList.remove("chip")
if (icon) {
icon.classList.add("unfocused");
}
} else {
if (input.value.charAt(0) != "@" || !input.value.includes(":")) return;
const send = {
jf_id: this.id,
user_id: input.value
}
_post("/users/matrix", send, (req: XMLHttpRequest) => {
if (req.readyState == 4) {
document.dispatchEvent(new CustomEvent("accounts-reload"));
if (req.status != 200) {
window.notifications.customError("errorConnectMatrix", window.lang.notif("errorFailureCheckLogs"));
return;
}
window.notifications.customSuccess("connectMatrix", window.lang.notif("accountConnected"));
}
});
}
}
get notify_matrix(): boolean { return this._notifyMatrix; } get notify_matrix(): boolean { return this._notifyMatrix; }
set notify_matrix(s: boolean) { set notify_matrix(s: boolean) {
if (this._notifyDropdown) { if (this._notifyDropdown) {