The nature of Presidential immunity was described in Nixon v. Fitzgerald, 457 U.S. 731 (1982). There is "absolute Presidential immunity from damages liability for acts within the 'outer perimeter' of his official responsibility."
Absolute Presidential immunity is "a functionally mandated incident of the President's unique office, rooted in the constitutional tradition of the separation of powers and supported by our history" (Nixon, p. 749).
This is distinct from qualified immunity. Qualified immunity would be insufficient to protect the interests that absolute immunity protects (Nixon, pp. 750–52).
In judging whether the impugned acts come within the 'outer perimeter' of a President's official duties, timing absolutely matters (e.g. acts that occurred before the person was President are not within the scope of Presidential immunity: Clinton v. Jones, 520 U.S. 681 (1997)), as do all the other circumstances of the speech like its function and content. The President's absolute immunity "should extend only to acts in performance of particular functions of his office." Nevertheless, the Court has "refused to draw functional lines finer than history and reason would support" (Nixon, p. 755).
Mr. Trump made the argument about Presidential immunity in a motion for summary judgment (that would have resolved the case in his favour). Judge Kaplan rejected this argument in his opinion denying summary judgment. He concluded:
Subjecting the president to damages liability for making a personal attack that is unrelated to the president’s official responsibilities would not threaten to distract the president from his or her official duties.
This is the conclusion that is being appealed to the United States Court of Appeals for the Second Circuit (hearing at 2pm, October 23, 2023; livestreamed here).