Add [Matrix] Badge (#2417)

* Added matrix badge

* decreased the size of the matrix logo by more than 50%

* returning the size in fetch() instead of an object

* found another way to register a throwaway account (guest account). this one actually works on matrix.org, but I kept the old way as a backup method. also changed the POST from /members to /state because guest accounts didn't work with /members

* updated logo to a recolored version of the official logo

* Removed unnecessary comments.
Added documentation on how to create the badge URL.
Added a test that hits a real room to test for API compliance.
URLs are now obtained from getter functions.
Added JSON schema for the /state API request.
Improved state response filter.
Replaced example URL room ID to a dedicated testing room.
Made some error messages more helpful.

* correctly implemented requested changes

* changed color hex codes to constants
This commit is contained in:
M C
2018-12-06 13:25:59 -06:00
committed by chris48s
parent 499ea363e0
commit 9e95020b18
4 changed files with 382 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ paulmelnikow:
- hexpm
- jenkins
- luarocks
- matrix
- maven-central
- node
- nom

1
logo/matrix.svg Normal file
View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="137.583" height="137.583" viewBox="0 0 137.583 36.402"><path fill="#fff" d="M3.625-47.442V83.844h9.445v3.149H0V-50.59h13.07v3.148zM44-5.823v6.64h.185c1.773-2.54 3.916-4.497 6.403-5.873 2.487-1.402 5.371-2.09 8.6-2.09 3.095 0 5.926.609 8.492 1.8 2.567 1.19 4.498 3.333 5.848 6.35 1.455-2.144 3.44-4.05 5.926-5.69 2.487-1.64 5.45-2.46 8.864-2.46 2.593 0 5 .318 7.223.953s4.101 1.64 5.689 3.042c1.587 1.403 2.804 3.202 3.704 5.45.873 2.25 1.323 4.949 1.323 8.124v32.834H92.789V15.45c0-1.64-.053-3.202-.185-4.657s-.476-2.725-1.032-3.784a6.338 6.338 0 0 0-2.512-2.566c-1.112-.635-2.62-.952-4.498-.952-1.905 0-3.44.37-4.604 1.084-1.164.74-2.09 1.667-2.752 2.858-.661 1.164-1.11 2.487-1.323 3.995a31.876 31.876 0 0 0-.343 4.498v27.33H62.07V15.742c0-1.455-.026-2.884-.106-4.313a11.892 11.892 0 0 0-.82-3.942 6.029 6.029 0 0 0-2.381-2.884c-1.111-.715-2.725-1.085-4.895-1.085-.635 0-1.481.132-2.513.423-1.032.29-2.064.82-3.043 1.614-.979.794-1.826 1.932-2.514 3.413-.688 1.482-1.031 3.44-1.031 5.848v28.468H31.3V-5.823zm89.959 89.667V-47.442h-9.446v-3.148h13.07V86.993h-13.07v-3.15z"/></svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -0,0 +1,147 @@
'use strict'
const Joi = require('joi')
const BaseJsonService = require('../base-json')
const matrixRegisterSchema = Joi.object({
access_token: Joi.string().required(),
}).required()
const matrixStateSchema = Joi.array()
.items(
Joi.object({
content: Joi.object({
membership: Joi.string().optional(),
}).required(),
type: Joi.string().required(),
sender: Joi.string().required(),
state_key: Joi.string()
.allow('')
.required(),
})
)
.required()
const documentation = `
<p>
In order for this badge to work, the host of your room must allow guest accounts or dummy accounts to register, and the room must be world readable (chat history visible to anyone).
</br>
The following steps will show you how to setup the badge URL using the Riot.im Matrix client.
</br>
<ul>
<li>Select the desired room inside the Riot.im client</li>
<li>Click on the room settings button (gear icon) located near the top right of the client</li>
<li>Scroll to the very bottom of the settings page and look under the <code>Advanced</code> tab</li>
<li>You should see the <code>Internal room ID</code> with your rooms ID next to it (ex: <code>!ltIjvaLydYAWZyihee:matrix.org</code>)</li>
<li>Replace the IDs <code>:</code> with <code>/</code></li>
<li>The final badge URL should look something like this <code>/matrix/!ltIjvaLydYAWZyihee/matrix.org.svg</code></li>
</ul>
</p>
`
module.exports = class Matrix extends BaseJsonService {
async registerAccount({ host, guest }) {
return this._requestJson({
url: `https://${host}/_matrix/client/r0/register`,
schema: matrixRegisterSchema,
options: {
method: 'POST',
qs: guest
? {
kind: 'guest',
}
: {},
body: JSON.stringify({
password: '',
auth: { type: 'm.login.dummy' },
}),
},
errorMessages: {
401: 'auth failed',
403: 'guests not allowed',
429: 'rate limited by rooms host',
},
})
}
async fetch({ host, roomId }) {
let auth
try {
auth = await this.registerAccount({ host, guest: true })
} catch (e) {
if (e.prettyMessage === 'guests not allowed') {
// attempt fallback method
auth = await this.registerAccount({ host, guest: false })
} else throw e
}
const data = await this._requestJson({
url: `https://${host}/_matrix/client/r0/rooms/${roomId}/state`,
schema: matrixStateSchema,
options: {
qs: {
access_token: auth.access_token,
},
},
errorMessages: {
400: 'unknown request',
401: 'bad auth token',
403: 'room not world readable or is invalid',
},
})
return Array.isArray(data)
? data.filter(
m =>
m.type === 'm.room.member' &&
m.sender === m.state_key &&
m.content.membership === 'join'
).length
: 0
}
static get _cacheLength() {
return 30
}
static render({ members }) {
return {
message: `${members} users`,
color: 'brightgreen',
}
}
async handle({ roomId, host, authServer }) {
const members = await this.fetch({
host,
roomId: `${roomId}:${host}`,
})
return this.constructor.render({ members })
}
static get defaultBadgeData() {
return { label: 'chat' }
}
static get category() {
return 'chat'
}
static get route() {
return {
base: 'matrix',
format: '([^/]+)/([^/]+)',
capture: ['roomId', 'host'],
}
}
static get examples() {
return [
{
title: 'Matrix',
exampleUrl: '!ltIjvaLydYAWZyihee/matrix.org',
pattern: ':roomId/:host',
staticExample: this.render({ members: 42 }),
documentation,
},
]
}
}

View File

@@ -0,0 +1,233 @@
'use strict'
const Joi = require('joi')
const ServiceTester = require('../service-tester')
const { colorScheme } = require('../test-helpers')
const t = new ServiceTester({ id: 'matrix', title: 'Matrix' })
module.exports = t
t.create('get room state as guest')
.get('/ROOM/DUMMY.dumb.json?style=_shields_test')
.intercept(nock =>
nock('https://DUMMY.dumb/')
.post('/_matrix/client/r0/register?kind=guest')
.reply(
200,
JSON.stringify({
access_token: 'TOKEN',
})
)
.get('/_matrix/client/r0/rooms/ROOM:DUMMY.dumb/state?access_token=TOKEN')
.reply(
200,
JSON.stringify([
{
// valid user 1
type: 'm.room.member',
sender: '@user1:DUMMY.dumb',
state_key: '@user1:DUMMY.dumb',
content: {
membership: 'join',
},
},
{
// valid user 2
type: 'm.room.member',
sender: '@user2:DUMMY.dumb',
state_key: '@user2:DUMMY.dumb',
content: {
membership: 'join',
},
},
{
// should exclude banned/invited/left members
type: 'm.room.member',
sender: '@user3:DUMMY.dumb',
state_key: '@user3:DUMMY.dumb',
content: {
membership: 'leave',
},
},
{
// exclude events like the room name
type: 'm.room.name',
sender: '@user4:DUMMY.dumb',
state_key: '@user4:DUMMY.dumb',
content: {
membership: 'fake room',
},
},
])
)
)
.expectJSON({
name: 'chat',
value: '2 users',
colorB: colorScheme.brightgreen,
})
t.create('get room state as member (backup method)')
.get('/ROOM/DUMMY.dumb.json?style=_shields_test')
.intercept(nock =>
nock('https://DUMMY.dumb/')
.post('/_matrix/client/r0/register?kind=guest')
.reply(
403,
JSON.stringify({
errcode: 'M_GUEST_ACCESS_FORBIDDEN', // i think this is the right one
error: 'Guest access not allowed',
})
)
.post('/_matrix/client/r0/register')
.reply(
200,
JSON.stringify({
access_token: 'TOKEN',
})
)
.get('/_matrix/client/r0/rooms/ROOM:DUMMY.dumb/state?access_token=TOKEN')
.reply(
200,
JSON.stringify([
{
// valid user 1
type: 'm.room.member',
sender: '@user1:DUMMY.dumb',
state_key: '@user1:DUMMY.dumb',
content: {
membership: 'join',
},
},
{
// valid user 2
type: 'm.room.member',
sender: '@user2:DUMMY.dumb',
state_key: '@user2:DUMMY.dumb',
content: {
membership: 'join',
},
},
{
// should exclude banned/invited/left members
type: 'm.room.member',
sender: '@user3:DUMMY.dumb',
state_key: '@user3:DUMMY.dumb',
content: {
membership: 'leave',
},
},
{
// exclude events like the room name
type: 'm.room.name',
sender: '@user4:DUMMY.dumb',
state_key: '@user4:DUMMY.dumb',
content: {
membership: 'fake room',
},
},
])
)
)
.expectJSON({
name: 'chat',
value: '2 users',
colorB: colorScheme.brightgreen,
})
t.create('bad server or connection')
.get('/ROOM/DUMMY.dumb.json?style=_shields_test')
.networkOff()
.expectJSON({
name: 'chat',
value: 'inaccessible',
colorB: colorScheme.lightgray,
})
t.create('invalid room')
.get('/ROOM/DUMMY.dumb.json?style=_shields_test')
.intercept(nock =>
nock('https://DUMMY.dumb/')
.post('/_matrix/client/r0/register?kind=guest')
.reply(
200,
JSON.stringify({
access_token: 'TOKEN',
})
)
.get('/_matrix/client/r0/rooms/ROOM:DUMMY.dumb/state?access_token=TOKEN')
.reply(
403,
JSON.stringify({
errcode: 'M_GUEST_ACCESS_FORBIDDEN',
error: 'Guest access not allowed',
})
)
)
.expectJSON({
name: 'chat',
value: 'room not world readable or is invalid',
colorB: colorScheme.lightgray,
})
t.create('invalid token')
.get('/ROOM/DUMMY.dumb.json?style=_shields_test')
.intercept(nock =>
nock('https://DUMMY.dumb/')
.post('/_matrix/client/r0/register?kind=guest')
.reply(
200,
JSON.stringify({
access_token: 'TOKEN',
})
)
.get('/_matrix/client/r0/rooms/ROOM:DUMMY.dumb/state?access_token=TOKEN')
.reply(
401,
JSON.stringify({
errcode: 'M_UNKNOWN_TOKEN',
error: 'Unrecognised access token.',
})
)
)
.expectJSON({
name: 'chat',
value: 'bad auth token',
colorB: colorScheme.lightgray,
})
t.create('unknown request')
.get('/ROOM/DUMMY.dumb.json?style=_shields_test')
.intercept(nock =>
nock('https://DUMMY.dumb/')
.post('/_matrix/client/r0/register?kind=guest')
.reply(
200,
JSON.stringify({
access_token: 'TOKEN',
})
)
.get('/_matrix/client/r0/rooms/ROOM:DUMMY.dumb/state?access_token=TOKEN')
.reply(
400,
JSON.stringify({
errcode: 'M_UNRECOGNIZED',
error: 'Unrecognized request',
})
)
)
.expectJSON({
name: 'chat',
value: 'unknown request',
colorB: colorScheme.lightgray,
})
t.create('test on real matrix room for API compliance')
.get('/!ltIjvaLydYAWZyihee/matrix.org.json?style=_shields_test')
.expectJSONTypes(
Joi.object().keys({
name: 'chat',
value: Joi.string().regex(/^[0-9]+ users$/),
colorB: colorScheme.brightgreen,
})
)