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 {
getEvents(callback) {
// different feeds should perhaps be in different components?
let since;
getSince() {
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') {
return PubSub.subscribe(
// TODO map to liked msg id
{ authors: [this.props.nostrUser], kinds: [7], since },
callback,
false,
@ -32,26 +40,23 @@ class Feed extends BaseFeed {
} else {
return PubSub.subscribe(
{ authors: [this.props.nostrUser], kinds: [1, 6], since },
(event) => {
if (this.props.index === 'posts') {
if (Events.getEventReplyingTo(event) && !Events.isRepost(event)) {
return;
}
}
callback(event);
},
this.getCallbackForPostsIndex(callback),
false,
false,
);
}
} else if (this.props.keyword) {
}
subscribeToKeyword(since, callback) {
const keyword = this.props.keyword.toLowerCase();
return PubSub.subscribe(
{ 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,
);
} else if (this.props.index === 'follows') {
}
subscribeToFollows(since, callback) {
const myPub = Key.getPubKey();
const followedUsers = Array.from(SocialNetwork.followedByUser.get(myPub) || []);
followedUsers.push(myPub);
@ -69,8 +74,24 @@ class Feed extends BaseFeed {
true,
);
}
subscribeToGlobalFeed(since, callback) {
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;