aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdijs <iamedijs@hotmail.com>2019-02-07 14:46:10 -0700
committerEdijs <iamedijs@hotmail.com>2019-02-07 14:46:10 -0700
commitc0b833cb85ccd10942accbefc3ba4d70cf3d745e (patch)
tree35e32bd99b184214c3d9e46b6b549c36f483d91c
parenta215f6856d0dff53869a4c20e538eb744b636a4d (diff)
Added tag extractor method and spec
-rw-r--r--src/services/matcher/matcher.service.js23
-rw-r--r--src/services/mention_matcher/mention_matcher.js9
-rw-r--r--test/unit/specs/services/matcher/matcher.spec.js (renamed from test/unit/specs/services/mention_matcher/mention_matcher.spec.js)35
3 files changed, 50 insertions, 17 deletions
diff --git a/src/services/matcher/matcher.service.js b/src/services/matcher/matcher.service.js
new file mode 100644
index 00000000..b6c4e909
--- /dev/null
+++ b/src/services/matcher/matcher.service.js
@@ -0,0 +1,23 @@
+export const mentionMatchesUrl = (attention, url) => {
+ if (url === attention.statusnet_profile_url) {
+ return true
+ }
+ const [namepart, instancepart] = attention.screen_name.split('@')
+ const matchstring = new RegExp('://' + instancepart + '/.*' + namepart + '$', 'g')
+
+ return !!url.match(matchstring)
+}
+
+/**
+ * Extract tag name from pleroma or mastodon url.
+ * i.e https://bikeshed.party/tag/photo or https://quey.org/tags/sky
+ * @param {string} url
+ */
+export const extractTagFromUrl = (url) => {
+ const regex = /tag[s]*\/(\w+)$/g
+ const result = regex.exec(url)
+ if (!result) {
+ return false
+ }
+ return result[1]
+}
diff --git a/src/services/mention_matcher/mention_matcher.js b/src/services/mention_matcher/mention_matcher.js
deleted file mode 100644
index 2c1ed970..00000000
--- a/src/services/mention_matcher/mention_matcher.js
+++ /dev/null
@@ -1,9 +0,0 @@
-
-export const mentionMatchesUrl = (attention, url) => {
- if (url === attention.statusnet_profile_url) {
- return true
- }
- const [namepart, instancepart] = attention.screen_name.split('@')
- const matchstring = new RegExp('://' + instancepart + '/.*' + namepart + '$', 'g')
- return !!url.match(matchstring)
-}
diff --git a/test/unit/specs/services/mention_matcher/mention_matcher.spec.js b/test/unit/specs/services/matcher/matcher.spec.js
index 4f6f58ff..0b5363b4 100644
--- a/test/unit/specs/services/mention_matcher/mention_matcher.spec.js
+++ b/test/unit/specs/services/matcher/matcher.spec.js
@@ -1,4 +1,4 @@
-import * as MentionMatcher from 'src/services/mention_matcher/mention_matcher.js'
+import * as MatcherService from 'src/services/matcher/matcher.service.js'
const localAttn = () => ({
id: 123,
@@ -16,48 +16,67 @@ const externalAttn = () => ({
statusnet_profile_url: 'https://instance.com/users/person'
})
-describe('MentionMatcher', () => {
+describe('MatcherService', () => {
describe.only('mentionMatchesUrl', () => {
it('should match local mention', () => {
const attention = localAttn()
const url = 'https://instance.com/users/person'
- expect(MentionMatcher.mentionMatchesUrl(attention, url)).to.eql(true)
+ expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(true)
})
it('should not match a local mention with same name but different instance', () => {
const attention = localAttn()
const url = 'https://website.com/users/person'
- expect(MentionMatcher.mentionMatchesUrl(attention, url)).to.eql(false)
+ expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(false)
})
it('should match external pleroma mention', () => {
const attention = externalAttn()
const url = 'https://instance.com/users/person'
- expect(MentionMatcher.mentionMatchesUrl(attention, url)).to.eql(true)
+ expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(true)
})
it('should not match external pleroma mention with same name but different instance', () => {
const attention = externalAttn()
const url = 'https://website.com/users/person'
- expect(MentionMatcher.mentionMatchesUrl(attention, url)).to.eql(false)
+ expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(false)
})
it('should match external mastodon mention', () => {
const attention = externalAttn()
const url = 'https://instance.com/@person'
- expect(MentionMatcher.mentionMatchesUrl(attention, url)).to.eql(true)
+ expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(true)
})
it('should not match external mastodon mention with same name but different instance', () => {
const attention = externalAttn()
const url = 'https://website.com/@person'
- expect(MentionMatcher.mentionMatchesUrl(attention, url)).to.eql(false)
+ expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(false)
+ })
+ })
+ describe.only('extractTagFromUrl', () => {
+ it('should return tag name from valid pleroma url', () => {
+ const url = 'https://website.com/tag/photo'
+
+ expect(MatcherService.extractTagFromUrl(url)).to.eql('photo')
+ })
+
+ it('should return tag name from valid mastodon url', () => {
+ const url = 'https://website.com/tags/sky'
+
+ expect(MatcherService.extractTagFromUrl(url)).to.eql('sky')
+ })
+
+ it('should not return string but false if invalid url', () => {
+ const url = 'https://website.com/users/sky'
+
+ expect(MatcherService.extractTagFromUrl(url)).to.eql(false)
})
})
})