refs #696 Fix toot parser for account, tag and link
This commit is contained in:
parent
64f337cfec
commit
8d45ab659c
|
@ -153,7 +153,7 @@ export default {
|
|||
}
|
||||
},
|
||||
tootClick (e) {
|
||||
const parsedTag = findTag(e.target, 'favourit')
|
||||
const parsedTag = findTag(e.target, 'favourite')
|
||||
if (parsedTag !== null) {
|
||||
const tag = `/${this.$route.params.id}/hashtag/${parsedTag}`
|
||||
this.$router.push({ path: tag })
|
||||
|
@ -167,15 +167,16 @@ export default {
|
|||
this.$store.dispatch('TimelineSpace/Contents/SideBar/openAccountComponent')
|
||||
this.$store.dispatch('TimelineSpace/Contents/SideBar/AccountProfile/changeAccount', account)
|
||||
})
|
||||
.catch(() => {
|
||||
this.$message({
|
||||
message: this.$t('message.find_account_error'),
|
||||
type: 'error'
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err)
|
||||
this.openLink(e)
|
||||
this.$store.commit('TimelineSpace/Contents/SideBar/changeOpenSideBar', false)
|
||||
})
|
||||
return parsedAccount.acct
|
||||
}
|
||||
this.openLink(e)
|
||||
},
|
||||
openLink (e) {
|
||||
const link = findLink(e.target, 'favourite')
|
||||
if (link !== null) {
|
||||
return shell.openExternal(link)
|
||||
|
|
|
@ -167,14 +167,14 @@ export default {
|
|||
this.$store.dispatch('TimelineSpace/Contents/SideBar/AccountProfile/changeAccount', account)
|
||||
})
|
||||
.catch(() => {
|
||||
this.$message({
|
||||
message: this.$t('message.find_account_error'),
|
||||
type: 'error'
|
||||
})
|
||||
this.openLink(e)
|
||||
this.$store.commit('TimelineSpace/Contents/SideBar/changeOpenSideBar', false)
|
||||
})
|
||||
return parsedAccount
|
||||
}
|
||||
this.openLink(e)
|
||||
},
|
||||
openLink (e) {
|
||||
const link = findLink(e.target, 'reblog')
|
||||
if (link !== null) {
|
||||
return shell.openExternal(link)
|
||||
|
|
|
@ -244,7 +244,6 @@ export default {
|
|||
const parsedTag = findTag(e.target, 'toot')
|
||||
if (parsedTag !== null) {
|
||||
const tag = `/${this.$route.params.id}/hashtag/${parsedTag}`
|
||||
console.log(tag)
|
||||
this.$router.push({ path: tag })
|
||||
return tag
|
||||
}
|
||||
|
@ -256,15 +255,16 @@ export default {
|
|||
this.$store.dispatch('TimelineSpace/Contents/SideBar/openAccountComponent')
|
||||
this.$store.dispatch('TimelineSpace/Contents/SideBar/AccountProfile/changeAccount', account)
|
||||
})
|
||||
.catch(() => {
|
||||
this.$message({
|
||||
message: this.$t('message.find_account_error'),
|
||||
type: 'error'
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err)
|
||||
this.openLink(e)
|
||||
this.$store.commit('TimelineSpace/Contents/SideBar/changeOpenSideBar', false)
|
||||
})
|
||||
return parsedAccount.acct
|
||||
}
|
||||
this.openLink(e)
|
||||
},
|
||||
openLink (e) {
|
||||
const link = findLink(e.target, 'toot')
|
||||
if (link !== null) {
|
||||
return shell.openExternal(link)
|
||||
|
|
|
@ -42,9 +42,9 @@ const AccountProfile = {
|
|||
)
|
||||
return client.get('/search', { q: parsedAccount.acct, resolve: true })
|
||||
.then(res => {
|
||||
if (res.data.accounts.length <= 0) throw new AccountNotFound('not found')
|
||||
const account = res.data.accounts[0]
|
||||
if (`@${account.username}` !== parsedAccount.username) throw new AccountNotFound('not found')
|
||||
if (res.data.accounts.length <= 0) throw new AccountNotFound('empty result')
|
||||
const account = res.data.accounts.find(a => `@${a.username}` === parsedAccount.username)
|
||||
if (!account) throw new AccountNotFound('not found')
|
||||
return account
|
||||
})
|
||||
},
|
||||
|
|
|
@ -36,12 +36,16 @@ export function parseTag (tagURL) {
|
|||
|
||||
export function findAccount (target, parentClass = 'toot') {
|
||||
if (target.getAttribute('class') && target.getAttribute('class').includes('u-url')) {
|
||||
return parseAccount(target.href)
|
||||
return parseMastodonAccount(target.href)
|
||||
}
|
||||
// In Pleroma, link does not have class.
|
||||
// So I have to check URL.
|
||||
// So we have to check URL.
|
||||
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/@[a-zA-Z0-9-_.]+/)) {
|
||||
return parseAccount(target.href)
|
||||
return parseMastodonAccount(target.href)
|
||||
}
|
||||
// Toot URL of Pleroma does not contain @.
|
||||
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/users\/[a-zA-Z0-9-_.]+/)) {
|
||||
return parsePleromaAccount(target.href)
|
||||
}
|
||||
if (target.parentNode === undefined || target.parentNode === null) {
|
||||
return null
|
||||
|
@ -52,7 +56,7 @@ export function findAccount (target, parentClass = 'toot') {
|
|||
return findAccount(target.parentNode, parentClass)
|
||||
}
|
||||
|
||||
export function parseAccount (accountURL) {
|
||||
export function parseMastodonAccount (accountURL) {
|
||||
const res = accountURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/(@[a-zA-Z0-9-_.]+)/)
|
||||
const domainName = res[1]
|
||||
const accountName = res[2]
|
||||
|
@ -61,3 +65,13 @@ export function parseAccount (accountURL) {
|
|||
acct: `${accountName}@${domainName}`
|
||||
}
|
||||
}
|
||||
|
||||
export function parsePleromaAccount (accountURL) {
|
||||
const res = accountURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/users\/([a-zA-Z0-9-_.]+)/)
|
||||
const domainName = res[1]
|
||||
const accountName = res[2]
|
||||
return {
|
||||
username: `@${accountName}`,
|
||||
acct: `@${accountName}@${domainName}`
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,18 +56,35 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami
|
|||
})
|
||||
|
||||
describe('findAccount', () => {
|
||||
context('Pleroma', () => {
|
||||
const doc = (new JSDOM(`<html><head></head><body>
|
||||
context('in Pleroma', () => {
|
||||
context('from Mastodon', () => {
|
||||
const doc = (new JSDOM(`<html><head></head><body>
|
||||
<div class="toot">
|
||||
<p><span><a href="https://social.mikutter.hachune.net/@h3_poteto">@<span id="user">h3_poteto</span></a></span> hogehoge</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>`)).window.document
|
||||
const target = doc.getElementById('user')
|
||||
it('should find', () => {
|
||||
const res = findAccount(target)
|
||||
assert.strictEqual(res.username, '@h3_poteto')
|
||||
assert.strictEqual(res.acct, '@h3_poteto@social.mikutter.hachune.net')
|
||||
const target = doc.getElementById('user')
|
||||
it('should find', () => {
|
||||
const res = findAccount(target)
|
||||
assert.strictEqual(res.username, '@h3_poteto')
|
||||
assert.strictEqual(res.acct, '@h3_poteto@social.mikutter.hachune.net')
|
||||
})
|
||||
})
|
||||
|
||||
context('from Pleroma', () => {
|
||||
const doc = (new JSDOM(`<html><head></head><body>
|
||||
<div class="toot">
|
||||
<p><span><a href="https://pleroma.io/users/h3poteto">@<span id="user">h3_poteto</span></a></span> hogehoge</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>`)).window.document
|
||||
const target = doc.getElementById('user')
|
||||
it('should find', () => {
|
||||
const res = findAccount(target)
|
||||
assert.strictEqual(res.username, '@h3poteto')
|
||||
assert.strictEqual(res.acct, '@h3poteto@pleroma.io')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue