Skip to content

Commit

Permalink
add functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsdevera committed Dec 14, 2023
1 parent e4c6727 commit 1c639ae
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
63 changes: 63 additions & 0 deletions supabase/functions/urlmon-jobs-router/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.

import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'

const supabase = createClient('https://bttqjbjajfomldjfwcxq.supabase.co', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJ0dHFqYmphamZvbWxkamZ3Y3hxIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTY3MjI5NjMxMiwiZXhwIjoxOTg3ODcyMzEyfQ._9OAY37rFqJb6S8jnveCR1-ZdTeYhd-quXeBpvs3AkY')

const sha256 = async (message: string): Promise<T> => {
// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message);

// hash the message
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);

// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer));

// convert bytes to hex string
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}

const JOB_BOARD_CHANNELS = [
'929538334550806538'
]

serve(async (req) => {
const data = await req.json()
const observed_channel_id = data.record.channel_id
let status = 400
if (JOB_BOARD_CHANNELS.includes(observed_channel_id)) {

const post_urls_id = data.record.id
const post_id = data.record.post_id
const author_sha256 = await sha256(data.record.author_id)
const channel_sha256 = await sha256(data.record.channel_id)
const guild_sha256 = await sha256(data.record.guild_id)
const content_sha256 = await sha256(data.record.content)
const created_at = data.record.created_at
const url = data.record.url
const title = data.record.title
const description = data.record.description

const { response, error } = await supabase
.from('job_urls')
.insert([
{ post_id,post_urls_id,author_sha256,channel_sha256,guild_sha256,content_sha256,created_at,url,title,description },
])
if(!error) {
status = 200
}
}
else {
status = 401
}

return new Response(
JSON.stringify(status),
{ headers: { "Content-Type": "application/json" } },
)
})
62 changes: 62 additions & 0 deletions supabase/functions/urlmon-url-extractor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.

import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'

const supabase = createClient('https://bttqjbjajfomldjfwcxq.supabase.co', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJ0dHFqYmphamZvbWxkamZ3Y3hxIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTY3MjI5NjMxMiwiZXhwIjoxOTg3ODcyMzEyfQ._9OAY37rFqJb6S8jnveCR1-ZdTeYhd-quXeBpvs3AkY')


const get_preview = async (url: string): Promise<T> => {
try {
const response = await fetch('https://api.linkpreview.net',{
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
body: `key=b94a6802883768742457381f05d751e4&q=${url}`
})
return response.json()
} catch {
return {
title: null,
description: null
}
}

}

serve(async (req) => {
const data = await req.json()
const post_id = data.record.id
const author_id = data.record.author_id
const channel_id = data.record.channel_id
const guild_id = data.record.guild_id
const content = data.record.content
const created_at = data.record.created_at

const matches = content.match(/\bhttps?:\/\/\S+/gi);
var status = 200
await matches.forEach(async url => {
const preview = await get_preview(url)
const title = preview.title
const description = preview.description
try {
const { response, error } = await supabase
.from('post_urls')
.insert([
{ post_id,author_id,channel_id,guild_id,content,created_at,url,title,description },
])
} catch(e) {
status = 500
}

});


return new Response(
JSON.stringify(status),
{ headers: { "Content-Type": "application/json" } },
)
})

0 comments on commit 1c639ae

Please sign in to comment.