mirror of
https://github.com/koodiklinikka/koodiklinikka.fi.git
synced 2026-02-20 16:55:13 +00:00
WIP
This commit is contained in:
47
components/feed-transformers/github.ts
Normal file
47
components/feed-transformers/github.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { GithubApi } from "parse-github-event/lib/types";
|
||||
import { KKEvent } from "../../data/events";
|
||||
import githubEvent from "parse-github-event";
|
||||
import lodashTemplate from "lodash/template";
|
||||
import defaultTemplateSettings from "lodash/templateSettings";
|
||||
|
||||
const isVisibleGithubEvent = ({ type }: GithubApi.GithubEvent) =>
|
||||
type !== "PushEvent" && type !== "DeleteEvent";
|
||||
const templateSettings = {
|
||||
...defaultTemplateSettings,
|
||||
interpolate: /{{([\s\S]+?)}}/g,
|
||||
};
|
||||
|
||||
function convertGithubEvent(item: GithubApi.GithubEvent): KKEvent {
|
||||
const parsedEvent = githubEvent.parse(item);
|
||||
const template = lodashTemplate(parsedEvent?.text, templateSettings, false);
|
||||
|
||||
const repository = `https://github.com/${item.repo.name}`;
|
||||
let branch;
|
||||
if (item.payload.ref) {
|
||||
branch = item.payload.ref.replace("refs/heads/", "");
|
||||
}
|
||||
|
||||
const message = template({
|
||||
repository: `<a target="_blank" href="${repository}">${item.repo.name}</a>`,
|
||||
branch: branch,
|
||||
number: item.payload.number,
|
||||
ref_type: item.payload.ref_type,
|
||||
ref: item.payload.ref,
|
||||
});
|
||||
|
||||
const url = `https://github.com/${item.actor.login}`;
|
||||
|
||||
return {
|
||||
user: item.actor.login,
|
||||
userLink: url,
|
||||
image: item.actor.avatar_url,
|
||||
imageLink: url,
|
||||
body: message,
|
||||
timestamp: new Date(item.created_at),
|
||||
type: "github",
|
||||
};
|
||||
}
|
||||
|
||||
export function convertGithubItems(items: readonly GithubApi.GithubEvent[]) {
|
||||
return items.filter(isVisibleGithubEvent).map(convertGithubEvent);
|
||||
}
|
||||
35
components/feed-transformers/twitter.ts
Normal file
35
components/feed-transformers/twitter.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { KKEvent } from "../../data/events";
|
||||
import twitterText from "twitter-text";
|
||||
|
||||
export interface TwitterItem {
|
||||
retweeted?: boolean;
|
||||
retweeted_status?: TwitterItem;
|
||||
user: {
|
||||
screen_name: string;
|
||||
profile_image_url_https: string;
|
||||
};
|
||||
text: string;
|
||||
created_at: number | string; // TODO: type?
|
||||
}
|
||||
|
||||
function convertTwitterItem(item: TwitterItem): KKEvent {
|
||||
if (item.retweeted && item.retweeted_status) {
|
||||
item = item.retweeted_status;
|
||||
}
|
||||
|
||||
const url = `https://twitter.com/${item.user.screen_name}`;
|
||||
|
||||
return {
|
||||
user: `@${item.user.screen_name}`,
|
||||
userLink: url,
|
||||
image: item.user.profile_image_url_https,
|
||||
imageLink: url,
|
||||
body: twitterText.autoLink(item.text),
|
||||
timestamp: new Date(item.created_at),
|
||||
type: "twitter",
|
||||
};
|
||||
}
|
||||
|
||||
export function convertTwitterItems(items: readonly TwitterItem[]) {
|
||||
return items.map(convertTwitterItem);
|
||||
}
|
||||
Reference in New Issue
Block a user