Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from valknight/feature/notifImprovements
Browse files Browse the repository at this point in the history
Notification improvements
  • Loading branch information
valknight authored Nov 6, 2022
2 parents f069e42 + 2f492c8 commit cf83625
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cohost/models/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def __init__(self, createdAt, fromProject, toPost, comment, inReplyTo):
self.inReplyTo = inReplyTo

def __str__(self) -> str:
if self.toPost == None:
return "{} commented on [post that couldn't be retrieved] - \"{}\" | {}".format(self.fromProject.handle, self.comment.body, self.timestamp)
return "{} commented on {} - \"{}\" | {}".format(self.fromProject.handle, self.toPost.postId, self.comment.body, self.timestamp)

class Follow(BaseNotification):
Expand Down Expand Up @@ -85,6 +87,8 @@ def buildFromNotifList(notificationsApiResp: dict, user):
comments = []
while len(commentQueue) > 0:
nextNotif = commentQueue.pop(0)
if nextNotif.get('attemptsToProcess', 0) == 0:
nextNotif['attemptsToProcess'] = 0
replyComment = None
if nextNotif['comment']['inReplyTo']:
found = False
Expand All @@ -94,7 +98,8 @@ def buildFromNotifList(notificationsApiResp: dict, user):
replyComment = n
break
# we still have other notifications to be processed
if not found and len(commentQueue) > 0:
nextNotif['attemptsToProcess'] += 1
if not found and len(commentQueue) > 0 and not(nextNotif['attemptsToProcess'] > 50):
commentQueue.append(nextNotif)
continue
# Ok, sick, so, we either don't have a reply, or we do but it's already processed!
Expand Down
20 changes: 18 additions & 2 deletions cohost/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,25 @@ def resolveSecondaryProject(self, projectData):

@property
def notifications(self):
return self.notificationsPagified(notificationsPerPage = 10)

@property
def allNotifications(self):
page = 0
notifsPerPage = 100
notifs = self.notificationsPagified(page = page, notificationsPerPage = notifsPerPage)
newNotifs = notifs
while len(newNotifs) == notifsPerPage:
page += 1
newNotifs = self.notificationsPagified(page = page, notificationsPerPage = notifsPerPage)
for n in newNotifs:
notifs.append(n)
return notifs

def notificationsPagified(self, page = 0, notificationsPerPage = 10):
nJson = fetch('GET', 'notifications/list', {
'offset': 0,
'limit': 5000
'offset': page * notificationsPerPage,
'limit': notificationsPerPage
}, generate_login_cookies(self.cookie))
return buildFromNotifList(nJson, self)

Expand Down
30 changes: 30 additions & 0 deletions demos/printNotifs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
from cohost.models.user import User

def main():
print('logging in... ')
username = os.environ.get('cohostUser')
password = os.environ.get('cohostPass')
handle = os.environ.get('cohostHandle')
if username is None:
username = input('username: ')
if password is None:
password = input('password: ')
if handle is None:
handle = input('handle: ')
user = User.login(username, password)

print('done!', end='\n\n')
# get last 10 notifications
print('last 10 notifs: {}'.format(len(user.notifications)), end='\n\n')

# example of custom pagination
user.notificationsPagified(page = 2, notificationsPerPage=5)

# Example of all notifications!
print('retrieving all notifs... ')
notifs = user.allNotifications
print('you have recieved {} total lifetime notifs.'.format(len(notifs)))

if __name__ == '__main__':
main()

0 comments on commit cf83625

Please sign in to comment.