feed refactoring

This commit is contained in:
Martti Malmi 2023-04-24 00:19:49 +03:00
parent a974cc514c
commit 51d4db0811

View File

@ -14,16 +14,24 @@ const TIMESPANS = {
}; };
class Feed extends BaseFeed { class Feed extends BaseFeed {
getEvents(callback) { getSince() {
// different feeds should perhaps be in different components?
let since;
if (this.state.settings.timespan !== 'all') { if (this.state.settings.timespan !== 'all') {
since = Math.floor(Date.now() / 1000) - TIMESPANS[this.state.settings.timespan]; return Math.floor(Date.now() / 1000) - TIMESPANS[this.state.settings.timespan];
} }
if (this.props.nostrUser) { }
getCallbackForPostsIndex(callback) {
return (event) => {
if (Events.getEventReplyingTo(event) && !Events.isRepost(event)) {
return;
}
callback(event);
};
}
subscribeToNostrUser(since, callback) {
if (this.props.index === 'likes') { if (this.props.index === 'likes') {
return PubSub.subscribe( return PubSub.subscribe(
// TODO map to liked msg id
{ authors: [this.props.nostrUser], kinds: [7], since }, { authors: [this.props.nostrUser], kinds: [7], since },
callback, callback,
false, false,
@ -32,26 +40,23 @@ class Feed extends BaseFeed {
} else { } else {
return PubSub.subscribe( return PubSub.subscribe(
{ authors: [this.props.nostrUser], kinds: [1, 6], since }, { authors: [this.props.nostrUser], kinds: [1, 6], since },
(event) => { this.getCallbackForPostsIndex(callback),
if (this.props.index === 'posts') {
if (Events.getEventReplyingTo(event) && !Events.isRepost(event)) {
return;
}
}
callback(event);
},
false, false,
false, false,
); );
} }
} else if (this.props.keyword) { }
subscribeToKeyword(since, callback) {
const keyword = this.props.keyword.toLowerCase(); const keyword = this.props.keyword.toLowerCase();
return PubSub.subscribe( return PubSub.subscribe(
{ keywords: [keyword], kinds: [1], limit: 1000, since }, { keywords: [keyword], kinds: [1], limit: 1000, since },
(e) => e.content?.toLowerCase().includes(keyword) && callback(e), // TODO this should not be necessary. seems subscribe still asks non-search relays (e) => e.content?.toLowerCase().includes(keyword) && callback(e),
false, false,
); );
} else if (this.props.index === 'follows') { }
subscribeToFollows(since, callback) {
const myPub = Key.getPubKey(); const myPub = Key.getPubKey();
const followedUsers = Array.from(SocialNetwork.followedByUser.get(myPub) || []); const followedUsers = Array.from(SocialNetwork.followedByUser.get(myPub) || []);
followedUsers.push(myPub); followedUsers.push(myPub);
@ -69,8 +74,24 @@ class Feed extends BaseFeed {
true, true,
); );
} }
subscribeToGlobalFeed(since, callback) {
return PubSub.subscribe({ kinds: [1, 6], limit: 300, since }, callback, true); return PubSub.subscribe({ kinds: [1, 6], limit: 300, since }, callback, true);
} }
getEvents(callback) {
const since = this.getSince();
if (this.props.nostrUser) {
return this.subscribeToNostrUser(since, callback);
} else if (this.props.keyword) {
return this.subscribeToKeyword(since, callback);
} else if (this.props.index === 'follows') {
return this.subscribeToFollows(since, callback);
} else {
return this.subscribeToGlobalFeed(since, callback);
}
}
} }
export default Feed; export default Feed;