Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP implement mentions + findUser falls back to webfinger #28

Merged
merged 2 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions philip/src/components/Publish.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import { getCreateObject, getHashTag } from "../utils/pubGate";
import { getCreateObject, getHashTag, getMention } from "../utils/pubGate";
import { getUserId } from "../utils";

export let reply = null;
export let session;
Expand All @@ -9,13 +10,24 @@
let content = "";

const hashTagMatcher = /(^|\W)(#[^#\s]+)/gi;
const mentionMatcher = /(^|\W)@([^@\s]+)/gi;

const wrapHashTagsWithLink = text =>
text.match(hashTagMatcher)
? text.replace(hashTagMatcher, '$1<a href="" rel="tag">$2</a>')
: text;
const wrapMentions = text =>
text.match(mentionMatcher)
? text.replace(
mentionMatcher,
"$1<span class='h-card'><a href='" +
getUserId("$2") +
"' class='u-url mention'>@<span>$2</span></a></span>"
)
: text;

const getAllHashTags = text => text.match(hashTagMatcher) || [];
const getAllMentions = text => text.match(mentionMatcher) || [];

const wrapLinksWithTags = text =>
text.replace(/(https?:\/\/([^\s]+))/gi, '<a href="$1">$2</a>');
Expand All @@ -27,14 +39,17 @@
let tags = getAllHashTags(content)
.map(v => v.trim())
.map(getHashTag);
let mentions = getAllMentions(content)
.map(m => m.trim())
.map(m => getMention(m, getUserId(m)));

const data = wrapHashTagsWithLink(wrapLinksWithTags(content));

let ap_object = getCreateObject(data, tags);
const data = wrapMentions(wrapHashTagsWithLink(wrapLinksWithTags(content)));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)))

let ap_object = getCreateObject(data, tags.concat(mentions));
ap_object.cc = mentions.map(m => m.href);

if (reply) {
ap_object.object.inReplyTo = reply.id;
ap_object.cc = [reply.attributedTo];
ap_object.cc = ap_object.cc.concat(reply.attributedTo);
}

try {
Expand Down
1 change: 1 addition & 0 deletions philip/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as xhr } from "./xhr";
export { ensureObject } from "./objectUtils";
export { getUserId } from "./user";
8 changes: 3 additions & 5 deletions philip/src/utils/pubGate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export const getHashTag = name => ({
name,
href: "",
type: "Hashtag",
});
export const getHashTag = name => ({ name, href: "", type: "Hashtag" });

export const getMention = (name, href) => ({ name, href, type: "Mention" });

export const getCreateObject = (content, tag) => ({
type: "Create",
Expand Down
5 changes: 5 additions & 0 deletions philip/src/utils/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getUserId = name => {
// TODO implement webFinger (#26)
const id = `${base_url}/@${name}`; // FollowYourNose
return id;
};