XF 2.0 Bests way to change the author of a post

Komposten

Member
Hi,

I'm working on an add-on that changes the author of posts posted in specific threads, but there are a couple of things I haven't managed to figure out entirely:
  1. Replying to a thread: Currently I'm using a code event listener for post_pre_save to edit replies before they are saved. This works well enough, but is there a better way? I have tried e.g. using Service\Thread\Replier->_save() and Service\Post\Preparer->afterInsert(), but in both cases the reply did not appear changed in the thread until after a refresh.
  2. Creating a new thread: In new threads of this type the author of the OP should also be changed. For this I'm using setupThreadCreate() and Service\Thread\Creator. It works fine, but is it the best way?
  3. Changing the author: Currently I change the post author by changing the user_id of the post entity, and then calling Thread->rebuildCounters() and Forum->rebuildLastPost(). However, this is obviously not enough as it doesn't update e.g. User->message_count and Thread->UserPosts. Is a manual update like this the way to go, or are there helper methods/classes to ensure everything linked to a post author (e.g. message counts) is updated properly?
I'm really new at add-on development for XenForo and am still trying to figure out how it all works, so I would very much appreciate any suggestions and information you give. :)

Thank you,
Komposten
 
I've improved my code a bit since I posted this, and have managed to successfully update forums, threads, and users when I change the post authors. However, I'd still be interested in if I'm doing this right or not (i.e. the questions in the OP still stand).
 
  1. Replying to a thread: Currently I'm using a code event listener for post_pre_save to edit replies before they are saved. This works well enough, but is there a better way? I have tried e.g. using Service\Thread\Replier->_save() and Service\Post\Preparer->afterInsert(), but in both cases the reply did not appear changed in the thread until after a refresh.
  2. Creating a new thread: In new threads of this type the author of the OP should also be changed. For this I'm using setupThreadCreate() and Service\Thread\Creator. It works fine, but is it the best way?

You should edit controllers and forms. Add another form field, like, "post as user". Catch that in the form save process in the controller. If it's set, set the author to that user. No need to manipulate data when you can pass validated data.
Changing the author: Currently I change the post author by changing the user_id of the post entity, and then calling Thread->rebuildCounters() and Forum->rebuildLastPost(). However, this is obviously not enough as it doesn't update e.g. User->message_count and Thread->UserPosts. Is a manual update like this the way to go, or are there helper methods/classes to ensure everything linked to a post author (e.g. message counts) is updated properly?
https://github.com/ticktackk/Change...tOwner/XF/Service/Post/AuthorChanger.php#L238
 
You should edit controllers and forms. Add another form field, like, "post as user". Catch that in the form save process in the controller. If it's set, set the author to that user. No need to manipulate data when you can pass validated data.
I will definitely look into it. Sounds more clean than the mess I have. :)
Which methods would you suggest I use/override in the controllers, setupThreadCreate() or similar?
Since this user-change is automatic and should apply to all users that post in a given thread/forum, I assume that I would hide the "post as user" field and auto-populate it with the user to change to.

I found those methods in the default Entity\Post and have made use of them to successfully change the author of existing posts (by extending Entity\Post and adding change-author methods).
 
Last edited:
Which methods would you suggest I use/override in the controllers, setupThreadCreate() or similar?
Since this user-change is automatic and should apply to all users that post in a given thread/forum, I assume that I would hide the "post as user" field and auto-populate it with the user to change to.
Not quite. In that case create a node option "anon forum". Then check for the "anon forum" option in setupThreadReply/Edit() right before for the service caller. If it's set to "anon", call the constructor calls manually with your "anon user" instead of the visitor.
 
You need two addons! The one mentioned above

https://xenforo.com/community/resources/change-content-owner.6124/

The owner dont want you to change the first post. Because of this you need the second addon from AndyB.


Only with both addons you can manage all situations.
 
Top Bottom