Skip to content

Commit

Permalink
update communities
Browse files Browse the repository at this point in the history
  • Loading branch information
lovvtide committed Jul 9, 2023
1 parent 3b24e62 commit 88ebd84
Show file tree
Hide file tree
Showing 19 changed files with 1,611 additions and 361 deletions.
181 changes: 180 additions & 1 deletion src/actions/Nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,119 @@ export const revokeDeviceAuth = (alias, options) => {
}
};

export const subscribeToCommunity = (params) => {

return async (dispatch, getState) => {

let event, atags;

const { a, subscribe } = params;
const { followingList } = getState().communities;

if (!a) { return; }

if (subscribe) { // Subscribe to community

if (followingList[a]) { return; }

atags = ([ a, ...Object.keys(followingList) ]).map(ident => {
return [ 'a', ident ];
});

} else { // Unsubscribe from community

if (!followingList[a]) { return; }

atags = Object.keys(followingList).filter(ident => {
return ident !== a;
}).map(ident => {
return [ 'a', ident ];
});
}

try {

event = await window.client.createEvent({
content: '',
kind: 30001,
tags: [
[ 'd', 'communities' ],
...atags
]
}, {
privateKey: getLocalPrivateKey()
});

} catch (err) {
console.log('err', err);
}

if (!event) { return; }

dispatch({
type: RECEIVE_COMMUNITY_FOLLOWING_LIST,
data: { event }
});

try {

await new Promise((resolve, reject) => {
window.client.publishEvent(event, (status, relay) => {
console.log(status, relay.url);
});
});

} catch (err) {
console.log(err);
}
};
};

export const handleApprovePost = async (item, params = {}, handlers = {}) => {

let event;

try {

event = await window.client.createEvent({
content: JSON.stringify(item.event),
kind: 4550,
tags: [
[ 'a', `34550:${params.ownerpubkey}:${params.name}` ],
[ 'e', item.event.id ],
[ 'p', item.event.pubkey ],
[ 'k', String(item.event.kind) ]
]
}, {
privateKey: getLocalPrivateKey()
});

} catch (err) {
console.log('err', err);
}

if (!event) { return; }

if (handlers.onSignedEvent) {

handlers.onSignedEvent(event);
}

//this.state.feed.update(event, null, { newpub: true });

try {

await new Promise((resolve, reject) => {
window.client.publishEvent(event, (status, relay) => {
console.log(status, relay.url);
});
});

} catch (err) {
console.log(err);
}
}

export const SHOW_ZAP_REQUEST = 'SHOW_ZAP_REQUEST';
export const handleZapRequest = (recipient = {}, event = {}, handlers = {}) => {

Expand Down Expand Up @@ -327,7 +440,9 @@ export const SET_PENDING_CONTACTS = 'SET_PENDING_CONTACTS';
export const LOAD_ACTIVE_NOSTR = 'LOAD_ACTIVE_NOSTR';
export const RECEIVE_DM_METADATA = 'RECEIVE_DM_METADATA';
export const RECEIVE_COMMUNITY_EVENT = 'RECEIVE_COMMUNITY_EVENT';
export const RECEIVE_COMMUNITY_POST = 'RECEIVE_COMMUNITY_POST';
export const RECEIVE_COMMUNITY_METADATA = 'RECEIVE_COMMUNITY_METADATA';
export const RECEIVE_COMMUNITY_FOLLOWING_LIST = 'RECEIVE_COMMUNITY_FOLLOWING_LIST';
export const loadActiveNostr = (callback) => { // load active address / alias

return async (dispatch, getState) => {
Expand Down Expand Up @@ -371,8 +486,63 @@ export const loadActiveNostr = (callback) => { // load active address / alias
data: { pubkey, privateKey }
});

const parsedEventId = {};

dispatch(nostrProfileInit(window.client.listenForProfile(pubkey, {

onLoadedCommunityFollowingList: (event) => {

dispatch({
type: RECEIVE_COMMUNITY_FOLLOWING_LIST,
data: { event }
});
},

onLoadedModqueue: (feed, relay) => {

setTimeout(() => {

const { modqueue } = getState().communities;
const filters = [];
const p = {};

for (let key of Object.keys(modqueue)) {

const item = modqueue[key];

p[item.event.pubkey] = true;

if (parsedEventId[item.event.id] || !item.event.content) { continue; }

Object.assign(parsedEventId, window.client.parseContentRefs(item.event.content)['e']);
}

const authors = Object.keys(p);

if (authors.length > 0) {

filters.push({
authors,
kinds: [ 0 ]
});
}
const ids = Object.keys(parsedEventId);

if (ids.length > 0) {

filters.push({
ids
});
}

if (filters.length > 0) {

feed.subscribe(`modqueue_metadata`, relay, filters);
}

}, 1500);
},

onCommunity: (event) => {

dispatch({
Expand All @@ -381,6 +551,14 @@ export const loadActiveNostr = (callback) => { // load active address / alias
});
},

onCommunityPost: (events) => {

dispatch({
type: RECEIVE_COMMUNITY_POST,
data: { events }
});
},

onNotify: (events = []) => {

dispatch(receiveNotifications({ events }));
Expand Down Expand Up @@ -584,7 +762,8 @@ export const nostrMainInit = () => {
const { communities } = getState();

main.subscribe(`community_index_metadata`, relay, [{
authors: communities.list.map(item => {
authors: Object.keys(communities.list).map(d => {
const item = communities.list[d];
return item.event.pubkey;
}),
kinds: [ 0 ]
Expand Down
8 changes: 6 additions & 2 deletions src/components/CommunityPage/ApproveButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ class ApproveButton extends PureComponent {
whiteSpace: 'nowrap',
textTransform: 'uppercase',
cursor: 'pointer',
fontSize: 11,
fontSize: 10,
fontFamily: 'JetBrains-Mono-Bold',
color: this.state.hover ? '#fff' : COLORS.secondaryBright
color: this.state.hover ? '#fff' : COLORS.secondaryBright,
border: '1px solid',
paddingLeft: 5,
paddingRight: 5,
borderRadius: 3
}}
>
<Icon name='circle check' />
Expand Down
14 changes: 13 additions & 1 deletion src/components/CommunityPage/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ class List extends PureComponent {
{items.map((item, index) => {
return (
<Post
feed={this.props.feed}
mobile={this.props.mobile}
key={item.event.id}
event={item.event}
base={`/n/${this.props.name}/${this.props.ownernpub}`}
approval={item.approval}
profile={this.props.metadata[item.event.pubkey]}
metadata={this.props.metadata}
//profile={this.props.metadata[item.event.pubkey]}
profile={this.props.metadata[item.event.pubkey] ? (this.props.metadata[item.event.pubkey].profile) || {} : {}}
searchActive={this.props.searchActive}
handlePost={this.props.handlePost}
handleMobileReply={this.props.handleMobileReply}
handleSelectThread={this.props.handleSelectThread}
handleQueryProfiles={this.props.handleQueryProfiles}
handleZapRequest={this.props.handleZapRequest}
handleFollow={this.props.handleFollow}
navigate={this.props.navigate}
/>
);
})}
Expand Down
35 changes: 28 additions & 7 deletions src/components/CommunityPage/ModQueue.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import { COLORS } from '../../constants';

class ModQueue extends PureComponent {

// componentDidMount = () => {
// window.scrollTo({ top: 0 });
// };

render = () => {

const { items } = this.props;
Expand All @@ -25,15 +21,40 @@ class ModQueue extends PureComponent {
<div style={{
paddingBottom: 196
}}>
{items.map((item, index) => {
{items.map(item => {
const name = item.postedTo ? item.postedTo.name : this.props.name;
const owner = item.postedTo ? item.postedTo.owner : this.props.ownernpub;
return {
...item,
name,
owner
};
}).filter(item => {
return !item.coord
|| !this.props.approvals
|| !this.props.approvals[item.coord];
}).map((item, index) => {
return (
<Post
modqueue
feed={this.props.feed}
key={item.event.id}
event={item.event}
base={`/n/${this.props.name}/${this.props.ownernpub}`}
base={`/n/${item.name}/${item.owner}`}
handleApprove={() => this.props.handleApprovePost(item)}
moderator={this.props.moderator}
profile={this.props.metadata[item.event.pubkey]}
profile={this.props.metadata[item.event.pubkey] ? (this.props.metadata[item.event.pubkey].profile) || {} : {}}
metadata={this.props.metadata}
postedTo={item.postedTo}
mobile={this.props.mobile}
searchActive={this.props.searchActive}
handlePost={this.props.handlePost}
handleMobileReply={this.props.handleMobileReply}
handleSelectThread={this.props.handleSelectThread}
handleQueryProfiles={this.props.handleQueryProfiles}
handleZapRequest={this.props.handleZapRequest}
handleFollow={this.props.handleFollow}
navigate={this.props.navigate}
/>
);
})}
Expand Down
Loading

0 comments on commit 88ebd84

Please sign in to comment.