aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Feed.svelte
blob: 72443a8b1ae3b9c4f9292e69c8684ccd0543c6dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<script>
	import { Avatar } from '$comp';
	import { browser } from '$app/environment';
	import VirtualScroll from 'svelte-virtual-scroll-list';
	import { punk } from '$lib/utils';
	import { Buffer } from 'buffer';
	import { entropyToMnemonic, mnemonicToSeedSync } from 'bip39';
	import { enhance } from '$app/forms';
	import { bech32m } from 'bech32';
	import { calculateId, signId } from 'nostr';
	import { bip32, stretch, post } from '$lib/utils';

	export let events;
	export let user;

	const { decode, fromWords } = bech32m;

	let message;

	let submit = async () => {
		let mnemonic, key, password, seed, entropy, child, privkey;
		entropy = Buffer.from(
			await crypto.subtle.decrypt(
				{ name: 'AES-GCM', iv: new Uint8Array(16) },
				await stretch(password, Buffer.from(user.salt, 'hex')),
				Uint8Array.from(fromWords(decode(user.cipher, 180).words))
			),
			'hex'
		).toString('hex');

		mnemonic = entropyToMnemonic(entropy);
		seed = mnemonicToSeedSync(mnemonic);
		key = bip32.fromSeed(seed);
		child = key.derivePath("m/44'/1237'/0'/0/0");
		privkey = child.privateKey;

		let event = {
			pubkey: user.pubkey,
			created_at: Math.floor(Date.now() / 1000),
			kind: 1,
			content: message,
			tags: []
		};

		event.id = await calculateId(event);
		event.sig = await signId(privkey, event.id);
		await post(`/${user.username}`, { event });

		event.user = user;
		event.seen = event.created_at;
		events[event.id] = event;
		events = events;
	};

	let distance = (date) => {
		let m = 60;
		let h = 60 * m;
		let d = 24 * h;

		let diff = Math.round(Date.now() / 1000) - date;
		return diff > d
			? Math.round(diff / d) + 'd'
			: diff > h
			? Math.round(diff / h) + 'h'
			: diff > m
			? Math.round(diff / m) + 'm'
			: diff > 2
			? diff + 's'
			: 'now';
	};

	let w;

	$: sorted = Object.values(events)
		.filter((ev) => ev?.pubkey)
		.sort((a, b) => b.seen - a.seen);
</script>

<svelte:window bind:innerWidth={w} />

<form on:submit|preventDefault={submit} class="flex justify-center gap-4 max-w-2xl mx-auto">
	<div class="grow">
		<input
			name="message"
			bind:value={message}
			placeholder="What's happening?"
			class="my-5 mx-auto"
		/>
	</div>
	<button class="rounded-full border py-4 px-5 font-bold hover:opacity-80 w-40 my-5">Post</button>
</form>

{#if browser}
	<VirtualScroll data={sorted} key="id" let:data pageMode={true} estimateSize={200}>
		<div class="flex border-b py-4 text-sm lg:text-lg text-secondary" :key={data.id}>
			<a href={`/${data.user.anon ? data.user.pubkey : data.user.username}`}>
				<div class="mb-auto mr-2">
					<div class="md:hidden">
						<Avatar size={12} user={data.user} disabled={true} />
					</div>
					<div class="hidden md:block">
						<Avatar size={20} user={data.user} disabled={true} />
					</div>
				</div>
			</a>

			<div class="grow" style={`max-width: ${w - 100}px`}>
				<div class="w-full flex pb-1 text-black">
					<div>{data.user.username}</div>
					<div class="text-secondary">&nbsp;{distance(data.seen)}</div>
				</div>
				<div class="break-words">
					{data.content}
				</div>
			</div>
		</div>
	</VirtualScroll>
{/if}