Skip to content

Commit

Permalink
Merge "Implement pywikibot support for adding thanks to normal revisi…
Browse files Browse the repository at this point in the history
…ons"
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Jun 9, 2017
2 parents 504b595 + a0a6d16 commit 55c3db5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pywikibot/page.py
Expand Up @@ -3462,6 +3462,23 @@ def uploadedImages(self, total=10):
item.pageid() > 0
)

@property
def is_thankable(self):
"""
Determine if the user has thanks notifications enabled.
NOTE: This doesn't accurately determine if thanks is enabled for user.
Privacy of thanks preferences is under discussion, please see
https://phabricator.wikimedia.org/T57401#2216861, and
https://phabricator.wikimedia.org/T120753#1863894
@rtype: bool
"""
if self.isAnonymous():
return False

return True


class WikibasePage(BasePage):

Expand Down Expand Up @@ -5167,6 +5184,17 @@ def full_hist_entry(self):
return Revision.FullHistEntry(self.revid, self.timestamp, self.user,
self.text, self.rollbacktoken)

@staticmethod
def _thank(revid, site, source='pywikibot'):
"""Thank a user for this revision.
@param site: The Site object for this revision.
@type site: Site
@param source: An optional source to pass to the API.
@type source: str
"""
site.thank_revision(revid, source)


class FileInfo(DotReadableDict):

Expand Down
19 changes: 19 additions & 0 deletions pywikibot/site.py
Expand Up @@ -6774,6 +6774,25 @@ def get_param(item):
comparison = data['compare']['*']
return comparison

@need_extension('Thanks')
def thank_revision(self, revid, source=None):
"""Corresponding method to the 'action=thank' API action.
@param revid: Revision ID for the revision to be thanked.
@type revid: int
@param source: A source for the thanking operation.
@type source: str
@raise APIError: On thanking oneself or other API errors.
@return: The API response.
"""
token = self.tokens['csrf']
req = self._simple_request(action='thank', rev=revid, token=token,
source=source)
data = req.submit()
if data['result']['success'] != 1:
raise api.APIError('Thanking unsuccessful')
return data

# Flow API calls
@need_extension('Flow')
def load_board(self, page):
Expand Down
49 changes: 49 additions & 0 deletions tests/thanks_tests.py
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""Tests for thanks-related code."""
#
# (C) Pywikibot team, 2016-17
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, unicode_literals

from pywikibot.page import Revision, User

from tests.aspects import TestCase


class TestThankRevision(TestCase):

"""Test thanks for revisions."""

family = 'test'
code = 'test'

write = True

def test_thank_revision(self):
"""Test thanks for normal revisions.
NOTE: This test relies on activity in recentchanges, and
there must make edits made before reruns of this test.
Please see https://phabricator.wikimedia.org/T137836.
"""
found_log = can_thank = False
site = self.get_site()
data = site.recentchanges(total=50, reverse=True)
for i in data:
revid = i['revid']
username = i['user']
user = User(site, username)
if user.is_thankable:
can_thank = True
break
if not can_thank:
self.skipTest('There is no recent change which can be test thanked.')
before_time = site.getcurrenttimestamp()
Revision._thank(revid, site, source='pywikibot test')
log_entries = site.logevents(logtype='thanks', total=5, start=before_time, page=user)
for __ in log_entries:
found_log = True
break
self.assertTrue(found_log)

0 comments on commit 55c3db5

Please sign in to comment.